Koha/t/db_dependent/api/v1/unhandled_exceptions.t
Tomas Cohen Arazi 320437e7bd 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 <tomascohen@theke.io>

Signed-off-by: Andrew Fuerste-Henry <andrew@bywatersolutions.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
2022-01-05 13:33:46 -10:00

69 lines
2 KiB
Perl
Executable file

#!/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 <http://www.gnu.org/licenses>.
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;
};