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; } );
57 $branch = $builder->build({
60 $branchcode = $branch->{branchcode
};
63 subtest
'Testing Patron Status Request V2' => sub {
64 $schema->storage->txn_begin;
66 $C4::SIP
::Sip
::protocol_version
= 2;
67 test_request_patron_status_v2
();
68 $schema->storage->txn_rollback;
71 subtest
'Testing Patron Info Request V2' => sub {
72 $schema->storage->txn_begin;
74 $C4::SIP
::Sip
::protocol_version
= 2;
75 test_request_patron_info_v2
();
76 $schema->storage->txn_rollback;
79 # Here is room for some more subtests
83 sub test_request_patron_status_v2
{
84 my $patron1 = $builder->build({
87 password
=> hash_password
( PATRON_PW
),
90 my $card1 = $patron1->{cardnumber
};
91 my $sip_patron1 = C4
::SIP
::ILS
::Patron
->new( $card1 );
92 $findpatron = $sip_patron1;
94 my $siprequest = PATRON_STATUS_REQ
. 'engYYYYMMDDZZZZHHMMSS'.
95 FID_INST_ID
. $branchcode. '|'.
96 FID_PATRON_ID
. $card1. '|'.
97 FID_PATRON_PWD
. PATRON_PW
. '|';
98 my $msg = C4
::SIP
::Sip
::MsgType
->new( $siprequest, 0 );
100 my $server = { ils
=> $mockILS };
102 $msg->handle_patron_status( $server );
104 isnt
( $response, undef, 'At least we got a response.' );
105 my $respcode = substr( $response, 0, 2 );
106 is
( $respcode, PATRON_STATUS_RESP
, 'Response code fine' );
108 check_field
( $respcode, $response, FID_INST_ID
, $branchcode , 'Verified institution id' );
109 check_field
( $respcode, $response, FID_PATRON_ID
, $card1, 'Verified patron id' );
110 check_field
( $respcode, $response, FID_PERSONAL_NAME
, $patron1->{surname
}, 'Verified patron name', 'contains' );
111 check_field
( $respcode, $response, FID_VALID_PATRON
, 'Y', 'Verified code BL' );
112 check_field
( $respcode, $response, FID_VALID_PATRON_PWD
, 'Y', 'Verified code CQ' );
113 check_field
( $respcode, $response, FID_SCREEN_MSG
, '.+', 'Verified non-empty screen message', 'regex' );
115 # Now, we pass a wrong password and verify CQ again
116 $siprequest = PATRON_STATUS_REQ
. 'engYYYYMMDDZZZZHHMMSS'.
117 FID_INST_ID
. $branchcode. '|'.
118 FID_PATRON_ID
. $card1. '|'.
119 FID_PATRON_PWD
. 'wrong_password'. '|';
120 $msg = C4
::SIP
::Sip
::MsgType
->new( $siprequest, 0 );
122 $msg->handle_patron_status( $server );
123 $respcode = substr( $response, 0, 2 );
124 check_field
( $respcode, $response, FID_VALID_PATRON_PWD
, 'N', 'Verified code CQ for wrong pw' );
126 # Check empty password and verify CQ again
127 $siprequest = PATRON_STATUS_REQ
. 'engYYYYMMDDZZZZHHMMSS'.
128 FID_INST_ID
. $branchcode. '|'.
129 FID_PATRON_ID
. $card1. '|'.
131 $msg = C4
::SIP
::Sip
::MsgType
->new( $siprequest, 0 );
133 $msg->handle_patron_status( $server );
134 $respcode = substr( $response, 0, 2 );
135 check_field
( $respcode, $response, FID_VALID_PATRON_PWD
, 'N', 'code CQ should be N for empty AD' );
137 # Finally, we send a wrong card number and check AE, BL
138 # This is done by removing the new patron first
139 $schema->resultset('Borrower')->search({ cardnumber
=> $card1 })->delete;
141 $siprequest = PATRON_STATUS_REQ
. 'engYYYYMMDDZZZZHHMMSS'.
142 FID_INST_ID
. $branchcode. '|'.
143 FID_PATRON_ID
. $card1. '|'.
144 FID_PATRON_PWD
. PATRON_PW
. '|';
145 $msg = C4
::SIP
::Sip
::MsgType
->new( $siprequest, 0 );
147 $msg->handle_patron_status( $server );
148 $respcode = substr( $response, 0, 2 );
149 check_field
( $respcode, $response, FID_VALID_PATRON
, 'N', 'Verified code BL for wrong cardnumber' );
150 check_field
( $respcode, $response, FID_PERSONAL_NAME
, '', 'Name should be empty now' );
151 check_field
( $respcode, $response, FID_SCREEN_MSG
, '.+', 'But we have a screen msg', 'regex' );
154 sub test_request_patron_info_v2
{
155 my $patron2 = $builder->build({
156 source
=> 'Borrower',
158 password
=> hash_password
( PATRON_PW
),
161 my $card = $patron2->{cardnumber
};
162 my $sip_patron2 = C4
::SIP
::ILS
::Patron
->new( $card );
163 $findpatron = $sip_patron2;
164 my $siprequest = PATRON_INFO
. 'engYYYYMMDDZZZZHHMMSS'.'Y '.
165 FID_INST_ID
. $branchcode. '|'.
166 FID_PATRON_ID
. $card. '|'.
167 FID_PATRON_PWD
. PATRON_PW
. '|';
168 my $msg = C4
::SIP
::Sip
::MsgType
->new( $siprequest, 0 );
170 my $server = { ils
=> $mockILS };
172 $msg->handle_patron_info( $server );
173 isnt
( $response, undef, 'At least we got a response.' );
174 my $respcode = substr( $response, 0, 2 );
175 is
( $respcode, PATRON_INFO_RESP
, 'Response code fine' );
177 check_field
( $respcode, $response, FID_INST_ID
, $branchcode , 'Verified institution id' );
178 check_field
( $respcode, $response, FID_PATRON_ID
, $card, 'Verified patron id' );
179 check_field
( $respcode, $response, FID_PERSONAL_NAME
, $patron2->{surname
}, 'Verified patron name', 'contains' );
180 check_field
( $respcode, $response, FID_VALID_PATRON
, 'Y', 'Verified code BL' );
181 check_field
( $respcode, $response, FID_VALID_PATRON_PWD
, 'Y', 'Verified code CQ' );
182 check_field
( $respcode, $response, FID_FEE_LMT
, '.*', 'Checked existence of fee limit', 'regex' );
183 check_field
( $respcode, $response, FID_HOME_ADDR
, $patron2->{address
}, 'Address in BD', 'contains' );
184 check_field
( $respcode, $response, FID_EMAIL
, $patron2->{email
}, 'Verified email in BE' );
185 check_field
( $respcode, $response, FID_HOME_PHONE
, $patron2->{phone
}, 'Verified home phone in BF' );
186 # No check for custom fields here (unofficial PB, PC and PI)
187 check_field
( $respcode, $response, FID_SCREEN_MSG
, '.+', 'We have a screen msg', 'regex' );
189 # Check empty password and verify CQ again
190 $siprequest = PATRON_INFO
. 'engYYYYMMDDZZZZHHMMSS'.'Y '.
191 FID_INST_ID
. $branchcode. '|'.
192 FID_PATRON_ID
. $card. '|'.
194 $msg = C4
::SIP
::Sip
::MsgType
->new( $siprequest, 0 );
196 $msg->handle_patron_info( $server );
197 $respcode = substr( $response, 0, 2 );
198 check_field
( $respcode, $response, FID_VALID_PATRON_PWD
, 'N', 'code CQ should be N for empty AD' );
199 # Test empty password is OK if account configured to allow
200 $server->{account
}->{allow_empty_passwords
} = 1;
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
, 'Y', 'code CQ should be Y if empty AD allowed' );
207 # Finally, we send a wrong card number
208 $schema->resultset('Borrower')->search({ cardnumber
=> $card })->delete;
210 $msg = C4
::SIP
::Sip
::MsgType
->new( $siprequest, 0 );
212 $msg->handle_patron_info( $server );
213 $respcode = substr( $response, 0, 2 );
214 check_field
( $respcode, $response, FID_VALID_PATRON
, 'N', 'Verified code BL for wrong cardnumber' );
215 check_field
( $respcode, $response, FID_PERSONAL_NAME
, '', 'Name should be empty now' );
216 check_field
( $respcode, $response, FID_SCREEN_MSG
, '.+', 'But we have a screen msg', 'regex' );
222 my ( $code, $resp, $fld, $expr, $msg, $mode ) = @_;
223 # mode: contains || equals || regex (by default: equals)
225 # strip fixed part; prefix to simplify next regex
226 $resp = '|'. substr( $resp, $fixed_length->{$code} );
228 if( $resp =~ /\|$fld([^\|]*)\|/ ) {
230 } else { # test fails
231 is
( 0, 1, "Code $fld not found in '$resp'?" );
235 if( !$mode || $mode eq 'equals' ) { # default
236 is
( $fldval, $expr, $msg );
237 } elsif( $mode eq 'regex' ) {
238 is
( $fldval =~ /$expr/, 1, $msg );
240 is
( index( $fldval, $expr ) > -1, 1, $msg );