From 53b985f0ce6f42db32b52ccf442e8c2f90446147 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 27 Dec 2021 08:20:11 -0300 Subject: [PATCH] Bug 28020: Unit tests The unhandled_exception() Mojo helper didn't have proper tests. As this bug is adding a slight behavior change, I tried to add some. As it relies on the OpenAPI plugin, it cannot be done the usual way using Mojo::Lite. So I picked a route and forced an exception through mocking to be able to write tests. To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/db_dependent/api/v1/unhandled_exceptions.t => FAIL: The unhandled_exception() helper doesn't return an error_code in the response payload. Signed-off-by: Tomas Cohen Arazi Signed-off-by: Andrew Fuerste-Henry --- t/db_dependent/api/v1/unhandled_exceptions.t | 69 ++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100755 t/db_dependent/api/v1/unhandled_exceptions.t diff --git a/t/db_dependent/api/v1/unhandled_exceptions.t b/t/db_dependent/api/v1/unhandled_exceptions.t new file mode 100755 index 0000000000..4ebba88b19 --- /dev/null +++ b/t/db_dependent/api/v1/unhandled_exceptions.t @@ -0,0 +1,69 @@ +#!/usr/bin/env perl + +# This file is part of Koha. +# +# 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 => 1; +use Test::MockModule; +use Test::Mojo; + +use t::lib::Mocks; +use t::lib::TestBuilder; + +use Koha::Database; +use Koha::Exceptions; + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +my $t = Test::Mojo->new('Koha::REST::V1'); +t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); + +subtest 'unhandled_exception() tests' => sub { + + plan tests => 3; + + $schema->storage->txn_begin; + + my $authorized_patron = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 2**4 } # borrowers flag = 4 + } + ); + my $password = 'thePassword123'; + $authorized_patron->set_password( + { password => $password, skip_validation => 1 } ); + my $userid = $authorized_patron->userid; + + my $message = 'delete died'; + + my $mock_patron = Test::MockModule->new('Koha::Patron'); + $mock_patron->mock( 'delete', sub { Koha::Exceptions::Exception->throw($message); } ); + + my $patron = $builder->build_object({ class => 'Koha::Patrons' }); + + $t->delete_ok( "//$userid:$password@/api/v1/patrons/" . $patron->id ) + ->status_is('500') + ->json_is( + { error => 'Something went wrong, check Koha logs for details.', + error_code => 'internal_server_error', + } + ); + + $schema->storage->txn_rollback; +}; -- 2.32.0