]>
git.defcon.no Git - hermes/blob - api/t/tests_common.pm
14 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
16 @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);
17 %EXPORT_TAGS = ( 'all' => [ qw( ) ] );
18 @EXPORT_OK = ( @
{ $EXPORT_TAGS{'all'} } );
20 our $api_base = "http://10.0.2.5/hermes/api/";
21 our $api_key = "4hErgw3QFJLeuXGW";
22 our $invalid_key = "invalidkey" . int(rand(255));
24 our $test_username = "testauth-" . int(rand(255));
25 our $test_password = "foobarbaz";
26 our $invalid_username = "failauth-" . int(rand(255));
28 our ($g_ua, $session, $auth_key);
30 $g_ua = LWP
::UserAgent
->new;
31 $g_ua->cookie_jar({}); # In-memory jar, look at HTTP::Cookies for persistant
39 my ( $response, $data );
41 $session = "" if not defined $session;
42 $auth_key = "" if not defined $auth_key;
43 my $url = $api_base . $node;
45 $param->{'session'} = $session;
46 $param->{'auth_key'} = $auth_key;
48 $response = $g_ua->post( $url, $param );
49 if ( $response->is_success )
51 if ( $response->content =~ m/\s*{/ )
53 $data = decode_json
( $response->content);
57 $data = $response->content;
66 my $response = $g_ua->post( $api_base . "auth/login",
67 [ "api_key" => $api_key ] );
69 ok
($response->is_success, 'login_apikey request is_success');
70 my $data = decode_json
( $response->content) if $response->is_success;
71 die("HTTP error") unless $response->is_success;
73 is
( $data->{'response'}, 'ok', 'login_apikey logged in');
74 ok
( $data->{'session'}, 'login_apikey session set');
75 ok
( $data->{'auth_key'}, 'login_apikey auth_key set');
76 if ( $data->{'response'} eq "ok" )
78 $session = $data->{'session'};
79 $auth_key = $data->{'auth_key'};
85 undef $data; undef $response;
90 my $response = $g_ua->post( $api_base . "auth/login",
91 [ "username" => $test_username,
92 "password" => $test_password ] );
95 ok
($response->is_success, 'login_user request is_success');
96 my $data = decode_json
( $response->content) if $response->is_success;
97 die("HTTP error") unless $response->is_success;
99 is
( $data->{'response'}, 'ok', 'login_user logged in');
100 ok
( $data->{'session'}, 'login_user session set');
101 ok
( $data->{'auth_key'}, 'login_user auth_key set');
102 if ( $data->{'response'} eq "ok" )
104 $session = $data->{'session'};
105 $auth_key = $data->{'auth_key'};
111 undef $data; undef $response;
116 my $response = $g_ua->post( $api_base . "auth/logout",
117 [ "session" => $session ] );
119 ok
($response->is_success, 'logout request is_success');
120 die("HTTP error") unless $response->is_success;
122 my $data = decode_json
( $response->content) if $response->is_success;
123 is
( $data->{'response'}, 'ok', 'logout ok');
125 undef $session; undef $auth_key;