]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/live_t/05-pay_bills.t
LP#1670407 Add tests for xact_finish close/re-open
[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, full-auto install by
47 # eg_wheezy_installer.sh, etc.
48 #----------------------------------------------------------------
49
50 my $storage_ses = $script->session('open-ils.storage');
51
52 my $user_obj;
53 my $user_req = $storage_ses->request('open-ils.storage.direct.actor.user.retrieve', USER_ID);
54 if (my $user_resp = $user_req->recv) {
55     if ($user_obj = $user_resp->content) {
56         is(
57             ref $user_obj,
58             'Fieldmapper::actor::user',
59             'open-ils.storage.direct.actor.user.retrieve returned aou object'
60         );
61         is(
62             $user_obj->usrname,
63             USER_USRNAME,
64             'User with id = ' . USER_ID . ' is ' . USER_USRNAME . ' user'
65         );
66     }
67 }
68
69 $script->authenticate({
70     username => 'admin',
71     password => 'demo123',
72     type => 'staff'});
73 ok(
74     $script->authtoken,
75     'Have an authtoken'
76 );
77 my $ws = $script->register_workstation(WORKSTATION_NAME,WORKSTATION_LIB);
78 ok(
79     ! ref $ws,
80     'Registered a new workstation'
81 );
82
83 $script->logout();
84 $script->authenticate({
85     username => 'admin',
86     password => 'demo123',
87     type => 'staff',
88     workstation => WORKSTATION_NAME});
89 ok(
90     $script->authtoken,
91     'Have an authtoken associated with the workstation'
92 );
93
94 my $summaries = fetch_billing_summaries();
95
96 is(
97     scalar(@{ $summaries }),
98     2,
99     'Two billable xacts for ' . USER_USRNAME . ' user from previous tests'
100 );
101
102 is(
103     @{ $summaries }[0]->balance_owed + @{ $summaries }[1]->balance_owed,
104     1.25,
105     'Both transactions combined have a balance owed of 1.25'
106 );
107
108 my $payment_blob = {
109     userid => USER_ID,
110     note => '05-pay_bills.t',
111     payment_type => 'cash_payment',
112     patron_credit => '0.00',
113     payments => [ map { [ $_->id, $_->balance_owed ] } @{ $summaries } ]
114 };
115
116 my $pay_resp = pay_bills($user_obj,$payment_blob);
117
118 is(
119     ref $pay_resp,
120     'HASH',
121     'Payment attempt returned HASH'
122 );
123
124 is(
125     scalar( @{ $pay_resp->{payments} } ),
126     2,
127     'Payment response included two payment ids'
128 );
129
130 my $new_summaries = fetch_billing_summaries();
131 is(
132     scalar(@{ $new_summaries }),
133     0,
134     'Zero billable xacts for ' . USER_USRNAME . ' user after payment'
135 );
136
137 $script->logout();
138
139