summaryrefslogtreecommitdiff
path: root/test/webadmin_tests.erl
blob: 160b24a094aa154f9d074eb66bd4833985fb917b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
%%%-------------------------------------------------------------------
%%% Author  : Pawel Chmielowski <pawel@process-one.net>
%%% Created : 23 Mar 2020 by Pawel Chmielowski <pawel@process-one.net>
%%%
%%%
%%% ejabberd, Copyright (C) 2002-2020   ProcessOne
%%%
%%% This program is free software; you can redistribute it and/or
%%% modify it under the terms of the GNU General Public License as
%%% published by the Free Software Foundation; either version 2 of the
%%% License, or (at your option) any later version.
%%%
%%% This program is distributed in the hope that it will be useful,
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
%%% General Public License for more details.
%%%
%%% You should have received a copy of the GNU General Public License along
%%% with this program; if not, write to the Free Software Foundation, Inc.,
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
%%%
%%%-------------------------------------------------------------------
-module(webadmin_tests).

%% API
-compile(export_all).
-import(suite, [disconnect/1, is_feature_advertised/3, upload_jid/1,
my_jid/1, wait_for_slave/1, wait_for_master/1,
send_recv/2, put_event/2, get_event/1]).

-include("suite.hrl").
-include_lib("stdlib/include/assert.hrl").

%%%===================================================================
%%% API
%%%===================================================================
%%%===================================================================
%%% Single user tests
%%%===================================================================
single_cases() ->
    {webadmin_single, [sequence],
     [single_test(login_page),
      single_test(welcome_page),
      single_test(user_page),
      single_test(adduser),
      single_test(changepassword),
      single_test(removeuser)]}.

login_page(Config) ->
    Headers = ?match({ok, {{"HTTP/1.1", 401, _}, Headers, _}},
		     httpc:request(get, {page(Config, ""), []}, [],
				   [{body_format, binary}]),
		     Headers),
    ?match("basic realm=\"ejabberd\"", proplists:get_value("www-authenticate", Headers, none)).

welcome_page(Config) ->
    Body = ?match({ok, {{"HTTP/1.1", 200, _}, _, Body}},
		     httpc:request(get, {page(Config, ""), [basic_auth_header(Config)]}, [],
				   [{body_format, binary}]),
		     Body),
    ?match({_, _}, binary:match(Body, <<"ejabberd Web Admin">>)).

user_page(Config) ->
    Server = ?config(server, Config),
    URL = "server/" ++ binary_to_list(Server) ++ "/user/admin/",
    Body = ?match({ok, {{"HTTP/1.1", 200, _}, _, Body}},
		  httpc:request(get, {page(Config, URL), [basic_auth_header(Config)]}, [],
				[{body_format, binary}]),
		  Body),
    ?match({_, _}, binary:match(Body, <<"<title>ejabberd Web Admin">>)).

adduser(Config) ->
    User = <<"webadmin-test">>,
    Server = ?config(server, Config),
    Password = ?config(password, Config),
    Body = make_query(
	     Config,
	     "server/" ++ binary_to_list(Server) ++ "/users/",
	     <<"newusername=", (mue(User))/binary, "&newuserpassword=",
	       (mue(Password))/binary, "&addnewuser=Add+User">>),
    Password = ejabberd_auth:get_password(User, Server),
    ?match({_, _}, binary:match(Body, <<"<a href='../user/">>)).

changepassword(Config) ->
    User = <<"webadmin-test">>,
    Server = ?config(server, Config),
    Password = <<"newpassword-", (?config(password, Config))/binary>>,
    Body = make_query(
	     Config,
	     "server/" ++ binary_to_list(Server)
	     ++ "/user/" ++ binary_to_list(mue(User)) ++ "/",
	     <<"password=", (mue(Password))/binary,
	       "&chpassword=Change+Password">>),
    Password = ejabberd_auth:get_password(User, Server),
    ?match({_, _}, binary:match(Body, <<"<p class='result'>Submitted</p>">>)).

removeuser(Config) ->
    User = <<"webadmin-test">>,
    Server = ?config(server, Config),
    Body = make_query(
	     Config,
	     "server/" ++ binary_to_list(Server)
	     ++ "/user/" ++ binary_to_list(mue(User)) ++ "/",
	     <<"password=&removeuser=Remove+User">>),
    false = ejabberd_auth:user_exists(User, Server),
    ?match(nomatch, binary:match(Body, <<"<h3>Last Activity</h3>20">>)).

%%%===================================================================
%%% Internal functions
%%%===================================================================
single_test(T) ->
    list_to_atom("webadmin_" ++ atom_to_list(T)).

basic_auth_header(Config) ->
    User = <<"admin">>,
    Server = ?config(server, Config),
    Password = ?config(password, Config),
    ejabberd_auth:try_register(User, Server, Password),
    basic_auth_header(User, Server, Password).

basic_auth_header(Username, Server, Password) ->
    JidBin = <<Username/binary, "@", Server/binary, ":", Password/binary>>,
    {"authorization", "Basic " ++ base64:encode_to_string(JidBin)}.

page(Config, Tail) ->
    Server = ?config(server_host, Config),
    Port = ct:get_config(web_port, 5280),
    Url = "http://" ++ Server ++ ":" ++ integer_to_list(Port) ++ "/admin/" ++ Tail,
    string:replace(Url, "%25", "%2525"). % Required by httpc:request for paths in URLs

mue(Binary) ->
    misc:url_encode(Binary).

make_query(Config, URL, BodyQ) ->
    ?match({ok, {{"HTTP/1.1", 200, _}, _, Body}},
	   httpc:request(post, {page(Config, URL),
				[basic_auth_header(Config)],
				"application/x-www-form-urlencoded",
				BodyQ}, [],
			 [{body_format, binary}]),
	   Body).