3 use Test::More tests => 10;
5 diag("Test bill payment against the admin user.");
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';
16 use OpenSRF::AppSession;
17 use Digest::MD5 qw(md5_hex);
18 use OpenILS::Utils::Fieldmapper;
19 use OpenILS::Application::AppUtils;
21 use DateTime::Format::ISO8601;
22 use OpenSRF::Utils qw/cleanse_ISO8601/;
23 use OpenSRF::Utils::SettingsClient;
26 our $cache = "OpenSRF::Utils::Cache";
27 our $apputils = "OpenILS::Application::AppUtils";
32 #----------------------------------------------------------------
34 #----------------------------------------------------------------
36 my ($pkg, $file, $line, $sub) = _caller();
38 die "Script halted with error ".
39 "($pkg : $file : $line : $sub):\n" . shift() . "\n";
42 #----------------------------------------------------------------
43 # This is not the function you're looking for
44 #----------------------------------------------------------------
46 my ($pkg, $file, $line, $sub) = caller(2);
48 ($pkg, $file, $line) = caller(1);
51 return ($pkg, $file, $line, $sub);
54 #----------------------------------------------------------------
55 # Connect to the servers
56 #----------------------------------------------------------------
58 my $config = `osrf_config --sysconfdir`;
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"));
67 #----------------------------------------------------------------
68 # Is the given object an OILS event?
69 #----------------------------------------------------------------
72 if( $e and ref($e) eq 'HASH' ) {
73 return 1 if defined($e->{ilsevent});
78 #----------------------------------------------------------------
79 # If the given object is an event, this prints the event info
80 # and exits the script
81 #----------------------------------------------------------------
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));
93 #----------------------------------------------------------------
94 # Login to the auth server and set the global $authtoken var
95 #----------------------------------------------------------------
97 my( $username, $password, $type, $ws ) = @_;
101 my $seed = $apputils->simplereq( 'open-ils.auth',
102 'open-ils.auth.authenticate.init', $username );
103 err("No auth seed") unless $seed;
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 });
111 err("No auth response returned on login") unless $response;
113 oils_event_die($response);
115 $authtime = $response->{payload}->{authtime};
116 $authtoken = $response->{payload}->{authtoken};
117 diag("authtime is $authtime, authtoken is $authtoken");
121 #----------------------------------------------------------------
122 # Destroys the login session on the server
123 #----------------------------------------------------------------
125 $apputils->simplereq(
127 'open-ils.auth.session.delete', (@_ ? shift : $authtoken) );
130 #----------------------------------------------------------------
131 # var $response = simplereq( $service, $method, @params );
132 #----------------------------------------------------------------
133 sub simplereq { return $apputils->simplereq(@_); }
134 sub osrf_request { return $apputils->simplereq(@_); }
136 #----------------------------------------------------------------
138 sub register_workstation {
139 my $resp = osrf_request(
141 'open-ils.actor.workstation.register',
142 $authtoken, WORKSTATION_NAME, WORKSTATION_LIB);
146 sub fetch_billing_summaries {
147 my $resp = osrf_request(
149 'open-ils.actor.user.transactions.history.have_balance.authoritative',
157 my ($user_obj, $payment_blob) = (shift, shift);
158 my $resp = osrf_request(
160 'open-ils.circ.money.payment',
163 $user_obj->last_xact_id
168 #----------------------------------------------------------------
169 # The tests... assumes stock sample data, full-auto install by
170 # eg_wheezy_installer.sh, etc.
171 #----------------------------------------------------------------
174 my $storage_ses = OpenSRF::AppSession->create('open-ils.storage');
177 my $user_req = $storage_ses->request('open-ils.storage.direct.actor.user.retrieve', USER_ID);
178 if (my $user_resp = $user_req->recv) {
179 if ($user_obj = $user_resp->content) {
182 'Fieldmapper::actor::user',
183 'open-ils.storage.direct.actor.user.retrieve returned aou object'
188 'User with id = ' . USER_ID . ' is ' . USER_USRNAME . ' user'
193 oils_login('admin','demo123','staff');
198 my $ws = register_workstation();
201 'Registered a new workstation'
205 oils_login('admin','demo123','staff',WORKSTATION_NAME);
208 'Have an authtoken associated with the workstation'
211 my $summaries = fetch_billing_summaries();
214 scalar(@{ $summaries }),
216 'Two billable xacts for ' . USER_USRNAME . ' user from previous tests'
220 @{ $summaries }[0]->balance_owed + @{ $summaries }[1]->balance_owed,
222 'Both transactions combined have a balance owed of 1.25'
227 note => '05-pay_bills.t',
228 payment_type => 'cash_payment',
229 patron_credit => '0.00',
230 payments => [ map { [ $_->id, $_->balance_owed ] } @{ $summaries } ]
233 my $pay_resp = pay_bills($user_obj,$payment_blob);
238 'Payment attempt returned HASH'
242 scalar( @{ $pay_resp->{payments} } ),
244 'Payment response included two payment ids'
247 my $new_summaries = fetch_billing_summaries();
249 scalar(@{ $new_summaries }),
251 'Zero billable xacts for ' . USER_USRNAME . ' user after payment'