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