]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/live_t/05-pay_bills.t
c93a5867c203b7462962e84e3d2d744df97a12db
[working/Evergreen.git] / Open-ILS / src / perlmods / live_t / 05-pay_bills.t
1 #!perl
2
3 use Test::More tests => 10;
4
5 diag("Test bill payment against the admin user.");
6
7 use constant WORKSTATION_NAME => 'BR4-test-05-pay-bills.t';
8 use constant WORKSTATION_LIB => 7;
9 use constant USER_ID => 1;
10 use constant USER_USRNAME => 'admin';
11
12 use strict; use warnings;
13
14 use OpenILS::Utils::TestUtils;
15 my $script = OpenILS::Utils::TestUtils->new();
16
17 use DateTime;
18 use DateTime::Format::ISO8601;
19 use OpenSRF::Utils qw/cleanse_ISO8601/;
20
21 our $apputils   = "OpenILS::Application::AppUtils";
22
23 sub fetch_billing_summaries {
24     my $resp = $apputils->simplereq(
25         'open-ils.actor',
26         'open-ils.actor.user.transactions.history.have_balance.authoritative',
27         $script->authtoken,
28         USER_ID
29     );
30     return $resp;
31 }
32
33 sub pay_bills {
34     my ($user_obj, $payment_blob) = (shift, shift);
35     my $resp = $apputils->simplereq(
36         'open-ils.circ',
37         'open-ils.circ.money.payment',
38         $script->authtoken,
39         $payment_blob,
40         $user_obj->last_xact_id
41     );
42     return $resp;
43 }
44
45 #----------------------------------------------------------------
46 # The tests...  assumes stock sample data
47 #----------------------------------------------------------------
48
49 my $storage_ses = $script->session('open-ils.storage');
50
51 my $user_obj;
52 my $user_req = $storage_ses->request('open-ils.storage.direct.actor.user.retrieve', USER_ID);
53 if (my $user_resp = $user_req->recv) {
54     if ($user_obj = $user_resp->content) {
55         is(
56             ref $user_obj,
57             'Fieldmapper::actor::user',
58             'open-ils.storage.direct.actor.user.retrieve returned aou object'
59         );
60         is(
61             $user_obj->usrname,
62             USER_USRNAME,
63             'User with id = ' . USER_ID . ' is ' . USER_USRNAME . ' user'
64         );
65     }
66 }
67
68 $script->authenticate({
69     username => 'admin',
70     password => 'demo123',
71     type => 'staff'});
72 ok(
73     $script->authtoken,
74     'Have an authtoken'
75 );
76 my $ws = $script->register_workstation(WORKSTATION_NAME,WORKSTATION_LIB);
77 ok(
78     ! ref $ws,
79     'Registered a new workstation'
80 );
81
82 $script->logout();
83 $script->authenticate({
84     username => 'admin',
85     password => 'demo123',
86     type => 'staff',
87     workstation => WORKSTATION_NAME});
88 ok(
89     $script->authtoken,
90     'Have an authtoken associated with the workstation'
91 );
92
93 my $summaries = fetch_billing_summaries();
94
95 is(
96     scalar(@{ $summaries }),
97     2,
98     'Two billable xacts for ' . USER_USRNAME . ' user from previous tests'
99 );
100
101 is(
102     @{ $summaries }[0]->balance_owed + @{ $summaries }[1]->balance_owed,
103     1.25,
104     'Both transactions combined have a balance owed of 1.25'
105 );
106
107 my $payment_blob = {
108     userid => USER_ID,
109     note => '05-pay_bills.t',
110     payment_type => 'cash_payment',
111     patron_credit => '0.00',
112     payments => [ map { [ $_->id, $_->balance_owed ] } @{ $summaries } ]
113 };
114
115 my $pay_resp = pay_bills($user_obj,$payment_blob);
116
117 is(
118     ref $pay_resp,
119     'HASH',
120     'Payment attempt returned HASH'
121 );
122
123 is(
124     scalar( @{ $pay_resp->{payments} } ),
125     2,
126     'Payment response included two payment ids'
127 );
128
129 my $new_summaries = fetch_billing_summaries();
130 is(
131     scalar(@{ $new_summaries }),
132     0,
133     'Zero billable xacts for ' . USER_USRNAME . ' user after payment'
134 );
135
136 $script->logout();
137
138