]> git.defcon.no Git - hermes/blob - api/t/user.t
Ooops on last commit. forgot to include a majority of the changes...
[hermes] / api / t / user.t
1 #!/usr/bin/perl
2
3 use Test::More 'no_plan';
4
5 use strict;
6 use LWP;
7 use Data::Dumper;
8 use JSON;
9
10 my $api_base = "http://10.0.2.5/hermes/api/";
11 my $api_key = "6327c08b70f9";
12 my $api_user = "test";
13 my $api_pass = "Very5ecr3t";
14
15 my $test_username = "testuser-" . int(rand(255));
16 my $test_aliasname = "testalias-" . int(rand(255));
17
18 my $test_domain = undef;
19
20 my $test_remote_domain = undef;
21
22
23 # plan tests => 2;
24 my ($g_ua, $response, $data, $temp);
25
26
27 $g_ua = LWP::UserAgent->new;
28 isa_ok( $g_ua, 'LWP::UserAgent', '$g_ua');
29
30 $g_ua->cookie_jar({}); # In-memory jar, look at HTTP::Cookies for persistant
31 isa_ok( $g_ua->cookie_jar, 'HTTP::Cookies', '$g_ua->cookies');
32
33 TODO: {
34 local $TODO = 'auth/login not implemented yet';
35 $response = $g_ua->get( $api_base . "auth/login?username=" . $api_user .
36 "password=" . $api_pass . "&key=" . $api_key);
37
38 #$data = decode_json( $response->content);
39 is( $data->{'response'}, 'ok', 'auth/login');
40 undef $response; undef $data;
41 }
42
43 # First: fetch a supported domain from the API...
44 $response = $g_ua->get( $api_base . "domain/list");
45 isa_ok( $response, 'HTTP::Response', 'domain/list $response');
46 ok ($response->is_success, 'domain/list is_success');
47 $data = decode_json( $response->content);
48 ok($data, 'domain/list JSON decode');
49 is( $data->{'response'}, 'ok', 'domain/list result');
50 ok($data->{'list'}, 'domain/list array');
51
52 # NOW: Set the $test_domain to something useful (i.e. the first reported domain)
53 $test_domain = $data->{'list'}[0];
54 $test_remote_domain = "external." . $test_domain;
55
56 ok($test_domain, 'test_domain set.');
57 ok($test_remote_domain, 'test_remote_domain set.');
58 undef $response; undef $data;
59
60 $response = $g_ua->get( $api_base . "user/list");
61 isa_ok( $response, 'HTTP::Response', 'user/list $response');
62 ok ($response->is_success, 'user/list is_success');
63 $data = decode_json( $response->content);
64 ok($data, 'user/list JSON decode');
65 is( $data->{'response'}, 'ok', 'user/list result');
66 ok($data->{'list'}, 'user/list array');
67 undef $response; undef $data;
68
69 $response = $g_ua->get( $api_base . "user/available?user=" . $test_username . "\@" . $test_domain);
70 $data = decode_json( $response->content);
71 is( $data->{'response'}, 'ok', 'user/available is available');
72 undef $response; undef $data;
73
74 $response = $g_ua->get( $api_base . "user/add_local?user=" . $test_username . "\@" . $test_domain . "&displayname=Automatic testing&email=noreply\@" . $test_domain);
75 $data = decode_json( $response->content);
76 is( $data->{'response'}, 'ok', 'user/add_local created new account');
77 #TODO: Add tests to verify the data from add_local...
78 undef $response; undef $data;
79
80 $response = $g_ua->get( $api_base . "user/available?user=" . $test_username . "\@" . $test_domain);
81 $data = decode_json( $response->content);
82 is( $data->{'response'}, 'failed', 'user/available has been created');
83 undef $response; undef $data;
84
85 $response = $g_ua->get( $api_base . "user/get?user=" . $test_username . "\@" . $test_domain);
86 $data = decode_json( $response->content);
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 $response; undef $data;
92
93 $response = $g_ua->get( $api_base . "user/gen_pw");
94 ok( $response->content =~ m/[a-z0-9]{24}/, 'user/gen_pw format OK');
95 $temp = $response->content;
96 undef $response; undef $data;
97
98 $response = $g_ua->get( $api_base . "user/change_pw?user=" . $test_username . "\@" . $test_domain . "&password=" . $temp );
99 $data = decode_json( $response->content);
100 is( $data->{'response'}, 'ok', 'user/change_pw');
101 undef $response; undef $data;
102
103 # TODO: This tests _almost_ all parameters. Update API docs, then update this test.
104 $response = $g_ua->get( $api_base . "user/update?user=" . $test_username . "\@" . $test_domain .
105 "&displayname=Changed" .
106 "&linetext=" . $test_username .
107 "&registrar=registrar." . $test_domain .
108 "&r_port=5061" .
109 "&proxy=proxy." . $test_domain .
110 "&p_port=5060");
111
112 $data = decode_json( $response->content);
113 is( $data->{'response'}, 'ok', 'user/update');
114 ok( $data->{'updated'}, 'user/update updated list');
115 ok( $data->{'skipped'}, 'user/update skipped list');
116 undef $response; undef $data;
117
118 $response = $g_ua->get( $api_base . "user/remove?user=" . $test_username . "\@" . $test_domain);
119 $data = decode_json( $response->content);
120 is( $data->{'response'}, 'ok', 'user/remove deleted user');
121 undef $response; undef $data;
122
123 $response = $g_ua->get( $api_base . "user/add_remote?user=" . $test_username . "\@" . $test_domain .
124 "&displayname=Automatic testing" .
125 "&password=verysecretpasswd" .
126 "&registrar=registrar" . $test_domain);
127
128 $data = decode_json( $response->content);
129 is( $data->{'response'}, 'failed', 'user/add_remote fails for local domain');
130 #TODO: Add tests to verify the data from add_local...
131 undef $response; undef $data;
132
133 $response = $g_ua->get( $api_base . "user/add_remote?user=" . $test_username . "\@" . $test_remote_domain .
134 "&displayname=Automatic testing" .
135 "&password=verysecretpasswd" .
136 "&registrar=registrar" . $test_domain);
137
138 $data = decode_json( $response->content);
139 is( $data->{'response'}, 'ok', 'user/add_remote ok for remote domain');
140 #TODO: Add tests to verify the data from add_local...
141 undef $response; undef $data;
142
143
144 $response = $g_ua->get( $api_base . "user/available?user=" . $test_username . "\@" . $test_remote_domain);
145 $data = decode_json( $response->content);
146 is( $data->{'response'}, 'failed', 'user/available has been created');
147 undef $response; undef $data;
148
149 $response = $g_ua->get( $api_base . "user/remove?user=" . $test_username . "\@" . $test_remote_domain);
150 $data = decode_json( $response->content);
151 is( $data->{'response'}, 'ok', 'user/remove deleted user');
152 undef $response; undef $data;
153
154 TODO: {
155 local $TODO = 'auth/logout not implemented yet';
156 $response = $g_ua->get( $api_base . "auth/logout");
157 #$data = decode_json( $response->content);
158 is( $data->{'response'}, 'ok', 'auth/logout');
159 undef $response; undef $data;
160 }