Bug 23517: Add a spec for PUT /holds/{hold_id}/priority
[koha.git] / t / db_dependent / api / v1 / items.t
1 #!/usr/bin/env perl
2
3 # Copyright 2016 Koha-Suomi
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use Modern::Perl;
21
22 use Test::More tests => 1;
23 use Test::Mojo;
24 use Test::Warn;
25
26 use t::lib::TestBuilder;
27 use t::lib::Mocks;
28
29 use C4::Auth;
30 use Koha::Items;
31 use Koha::Database;
32
33 my $schema  = Koha::Database->new->schema;
34 my $builder = t::lib::TestBuilder->new;
35
36 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
37
38 my $t = Test::Mojo->new('Koha::REST::V1');
39
40 subtest 'get() tests' => sub {
41
42     plan tests => 9;
43
44     $schema->storage->txn_begin;
45
46     my $item = $builder->build_object( { class => 'Koha::Items' } );
47     my $patron = $builder->build_object({
48         class => 'Koha::Patrons',
49         value => { flags => 4 }
50     });
51
52     my $nonprivilegedpatron = $builder->build_object({
53         class => 'Koha::Patrons',
54         value => { flags => 0 }
55     });
56
57     my $password = 'thePassword123';
58
59     $nonprivilegedpatron->set_password({ password => $password, skip_validation => 1 });
60     my $userid = $nonprivilegedpatron->userid;
61
62     $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->itemnumber )
63       ->status_is(403)
64       ->json_is( '/error' => 'Authorization failure. Missing required permission(s).' );
65
66     $patron->set_password({ password => $password, skip_validation => 1 });
67     $userid = $patron->userid;
68
69     $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->itemnumber )
70       ->status_is( 200, 'SWAGGER3.2.2' )
71       ->json_is( '' => Koha::REST::V1::Items::_to_api( $item->TO_JSON ), 'SWAGGER3.3.2' );
72
73     my $non_existent_code = $item->itemnumber;
74     $item->delete;
75
76     $t->get_ok( "//$userid:$password@/api/v1/items/" . $non_existent_code )
77       ->status_is(404)
78       ->json_is( '/error' => 'Item not found' );
79
80     $schema->storage->txn_rollback;
81 };