From 23e3b4920b22d1e57b16ea7d0aa3b1bccc954311 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Wed, 2 Mar 2016 14:19:03 +0100 Subject: [PATCH] Bug 15956: New unit test SIP/Message.t Content-Type: text/plain; charset=utf-8 This patch adds a unit test for testing all handlers in MsgType.pm. For a start, we test the handler for Patron Status requests. A next patch will add tests for Patron Info requests. And hopefully the other handlers will follow.. Note: The tests have been written for SIP protocol version 2. Test plan: Run t/db_dependent/SIP/Message.t If bug 13871 has not yet been pushed, test 12 (subtest 1) will fail. That is no problem. After that push, all tests should no longer fail. Signed-off-by: Marcel de Rooy --- t/db_dependent/SIP/Message.t | 166 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100755 t/db_dependent/SIP/Message.t diff --git a/t/db_dependent/SIP/Message.t b/t/db_dependent/SIP/Message.t new file mode 100755 index 0000000..02e2d25 --- /dev/null +++ b/t/db_dependent/SIP/Message.t @@ -0,0 +1,166 @@ +#!/usr/bin/perl + +# Tests for SIP::Sip::MsgType +# Please help to extend it! + +# This file is part of Koha. +# +# Copyright 2016 Rijksmuseum +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; +use Test::More tests => 2; +use Test::MockObject; +use Test::MockModule; + +use Koha::Database; +use t::lib::TestBuilder; +use Koha::AuthUtils qw(hash_password); + +use C4::SIP::ILS::Patron; +use C4::SIP::Sip qw(write_msg); +use C4::SIP::Sip::Constants qw(:all); +use C4::SIP::Sip::MsgType; + +use constant PATRON1_PW => 'do_not_ever_use_this_one'; + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new(); + +# Some common stuff for all/most subtests +my $response; +# mock write_msg (imported from Sip.pm into Message.pm) +my $mockMsg = Test::MockModule->new( 'C4::SIP::Sip::MsgType' ); +$mockMsg->mock( 'write_msg', sub { $response = $_[1]; } ); # save response +my $branch = $builder->build({ + source => 'Branch', +}); +my $branchcode = $branch->{branchcode}; + +# Start testing +subtest 'Testing Patron Status Request V2' => sub { + $schema->storage->txn_begin; + plan tests => 12; + $C4::SIP::Sip::protocol_version = 2; + test_request_patron_status_v2(); + $schema->storage->txn_rollback; +}; + +subtest 'Testing Patron Info Request V2' => sub { + $schema->storage->txn_begin; + plan tests => 1; + $C4::SIP::Sip::protocol_version = 2; + test_request_patron_info_v2(); + $schema->storage->txn_rollback; +}; + +# Here is room for some more subtests + +# End of main code + +sub test_request_patron_status_v2 { + my $patron1 = $builder->build({ + source => 'Borrower', + value => { + password => hash_password( PATRON1_PW ), + } + }); + my $card1 = $patron1->{cardnumber}; + my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 ); + + my $mockILS = Test::MockObject->new; + $mockILS->mock( 'check_inst_id', sub {} ); + $mockILS->mock( 'find_patron', sub { $sip_patron1; } ); + + my $siprequest = '23engYYYYMMDDZZZZHHMMSS|'. + FID_INST_ID. $branchcode. '|'. + FID_PATRON_ID. $card1. '|'. + FID_PATRON_PWD. PATRON1_PW. '|'; + my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 ); + + my $server = { ils => $mockILS }; + undef $response; + $msg->handle_patron_status( $server ); + isnt( $response, undef, 'At least we got a response.' ); + is( substr( $response, 0, 2 ), PATRON_STATUS_RESP, 'Response code fine' ); + + # Testing institution part (AO). We do not check its position here.. + my $response_var = substr( $response, 37 ); + my $fld = FID_INST_ID; + my $fldval; + if( $response_var =~ /(\||^)($fld[^\|]+\|)/ ) { + $fldval = $2; + } + is( $fldval, FID_INST_ID. $branchcode. '|', 'Verified institution id' ); + + # Testing patron id and name (position not checked) + $fld = FID_PATRON_ID; + undef $fldval; + if( $response_var =~ /(\||^)($fld[^\|]+\|)/ ) { + $fldval = $2; + } + is( $fldval, $fld. $card1. '|', 'Verified patron id' ); + $fld = FID_PERSONAL_NAME; + undef $fldval; + if( $response_var =~ /(\||^)($fld[^\|]+\|)/ ) { + $fldval = $2; + } + is( $fldval =~ /$patron1->{surname}/, 1, 'Verified patron name' ); + + # Testing returned credential codes BL and CQ + $fld = FID_VALID_PATRON; + undef $fldval; + is( $response_var =~ /\|${fld}Y\|/, 1, 'Verified code BL' ); + $fld = FID_VALID_PATRON_PWD; + is( $response_var =~ /\|${fld}Y\|/, 1, 'Verified code CQ' ); + + # Testing screen message + $fld = FID_SCREEN_MSG; + is( $response_var =~ /\|${fld}.+\|/, 1, 'Verified screen msg' ); + + # Now, we pass a wrong password and verify CQ again + $siprequest = '23engYYYYMMDDZZZZHHMMSS|'. + FID_INST_ID. $branchcode. '|'. + FID_PATRON_ID. $card1. '|'. + FID_PATRON_PWD. 'wrong_password'. '|'; + $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 ); + undef $response; + $msg->handle_patron_status( $server ); + $fld = FID_VALID_PATRON_PWD; + is( $response =~ /\|${fld}N\|/, 1, 'Verified code CQ for wrong pw' ); + + # Finally, we send a wrong card number and check AE, BL + # This is done by removing the new patron first + $schema->resultset('Borrower')->search({ cardnumber => $card1 })->delete; + $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 ); + $siprequest = '23engYYYYMMDDZZZZHHMMSS|'. + FID_INST_ID. $branchcode. '|'. + FID_PATRON_ID. $card1. '|'. + FID_PATRON_PWD. PATRON1_PW. '|'; + $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 ); + undef $response; + $msg->handle_patron_status( $server ); + $fld = FID_VALID_PATRON; + is( $response =~ /\|${fld}N\|/, 1, 'Verified code BL for wrong card' ); + $response_var = substr( $response, 37 ); + $fld = FID_PERSONAL_NAME; + is( $response_var =~ /(\||^)${fld}\|/, 1, 'Name should be empty now' ); + $fld = FID_SCREEN_MSG; + is( $response_var =~ /\|${fld}.+\|/, 1, 'But we have a screen msg' ); +} + +sub test_request_patron_info_v2 { + is( 1, 1, 'Your test could be here...' ); +} -- 1.7.10.4