]>
git.defcon.no Git - hermes/blob - api/t/tests_common.pm
2 # Copyright (c) 2012, Gjøvik University College
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.
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.
38 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
40 @EXPORT = qw($api_base $api_key $invalid_key $test_username $test_password $invalid_username $g_ua $session $auth_key &exec_apinode &login_apikey &login_user &logout);
41 %EXPORT_TAGS = ( 'all' => [ qw( ) ] );
42 @EXPORT_OK = ( @
{ $EXPORT_TAGS{'all'} } );
44 our $api_base = "http://sippbx.hig.no/hermes/api/";
45 our $api_key = "e065gBpUwqzY0a5o";
46 our $invalid_key = "invalidkey" . int(rand(255));
48 our $test_username = "testauth-" . int(rand(255));
49 our $test_password = "foobarbaz";
50 our $invalid_username = "failauth-" . int(rand(255));
52 our ($g_ua, $session, $auth_key);
54 $g_ua = LWP
::UserAgent
->new;
55 $g_ua->cookie_jar({}); # In-memory jar, look at HTTP::Cookies for persistant
63 my ( $response, $data );
65 $session = "" if not defined $session;
66 $auth_key = "" if not defined $auth_key;
67 my $url = $api_base . $node;
69 $param->{'session'} = $session;
70 $param->{'auth_key'} = $auth_key;
72 $response = $g_ua->post( $url, $param );
73 if ( $response->is_success )
75 if ( $response->content =~ m/\s*{/ )
77 $data = decode_json
( $response->content);
81 $data = $response->content;
90 my $response = $g_ua->post( $api_base . "auth/login",
91 [ "api_key" => $api_key ] );
93 ok
($response->is_success, 'login_apikey request is_success');
94 my $data = decode_json
( $response->content) if $response->is_success;
95 die("HTTP error") unless $response->is_success;
97 is
( $data->{'response'}, 'ok', 'login_apikey logged in');
98 ok
( $data->{'session'}, 'login_apikey session set');
99 ok
( $data->{'auth_key'}, 'login_apikey auth_key set');
100 if ( $data->{'response'} eq "ok" )
102 $session = $data->{'session'};
103 $auth_key = $data->{'auth_key'};
109 undef $data; undef $response;
114 my $response = $g_ua->post( $api_base . "auth/login",
115 [ "username" => $test_username,
116 "password" => $test_password ] );
119 ok
($response->is_success, 'login_user request is_success');
120 my $data = decode_json
( $response->content) if $response->is_success;
121 die("HTTP error") unless $response->is_success;
123 is
( $data->{'response'}, 'ok', 'login_user logged in');
124 ok
( $data->{'session'}, 'login_user session set');
125 ok
( $data->{'auth_key'}, 'login_user auth_key set');
126 if ( $data->{'response'} eq "ok" )
128 $session = $data->{'session'};
129 $auth_key = $data->{'auth_key'};
135 undef $data; undef $response;
140 my $response = $g_ua->post( $api_base . "auth/logout",
141 [ "session" => $session ] );
143 ok
($response->is_success, 'logout request is_success');
144 die("HTTP error") unless $response->is_success;
146 my $data = decode_json
( $response->content) if $response->is_success;
147 is
( $data->{'response'}, 'ok', 'logout ok');
149 undef $session; undef $auth_key;