From 4212e69e54c3db19e3606377cac51b31ae1292d1 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Tue, 11 May 2021 13:43:51 +0000 Subject: [PATCH] Bug 28320: Add DB connection check to the SIP SC status message This patch adds a lookup of the sip user during an SC status, confirming that our DB ocnnection is working, and that our user is still valid Additionally, it adds support for SC status to the sip_cli_emulator and adds basic test coverage for the SC status message To test: 1 - Apply patch 2 - Restart SP server 3 - perl misc/sip_cli_emulator.pl -a localhost -p 6001 -l CPL -m sc_status_request -su term1 -sp term1 4 - prove -v t/db_dependent/SIP/Message.t --- C4/SIP/Sip/MsgType.pm | 5 +++++ misc/sip_cli_emulator.pl | 13 +++++++++++++ t/db_dependent/SIP/Message.t | 41 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/C4/SIP/Sip/MsgType.pm b/C4/SIP/Sip/MsgType.pm index 1e38f29a68..583fb06b47 100644 --- a/C4/SIP/Sip/MsgType.pm +++ b/C4/SIP/Sip/MsgType.pm @@ -18,6 +18,7 @@ use Data::Dumper; use CGI qw ( -utf8 ); use C4::Auth qw(&check_api_auth); +use Koha::Patrons; use Koha::Patron::Attributes; use Koha::Items; @@ -1584,13 +1585,17 @@ my @message_type_names = ( sub send_acs_status { my ( $self, $server, $screen_msg, $print_line ) = @_; + my $msg = ACS_STATUS; ($server) or die "send_acs_status error: no \$server argument received"; my $account = $server->{account} or die "send_acs_status error: no 'account' in \$server object:\n" . Dumper($server); my $policy = $server->{policy} or die "send_acs_status error: no 'policy' in \$server object:\n" . Dumper($server); my $ils = $server->{ils} or die "send_acs_status error: no 'ils' in \$server object:\n" . Dumper($server); + my $sip_username = $server->{sip_username} or die "send_acs_status error: no 'sip_username' in \$server object:\n" . Dumper($server); my ( $online_status, $checkin_ok, $checkout_ok, $ACS_renewal_policy ); my ( $status_update_ok, $offline_ok, $timeout, $retries ); + my $sip_user = Koha::Patrons->find({ userid => $sip_username }); + die "send_acs_status error: sip_username cannot be found in DB or DB cannot be reached" unless $sip_user; $online_status = 'Y'; $checkout_ok = sipbool( $ils->checkout_ok ); diff --git a/misc/sip_cli_emulator.pl b/misc/sip_cli_emulator.pl index 3044b044b4..8336ced436 100755 --- a/misc/sip_cli_emulator.pl +++ b/misc/sip_cli_emulator.pl @@ -127,6 +127,12 @@ my $handlers = { location_code => $location_code, }, }, + sc_status_request => { + name => 'SC Status', + subroutine => \&build_sc_status_command_message, + parameters => { + }, + }, patron_status_request => { name => 'Patron Status Request', subroutine => \&build_patron_status_request_command_message, @@ -362,6 +368,12 @@ sub build_login_command_message { . build_field( FID_LOCATION_CODE, $location_code ); } +sub build_sc_status_command_message { + my ($params) = @_; + + return SC_STATUS . "0" . "030" . "2.00"; +} + sub build_patron_status_request_command_message { my ($params) = @_; @@ -653,6 +665,7 @@ Options: item_information patron_information patron_status_request + sc_status_request renew / } diff --git a/t/db_dependent/SIP/Message.t b/t/db_dependent/SIP/Message.t index e032b7964e..db48fde51e 100755 --- a/t/db_dependent/SIP/Message.t +++ b/t/db_dependent/SIP/Message.t @@ -21,7 +21,8 @@ # along with Koha; if not, see . use Modern::Perl; -use Test::More tests => 10; +use Test::More tests => 11; +use Test::Exception; use Test::MockObject; use Test::MockModule; use Test::Warn; @@ -337,6 +338,44 @@ subtest 'Patron info summary > 5 should not crash server' => sub { $schema->storage->txn_rollback; }; +subtest 'SC status tests' => sub { + + my $schema = Koha::Database->new->schema; + $schema->storage->txn_begin; + + plan tests => 2; + + my $builder = t::lib::TestBuilder->new(); + my $branchcode = $builder->build({ source => 'Branch' })->{branchcode}; + my $sip_user = $builder->build_object({ class => "Koha::Patrons" }); + + my ( $response, $findpatron ); + my $mocks = create_mocks( \$response, \$findpatron, \$branchcode ); + my $mockILS = $mocks->{ils}; + $mockILS->mock( 'checkout_ok', sub {1} ); + $mockILS->mock( 'checkin_ok', sub {1} ); + $mockILS->mock( 'status_update_ok', sub {1} ); + $mockILS->mock( 'offline_ok', sub {1} ); + $mockILS->mock( 'supports', sub {1} ); + my $server = Test::MockObject->new(); + $server->mock( 'get_timeout', sub {'100'}); + $server->{ils} = $mockILS; + $server->{sip_username} = $sip_user->userid; + $server->{account} = {}; + $server->{policy} = { renewal =>1,retries=>'000'}; + + my $siprequest = SC_STATUS . '0' . '030' . '2.00'; + my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 ); + $msg->handle_sc_status( $server ); + + like( $response, qr/98YYYYYY100000[0-9 ]{19}.00AO|BXYYYYYYYYYYYYYYYY|/, 'At least we got a response.' ); + + $sip_user->delete; + + dies_ok{ $msg->handle_sc_status( $server ) } ,"Dies if sip user cannot be found"; + +}; + # Here is room for some more subtests # END of main code -- 2.11.0