]>
git.defcon.no Git - hermes/blob - api/t/user.t
6548f26f7f8a4159fd497c4aad4dd1751ec322a4
3 use Test
::More
'no_plan';
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";
15 my $test_username = "testuser-" . int(rand(255));
16 my $test_aliasname = "testalias-" . int(rand(255));
18 my $test_domain = "example.com"; #TODO: Must fetch this from API
20 my $test_remote_domain = "external.example.com";
23 my ($g_ua, $response, $data, $temp);
26 $g_ua = LWP
::UserAgent
->new;
27 isa_ok
( $g_ua, 'LWP::UserAgent', '$g_ua');
29 $g_ua->cookie_jar({}); # In-memory jar, look at HTTP::Cookies for persistant
30 isa_ok
( $g_ua->cookie_jar, 'HTTP::Cookies', '$g_ua->cookies');
33 local $TODO = 'auth/login not implemented yet';
34 $response = $g_ua->get( $api_base . "auth/login?username=" . $api_user .
35 "password=" . $api_pass . "&key=" . $api_key);
37 #$data = decode_json( $response->content);
38 is
( $data->{'response'}, 'ok', 'auth/login');
39 undef $response; undef $data;
42 $response = $g_ua->get( $api_base . "user/list");
43 isa_ok
( $response, 'HTTP::Response', 'user/list $response');
44 ok
($response->is_success, 'user/list is_success');
45 $data = decode_json
( $response->content);
46 ok
($data, 'user/list JSON decode');
47 is
( $data->{'response'}, 'ok', 'user/list result');
48 ok
($data->{'list'}, 'user/list array');
49 undef $response; undef $data;
51 $response = $g_ua->get( $api_base . "user/available?user=" . $test_username . "\@" . $test_domain);
52 $data = decode_json
( $response->content);
53 is
( $data->{'response'}, 'ok', 'user/available is available');
54 undef $response; undef $data;
56 $response = $g_ua->get( $api_base . "user/add_local?user=" . $test_username . "\@" . $test_domain . "&displayname=Automatic testing&email=noreply\@" . $test_domain);
57 $data = decode_json
( $response->content);
58 is
( $data->{'response'}, 'ok', 'user/add_local created new account');
59 #TODO: Add tests to verify the data from add_local...
60 undef $response; undef $data;
62 $response = $g_ua->get( $api_base . "user/available?user=" . $test_username . "\@" . $test_domain);
63 $data = decode_json
( $response->content);
64 is
( $data->{'response'}, 'failed', 'user/available has been created');
65 undef $response; undef $data;
67 $response = $g_ua->get( $api_base . "user/get?user=" . $test_username . "\@" . $test_domain);
68 $data = decode_json
( $response->content);
69 is
( $data->{'response'}, 'ok', 'user/get returns');
70 is
( $data->{'user'}->{'username'}, $test_username, 'user/get username correct');
71 is
( $data->{'user'}->{'domain'}, $test_domain, 'user/get domain correct');
72 ok
( $data->{'user'}->{'ha1'} =~ m/[a-z0-9]{16,}/, 'user/get ha1 hash generated');
73 undef $response; undef $data;
75 $response = $g_ua->get( $api_base . "user/gen_pw");
76 ok
( $response->content =~ m/[a-z0-9]{24}/, 'user/gen_pw format OK');
77 $temp = $response->content;
78 undef $response; undef $data;
80 $response = $g_ua->get( $api_base . "user/change_pw?user=" . $test_username . "\@" . $test_domain . "&password=" . $temp );
81 $data = decode_json
( $response->content);
82 is
( $data->{'response'}, 'ok', 'user/change_pw');
83 undef $response; undef $data;
85 # TODO: This tests _almost_ all parameters. Update API docs, then update this test.
86 $response = $g_ua->get( $api_base . "user/update?user=" . $test_username . "\@" . $test_domain .
87 "&displayname=Changed" .
88 "&linetext=" . $test_username .
89 "®istrar=registrar." . $test_domain .
91 "&proxy=proxy." . $test_domain .
94 $data = decode_json
( $response->content);
95 is
( $data->{'response'}, 'ok', 'user/update');
96 ok
( $data->{'updated'}, 'user/update updated list');
97 ok
( $data->{'skipped'}, 'user/update skipped list');
98 undef $response; undef $data;
100 $response = $g_ua->get( $api_base . "user/remove?user=" . $test_username . "\@" . $test_domain);
101 $data = decode_json
( $response->content);
102 is
( $data->{'response'}, 'ok', 'user/remove deleted user');
103 undef $response; undef $data;
105 $response = $g_ua->get( $api_base . "user/add_remote?user=" . $test_username . "\@" . $test_domain .
106 "&displayname=Automatic testing" .
107 "&password=verysecretpasswd" .
108 "®istrar=registrar" . $test_domain);
110 $data = decode_json
( $response->content);
111 is
( $data->{'response'}, 'failed', 'user/add_remote fails for local domain');
112 #TODO: Add tests to verify the data from add_local...
113 undef $response; undef $data;
115 $response = $g_ua->get( $api_base . "user/add_remote?user=" . $test_username . "\@" . $test_remote_domain .
116 "&displayname=Automatic testing" .
117 "&password=verysecretpasswd" .
118 "®istrar=registrar" . $test_domain);
120 $data = decode_json
( $response->content);
121 is
( $data->{'response'}, 'ok', 'user/add_remote ok for remote domain');
122 #TODO: Add tests to verify the data from add_local...
123 undef $response; undef $data;
126 $response = $g_ua->get( $api_base . "user/available?user=" . $test_username . "\@" . $test_remote_domain);
127 $data = decode_json
( $response->content);
128 is
( $data->{'response'}, 'failed', 'user/available has been created');
129 undef $response; undef $data;
131 $response = $g_ua->get( $api_base . "user/remove?user=" . $test_username . "\@" . $test_remote_domain);
132 $data = decode_json
( $response->content);
133 is
( $data->{'response'}, 'ok', 'user/remove deleted user');
134 undef $response; undef $data;
137 local $TODO = 'auth/logout not implemented yet';
138 $response = $g_ua->get( $api_base . "auth/logout");
139 #$data = decode_json( $response->content);
140 is
( $data->{'response'}, 'ok', 'auth/logout');
141 undef $response; undef $data;