Bug 15496: Add API endoint for deleting a bib
[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 => 7;
41
42     $schema->storage->txn_begin;
43
44     my $patron = $builder->build_object(
45         {
46             class => 'Koha::Patrons',
47             value => { flags => 9 }
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     # Bibs with items cannot be deleted
58     $t->delete_ok("//$userid:$password@/api/v1/biblios/$biblio_id")
59       ->status_is(409);
60
61     $item->delete();
62
63     # Bibs with no items can be deleted
64     $t->delete_ok("//$userid:$password@/api/v1/biblios/$biblio_id")
65       ->status_is(200)->content_is(q{""});
66
67     $t->delete_ok("//$userid:$password@/api/v1/biblios/$biblio_id")
68       ->status_is(404);
69
70     $schema->storage->txn_rollback;
71 };