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 # Finally, we send a wrong card number and check AE, BL
127 # This is done by removing the new patron first
128 $schema->resultset('Borrower')->search({ cardnumber => $card1 })->delete;
130 $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
131 FID_INST_ID. $branchcode. '|'.
132 FID_PATRON_ID. $card1. '|'.
133 FID_PATRON_PWD. PATRON_PW. '|';
134 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
136 $msg->handle_patron_status( $server );
137 $respcode = substr( $response, 0, 2 );
138 check_field( $respcode, $response, FID_VALID_PATRON, 'N', 'Verified code BL for wrong cardnumber' );
139 check_field( $respcode, $response, FID_PERSONAL_NAME, '', 'Name should be empty now' );
140 check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'But we have a screen msg', 'regex' );
143 sub test_request_patron_info_v2 {
144 my $patron2 = $builder->build({
145 source => 'Borrower',
147 password => hash_password( PATRON_PW ),
150 my $card = $patron2->{cardnumber};
151 my $sip_patron2 = C4::SIP::ILS::Patron->new( $card );
152 $findpatron = $sip_patron2;
153 my $siprequest = PATRON_INFO. 'engYYYYMMDDZZZZHHMMSS'.'Y '.
154 FID_INST_ID. $branchcode. '|'.
155 FID_PATRON_ID. $card. '|'.
156 FID_PATRON_PWD. PATRON_PW. '|';
157 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
159 my $server = { ils => $mockILS };
161 $msg->handle_patron_info( $server );
162 isnt( $response, undef, 'At least we got a response.' );
163 my $respcode = substr( $response, 0, 2 );
164 is( $respcode, PATRON_INFO_RESP, 'Response code fine' );
166 check_field( $respcode, $response, FID_INST_ID, $branchcode , 'Verified institution id' );
167 check_field( $respcode, $response, FID_PATRON_ID, $card, 'Verified patron id' );
168 check_field( $respcode, $response, FID_PERSONAL_NAME, $patron2->{surname}, 'Verified patron name', 'contains' );
169 check_field( $respcode, $response, FID_VALID_PATRON, 'Y', 'Verified code BL' );
170 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'Y', 'Verified code CQ' );
171 check_field( $respcode, $response, FID_FEE_LMT, '.*', 'Checked existence of fee limit', 'regex' );
172 check_field( $respcode, $response, FID_HOME_ADDR, $patron2->{address}, 'Address in BD', 'contains' );
173 check_field( $respcode, $response, FID_EMAIL, $patron2->{email}, 'Verified email in BE' );
174 check_field( $respcode, $response, FID_HOME_PHONE, $patron2->{phone}, 'Verified home phone in BF' );
175 # No check for custom fields here (unofficial PB, PC and PI)
176 check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'We have a screen msg', 'regex' );
178 # Finally, we send a wrong card number
179 $schema->resultset('Borrower')->search({ cardnumber => $card })->delete;
181 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
183 $msg->handle_patron_info( $server );
184 $respcode = substr( $response, 0, 2 );
185 check_field( $respcode, $response, FID_VALID_PATRON, 'N', 'Verified code BL for wrong cardnumber' );
186 check_field( $respcode, $response, FID_PERSONAL_NAME, '', 'Name should be empty now' );
187 check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'But we have a screen msg', 'regex' );
193 my ( $code, $resp, $fld, $expr, $msg, $mode ) = @_;
194 # mode: contains || equals || regex (by default: equals)
196 # strip fixed part; prefix to simplify next regex
197 $resp = '|'. substr( $resp, $fixed_length->{$code} );
199 if( $resp =~ /\|$fld([^\|]*)\|/ ) {
201 } else { # test fails
202 is( 0, 1, "Code $fld not found in '$resp'?" );
206 if( !$mode || $mode eq 'equals' ) { # default
207 is( $fldval, $expr, $msg );
208 } elsif( $mode eq 'regex' ) {
209 is( $fldval =~ /$expr/, 1, $msg );
211 is( index( $fldval, $expr ) > -1, 1, $msg );