]> git.defcon.no Git - hermes/blob - api/t/user.t
fca67eb8acf96c5596cbbbeffe277ee91c41c811
[hermes] / api / t / user.t
1 #!/usr/bin/perl
2
3 use strict;
4 use Test::More 'no_plan';
5 use tests_common;
6
7 # Configuration variables defined in tests_common
8 # Make sure those are updated/set correctly..
9 # $api_base
10 # $api_key
11 #
12 # Further variables defined in tests_common:
13 # $invalid_key
14 # $test_username
15 # $test_password
16 # $invalid_username
17
18 my ($data, $temp, $test_domain, $test_remote_domain );
19
20 isa_ok( $g_ua, 'LWP::UserAgent', '$g_ua');
21 isa_ok( $g_ua->cookie_jar, 'HTTP::Cookies', '$g_ua->cookies');
22
23 login_apikey();
24
25 # First: fetch a supported domain from the API...
26 $data = exec_apinode("domain/list", undef);
27 ok($data, 'domain/list JSON decode');
28 is( $data->{'response'}, 'ok', 'domain/list result');
29 ok($data->{'list'}, 'domain/list array');
30
31 # NOW: Set the $test_domain to something useful (i.e. the first reported domain)
32 $test_domain = $data->{'list'}[0];
33 $test_remote_domain = "external." . $test_domain;
34
35 ok($test_domain, 'test_domain set.');
36 ok($test_remote_domain, 'test_remote_domain set.');
37 undef $data;
38
39 $data = exec_apinode("user/list", undef);
40 ok($data, 'user/list JSON decode');
41 is($data->{'response'}, 'ok', 'user/list result');
42 ok($data->{'list'}, 'user/list array');
43 undef $data;
44
45 $data = exec_apinode("user/available", { "user" => $test_username . "\@" . $test_domain });
46 is( $data->{'response'}, 'ok', 'user/available is available');
47 undef $data;
48
49 $data = exec_apinode("user/add_local", {
50 "user" => $test_username . "\@" . $test_domain,
51 "displayname" => "Automatic testing",
52 "email" => "noreply\@" . $test_domain,
53 } );
54 is( $data->{'response'}, 'ok', 'user/add_local created new account');
55 #TODO: Add tests to verify the data from add_local...
56 undef $data;
57
58 $data = exec_apinode("user/available", { "user" => $test_username . "\@" . $test_domain });
59 is( $data->{'response'}, 'failed', 'user/available has been created');
60 undef $data;
61
62 $data = exec_apinode("user/get", { "user" => $test_username . "\@" . $test_domain });
63 is( $data->{'response'}, 'ok', 'user/get returns');
64 is( $data->{'user'}->{'username'}, $test_username, 'user/get username correct');
65 is( $data->{'user'}->{'domain'}, $test_domain, 'user/get domain correct');
66 ok( $data->{'user'}->{'ha1'} =~ m/[a-z0-9]{16,}/, 'user/get ha1 hash generated');
67 undef $data;
68
69 $data = exec_apinode("user/gen_pw", undef);
70 ok( $data =~ m/[a-zA-Z0-9]{24}/, 'user/gen_pw format OK');
71 $temp = $data;
72 undef $data;
73
74 $data = exec_apinode("user/change_pw", {
75 "user" => $test_username . "\@" . $test_domain,
76 "password" => $temp,
77 } );
78 is( $data->{'response'}, 'ok', 'user/change_pw');
79 undef $data;
80
81 # TODO: This tests _almost_ all parameters. Update API docs, then update this test.
82 $data = exec_apinode("user/update", {
83 "user" => $test_username . "\@" . $test_domain,
84 "displayname" => "Changed",
85 "linetext" => $test_username,
86 "registrar" => "registrar." . $test_domain,
87 "r_port" => 5061,
88 "proxy" => "proxy." . $test_domain,
89 "p_port" => 5060,
90 });
91 is( $data->{'response'}, 'ok', 'user/update');
92 ok( $data->{'updated'}, 'user/update updated list');
93 ok( $data->{'skipped'}, 'user/update skipped list');
94 undef $data;
95
96 $data = exec_apinode("user/remove", { "user" => $test_username . "\@" . $test_domain });
97 is( $data->{'response'}, 'ok', 'user/remove deleted user');
98 undef $data;
99
100 $data = exec_apinode("user/add_remote", {
101 "user" => $test_username . "\@" . $test_domain,
102 "displayname" => "Automatic testing",
103 "password" => "verysecretpasswd",
104 "registrar" => "registrar" . $test_domain,
105 });
106 is( $data->{'response'}, 'failed', 'user/add_remote fails for local domain');
107 #TODO: Add tests to verify the data from add_local...
108 undef $data;
109
110 $data = exec_apinode("user/add_remote", {
111 "user" => $test_username . "\@" . $test_remote_domain,
112 "displayname" => "Automatic testing",
113 "password" => "verysecretpasswd",
114 "registrar" => "registrar" . $test_domain,
115
116 });
117 is( $data->{'response'}, 'ok', 'user/add_remote ok for remote domain');
118 #TODO: Add tests to verify the data from add_remote...
119 undef $data;
120
121 $data = exec_apinode("user/available", { "user" => $test_username . "\@" . $test_remote_domain });
122 is( $data->{'response'}, 'failed', 'user/available has been created');
123 undef $data;
124
125
126 $data = exec_apinode("user/remove", { "user" => $test_username . "\@" . $test_remote_domain });
127 is( $data->{'response'}, 'ok', 'user/remove deleted user');
128 undef $data;
129
130 logout();
131