Bug 18977: Rollback branch in t/db_dependent/SIP/Message.t
[koha.git] / t / db_dependent / SIP / Message.t
1 #!/usr/bin/perl
2
3 # Tests for SIP::Sip::MsgType
4 # Please help to extend it!
5
6 # This file is part of Koha.
7 #
8 # Copyright 2016 Rijksmuseum
9 #
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.
14 #
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.
19 #
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>.
22
23 use Modern::Perl;
24 use Test::More tests => 2;
25 use Test::MockObject;
26 use Test::MockModule;
27
28 use Koha::Database;
29 use t::lib::TestBuilder;
30 use Koha::AuthUtils qw(hash_password);
31
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;
36
37 use constant PATRON_PW => 'do_not_ever_use_this_one';
38
39 my $fixed_length = { #length of fixed fields including response code
40     ( PATRON_STATUS_RESP ) => 37,
41     ( PATRON_INFO_RESP )   => 61,
42 };
43
44 my $schema = Koha::Database->new->schema;
45 my $builder = t::lib::TestBuilder->new();
46
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
52 # mock ils object
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
58 # START testing
59 subtest 'Testing Patron Status Request V2' => sub {
60     $schema->storage->txn_begin;
61     plan tests => 13;
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;
66 };
67
68 subtest 'Testing Patron Info Request V2' => sub {
69     $schema->storage->txn_begin;
70     plan tests => 17;
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;
75 };
76
77 # Here is room for some more subtests
78
79 # END of main code
80
81 sub test_request_patron_status_v2 {
82     my $patron1 = $builder->build({
83         source => 'Borrower',
84         value  => {
85             password => hash_password( PATRON_PW ),
86         },
87     });
88     my $card1 = $patron1->{cardnumber};
89     my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
90     $findpatron = $sip_patron1;
91
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 );
97
98     my $server = { ils => $mockILS };
99     undef $response;
100     $msg->handle_patron_status( $server );
101
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' );
105
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' );
112
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 );
119     undef $response;
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' );
123
124     # Check empty password and verify CQ again
125     $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
126         FID_INST_ID. $branchcode. '|'.
127         FID_PATRON_ID. $card1. '|'.
128         FID_PATRON_PWD. '|';
129     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
130     undef $response;
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' );
134
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;
138     undef $findpatron;
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 );
144     undef $response;
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' );
150 }
151
152 sub test_request_patron_info_v2 {
153     my $patron2 = $builder->build({
154         source => 'Borrower',
155         value  => {
156             password => hash_password( PATRON_PW ),
157         },
158     });
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 );
167
168     my $server = { ils => $mockILS };
169     undef $response;
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' );
174
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' );
186
187     # Check empty password and verify CQ again
188     $siprequest = PATRON_INFO. 'engYYYYMMDDZZZZHHMMSS'.'Y         '.
189         FID_INST_ID. $branchcode. '|'.
190         FID_PATRON_ID. $card. '|'.
191         FID_PATRON_PWD. '|';
192     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
193     undef $response;
194     $msg->handle_patron_info( $server );
195     $respcode = substr( $response, 0, 2 );
196     check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'N', 'code CQ should be N for empty AD' );
197     # Test empty password is OK if account configured to allow
198     $server->{account}->{allow_empty_passwords} = 1;
199     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
200     undef $response;
201     $msg->handle_patron_info( $server );
202     $respcode = substr( $response, 0, 2 );
203     check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'Y', 'code CQ should be Y if empty AD allowed' );
204
205     # Finally, we send a wrong card number
206     $schema->resultset('Borrower')->search({ cardnumber => $card })->delete;
207     undef $findpatron;
208     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
209     undef $response;
210     $msg->handle_patron_info( $server );
211     $respcode = substr( $response, 0, 2 );
212     check_field( $respcode, $response, FID_VALID_PATRON, 'N', 'Verified code BL for wrong cardnumber' );
213     check_field( $respcode, $response, FID_PERSONAL_NAME, '', 'Name should be empty now' );
214     check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'But we have a screen msg', 'regex' );
215 }
216
217 # Helper routines
218
219 sub check_field {
220     my ( $code, $resp, $fld, $expr, $msg, $mode ) = @_;
221     # mode: contains || equals || regex (by default: equals)
222
223     # strip fixed part; prefix to simplify next regex
224     $resp = '|'. substr( $resp, $fixed_length->{$code} );
225     my $fldval;
226     if( $resp =~ /\|$fld([^\|]*)\|/ ) {
227         $fldval = $1;
228     } else { # test fails
229         is( 0, 1, "Code $fld not found in '$resp'?" );
230         return;
231     }
232
233     if( !$mode || $mode eq 'equals' ) { # default
234         is( $fldval, $expr, $msg );
235     } elsif( $mode eq 'regex' ) {
236         is( $fldval =~ /$expr/, 1, $msg );
237     } else { # contains
238         is( index( $fldval, $expr ) > -1, 1, $msg );
239     }
240 }