Bug 33146: Unit tests
[koha-ffzg.git] / t / db_dependent / api / v1 / unhandled_exceptions.t
1 #!/usr/bin/env perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 1;
21 use Test::MockModule;
22 use Test::Mojo;
23
24 use t::lib::Mocks;
25 use t::lib::TestBuilder;
26
27 use Koha::Database;
28 use Koha::Exceptions;
29
30 my $schema  = Koha::Database->new->schema;
31 my $builder = t::lib::TestBuilder->new;
32
33 my $t = Test::Mojo->new('Koha::REST::V1');
34 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
35
36 subtest 'unhandled_exception() tests' => sub {
37
38     plan tests => 3;
39
40     $schema->storage->txn_begin;
41
42     my $authorized_patron = $builder->build_object(
43         {
44             class => 'Koha::Patrons',
45             value => { flags => 2**4 }    # borrowers flag = 4
46         }
47     );
48     my $password = 'thePassword123';
49     $authorized_patron->set_password(
50         { password => $password, skip_validation => 1 } );
51     my $userid = $authorized_patron->userid;
52
53     my $message = 'delete died';
54
55     my $mock_patron = Test::MockModule->new('Koha::Patron');
56     $mock_patron->mock( 'delete', sub { Koha::Exception->throw($message); } );
57
58     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
59
60     $t->delete_ok( "//$userid:$password@/api/v1/patrons/" . $patron->id )
61       ->status_is('500')
62       ->json_is(
63         {   error      => 'Something went wrong, check Koha logs for details.',
64             error_code => 'internal_server_error',
65         }
66     );
67
68     $schema->storage->txn_rollback;
69 };