]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/live_t/03-overdue_circ.t
tests against stock test data and live Evergreen
[working/Evergreen.git] / Open-ILS / src / perlmods / live_t / 03-overdue_circ.t
1 #!perl
2
3 use Test::More tests => 20;
4
5 diag("Test fine generation on checkin against the admin user.");
6
7 use constant WORKSTATION_NAME => 'BR4-test-03-overdue-circ.t';
8 use constant WORKSTATION_LIB => 7;
9 use constant ITEM_BARCODE => 'CONC71000345';
10 use constant ITEM_ID => 810;
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 DateTime;
21 use DateTime::Format::ISO8601;
22 use OpenSRF::Utils qw/cleanse_ISO8601/;
23 use OpenSRF::Utils::SettingsClient;
24
25 # Some useful objects
26 our $cache      = "OpenSRF::Utils::Cache";
27 our $apputils   = "OpenILS::Application::AppUtils";
28 our $memcache;
29 our $authtoken;
30 our $authtime;
31
32 #----------------------------------------------------------------
33 # Exit a script
34 #----------------------------------------------------------------
35 sub err {
36     my ($pkg, $file, $line, $sub)  = _caller();
37     no warnings;
38     die "Script halted with error ".
39         "($pkg : $file : $line : $sub):\n" . shift() . "\n";
40 }
41
42 #----------------------------------------------------------------
43 # This is not the function you're looking for
44 #----------------------------------------------------------------
45 sub _caller {
46     my ($pkg, $file, $line, $sub)  = caller(2);
47     if(!$line) {
48         ($pkg, $file, $line)  = caller(1);
49         $sub = "";
50     }
51     return ($pkg, $file, $line, $sub);
52 }
53
54 #----------------------------------------------------------------
55 # Connect to the servers
56 #----------------------------------------------------------------
57 sub osrf_connect {
58     my $config = `osrf_config --sysconfdir`;
59     chomp $config;
60     $config .= '/opensrf_core.xml';
61     err("Bootstrap config required") unless $config;
62     OpenSRF::System->bootstrap_client( config_file => $config );
63     Fieldmapper->import(IDL =>
64         OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
65 }
66
67 #----------------------------------------------------------------
68 # Is the given object an OILS event?
69 #----------------------------------------------------------------
70 sub oils_is_event {
71     my $e = shift;
72     if( $e and ref($e) eq 'HASH' ) {
73         return 1 if defined($e->{ilsevent});
74     }
75     return 0;
76 }
77
78 #----------------------------------------------------------------
79 # If the given object is an event, this prints the event info 
80 # and exits the script
81 #----------------------------------------------------------------
82 sub oils_event_die {
83     my $evt = shift;
84     my ($pkg, $file, $line, $sub)  = _caller();
85     if(oils_is_event($evt)) {
86         if($evt->{ilsevent}) {
87             diag("\nReceived Event($pkg : $file : $line : $sub): \n" . Dumper($evt));
88             exit 1;
89         }
90     }
91 }
92
93 #----------------------------------------------------------------
94 # Login to the auth server and set the global $authtoken var
95 #----------------------------------------------------------------
96 sub oils_login {
97     my( $username, $password, $type, $ws ) = @_;
98
99     $type |= "staff";
100
101     my $seed = $apputils->simplereq( 'open-ils.auth',
102         'open-ils.auth.authenticate.init', $username );
103     err("No auth seed") unless $seed;
104
105     my $response = $apputils->simplereq( 'open-ils.auth',
106         'open-ils.auth.authenticate.complete',
107         {   username => $username,
108             password => md5_hex($seed . md5_hex($password)),
109             type => $type, workstation => $ws });
110
111     err("No auth response returned on login") unless $response;
112
113     oils_event_die($response);
114
115     $authtime  = $response->{payload}->{authtime};
116     $authtoken = $response->{payload}->{authtoken};
117     diag("authtime is $authtime, authtoken is $authtoken");
118     return $authtoken;
119 }
120
121 #----------------------------------------------------------------
122 # Destroys the login session on the server
123 #----------------------------------------------------------------
124 sub oils_logout {
125     $apputils->simplereq(
126         'open-ils.auth',
127         'open-ils.auth.session.delete', (@_ ? shift : $authtoken) );
128 }
129
130 #----------------------------------------------------------------
131 # var $response = simplereq( $service, $method, @params );
132 #----------------------------------------------------------------
133 sub simplereq    { return $apputils->simplereq(@_); }
134 sub osrf_request { return $apputils->simplereq(@_); }
135
136 #----------------------------------------------------------------
137
138 sub register_workstation {
139     my $resp = osrf_request(
140         'open-ils.actor',
141         'open-ils.actor.workstation.register',
142         $authtoken, WORKSTATION_NAME, WORKSTATION_LIB);
143     return $resp;
144 }
145
146 sub do_checkout {
147     my( $patronid, $barcode ) = @_;
148     my $args = { patron => $patronid, barcode => $barcode };
149     my $resp = osrf_request(
150         'open-ils.circ',
151         'open-ils.circ.checkout.full', $authtoken, $args );
152     return $resp;
153 }
154
155 sub do_checkin {
156     my $barcode  = shift;
157     my $args = { barcode => $barcode };
158     my $resp = osrf_request(
159         'open-ils.circ',
160         'open-ils.circ.checkin', $authtoken, $args );
161     return $resp;
162 }
163
164 #----------------------------------------------------------------
165 # The tests...  assumes stock sample data, full-auto install by
166 # eg_wheezy_installer.sh, etc.
167 #----------------------------------------------------------------
168
169 osrf_connect();
170 my $storage_ses = OpenSRF::AppSession->create('open-ils.storage');
171 my $circ_ses = OpenSRF::AppSession->create('open-ils.circ');
172 my $cstore_ses = OpenSRF::AppSession->connect('open-ils.cstore');
173
174 my $user_req = $storage_ses->request('open-ils.storage.direct.actor.user.retrieve', 1);
175 if (my $user_resp = $user_req->recv) {
176     if (my $user = $user_resp->content) {
177         is(
178             ref $user,
179             'Fieldmapper::actor::user',
180             'open-ils.storage.direct.actor.user.retrieve returned aou object'
181         );
182         is(
183             $user->usrname,
184             'admin',
185             'User with id = 1 is admin user'
186         );
187     }
188 }
189
190 my $item_req = $storage_ses->request('open-ils.storage.direct.asset.copy.retrieve', ITEM_ID);
191 if (my $item_resp = $item_req->recv) {
192     if (my $item = $item_resp->content) {
193         is(
194             ref $item,
195             'Fieldmapper::asset::copy',
196             'open-ils.storage.direct.asset.copy.retrieve returned acp object'
197         );
198         is(
199             $item->barcode,
200             ITEM_BARCODE,
201             'Item with id = ' . ITEM_ID . ' has barcode ' . ITEM_BARCODE
202         );
203         ok(
204             $item->status == 7 || $item->status == 0,
205             'Item with id = ' . ITEM_ID . ' has status of Reshelving or Available'
206         );
207     }
208 }
209
210 oils_login('admin','demo123','staff');
211 ok(
212     $authtoken,
213     'Have an authtoken'
214 );
215 my $ws = register_workstation();
216 ok(
217     ! ref $ws,
218     'Registered a new workstation'
219 );
220
221 oils_logout();
222 oils_login('admin','demo123','staff',WORKSTATION_NAME);
223 ok(
224     $authtoken,
225     'Have an authtoken associated with the workstation'
226 );
227
228 my $checkout_resp = do_checkout(1, ITEM_BARCODE);
229 is(
230     ref $checkout_resp,
231     'HASH',
232     'Checkout request returned a HASH'
233 );
234 is(
235     $checkout_resp->{ilsevent},
236     0,
237     'Checkout returned a SUCCESS event'
238 );
239 ok(
240     ref $checkout_resp->{payload},
241     'Checkout response object has payload object'
242 );
243 ok(
244     ref $checkout_resp->{payload}->{circ},
245     'Payload object has circ object'
246 );
247 is(
248     $checkout_resp->{payload}->{circ}->duration,
249     '7 days',
250     'Circ objection has loan duration of "7 days"'
251 );
252
253 my $circ = $checkout_resp->{payload}->{circ};
254
255 $item_req = $storage_ses->request('open-ils.storage.direct.asset.copy.retrieve', ITEM_ID);
256 if (my $item_resp = $item_req->recv) {
257     if (my $item = $item_resp->content) {
258         is(
259             $item->status,
260             1,
261             'Item with id = ' . ITEM_ID . ' has status of Checked Out after fresh Storage request'
262         );
263     }
264 }
265
266 my $bill_req = $circ_ses->request(
267     'open-ils.circ.money.billing.retrieve.all',
268     $authtoken,
269     $circ->id
270 );
271 if (my $bill_resp = $bill_req->recv) {
272     if (my $bills = $bill_resp->content) {
273         is(
274             scalar( @{ $bills } ),
275             0,
276             'Zero bills associated with circulation'
277         );
278     }
279 }
280
281 my $xact_start = DateTime::Format::ISO8601->parse_datetime(cleanse_ISO8601($circ->xact_start))->epoch;
282 my $due_date = DateTime::Format::ISO8601->parse_datetime(cleanse_ISO8601($circ->due_date))->epoch;
283 my $twenty_days = OpenSRF::Utils->interval_to_seconds('480 h 0 m 0 s');
284
285 # Rewrite history; technically we should rewrite status_changed_item on the copy as well, but, meh...
286 $circ->xact_start( $apputils->epoch2ISO8601($xact_start - $twenty_days) );
287 $circ->due_date( $apputils->epoch2ISO8601($due_date - $twenty_days) );
288
289 my $xact = $cstore_ses->request('open-ils.cstore.transaction.begin')->gather(1);
290 my $update_req = $cstore_ses->request(
291     'open-ils.cstore.direct.action.circulation.update',
292     $circ
293 );
294 if (my $update_resp = $update_req->gather(1)) {
295     pass(
296         'rewrote circ to have happened 20 days ago'
297     );
298 } else {
299     fail(
300         'rewrote circ to have happened 20 days ago'
301     );
302 }
303 $cstore_ses->request('open-ils.cstore.transaction.commit')->gather(1);
304
305 ########
306
307 my $checkin_resp = do_checkin(ITEM_BARCODE);
308 is(
309     ref $checkin_resp,
310     'HASH',
311     'Checkin request returned a HASH'
312 );
313 is(
314     $checkin_resp->{ilsevent},
315     0,
316     'Checkin returned a SUCCESS event'
317 );
318
319 $item_req = $storage_ses->request('open-ils.storage.direct.asset.copy.retrieve', ITEM_ID);
320 if (my $item_resp = $item_req->recv) {
321     if (my $item = $item_resp->content) {
322         ok(
323             $item->status == 7 || $item->status == 0,
324             'Item with id = ' . ITEM_ID . ' has status of Reshelving or Available after fresh Storage request'
325         );
326     }
327 }
328
329 $bill_req = $circ_ses->request(
330     'open-ils.circ.money.billing.retrieve.all',
331     $authtoken,
332     $circ->id
333 );
334 if (my $bill_resp = $bill_req->recv) {
335     if (my $bills = $bill_resp->content) {
336         is(
337             scalar( @{ $bills } ),
338             13,
339             'Thirteen bills associated with circulation'
340         );
341     }
342 }
343
344
345 oils_logout();
346
347