View | Details | Raw Unified | Return to bug 15956
Collapse All | Expand All

(-)a/t/db_dependent/SIP/Message.t (-1 / +213 lines)
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 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
$branch = $builder->build({
58
    source => 'Branch',
59
});
60
$branchcode = $branch->{branchcode};
61
62
# START testing
63
subtest 'Testing Patron Status Request V2' => sub {
64
    $schema->storage->txn_begin;
65
    plan tests => 12;
66
    $C4::SIP::Sip::protocol_version = 2;
67
    test_request_patron_status_v2();
68
    $schema->storage->txn_rollback;
69
};
70
71
subtest 'Testing Patron Info Request V2' => sub {
72
    $schema->storage->txn_begin;
73
    plan tests => 15;
74
    $C4::SIP::Sip::protocol_version = 2;
75
    test_request_patron_info_v2();
76
    $schema->storage->txn_rollback;
77
};
78
79
# Here is room for some more subtests
80
81
# END of main code
82
83
sub test_request_patron_status_v2 {
84
    my $patron1 = $builder->build({
85
        source => 'Borrower',
86
        value  => {
87
            password => hash_password( PATRON_PW ),
88
        },
89
    });
90
    my $card1 = $patron1->{cardnumber};
91
    my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
92
    $findpatron = $sip_patron1;
93
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 );
99
100
    my $server = { ils => $mockILS };
101
    undef $response;
102
    $msg->handle_patron_status( $server );
103
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' );
107
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' );
114
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 );
121
    undef $response;
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' );
125
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;
129
    undef $findpatron;
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 );
135
    undef $response;
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' );
141
}
142
143
sub test_request_patron_info_v2 {
144
    my $patron2 = $builder->build({
145
        source => 'Borrower',
146
        value  => {
147
            password => hash_password( PATRON_PW ),
148
        },
149
    });
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 );
158
159
    my $server = { ils => $mockILS };
160
    undef $response;
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' );
165
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' );
177
178
    # Finally, we send a wrong card number
179
    $schema->resultset('Borrower')->search({ cardnumber => $card })->delete;
180
    undef $findpatron;
181
    $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
182
    undef $response;
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' );
188
}
189
190
# Helper routines
191
192
sub check_field {
193
    my ( $code, $resp, $fld, $expr, $msg, $mode ) = @_;
194
    # mode: contains || equals || regex (by default: equals)
195
196
    # strip fixed part; prefix to simplify next regex
197
    $resp = '|'. substr( $resp, $fixed_length->{$code} );
198
    my $fldval;
199
    if( $resp =~ /\|$fld([^\|]*)\|/ ) {
200
        $fldval = $1;
201
    } else { # test fails
202
        is( 0, 1, "Code $fld not found in '$resp'?" );
203
        return;
204
    }
205
206
    if( !$mode || $mode eq 'equals' ) { # default
207
        is( $fldval, $expr, $msg );
208
    } elsif( $mode eq 'regex' ) {
209
        is( $fldval =~ /$expr/, 1, $msg );
210
    } else { # contains
211
        is( index( $fldval, $expr ) > -1, 1, $msg );
212
    }
213
}

Return to bug 15956