3 # Tests for SIP::Sip::MsgType
4 # Please help to extend it!
6 # This file is part of Koha.
8 # Copyright 2016 Rijksmuseum
10 # Koha is free software; you can redistribute it and/or modify it
11 # under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
15 # Koha is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with Koha; if not, see <http://www.gnu.org/licenses>.
24 use Test::More tests => 2;
29 use t::lib::TestBuilder;
30 use Koha::AuthUtils qw(hash_password);
32 use C4::SIP::ILS::Patron;
33 use C4::SIP::Sip qw(write_msg);
34 use C4::SIP::Sip::Constants qw(:all);
35 use C4::SIP::Sip::MsgType;
37 use constant PATRON_PW => 'do_not_ever_use_this_one';
39 my $fixed_length = { #length of fixed fields including response code
40 ( PATRON_STATUS_RESP ) => 37,
41 ( PATRON_INFO_RESP ) => 61,
44 my $schema = Koha::Database->new->schema;
45 my $builder = t::lib::TestBuilder->new();
47 # COMMON: Some common stuff for all/most subtests
48 my ( $response, $findpatron, $branch, $branchcode );
49 # mock write_msg (imported from Sip.pm into Message.pm)
50 my $mockMsg = Test::MockModule->new( 'C4::SIP::Sip::MsgType' );
51 $mockMsg->mock( 'write_msg', sub { $response = $_[1]; } ); # save response
53 my $mockILS = Test::MockObject->new;
54 $mockILS->mock( 'check_inst_id', sub {} );
55 $mockILS->mock( 'institution_id', sub { $branchcode; } );
56 $mockILS->mock( 'find_patron', sub { $findpatron; } );
59 subtest 'Testing Patron Status Request V2' => sub {
60 $schema->storage->txn_begin;
62 $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
63 $C4::SIP::Sip::protocol_version = 2;
64 test_request_patron_status_v2();
65 $schema->storage->txn_rollback;
68 subtest 'Testing Patron Info Request V2' => sub {
69 $schema->storage->txn_begin;
71 $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
72 $C4::SIP::Sip::protocol_version = 2;
73 test_request_patron_info_v2();
74 $schema->storage->txn_rollback;
77 # Here is room for some more subtests
81 sub test_request_patron_status_v2 {
82 my $patron1 = $builder->build({
85 password => hash_password( PATRON_PW ),
88 my $card1 = $patron1->{cardnumber};
89 my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
90 $findpatron = $sip_patron1;
92 my $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
93 FID_INST_ID. $branchcode. '|'.
94 FID_PATRON_ID. $card1. '|'.
95 FID_PATRON_PWD. PATRON_PW. '|';
96 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
98 my $server = { ils => $mockILS };
100 $msg->handle_patron_status( $server );
102 isnt( $response, undef, 'At least we got a response.' );
103 my $respcode = substr( $response, 0, 2 );
104 is( $respcode, PATRON_STATUS_RESP, 'Response code fine' );
106 check_field( $respcode, $response, FID_INST_ID, $branchcode , 'Verified institution id' );
107 check_field( $respcode, $response, FID_PATRON_ID, $card1, 'Verified patron id' );
108 check_field( $respcode, $response, FID_PERSONAL_NAME, $patron1->{surname}, 'Verified patron name', 'contains' );
109 check_field( $respcode, $response, FID_VALID_PATRON, 'Y', 'Verified code BL' );
110 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'Y', 'Verified code CQ' );
111 check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'Verified non-empty screen message', 'regex' );
113 # Now, we pass a wrong password and verify CQ again
114 $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
115 FID_INST_ID. $branchcode. '|'.
116 FID_PATRON_ID. $card1. '|'.
117 FID_PATRON_PWD. 'wrong_password'. '|';
118 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
120 $msg->handle_patron_status( $server );
121 $respcode = substr( $response, 0, 2 );
122 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'N', 'Verified code CQ for wrong pw' );
124 # Check empty password and verify CQ again
125 $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
126 FID_INST_ID. $branchcode. '|'.
127 FID_PATRON_ID. $card1. '|'.
129 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
131 $msg->handle_patron_status( $server );
132 $respcode = substr( $response, 0, 2 );
133 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'N', 'code CQ should be N for empty AD' );
135 # Finally, we send a wrong card number and check AE, BL
136 # This is done by removing the new patron first
137 $schema->resultset('Borrower')->search({ cardnumber => $card1 })->delete;
139 $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
140 FID_INST_ID. $branchcode. '|'.
141 FID_PATRON_ID. $card1. '|'.
142 FID_PATRON_PWD. PATRON_PW. '|';
143 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
145 $msg->handle_patron_status( $server );
146 $respcode = substr( $response, 0, 2 );
147 check_field( $respcode, $response, FID_VALID_PATRON, 'N', 'Verified code BL for wrong cardnumber' );
148 check_field( $respcode, $response, FID_PERSONAL_NAME, '', 'Name should be empty now' );
149 check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'But we have a screen msg', 'regex' );
152 sub test_request_patron_info_v2 {
153 my $patron2 = $builder->build({
154 source => 'Borrower',
156 password => hash_password( PATRON_PW ),
159 my $card = $patron2->{cardnumber};
160 my $sip_patron2 = C4::SIP::ILS::Patron->new( $card );
161 $findpatron = $sip_patron2;
162 my $siprequest = PATRON_INFO. 'engYYYYMMDDZZZZHHMMSS'.'Y '.
163 FID_INST_ID. $branchcode. '|'.
164 FID_PATRON_ID. $card. '|'.
165 FID_PATRON_PWD. PATRON_PW. '|';
166 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
168 my $server = { ils => $mockILS };
170 $msg->handle_patron_info( $server );
171 isnt( $response, undef, 'At least we got a response.' );
172 my $respcode = substr( $response, 0, 2 );
173 is( $respcode, PATRON_INFO_RESP, 'Response code fine' );
175 check_field( $respcode, $response, FID_INST_ID, $branchcode , 'Verified institution id' );
176 check_field( $respcode, $response, FID_PATRON_ID, $card, 'Verified patron id' );
177 check_field( $respcode, $response, FID_PERSONAL_NAME, $patron2->{surname}, 'Verified patron name', 'contains' );
178 check_field( $respcode, $response, FID_VALID_PATRON, 'Y', 'Verified code BL' );
179 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'Y', 'Verified code CQ' );
180 check_field( $respcode, $response, FID_FEE_LMT, '.*', 'Checked existence of fee limit', 'regex' );
181 check_field( $respcode, $response, FID_HOME_ADDR, $patron2->{address}, 'Address in BD', 'contains' );
182 check_field( $respcode, $response, FID_EMAIL, $patron2->{email}, 'Verified email in BE' );
183 check_field( $respcode, $response, FID_HOME_PHONE, $patron2->{phone}, 'Verified home phone in BF' );
184 # No check for custom fields here (unofficial PB, PC and PI)
185 check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'We have a screen msg', 'regex' );
187 # Test customized patron name in AE with same sip request
188 # This implicitly tests C4::SIP::ILS::Patron->name
189 $server->{account}->{ae_field_template} = "X[% patron.surname %]Y";
190 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
192 $msg->handle_patron_info( $server );
193 $respcode = substr( $response, 0, 2 );
194 check_field( $respcode, $response, FID_PERSONAL_NAME, 'X' . $patron2->{surname} . 'Y', 'Check customized patron name' );
196 # Check empty password and verify CQ again
197 $siprequest = PATRON_INFO. 'engYYYYMMDDZZZZHHMMSS'.'Y '.
198 FID_INST_ID. $branchcode. '|'.
199 FID_PATRON_ID. $card. '|'.
201 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
203 $msg->handle_patron_info( $server );
204 $respcode = substr( $response, 0, 2 );
205 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'N', 'code CQ should be N for empty AD' );
206 # Test empty password is OK if account configured to allow
207 $server->{account}->{allow_empty_passwords} = 1;
208 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
210 $msg->handle_patron_info( $server );
211 $respcode = substr( $response, 0, 2 );
212 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'Y', 'code CQ should be Y if empty AD allowed' );
214 # Finally, we send a wrong card number
215 $schema->resultset('Borrower')->search({ cardnumber => $card })->delete;
217 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
219 $msg->handle_patron_info( $server );
220 $respcode = substr( $response, 0, 2 );
221 check_field( $respcode, $response, FID_VALID_PATRON, 'N', 'Verified code BL for wrong cardnumber' );
222 check_field( $respcode, $response, FID_PERSONAL_NAME, '', 'Name should be empty now' );
223 check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'But we have a screen msg', 'regex' );
229 my ( $code, $resp, $fld, $expr, $msg, $mode ) = @_;
230 # mode: contains || equals || regex (by default: equals)
232 # strip fixed part; prefix to simplify next regex
233 $resp = '|'. substr( $resp, $fixed_length->{$code} );
235 if( $resp =~ /\|$fld([^\|]*)\|/ ) {
237 } else { # test fails
238 is( 0, 1, "Code $fld not found in '$resp'?" );
242 if( !$mode || $mode eq 'equals' ) { # default
243 is( $fldval, $expr, $msg );
244 } elsif( $mode eq 'regex' ) {
245 is( $fldval =~ /$expr/, 1, $msg );
247 is( index( $fldval, $expr ) > -1, 1, $msg );