]>
git.defcon.no Git - hermes/blob - api/t/tests_common.pm
9c2ec83266683cc68612be6476ad258c5fdfda4a
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 .
44 "?session=" . $session .
45 "&auth_key=" . $auth_key;
47 foreach my $key ( keys %$param )
49 $url .= "&" . $key . "=" . $param->{$key};
51 $response = $g_ua->get( $url );
52 if ( $response->is_success )
54 if ( $response->content =~ m/\s*{/ )
56 $data = decode_json
( $response->content);
60 $data = $response->content;
69 my $response = $g_ua->get( $api_base . "auth/login" .
70 "?api_key=" . $api_key );
71 ok
($response->is_success, 'login_apikey request is_success');
72 my $data = decode_json
( $response->content) if $response->is_success;
73 die("HTTP error") unless $response->is_success;
75 is
( $data->{'response'}, 'ok', 'login_apikey logged in');
76 ok
( $data->{'session'}, 'login_apikey session set');
77 ok
( $data->{'auth_key'}, 'login_apikey auth_key set');
78 if ( $data->{'response'} eq "ok" )
80 $session = $data->{'session'};
81 $auth_key = $data->{'auth_key'};
87 undef $data; undef $response;
92 my $response = $g_ua->get( $api_base . "auth/login" .
93 "?username=" . $test_username .
94 "&password=" . $test_password );
96 ok
($response->is_success, 'login_user request is_success');
97 my $data = decode_json
( $response->content) if $response->is_success;
98 die("HTTP error") unless $response->is_success;
100 is
( $data->{'response'}, 'ok', 'login_user logged in');
101 ok
( $data->{'session'}, 'login_user session set');
102 ok
( $data->{'auth_key'}, 'login_user auth_key set');
103 if ( $data->{'response'} eq "ok" )
105 $session = $data->{'session'};
106 $auth_key = $data->{'auth_key'};
112 undef $data; undef $response;
117 my $response = $g_ua->get( $api_base . "auth/logout" .
118 "?session=" . $session );
120 ok
($response->is_success, 'logout request is_success');
121 die("HTTP error") unless $response->is_success;
123 my $data = decode_json
( $response->content) if $response->is_success;
124 is
( $data->{'response'}, 'ok', 'logout ok');
126 undef $session; undef $auth_key;