Bug 24545: Fix license statements
[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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 2;
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 'list() tests' => sub {
41
42     plan tests => 12;
43
44     $schema->storage->txn_begin;
45
46     my $item   = $builder->build_object( { class => 'Koha::Items' } );
47     my $patron = $builder->build_object(
48         {
49             class => 'Koha::Patrons',
50             value => { flags => 4 }
51         }
52     );
53
54     my $nonprivilegedpatron = $builder->build_object(
55         {
56             class => 'Koha::Patrons',
57             value => { flags => 0 }
58         }
59     );
60
61     my $password = 'thePassword123';
62
63     $nonprivilegedpatron->set_password(
64         { password => $password, skip_validation => 1 } );
65     my $userid = $nonprivilegedpatron->userid;
66
67     $t->get_ok( "//$userid:$password@/api/v1/items" )
68       ->status_is(403)
69       ->json_is(
70         '/error' => 'Authorization failure. Missing required permission(s).' );
71
72     $patron->set_password( { password => $password, skip_validation => 1 } );
73     $userid = $patron->userid;
74
75     $t->get_ok( "//$userid:$password@/api/v1/items" )
76       ->status_is( 200, 'SWAGGER3.2.2' );
77
78     my $items_count = Koha::Items->search->count;
79     my $response_count = scalar @{ $t->tx->res->json };
80
81     is( $items_count, $response_count, 'The API returns all the items' );
82
83     $t->get_ok( "//$userid:$password@/api/v1/items?external_id=" . $item->barcode )
84       ->status_is(200)
85       ->json_is( '' => [ $item->to_api ], 'SWAGGER3.3.2');
86
87     my $barcode = $item->barcode;
88     $item->delete;
89
90     $t->get_ok( "//$userid:$password@/api/v1/items?external_id=" . $item->barcode )
91       ->status_is(200)
92       ->json_is( '' => [] );
93
94     $schema->storage->txn_rollback;
95 };
96
97
98 subtest 'get() tests' => sub {
99
100     plan tests => 9;
101
102     $schema->storage->txn_begin;
103
104     my $item = $builder->build_object( { class => 'Koha::Items' } );
105     my $patron = $builder->build_object({
106         class => 'Koha::Patrons',
107         value => { flags => 4 }
108     });
109
110     my $nonprivilegedpatron = $builder->build_object({
111         class => 'Koha::Patrons',
112         value => { flags => 0 }
113     });
114
115     my $password = 'thePassword123';
116
117     $nonprivilegedpatron->set_password({ password => $password, skip_validation => 1 });
118     my $userid = $nonprivilegedpatron->userid;
119
120     $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->itemnumber )
121       ->status_is(403)
122       ->json_is( '/error' => 'Authorization failure. Missing required permission(s).' );
123
124     $patron->set_password({ password => $password, skip_validation => 1 });
125     $userid = $patron->userid;
126
127     $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->itemnumber )
128       ->status_is( 200, 'SWAGGER3.2.2' )
129       ->json_is( '' => $item->to_api, 'SWAGGER3.3.2' );
130
131     my $non_existent_code = $item->itemnumber;
132     $item->delete;
133
134     $t->get_ok( "//$userid:$password@/api/v1/items/" . $non_existent_code )
135       ->status_is(404)
136       ->json_is( '/error' => 'Item not found' );
137
138     $schema->storage->txn_rollback;
139 };