]> git.defcon.no Git - hermes/blob - api/t/alias.t
Added license text
[hermes] / api / t / alias.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 my ($data, $temp, $test_number_start, $test_alias, $test_e164alias, $test_domain, $test_remote_domain);
42
43 our $test_alias = "testalias-" . int(rand(255));
44
45 $test_e164alias .= int(rand(255))
46 while ( length( $test_e164alias ) < 8 );
47
48 $test_e164alias = '+47' . substr( $test_e164alias, 0, 8 );
49
50 isa_ok( $g_ua, 'LWP::UserAgent', '$g_ua');
51 isa_ok( $g_ua->cookie_jar, 'HTTP::Cookies', '$g_ua->cookies');
52
53 login_apikey();
54
55 # First: fetch a supported domain from the API...
56 $data = exec_apinode("domain/list", undef);
57 ok($data, 'domain/list JSON decode');
58 is( $data->{'response'}, 'ok', 'domain/list result');
59 ok($data->{'list'}, 'domain/list array');
60
61 # NOW: Set the $test_domain to something useful (i.e. the first reported domain)
62 $test_domain = $data->{'list'}[0];
63 $test_remote_domain = "external." . $test_domain;
64
65 ok($test_domain, 'test_domain set.');
66 ok($test_remote_domain, 'test_remote_domain set.');
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
80 $data = exec_apinode("alias/list", undef);
81 is( $data->{'response'}, 'ok', 'alias/list result');
82 ok($data->{'aliases'}, 'alias/list array');
83 undef $data;
84
85 $data = exec_apinode("alias/add", {
86 'alias' => $test_alias . "\@" . $test_domain,
87 'destination' => $test_username . "\@" . $test_domain,
88 });
89 is( $data->{'response'}, 'ok', 'alias/add result');
90 ok( $data->{'destination'}, 'alias/add destination');
91 ok( $data->{'alias'}, 'alias/add alias');
92 undef $data;
93
94 $data = exec_apinode("alias/add", {
95 'alias' => $test_alias . "\@" . $test_domain,
96 'destination' => $test_username . "\@" . $test_domain,
97 });
98 is( $data->{'response'}, 'failed', 'alias/add for existing alias result');
99 undef $data;
100
101
102
103 $data = exec_apinode("alias/add", {
104 'alias' => $test_e164alias . "\@" . $test_domain,
105 'destination' => $test_username . "\@" . $test_domain,
106 });
107 is( $data->{'response'}, 'ok', 'alias/add e164 result');
108 ok( $data->{'destination'}, 'alias/add destination');
109 ok( $data->{'alias'}, 'alias/add alias');
110 undef $data;
111
112 $data = exec_apinode("alias/list", { 'destination' => $test_username . "\@" . $test_domain });
113 is( $data->{'response'}, 'ok', 'alias/list w/destination result');
114 ok($data->{'aliases'}, 'alias/list w/destination array');
115 undef $data;
116
117 $data = exec_apinode("alias/list", { 'destination' => $test_username . "\@" . $test_domain });
118 is( $data->{'response'}, 'ok', 'alias/list w/e164 result');
119 ok($data->{'aliases'}, 'alias/list w/e164 array');
120 ok($data->{'aliases'}[0]->{'destination'}, 'alias/list w/e164 destination');
121 undef $data;
122
123 $data = exec_apinode("alias/remove", {
124 'alias' => $test_alias . "\@" . $test_domain,
125 });
126 is( $data->{'response'}, 'ok', 'alias/remove result');
127 ok( $data->{'alias'}, 'alias/remove alias');
128 undef $data;
129
130 $data = exec_apinode("alias/remove", {
131 'alias' => $test_e164alias . "\@" . $test_domain,
132 });
133 is( $data->{'response'}, 'ok', 'alias/remove e164 result');
134 ok( $data->{'alias'}, 'alias/remove alias');
135 undef $data;
136
137 $data = exec_apinode("user/remove", { "user" => $test_username . "\@" . $test_domain });
138 is( $data->{'response'}, 'ok', 'user/remove deleted user');
139 undef $data;
140
141 $data = exec_apinode("alias/add", {
142 'alias' => $test_username . "\@" . $test_domain,
143 'destination' => $test_alias . "\@" . $test_domain,
144 });
145 is( $data->{'response'}, 'failed', 'alias/add for existing user result');
146 undef $data;
147
148
149 logout();