|
Line 0
Link Here
|
| 0 |
- |
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 |
| 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. |
| 9 |
# |
| 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. |
| 14 |
# |
| 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>. |
| 17 |
|
| 18 |
use Modern::Perl; |
| 19 |
|
| 20 |
use Test::More tests => 1; |
| 21 |
use Test::Mojo; |
| 22 |
|
| 23 |
use t::lib::TestBuilder; |
| 24 |
use t::lib::Mocks; |
| 25 |
|
| 26 |
use Koha::ItemTypes; |
| 27 |
use Koha::Database; |
| 28 |
|
| 29 |
my $schema = Koha::Database->new->schema; |
| 30 |
my $builder = t::lib::TestBuilder->new; |
| 31 |
|
| 32 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
| 33 |
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); |
| 34 |
|
| 35 |
subtest 'list_itemtypes() tests' => sub { |
| 36 |
|
| 37 |
plan tests => 12; |
| 38 |
|
| 39 |
$schema->storage->txn_begin; |
| 40 |
|
| 41 |
my $itemtype = $builder->build_object( |
| 42 |
{ |
| 43 |
class => 'Koha::ItemTypes', |
| 44 |
value => { |
| 45 |
itemtype => 'TEST_IT', |
| 46 |
parent_type => undef, |
| 47 |
description => 'Test itemtype', |
| 48 |
rentalcharge => 100.0, |
| 49 |
rentalcharge_daily => 50., |
| 50 |
rentalcharge_daily_calendar => 0, |
| 51 |
rentalcharge_hourly => 0.60, |
| 52 |
rentalcharge_hourly_calendar => 1, |
| 53 |
defaultreplacecost => 1000.0, |
| 54 |
processfee => 20.0, |
| 55 |
notforloan => 0, |
| 56 |
imageurl => 'https://upload.wikimedia.org/wikipedia/commons/1/1f/202208_test-tube-4.svg', |
| 57 |
summary => 'An itemtype for testing', |
| 58 |
checkinmsg => 'Checking in test', |
| 59 |
checkinmsgtype => 'message', |
| 60 |
sip_media_type => 'spt', |
| 61 |
hideinopac => 1, |
| 62 |
searchcategory => 'test search category', |
| 63 |
automatic_checkin => 0, |
| 64 |
} |
| 65 |
} |
| 66 |
); |
| 67 |
|
| 68 |
my $en = $builder->build_object( |
| 69 |
{ |
| 70 |
class => 'Koha::Localizations', |
| 71 |
value => { |
| 72 |
entity => 'itemtypes', |
| 73 |
code => 'TEST_IT', |
| 74 |
lang => 'en', |
| 75 |
translation => 'English word "test"', |
| 76 |
} |
| 77 |
} |
| 78 |
); |
| 79 |
my $sv = $builder->build_object( |
| 80 |
{ |
| 81 |
class => 'Koha::Localizations', |
| 82 |
value => { |
| 83 |
entity => 'itemtypes', |
| 84 |
code => 'TEST_IT', |
| 85 |
lang => 'sv_SE', |
| 86 |
translation => 'Swedish word "test"', |
| 87 |
} |
| 88 |
} |
| 89 |
); |
| 90 |
|
| 91 |
my $librarian = $builder->build_object( |
| 92 |
{ |
| 93 |
class => 'Koha::Patrons', |
| 94 |
value => { flags => 2 ** 2 } # catalogue flag = 2 |
| 95 |
} |
| 96 |
); |
| 97 |
my $password = 'thePassword123'; |
| 98 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 99 |
my $userid = $librarian->userid; |
| 100 |
|
| 101 |
my $patron = $builder->build_object( |
| 102 |
{ |
| 103 |
class => 'Koha::Patrons', |
| 104 |
value => { flags => 0 } |
| 105 |
} |
| 106 |
); |
| 107 |
|
| 108 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 109 |
my $unauth_userid = $patron->userid; |
| 110 |
|
| 111 |
## Authorized user tests |
| 112 |
# No category, 404 expected |
| 113 |
$t->get_ok("//$userid:$password@/api/v1/itemtypes") |
| 114 |
->status_is(200) |
| 115 |
->json_has('/0'); |
| 116 |
|
| 117 |
for my $json (@{$t->tx->res->json}) { |
| 118 |
if ($json->{itemtype} eq 'TEST_IT') { |
| 119 |
is($json->{description}, 'Test itemtype'); |
| 120 |
ok(!exists $json->{translated_descriptions}); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
$t->get_ok("//$userid:$password@/api/v1/itemtypes" => { 'x-koha-embed' => 'translated_descriptions' } ) |
| 125 |
->status_is(200) |
| 126 |
->json_has('/0'); |
| 127 |
|
| 128 |
for my $json (@{$t->tx->res->json}) { |
| 129 |
if ($json->{itemtype} eq 'TEST_IT') { |
| 130 |
is($json->{description}, 'Test itemtype'); |
| 131 |
is_deeply($json->{translated_descriptions}, [ |
| 132 |
{ lang => 'en', translation => 'English word "test"' }, |
| 133 |
{ lang => 'sv_SE', translation => 'Swedish word "test"' }, |
| 134 |
]); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
# Unauthorized access |
| 139 |
$t->get_ok("//$unauth_userid:$password@/api/v1/itemtypes") |
| 140 |
->status_is(403); |
| 141 |
|
| 142 |
$schema->storage->txn_rollback; |
| 143 |
}; |