3 # Copyright 2016 Koha-Suomi
5 # This file is part of Koha.
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.
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.
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>.
22 use Test::More tests => 2;
26 use t::lib::TestBuilder;
33 my $schema = Koha::Database->new->schema;
34 my $builder = t::lib::TestBuilder->new;
36 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
38 my $t = Test::Mojo->new('Koha::REST::V1');
40 subtest 'list() tests' => sub {
44 $schema->storage->txn_begin;
46 my $item = $builder->build_object( { class => 'Koha::Items' } );
47 my $patron = $builder->build_object(
49 class => 'Koha::Patrons',
50 value => { flags => 4 }
54 my $nonprivilegedpatron = $builder->build_object(
56 class => 'Koha::Patrons',
57 value => { flags => 0 }
61 my $password = 'thePassword123';
63 $nonprivilegedpatron->set_password(
64 { password => $password, skip_validation => 1 } );
65 my $userid = $nonprivilegedpatron->userid;
67 $t->get_ok( "//$userid:$password@/api/v1/items" )
70 '/error' => 'Authorization failure. Missing required permission(s).' );
72 $patron->set_password( { password => $password, skip_validation => 1 } );
73 $userid = $patron->userid;
75 $t->get_ok( "//$userid:$password@/api/v1/items" )
76 ->status_is( 200, 'SWAGGER3.2.2' );
78 my $items_count = Koha::Items->search->count;
79 my $response_count = scalar @{ $t->tx->res->json };
81 is( $items_count, $response_count, 'The API returns all the items' );
83 $t->get_ok( "//$userid:$password@/api/v1/items?external_id=" . $item->barcode )
85 ->json_is( '' => [ $item->to_api ], 'SWAGGER3.3.2');
87 my $barcode = $item->barcode;
90 $t->get_ok( "//$userid:$password@/api/v1/items?external_id=" . $item->barcode )
92 ->json_is( '' => [] );
94 $schema->storage->txn_rollback;
98 subtest 'get() tests' => sub {
102 $schema->storage->txn_begin;
104 my $item = $builder->build_object( { class => 'Koha::Items' } );
105 my $patron = $builder->build_object({
106 class => 'Koha::Patrons',
107 value => { flags => 4 }
110 my $nonprivilegedpatron = $builder->build_object({
111 class => 'Koha::Patrons',
112 value => { flags => 0 }
115 my $password = 'thePassword123';
117 $nonprivilegedpatron->set_password({ password => $password, skip_validation => 1 });
118 my $userid = $nonprivilegedpatron->userid;
120 $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->itemnumber )
122 ->json_is( '/error' => 'Authorization failure. Missing required permission(s).' );
124 $patron->set_password({ password => $password, skip_validation => 1 });
125 $userid = $patron->userid;
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' );
131 my $non_existent_code = $item->itemnumber;
134 $t->get_ok( "//$userid:$password@/api/v1/items/" . $non_existent_code )
136 ->json_is( '/error' => 'Item not found' );
138 $schema->storage->txn_rollback;