6b38b6778eee1aea83da75f227521680f5cc0846
[srvgit] / 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 => 2;
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 'get() tests' => sub {
39
40     plan tests => 18;
41
42     $schema->storage->txn_begin;
43
44     my $patron = $builder->build_object(
45         {
46             class => 'Koha::Patrons',
47             value => { flags => 0 }
48         }
49     );
50     my $password = 'thePassword123';
51     $patron->set_password( { password => $password, skip_validation => 1 } );
52     $patron->discard_changes;
53     my $userid = $patron->userid;
54
55     my $biblio = $builder->build_sample_biblio({
56         title  => 'The unbearable lightness of being',
57         author => 'Milan Kundera'
58     });
59     $t->get_ok("//$userid:$password@/api/v1/biblios/" . $biblio->biblionumber)
60       ->status_is(403);
61
62     $patron->flags(4)->store;
63
64     $t->get_ok( "//$userid:$password@/api/v1/biblios/" . $biblio->biblionumber
65                 => { Accept => 'application/weird+format' } )
66       ->status_is(406)
67       ->json_is( [ "application/json",
68                    "application/marcxml+xml",
69                    "application/marc-in-json",
70                    "application/marc" ] );
71
72     $t->get_ok( "//$userid:$password@/api/v1/biblios/" . $biblio->biblionumber
73                  => { Accept => 'application/json' } )
74       ->status_is(200)
75       ->json_is( '/title', 'The unbearable lightness of being' )
76       ->json_is( '/author', 'Milan Kundera' );
77
78     $t->get_ok( "//$userid:$password@/api/v1/biblios/" . $biblio->biblionumber
79                  => { Accept => 'application/marcxml+xml' } )
80       ->status_is(200);
81
82     $t->get_ok( "//$userid:$password@/api/v1/biblios/" . $biblio->biblionumber
83                  => { Accept => 'application/marc-in-json' } )
84       ->status_is(200);
85
86     $t->get_ok( "//$userid:$password@/api/v1/biblios/" . $biblio->biblionumber
87                  => { Accept => 'application/marc' } )
88       ->status_is(200);
89
90     $biblio->delete;
91     $t->get_ok( "//$userid:$password@/api/v1/biblios/" . $biblio->biblionumber
92                  => { Accept => 'application/marc' } )
93       ->status_is(404)
94       ->json_is( '/error', 'Object not found.' );
95
96     $schema->storage->txn_rollback;
97 };
98
99 subtest 'delete() tests' => sub {
100
101     plan tests => 9;
102
103     $schema->storage->txn_begin;
104
105     my $patron = $builder->build_object(
106         {
107             class => 'Koha::Patrons',
108             value => { flags => 0 } # no permissions
109         }
110     );
111     my $password = 'thePassword123';
112     $patron->set_password( { password => $password, skip_validation => 1 } );
113     my $userid = $patron->userid;
114
115     my $item      = $builder->build_sample_item();
116     my $biblio_id = $item->biblionumber;
117
118     $t->delete_ok("//$userid:$password@/api/v1/biblios/$biblio_id")
119       ->status_is(403, 'Not enough permissions makes it return the right code');
120
121     # Add permissions
122     $builder->build(
123         {
124             source => 'UserPermission',
125             value  => {
126                 borrowernumber => $patron->borrowernumber,
127                 module_bit     => 9,
128                 code           => 'edit_catalogue'
129             }
130         }
131     );
132
133
134     # Bibs with items cannot be deleted
135     $t->delete_ok("//$userid:$password@/api/v1/biblios/$biblio_id")
136       ->status_is(409);
137
138     $item->delete();
139
140     # Bibs with no items can be deleted
141     $t->delete_ok("//$userid:$password@/api/v1/biblios/$biblio_id")
142       ->status_is(204, 'SWAGGER3.2.4')
143       ->content_is('', 'SWAGGER3.3.4');
144
145     $t->delete_ok("//$userid:$password@/api/v1/biblios/$biblio_id")
146       ->status_is(404);
147
148     $schema->storage->txn_rollback;
149 };