]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/live_t/01-auth.t
tests against stock test data and live Evergreen
[working/Evergreen.git] / Open-ILS / src / perlmods / live_t / 01-auth.t
1 #!perl
2
3 use Test::More tests => 4;
4
5 diag("Simple tests against the open-ils.auth service, memcached, and the stock test data.");
6
7 use strict;
8 use warnings;
9 use Data::Dumper;
10 use OpenSRF::System;
11 use OpenSRF::AppSession;
12 use Digest::MD5 qw(md5_hex);
13 use OpenILS::Application::AppUtils;
14 use OpenSRF::Utils::SettingsClient;
15
16 # Some useful objects
17 our $cache      = "OpenSRF::Utils::Cache";
18 our $apputils   = "OpenILS::Application::AppUtils";
19 our $memcache;
20 our $authtoken;
21 our $authtime;
22
23 #----------------------------------------------------------------
24 # Exit a script
25 #----------------------------------------------------------------
26 sub err {
27     my ($pkg, $file, $line, $sub)  = _caller();
28     no warnings;
29     die "Script halted with error ".
30         "($pkg : $file : $line : $sub):\n" . shift() . "\n";
31 }
32
33 #----------------------------------------------------------------
34 # This is not the function you're looking for
35 #----------------------------------------------------------------
36 sub _caller {
37     my ($pkg, $file, $line, $sub)  = caller(2);
38     if(!$line) {
39         ($pkg, $file, $line)  = caller(1);
40         $sub = "";
41     }
42     return ($pkg, $file, $line, $sub);
43 }
44
45 #----------------------------------------------------------------
46 # Connect to the servers
47 #----------------------------------------------------------------
48 sub osrf_connect {
49     my $config = `osrf_config --sysconfdir`;
50     chomp $config;
51     $config .= '/opensrf_core.xml';
52     err("Bootstrap config required") unless $config;
53     OpenSRF::System->bootstrap_client( config_file => $config );
54 }
55
56 #----------------------------------------------------------------
57 # Get a handle for the memcache object
58 #----------------------------------------------------------------
59 sub osrf_cache {
60     $cache->use;
61     $memcache = $cache->new('global') unless $memcache;
62     return $memcache;
63 }
64
65 #----------------------------------------------------------------
66 # Is the given object an OILS event?
67 #----------------------------------------------------------------
68 sub oils_is_event {
69     my $e = shift;
70     if( $e and ref($e) eq 'HASH' ) {
71         return 1 if defined($e->{ilsevent});
72     }
73     return 0;
74 }
75
76 #----------------------------------------------------------------
77 # If the given object is an event, this prints the event info 
78 # and exits the script
79 #----------------------------------------------------------------
80 sub oils_event_die {
81     my $evt = shift;
82     my ($pkg, $file, $line, $sub)  = _caller();
83     if(oils_is_event($evt)) {
84         if($evt->{ilsevent}) {
85             diag("\nReceived Event($pkg : $file : $line : $sub): \n" . Dumper($evt));
86             exit 1;
87         }
88     }
89 }
90
91 #----------------------------------------------------------------
92 # Login to the auth server and set the global $authtoken var
93 #----------------------------------------------------------------
94 sub oils_login {
95     my( $username, $password, $type ) = @_;
96
97     $type |= "staff";
98
99     my $seed = $apputils->simplereq( 'open-ils.auth',
100         'open-ils.auth.authenticate.init', $username );
101     err("No auth seed") unless $seed;
102
103     my $response = $apputils->simplereq( 'open-ils.auth',
104         'open-ils.auth.authenticate.complete',
105         {   username => $username,
106             password => md5_hex($seed . md5_hex($password)),
107             type => $type });
108
109     err("No auth response returned on login") unless $response;
110
111     oils_event_die($response);
112
113     $authtime  = $response->{payload}->{authtime};
114     $authtoken = $response->{payload}->{authtoken};
115     diag("authtime is $authtime, authtoken is $authtoken");
116     return $authtoken;
117 }
118
119 #----------------------------------------------------------------
120 # Destroys the login session on the server
121 #----------------------------------------------------------------
122 sub oils_logout {
123     $apputils->simplereq(
124         'open-ils.auth',
125         'open-ils.auth.session.delete', (@_ ? shift : $authtoken) );
126 }
127
128 #----------------------------------------------------------------
129 # var $response = simplereq( $service, $method, @params );
130 #----------------------------------------------------------------
131 sub simplereq    { return $apputils->simplereq(@_); }
132 sub osrf_request { return $apputils->simplereq(@_); }
133
134 #----------------------------------------------------------------
135 # The tests...  assumes stock sample data, full-auto install by
136 # eg_wheezy_installer.sh, etc.
137 #----------------------------------------------------------------
138
139 osrf_connect();
140 oils_login('admin','demo123','staff');
141
142 ok(
143     $authtoken,
144     'Have an authtoken'
145 );
146 is(
147     $authtime,
148     7200,
149     'Default authtime for staff login is 7200 seconds'
150 );
151
152 osrf_cache();
153 my $cached_obj = $memcache->get_cache("oils_auth_$authtoken");
154
155 ok(
156     ref $cached_obj,
157     'Can retrieve authtoken from memcached'
158 );
159
160 oils_logout();
161
162 $cached_obj = $memcache->get_cache("oils_auth_$authtoken");
163 ok(
164     ! $cached_obj,
165     'Authtoken is removed from memcached after logout'
166 );
167