]> git.defcon.no Git - hermes/blob - api/t/tests_common.pm
Making the test-harness more stream-lined. Only updated auth.t so far...
[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 "?session=" . $session .
45 "&auth_key=" . $auth_key;
46
47 foreach my $key ( keys %$param )
48 {
49 $url .= "&" . $key . "=" . $param->{$key};
50 }
51 $response = $g_ua->get( $url );
52 if ( $response->is_success )
53 {
54 if ( $response->content =~ m/\s*{/ )
55 {
56 $data = decode_json( $response->content);
57 }
58 else
59 {
60 $data = $response->content;
61 }
62
63 }
64 return $data;
65 }
66
67 sub login_apikey
68 {
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;
74
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" )
79 {
80 $session = $data->{'session'};
81 $auth_key = $data->{'auth_key'};
82 }
83 else
84 {
85 exit;
86 }
87 undef $data; undef $response;
88 }
89
90 sub login_user
91 {
92 my $response = $g_ua->get( $api_base . "auth/login" .
93 "?username=" . $test_username .
94 "&password=" . $test_password );
95
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;
99
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" )
104 {
105 $session = $data->{'session'};
106 $auth_key = $data->{'auth_key'};
107 }
108 else
109 {
110 exit;
111 }
112 undef $data; undef $response;
113 }
114
115 sub logout
116 {
117 my $response = $g_ua->get( $api_base . "auth/logout" .
118 "?session=" . $session );
119
120 ok ($response->is_success, 'logout request is_success');
121 die("HTTP error") unless $response->is_success;
122
123 my $data = decode_json( $response->content) if $response->is_success;
124 is( $data->{'response'}, 'ok', 'logout ok');
125
126 undef $session; undef $auth_key;
127 }
128
129
130 1;