]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/live_t/02-simple_circ.t
tests against stock test data and live Evergreen
[working/Evergreen.git] / Open-ILS / src / perlmods / live_t / 02-simple_circ.t
1 #!perl
2
3 use Test::More tests => 14;
4
5 diag("Test circulation of item CONC70000345 against the admin user.");
6
7 use constant WORKSTATION_NAME => 'BR4-test-02-simple-circ.t';
8 use constant WORKSTATION_LIB => 7;
9 use constant ITEM_BARCODE => 'CONC70000345';
10 use constant ITEM_ID => 310;
11
12 use strict;
13 use warnings;
14 use Data::Dumper;
15 use OpenSRF::System;
16 use OpenSRF::AppSession;
17 use Digest::MD5 qw(md5_hex);
18 use OpenILS::Utils::Fieldmapper;
19 use OpenILS::Application::AppUtils;
20 use OpenSRF::Utils::SettingsClient;
21
22 # Some useful objects
23 our $cache      = "OpenSRF::Utils::Cache";
24 our $apputils   = "OpenILS::Application::AppUtils";
25 our $memcache;
26 our $authtoken;
27 our $authtime;
28
29 #----------------------------------------------------------------
30 # Exit a script
31 #----------------------------------------------------------------
32 sub err {
33     my ($pkg, $file, $line, $sub)  = _caller();
34     no warnings;
35     die "Script halted with error ".
36         "($pkg : $file : $line : $sub):\n" . shift() . "\n";
37 }
38
39 #----------------------------------------------------------------
40 # This is not the function you're looking for
41 #----------------------------------------------------------------
42 sub _caller {
43     my ($pkg, $file, $line, $sub)  = caller(2);
44     if(!$line) {
45         ($pkg, $file, $line)  = caller(1);
46         $sub = "";
47     }
48     return ($pkg, $file, $line, $sub);
49 }
50
51 #----------------------------------------------------------------
52 # Connect to the servers
53 #----------------------------------------------------------------
54 sub osrf_connect {
55     my $config = `osrf_config --sysconfdir`;
56     chomp $config;
57     $config .= '/opensrf_core.xml';
58     err("Bootstrap config required") unless $config;
59     OpenSRF::System->bootstrap_client( config_file => $config );
60     Fieldmapper->import(IDL =>
61         OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
62 }
63
64 #----------------------------------------------------------------
65 # Is the given object an OILS event?
66 #----------------------------------------------------------------
67 sub oils_is_event {
68     my $e = shift;
69     if( $e and ref($e) eq 'HASH' ) {
70         return 1 if defined($e->{ilsevent});
71     }
72     return 0;
73 }
74
75 #----------------------------------------------------------------
76 # If the given object is an event, this prints the event info 
77 # and exits the script
78 #----------------------------------------------------------------
79 sub oils_event_die {
80     my $evt = shift;
81     my ($pkg, $file, $line, $sub)  = _caller();
82     if(oils_is_event($evt)) {
83         if($evt->{ilsevent}) {
84             diag("\nReceived Event($pkg : $file : $line : $sub): \n" . Dumper($evt));
85             exit 1;
86         }
87     }
88 }
89
90 #----------------------------------------------------------------
91 # Login to the auth server and set the global $authtoken var
92 #----------------------------------------------------------------
93 sub oils_login {
94     my( $username, $password, $type, $ws ) = @_;
95
96     $type |= "staff";
97
98     my $seed = $apputils->simplereq( 'open-ils.auth',
99         'open-ils.auth.authenticate.init', $username );
100     err("No auth seed") unless $seed;
101
102     my $response = $apputils->simplereq( 'open-ils.auth',
103         'open-ils.auth.authenticate.complete',
104         {   username => $username,
105             password => md5_hex($seed . md5_hex($password)),
106             type => $type, workstation => $ws });
107
108     err("No auth response returned on login") unless $response;
109
110     oils_event_die($response);
111
112     $authtime  = $response->{payload}->{authtime};
113     $authtoken = $response->{payload}->{authtoken};
114     diag("authtime is $authtime, authtoken is $authtoken");
115     return $authtoken;
116 }
117
118 #----------------------------------------------------------------
119 # Destroys the login session on the server
120 #----------------------------------------------------------------
121 sub oils_logout {
122     $apputils->simplereq(
123         'open-ils.auth',
124         'open-ils.auth.session.delete', (@_ ? shift : $authtoken) );
125 }
126
127 #----------------------------------------------------------------
128 # var $response = simplereq( $service, $method, @params );
129 #----------------------------------------------------------------
130 sub simplereq    { return $apputils->simplereq(@_); }
131 sub osrf_request { return $apputils->simplereq(@_); }
132
133 #----------------------------------------------------------------
134
135 sub register_workstation {
136     my $resp = osrf_request(
137         'open-ils.actor',
138         'open-ils.actor.workstation.register',
139         $authtoken, WORKSTATION_NAME, WORKSTATION_LIB);
140     return $resp;
141 }
142
143 sub do_checkout {
144     my( $patronid, $barcode ) = @_;
145     my $args = { patron => $patronid, barcode => $barcode };
146     my $resp = osrf_request(
147         'open-ils.circ',
148         'open-ils.circ.checkout.full', $authtoken, $args );
149     return $resp;
150 }
151
152 sub do_checkin {
153     my $barcode  = shift;
154     my $args = { barcode => $barcode };
155     my $resp = osrf_request(
156         'open-ils.circ',
157         'open-ils.circ.checkin', $authtoken, $args );
158     return $resp;
159 }
160
161 #----------------------------------------------------------------
162 # The tests...  assumes stock sample data, full-auto install by
163 # eg_wheezy_installer.sh, etc.
164 #----------------------------------------------------------------
165
166 osrf_connect();
167 my $storage_ses = OpenSRF::AppSession->create('open-ils.storage');
168
169 my $user_req = $storage_ses->request('open-ils.storage.direct.actor.user.retrieve', 1);
170 if (my $user_resp = $user_req->recv) {
171     if (my $user = $user_resp->content) {
172         is(
173             ref $user,
174             'Fieldmapper::actor::user',
175             'open-ils.storage.direct.actor.user.retrieve returned aou object'
176         );
177         is(
178             $user->usrname,
179             'admin',
180             'User with id = 1 is admin user'
181         );
182     }
183 }
184
185 my $item_req = $storage_ses->request('open-ils.storage.direct.asset.copy.retrieve', ITEM_ID);
186 if (my $item_resp = $item_req->recv) {
187     if (my $item = $item_resp->content) {
188         is(
189             ref $item,
190             'Fieldmapper::asset::copy',
191             'open-ils.storage.direct.asset.copy.retrieve returned acp object'
192         );
193         is(
194             $item->barcode,
195             ITEM_BARCODE,
196             'Item with id = ' . ITEM_ID . ' has barcode ' . ITEM_BARCODE
197         );
198         ok(
199             $item->status == 7 || $item->status == 0,
200             'Item with id = ' . ITEM_ID . ' has status of Reshelving or Available'
201         );
202     }
203 }
204
205 oils_login('admin','demo123','staff');
206 ok(
207     $authtoken,
208     'Have an authtoken'
209 );
210 my $ws = register_workstation();
211 ok(
212     ! ref $ws,
213     'Registered a new workstation'
214 );
215
216 oils_logout();
217 oils_login('admin','demo123','staff',WORKSTATION_NAME);
218 ok(
219     $authtoken,
220     'Have an authtoken associated with the workstation'
221 );
222
223 my $checkout_resp = do_checkout(1, ITEM_BARCODE);
224 is(
225     ref $checkout_resp,
226     'HASH',
227     'Checkout request returned a HASH'
228 );
229 is(
230     $checkout_resp->{ilsevent},
231     0,
232     'Checkout returned a SUCCESS event'
233 );
234    
235 $item_req = $storage_ses->request('open-ils.storage.direct.asset.copy.retrieve', 310);
236 if (my $item_resp = $item_req->recv) {
237     if (my $item = $item_resp->content) {
238         is(
239             $item->status,
240             1,
241             'Item with id = ' . ITEM_ID . ' has status of Checked Out after fresh Storage request'
242         );
243     }
244 }
245
246 my $checkin_resp = do_checkin(ITEM_BARCODE);
247 is(
248     ref $checkin_resp,
249     'HASH',
250     'Checkin request returned a HASH'
251 );
252 is(
253     $checkin_resp->{ilsevent},
254     0,
255     'Checkin returned a SUCCESS event'
256 );
257
258 $item_req = $storage_ses->request('open-ils.storage.direct.asset.copy.retrieve', ITEM_ID);
259 if (my $item_resp = $item_req->recv) {
260     if (my $item = $item_resp->content) {
261         ok(
262             $item->status == 7 || $item->status == 0,
263             'Item with id = ' . ITEM_ID . ' has status of Reshelving or Available after fresh Storage request'
264         );
265     }
266 }
267
268 oils_logout();
269
270