]> git.defcon.no Git - hermes/blob - api/t/user.t
Added license text
[hermes] / api / t / user.t
1 #!/usr/bin/perl
2 # Copyright (c) 2012, Gjøvik University College
3 # All rights reserved.
4
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are met:
7 # * Redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer.
9 # * Redistributions in binary form must reproduce the above copyright
10 # notice, this list of conditions and the following disclaimer in the
11 # documentation and/or other materials provided with the distribution.
12 # * Neither the name of the Gjøvik University College nor the
13 # names of its contributors may be used to endorse or promote products
14 # derived from this software without specific prior written permission.
15 #
16 # THIS SOFTWARE IS PROVIDED BY Gjøvik University College ''AS IS'' AND ANY
17 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 # DISCLAIMED. IN NO EVENT SHALL Gjøvik University College BE LIABLE FOR ANY
20 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 use strict;
28 use Test::More 'no_plan';
29 use tests_common;
30
31 # Configuration variables defined in tests_common
32 # Make sure those are updated/set correctly..
33 # $api_base
34 # $api_key
35 #
36 # Further variables defined in tests_common:
37 # $invalid_key
38 # $test_username
39 # $test_password
40 # $invalid_username
41
42 my ($data, $temp, $test_domain, $test_remote_domain );
43
44 isa_ok( $g_ua, 'LWP::UserAgent', '$g_ua');
45 isa_ok( $g_ua->cookie_jar, 'HTTP::Cookies', '$g_ua->cookies');
46
47 login_apikey();
48
49 # First: fetch a supported domain from the API...
50 $data = exec_apinode("domain/list", undef);
51 ok($data, 'domain/list JSON decode');
52 is( $data->{'response'}, 'ok', 'domain/list result');
53 ok($data->{'list'}, 'domain/list array');
54
55 # NOW: Set the $test_domain to something useful (i.e. the first reported domain)
56 $test_domain = $data->{'list'}[0];
57 $test_remote_domain = "external." . $test_domain;
58
59 ok($test_domain, 'test_domain set.');
60 ok($test_remote_domain, 'test_remote_domain set.');
61 undef $data;
62
63 $data = exec_apinode("user/list", undef);
64 ok($data, 'user/list JSON decode');
65 is($data->{'response'}, 'ok', 'user/list result');
66 ok($data->{'list'}, 'user/list array');
67 undef $data;
68
69 $data = exec_apinode("user/available", { "user" => $test_username . "\@" . $test_domain });
70 is( $data->{'response'}, 'ok', 'user/available is available');
71 undef $data;
72
73 $data = exec_apinode("user/add_local", {
74 "user" => $test_username . "\@" . $test_domain,
75 "displayname" => "Automatic testing",
76 "email" => "noreply\@" . $test_domain,
77 } );
78 is( $data->{'response'}, 'ok', 'user/add_local created new account');
79 #TODO: Add tests to verify the data from add_local...
80 undef $data;
81
82 $data = exec_apinode("user/available", { "user" => $test_username . "\@" . $test_domain });
83 is( $data->{'response'}, 'failed', 'user/available has been created');
84 undef $data;
85
86 $data = exec_apinode("user/get", { "user" => $test_username . "\@" . $test_domain });
87 is( $data->{'response'}, 'ok', 'user/get returns');
88 is( $data->{'user'}->{'username'}, $test_username, 'user/get username correct');
89 is( $data->{'user'}->{'domain'}, $test_domain, 'user/get domain correct');
90 ok( $data->{'user'}->{'ha1'} =~ m/[a-z0-9]{16,}/, 'user/get ha1 hash generated');
91 undef $data;
92
93 $data = exec_apinode("user/gen_pw", undef);
94 ok( $data =~ m/[a-zA-Z0-9]{24}/, 'user/gen_pw format OK');
95 $temp = $data;
96 undef $data;
97
98 $data = exec_apinode("user/change_pw", {
99 "user" => $test_username . "\@" . $test_domain,
100 "password" => $temp,
101 } );
102 is( $data->{'response'}, 'ok', 'user/change_pw');
103 undef $data;
104
105 # TODO: This tests _almost_ all parameters. Update API docs, then update this test.
106 $data = exec_apinode("user/update", {
107 "user" => $test_username . "\@" . $test_domain,
108 "displayname" => "Changed",
109 "linetext" => $test_username,
110 "registrar" => "registrar." . $test_domain,
111 "r_port" => 5061,
112 "proxy" => "proxy." . $test_domain,
113 "p_port" => 5060,
114 });
115 is( $data->{'response'}, 'ok', 'user/update');
116 ok( $data->{'updated'}, 'user/update updated list');
117 ok( $data->{'skipped'}, 'user/update skipped list');
118 undef $data;
119
120 $data = exec_apinode("user/remove", { "user" => $test_username . "\@" . $test_domain });
121 is( $data->{'response'}, 'ok', 'user/remove deleted user');
122 undef $data;
123
124 $data = exec_apinode("user/add_remote", {
125 "user" => $test_username . "\@" . $test_domain,
126 "displayname" => "Automatic testing",
127 "password" => "verysecretpasswd",
128 "registrar" => "registrar" . $test_domain,
129 });
130 is( $data->{'response'}, 'failed', 'user/add_remote fails for local domain');
131 #TODO: Add tests to verify the data from add_local...
132 undef $data;
133
134 $data = exec_apinode("user/add_remote", {
135 "user" => $test_username . "\@" . $test_remote_domain,
136 "displayname" => "Automatic testing",
137 "password" => "verysecretpasswd",
138 "registrar" => "registrar" . $test_domain,
139
140 });
141 is( $data->{'response'}, 'ok', 'user/add_remote ok for remote domain');
142 #TODO: Add tests to verify the data from add_remote...
143 undef $data;
144
145 $data = exec_apinode("user/available", { "user" => $test_username . "\@" . $test_remote_domain });
146 is( $data->{'response'}, 'failed', 'user/available has been created');
147 undef $data;
148
149
150 $data = exec_apinode("user/remove", { "user" => $test_username . "\@" . $test_remote_domain });
151 is( $data->{'response'}, 'ok', 'user/remove deleted user');
152 undef $data;
153
154 logout();
155