Line 0
Link Here
|
0 |
- |
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 PATRON1_PW => 'do_not_ever_use_this_one'; |
38 |
|
39 |
my $schema = Koha::Database->new->schema; |
40 |
my $builder = t::lib::TestBuilder->new(); |
41 |
|
42 |
# Some common stuff for all/most subtests |
43 |
my $response; |
44 |
# mock write_msg (imported from Sip.pm into Message.pm) |
45 |
my $mockMsg = Test::MockModule->new( 'C4::SIP::Sip::MsgType' ); |
46 |
$mockMsg->mock( 'write_msg', sub { $response = $_[1]; } ); # save response |
47 |
my $branch = $builder->build({ |
48 |
source => 'Branch', |
49 |
}); |
50 |
my $branchcode = $branch->{branchcode}; |
51 |
|
52 |
# Start testing |
53 |
subtest 'Testing Patron Status Request V2' => sub { |
54 |
$schema->storage->txn_begin; |
55 |
plan tests => 12; |
56 |
$C4::SIP::Sip::protocol_version = 2; |
57 |
test_request_patron_status_v2(); |
58 |
$schema->storage->txn_rollback; |
59 |
}; |
60 |
|
61 |
subtest 'Testing Patron Info Request V2' => sub { |
62 |
$schema->storage->txn_begin; |
63 |
plan tests => 1; |
64 |
$C4::SIP::Sip::protocol_version = 2; |
65 |
test_request_patron_info_v2(); |
66 |
$schema->storage->txn_rollback; |
67 |
}; |
68 |
|
69 |
# Here is room for some more subtests |
70 |
|
71 |
# End of main code |
72 |
|
73 |
sub test_request_patron_status_v2 { |
74 |
my $patron1 = $builder->build({ |
75 |
source => 'Borrower', |
76 |
value => { |
77 |
password => hash_password( PATRON1_PW ), |
78 |
} |
79 |
}); |
80 |
my $card1 = $patron1->{cardnumber}; |
81 |
my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 ); |
82 |
|
83 |
my $mockILS = Test::MockObject->new; |
84 |
$mockILS->mock( 'check_inst_id', sub {} ); |
85 |
$mockILS->mock( 'find_patron', sub { $sip_patron1; } ); |
86 |
|
87 |
my $siprequest = '23engYYYYMMDDZZZZHHMMSS|'. |
88 |
FID_INST_ID. $branchcode. '|'. |
89 |
FID_PATRON_ID. $card1. '|'. |
90 |
FID_PATRON_PWD. PATRON1_PW. '|'; |
91 |
my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 ); |
92 |
|
93 |
my $server = { ils => $mockILS }; |
94 |
undef $response; |
95 |
$msg->handle_patron_status( $server ); |
96 |
isnt( $response, undef, 'At least we got a response.' ); |
97 |
is( substr( $response, 0, 2 ), PATRON_STATUS_RESP, 'Response code fine' ); |
98 |
|
99 |
# Testing institution part (AO). We do not check its position here.. |
100 |
my $response_var = substr( $response, 37 ); |
101 |
my $fld = FID_INST_ID; |
102 |
my $fldval; |
103 |
if( $response_var =~ /(\||^)($fld[^\|]+\|)/ ) { |
104 |
$fldval = $2; |
105 |
} |
106 |
is( $fldval, FID_INST_ID. $branchcode. '|', 'Verified institution id' ); |
107 |
|
108 |
# Testing patron id and name (position not checked) |
109 |
$fld = FID_PATRON_ID; |
110 |
undef $fldval; |
111 |
if( $response_var =~ /(\||^)($fld[^\|]+\|)/ ) { |
112 |
$fldval = $2; |
113 |
} |
114 |
is( $fldval, $fld. $card1. '|', 'Verified patron id' ); |
115 |
$fld = FID_PERSONAL_NAME; |
116 |
undef $fldval; |
117 |
if( $response_var =~ /(\||^)($fld[^\|]+\|)/ ) { |
118 |
$fldval = $2; |
119 |
} |
120 |
is( $fldval =~ /$patron1->{surname}/, 1, 'Verified patron name' ); |
121 |
|
122 |
# Testing returned credential codes BL and CQ |
123 |
$fld = FID_VALID_PATRON; |
124 |
undef $fldval; |
125 |
is( $response_var =~ /\|${fld}Y\|/, 1, 'Verified code BL' ); |
126 |
$fld = FID_VALID_PATRON_PWD; |
127 |
is( $response_var =~ /\|${fld}Y\|/, 1, 'Verified code CQ' ); |
128 |
|
129 |
# Testing screen message |
130 |
$fld = FID_SCREEN_MSG; |
131 |
is( $response_var =~ /\|${fld}.+\|/, 1, 'Verified screen msg' ); |
132 |
|
133 |
# Now, we pass a wrong password and verify CQ again |
134 |
$siprequest = '23engYYYYMMDDZZZZHHMMSS|'. |
135 |
FID_INST_ID. $branchcode. '|'. |
136 |
FID_PATRON_ID. $card1. '|'. |
137 |
FID_PATRON_PWD. 'wrong_password'. '|'; |
138 |
$msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 ); |
139 |
undef $response; |
140 |
$msg->handle_patron_status( $server ); |
141 |
$fld = FID_VALID_PATRON_PWD; |
142 |
is( $response =~ /\|${fld}N\|/, 1, 'Verified code CQ for wrong pw' ); |
143 |
|
144 |
# Finally, we send a wrong card number and check AE, BL |
145 |
# This is done by removing the new patron first |
146 |
$schema->resultset('Borrower')->search({ cardnumber => $card1 })->delete; |
147 |
$sip_patron1 = C4::SIP::ILS::Patron->new( $card1 ); |
148 |
$siprequest = '23engYYYYMMDDZZZZHHMMSS|'. |
149 |
FID_INST_ID. $branchcode. '|'. |
150 |
FID_PATRON_ID. $card1. '|'. |
151 |
FID_PATRON_PWD. PATRON1_PW. '|'; |
152 |
$msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 ); |
153 |
undef $response; |
154 |
$msg->handle_patron_status( $server ); |
155 |
$fld = FID_VALID_PATRON; |
156 |
is( $response =~ /\|${fld}N\|/, 1, 'Verified code BL for wrong card' ); |
157 |
$response_var = substr( $response, 37 ); |
158 |
$fld = FID_PERSONAL_NAME; |
159 |
is( $response_var =~ /(\||^)${fld}\|/, 1, 'Name should be empty now' ); |
160 |
$fld = FID_SCREEN_MSG; |
161 |
is( $response_var =~ /\|${fld}.+\|/, 1, 'But we have a screen msg' ); |
162 |
} |
163 |
|
164 |
sub test_request_patron_info_v2 { |
165 |
is( 1, 1, 'Your test could be here...' ); |
166 |
} |