]> git.defcon.no Git - hermes/blob - api/t/permissions.t
Permissions, a naive approach
[hermes] / api / t / permissions.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 my ($data, $temp, $test_domain );
32
33 isa_ok( $g_ua, 'LWP::UserAgent', '$g_ua');
34 isa_ok( $g_ua->cookie_jar, 'HTTP::Cookies', '$g_ua->cookies');
35
36 login_apikey();
37
38 # First: fetch a supported domain from the API...
39 $data = exec_apinode("domain/list", undef);
40 ok($data, 'domain/list JSON decode');
41 is( $data->{'response'}, 'ok', 'domain/list result');
42 ok($data->{'list'}, 'domain/list array');
43
44 # NOW: Set the $test_domain to something useful (i.e. the first reported domain)
45 $test_domain = $data->{'list'}[0];
46
47 ok($test_domain, 'test_domain set.');
48 undef $data;
49
50 $data = exec_apinode("user/available", { "user" => $test_username . "\@" . $test_domain });
51 is( $data->{'response'}, 'ok', 'user/available is available');
52 undef $data;
53
54 $data = exec_apinode("permissions/get", { "user" => $test_username . "\@" . $test_domain });
55 is( $data->{'response'}, 'failed', 'permissions/get returns failed');
56 is( $data->{'cause'}, 'nonexistant', 'permissions/get gives nonexistant');
57 undef $data;
58
59 $data = exec_apinode("user/add_local", {
60 "user" => $test_username . "\@" . $test_domain,
61 "displayname" => "Automatic testing",
62 "email" => "noreply\@" . $test_domain,
63 } );
64 is( $data->{'response'}, 'ok', 'user/add_local created new account');
65 undef $data;
66
67 $data = exec_apinode("permissions/get", { "user" => $test_username . "\@" . $test_domain });
68 is( $data->{'response'}, 'ok', 'permissions/get returns');
69 ok( $data->{'permission'}, 'permissions/get is set');
70 isnt( $data->{'permission'}, -1, 'permissions/get is not -1');
71 undef $data;
72
73 $data = exec_apinode("permissions/set", { "user" => $test_username . "\@" . $test_domain, "permission" => 31 });
74 is( $data->{'response'}, 'ok', 'permissions/set returns');
75 is( $data->{'permission'}, 31, 'permissions/set is 31');
76 undef $data;
77
78 $data = exec_apinode("permissions/get", { "user" => $test_username . "\@" . $test_domain });
79 is( $data->{'response'}, 'ok', 'permissions/get returns');
80 ok( $data->{'permission'}, 'permissions/get is set');
81 is( $data->{'permission'}, 31, 'permissions/get is 31');
82 undef $data;
83
84 $data = exec_apinode("user/remove", { "user" => $test_username . "\@" . $test_domain });
85 is( $data->{'response'}, 'ok', 'user/remove deleted user');
86 undef $data;
87
88 $data = exec_apinode("permissions/get", { "user" => $test_username . "\@" . $test_domain });
89 is( $data->{'response'}, 'failed', 'permissions/get return failed');
90 undef $data;
91
92 logout();