]> git.defcon.no Git - hermes/blob - api/t/tests_common.pm
ee6d3a5507c8d4754951a3acc0ef0ec20046fc1c
[hermes] / api / t / tests_common.pm
1 package tests_common;
2 # Copyright (c) 2012, Gjøvik University College
3 # All rights reserved.
4
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.
15 #
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.
26
27 use Test::More;
28
29 use strict;
30 use warnings;
31
32 use LWP;
33 use Data::Dumper;
34 use JSON;
35
36 BEGIN {
37 use Exporter();
38 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
39 @ISA = qw(Exporter);
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'} } );
43 }
44 our $api_base = "http://10.0.2.5/hermes/api/";
45 our $api_key = "4hErgw3QFJLeuXGW";
46 our $invalid_key = "invalidkey" . int(rand(255));
47
48 our $test_username = "testauth-" . int(rand(255));
49 our $test_password = "foobarbaz";
50 our $invalid_username = "failauth-" . int(rand(255));
51
52 our ($g_ua, $session, $auth_key);
53
54 $g_ua = LWP::UserAgent->new;
55 $g_ua->cookie_jar({}); # In-memory jar, look at HTTP::Cookies for persistant
56
57
58 sub exec_apinode($$)
59 {
60 my $node = shift;
61 my $param = shift;
62
63 my ( $response, $data );
64
65 $session = "" if not defined $session;
66 $auth_key = "" if not defined $auth_key;
67 my $url = $api_base . $node;
68
69 $param->{'session'} = $session;
70 $param->{'auth_key'} = $auth_key;
71
72 $response = $g_ua->post( $url, $param );
73 if ( $response->is_success )
74 {
75 if ( $response->content =~ m/\s*{/ )
76 {
77 $data = decode_json( $response->content);
78 }
79 else
80 {
81 $data = $response->content;
82 }
83
84 }
85 return $data;
86 }
87
88 sub login_apikey
89 {
90 my $response = $g_ua->post( $api_base . "auth/login",
91 [ "api_key" => $api_key ] );
92
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;
96
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" )
101 {
102 $session = $data->{'session'};
103 $auth_key = $data->{'auth_key'};
104 }
105 else
106 {
107 exit;
108 }
109 undef $data; undef $response;
110 }
111
112 sub login_user
113 {
114 my $response = $g_ua->post( $api_base . "auth/login",
115 [ "username" => $test_username,
116 "password" => $test_password ] );
117
118
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;
122
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" )
127 {
128 $session = $data->{'session'};
129 $auth_key = $data->{'auth_key'};
130 }
131 else
132 {
133 exit;
134 }
135 undef $data; undef $response;
136 }
137
138 sub logout
139 {
140 my $response = $g_ua->post( $api_base . "auth/logout",
141 [ "session" => $session ] );
142
143 ok ($response->is_success, 'logout request is_success');
144 die("HTTP error") unless $response->is_success;
145
146 my $data = decode_json( $response->content) if $response->is_success;
147 is( $data->{'response'}, 'ok', 'logout ok');
148
149 undef $session; undef $auth_key;
150 }
151
152
153 1;