]> git.defcon.no Git - hermes/blob - api/t/tests_common.pm
59a43e88e94d590074a4a2a14ec4260b6ef331e2
[hermes] / api / t / tests_common.pm
1 package tests_common;
2
3 use Test::More;
4
5 use strict;
6 use warnings;
7
8 use LWP;
9 use Data::Dumper;
10 use JSON;
11
12 BEGIN {
13 use Exporter();
14 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
15 @ISA = qw(Exporter);
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'} } );
19 }
20 our $api_base = "http://10.0.2.5/hermes/api/";
21 our $api_key = "4hErgw3QFJLeuXGW";
22 our $invalid_key = "invalidkey" . int(rand(255));
23
24 our $test_username = "testauth-" . int(rand(255));
25 our $test_password = "foobarbaz";
26 our $invalid_username = "failauth-" . int(rand(255));
27
28 our ($g_ua, $session, $auth_key);
29
30 $g_ua = LWP::UserAgent->new;
31 $g_ua->cookie_jar({}); # In-memory jar, look at HTTP::Cookies for persistant
32
33
34 sub exec_apinode($$)
35 {
36 my $node = shift;
37 my $param = shift;
38
39 my ( $response, $data );
40
41 $session = "" if not defined $session;
42 $auth_key = "" if not defined $auth_key;
43 my $url = $api_base . $node;
44
45 $param->{'session'} = $session;
46 $param->{'auth_key'} = $auth_key;
47
48 $response = $g_ua->post( $url, $param );
49 if ( $response->is_success )
50 {
51 if ( $response->content =~ m/\s*{/ )
52 {
53 $data = decode_json( $response->content);
54 }
55 else
56 {
57 $data = $response->content;
58 }
59
60 }
61 return $data;
62 }
63
64 sub login_apikey
65 {
66 my $response = $g_ua->post( $api_base . "auth/login",
67 [ "api_key" => $api_key ] );
68
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;
72
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" )
77 {
78 $session = $data->{'session'};
79 $auth_key = $data->{'auth_key'};
80 }
81 else
82 {
83 exit;
84 }
85 undef $data; undef $response;
86 }
87
88 sub login_user
89 {
90 my $response = $g_ua->post( $api_base . "auth/login",
91 [ "username" => $test_username,
92 "password" => $test_password ] );
93
94
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;
98
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" )
103 {
104 $session = $data->{'session'};
105 $auth_key = $data->{'auth_key'};
106 }
107 else
108 {
109 exit;
110 }
111 undef $data; undef $response;
112 }
113
114 sub logout
115 {
116 my $response = $g_ua->post( $api_base . "auth/logout",
117 [ "session" => $session ] );
118
119 ok ($response->is_success, 'logout request is_success');
120 die("HTTP error") unless $response->is_success;
121
122 my $data = decode_json( $response->content) if $response->is_success;
123 is( $data->{'response'}, 'ok', 'logout ok');
124
125 undef $session; undef $auth_key;
126 }
127
128
129 1;