3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Test::More tests => 1;
23 use t::lib::TestBuilder;
26 use Mojo::JSON qw(encode_json);
31 my $schema = Koha::Database->new->schema;
32 my $builder = t::lib::TestBuilder->new;
34 my $t = Test::Mojo->new('Koha::REST::V1');
35 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
37 subtest 'list() tests' => sub {
41 $schema->storage->txn_begin;
43 my $item_type = $builder->build_object(
45 class => 'Koha::ItemTypes',
48 description => 'Test item type',
49 rentalcharge => 100.0,
50 rentalcharge_daily => 50.,
51 rentalcharge_daily_calendar => 0,
52 rentalcharge_hourly => 0.60,
53 rentalcharge_hourly_calendar => 1,
54 defaultreplacecost => 1000.0,
57 imageurl => 'https://upload.wikimedia.org/wikipedia/commons/1/1f/202208_test-tube-4.svg',
58 summary => 'An item type for testing',
59 checkinmsg => 'Checking in test',
60 checkinmsgtype => 'message',
61 sip_media_type => 'spt',
63 searchcategory => 'test search category',
64 automatic_checkin => 0,
69 my $en = $builder->build_object(
71 class => 'Koha::Localizations',
73 entity => 'itemtypes',
74 code => $item_type->id,
76 translation => 'English word "test"',
80 my $sv = $builder->build_object(
82 class => 'Koha::Localizations',
84 entity => 'itemtypes',
85 code => $item_type->id,
87 translation => 'Swedish word "test"',
92 my $librarian = $builder->build_object(
94 class => 'Koha::Patrons',
95 value => { flags => 2**2 } # catalogue flag = 2
98 my $password = 'thePassword123';
99 $librarian->set_password( { password => $password, skip_validation => 1 } );
100 my $userid = $librarian->userid;
102 my $patron = $builder->build_object(
104 class => 'Koha::Patrons',
105 value => { flags => 0 }
109 $patron->set_password( { password => $password, skip_validation => 1 } );
110 my $unauth_userid = $patron->userid;
112 ## Authorized user tests
113 my $query = encode_json( { item_type_id => $item_type->id } );
114 $t->get_ok("//$userid:$password@/api/v1/item_types?q=$query")->status_is(200)->json_has('/0')
115 ->json_is( '/0/description', $item_type->description )->json_hasnt('/0/translated_descriptions');
117 $t->get_ok( "//$userid:$password@/api/v1/item_types?q=$query" => { 'x-koha-embed' => 'translated_descriptions' } )
118 ->status_is(200)->json_has('/0')->json_is( '/0/description', $item_type->description )
119 ->json_has('/0/translated_descriptions')->json_is(
120 '/0/translated_descriptions',
122 { lang => 'en', translation => 'English word "test"' },
123 { lang => 'sv_SE', translation => 'Swedish word "test"' },
127 # Unauthorized access
128 $t->get_ok("//$unauth_userid:$password@/api/v1/item_types")->status_is(403);
130 $schema->storage->txn_rollback;