Bug 24232: Regression tests
[koha.git] / t / db_dependent / api / v1 / biblios.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 under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 use Modern::Perl;
19
20 use Test::More tests => 1;
21 use Test::Mojo;
22 use Test::Warn;
23
24 use t::lib::Mocks;
25 use t::lib::TestBuilder;
26
27 use C4::Auth;
28 use Koha::Biblios;
29 use Koha::Database;
30
31 my $schema  = Koha::Database->new->schema;
32 my $builder = t::lib::TestBuilder->new;
33
34 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
35
36 my $t = Test::Mojo->new('Koha::REST::V1');
37
38 subtest 'delete() tests' => sub {
39
40     plan tests => 9;
41
42     $schema->storage->txn_begin;
43
44     my $patron = $builder->build_object(
45         {
46             class => 'Koha::Patrons',
47             value => { flags => 0 } # no permissions
48         }
49     );
50     my $password = 'thePassword123';
51     $patron->set_password( { password => $password, skip_validation => 1 } );
52     my $userid = $patron->userid;
53
54     my $item      = $builder->build_sample_item();
55     my $biblio_id = $item->biblionumber;
56
57     $t->delete_ok("//$userid:$password@/api/v1/biblios/$biblio_id")
58       ->status_is(403, 'Not enough permissions makes it return the right code');
59
60     # Add permissions
61     $builder->build(
62         {
63             source => 'UserPermission',
64             value  => {
65                 borrowernumber => $patron->borrowernumber,
66                 module_bit     => 9,
67                 code           => 'edit_catalogue'
68             }
69         }
70     );
71
72
73     # Bibs with items cannot be deleted
74     $t->delete_ok("//$userid:$password@/api/v1/biblios/$biblio_id")
75       ->status_is(409);
76
77     $item->delete();
78
79     # Bibs with no items can be deleted
80     $t->delete_ok("//$userid:$password@/api/v1/biblios/$biblio_id")
81       ->status_is(204, 'SWAGGER3.2.4')
82       ->content_is('', 'SWAGGER3.3.4');
83
84     $t->delete_ok("//$userid:$password@/api/v1/biblios/$biblio_id")
85       ->status_is(404);
86
87     $schema->storage->txn_rollback;
88 };