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 => 5;
23 use t::lib::TestBuilder;
26 use Koha::IllbatchStatus;
27 use Koha::IllbatchStatuses;
30 my $schema = Koha::Database->new->schema;
31 my $builder = t::lib::TestBuilder->new;
33 my $t = Test::Mojo->new('Koha::REST::V1');
34 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
36 subtest 'list() tests' => sub {
40 $schema->storage->txn_begin;
42 Koha::IllbatchStatuses->search->delete;
44 # Create an admin user
45 my $librarian = $builder->build_object(
47 class => 'Koha::Patrons',
49 flags => 2**22 # 22 => ill
53 my $password = 'yoda4ever!';
54 $librarian->set_password( { password => $password, skip_validation => 1 } );
55 my $userid = $librarian->userid;
57 ## Authorized user tests
58 # No statuses, so empty array should be returned
59 $t->get_ok("//$userid:$password@/api/v1/illbatchstatuses")->status_is(200)->json_is( [] );
61 my $status = $builder->build_object(
63 class => 'Koha::IllbatchStatuses',
72 # One batch created, should get returned
73 $t->get_ok("//$userid:$password@/api/v1/illbatchstatuses")->status_is(200)->json_has( '/0/id', 'ID' )
74 ->json_has( '/0/name', 'Name' )->json_has( '/0/code', 'Code' )->json_has( '/0/is_system', 'is_system' );
76 $schema->storage->txn_rollback;
79 subtest 'get() tests' => sub {
83 $schema->storage->txn_begin;
85 my $librarian = $builder->build_object(
87 class => 'Koha::Patrons',
88 value => { flags => 2**22 } # 22 => ill
91 my $password = 'Rebelz4DaWin';
92 $librarian->set_password( { password => $password, skip_validation => 1 } );
93 my $userid = $librarian->userid;
95 my $status = $builder->build_object(
97 class => 'Koha::IllbatchStatuses',
107 my $patron = $builder->build_object(
109 class => 'Koha::Patrons',
110 value => { flags => 0 }
113 $patron->set_password( { password => $password, skip_validation => 1 } );
114 my $unauth_userid = $patron->userid;
116 $t->get_ok( "//$userid:$password@/api/v1/illbatchstatuses/" . $status->code )->status_is(200)
117 ->json_has( '/id', 'ID' )->json_has( '/name', 'Name' )->json_has( '/code', 'Code' )
118 ->json_has( '/is_system', 'is_system' );
120 $t->get_ok( "//$unauth_userid:$password@/api/v1/illbatchstatuses/" . $status->id )->status_is(403);
122 my $status_to_delete = $builder->build_object( { class => 'Koha::IllbatchStatuses' } );
123 my $non_existent_code = $status_to_delete->code;
124 $status_to_delete->delete;
126 $t->get_ok("//$userid:$password@/api/v1/illbatchstatuses/$non_existent_code")->status_is(404)
127 ->json_is( '/error' => 'ILL batch status not found' );
129 $schema->storage->txn_rollback;
132 subtest 'add() tests' => sub {
136 $schema->storage->txn_begin;
138 my $librarian = $builder->build_object(
140 class => 'Koha::Patrons',
141 value => { flags => 2**22 } # 22 => ill
144 my $password = '3poRox';
145 $librarian->set_password( { password => $password, skip_validation => 1 } );
146 my $userid = $librarian->userid;
148 my $patron = $builder->build_object(
150 class => 'Koha::Patrons',
151 value => { flags => 0 }
154 $patron->set_password( { password => $password, skip_validation => 1 } );
155 my $unauth_userid = $patron->userid;
157 my $status_metadata = {
158 name => "In a bacta tank",
163 # Unauthorized attempt to write
164 $t->post_ok( "//$unauth_userid:$password@/api/v1/illbatchstatuses" => json => $status_metadata )->status_is(403);
166 # Authorized attempt to write invalid data
167 my $status_with_invalid_field = {
172 $t->post_ok( "//$userid:$password@/api/v1/illbatchstatuses" => json => $status_with_invalid_field )->status_is(400)
176 message => "Properties not allowed: doh.",
182 # Authorized attempt to write
184 $t->post_ok( "//$userid:$password@/api/v1/illbatchstatuses" => json => $status_metadata )->status_is(201)
185 ->json_has( '/id', 'ID' )->json_has( '/name', 'Name' )->json_has( '/code', 'Code' )
186 ->json_has( '/is_system', 'is_system' );
188 # Authorized attempt to create with null id
189 $status_metadata->{id} = undef;
190 $t->post_ok( "//$userid:$password@/api/v1/illbatchstatuses" => json => $status_metadata )->status_is(400)
191 ->json_has('/errors');
193 $schema->storage->txn_rollback;
196 subtest 'update() tests' => sub {
200 $schema->storage->txn_begin;
202 my $librarian = $builder->build_object(
204 class => 'Koha::Patrons',
205 value => { flags => 2**22 } # 22 => ill
208 my $password = 'aw3s0m3y0d41z';
209 $librarian->set_password( { password => $password, skip_validation => 1 } );
210 my $userid = $librarian->userid;
212 my $patron = $builder->build_object(
214 class => 'Koha::Patrons',
215 value => { flags => 0 }
218 $patron->set_password( { password => $password, skip_validation => 1 } );
219 my $unauth_userid = $patron->userid;
221 my $status_code = $builder->build_object( { class => 'Koha::IllbatchStatuses' } )->code;
223 # Unauthorized attempt to update
224 $t->put_ok( "//$unauth_userid:$password@/api/v1/illbatchstatuses/$status_code" => json =>
225 { name => 'These are not the droids you are looking for' } )->status_is(403);
227 # Attempt partial update on a PUT
228 my $status_with_missing_field = {
229 code => $status_code,
233 $t->put_ok( "//$userid:$password@/api/v1/illbatchstatuses/$status_code" => json => $status_with_missing_field )
234 ->status_is(400)->json_is( "/errors" => [ { message => "Missing property.", path => "/body/name" } ] );
236 # Full object update on PUT
237 my $status_with_updated_field = {
238 name => "Master Ploo Koon",
239 code => $status_code,
243 $t->put_ok( "//$userid:$password@/api/v1/illbatchstatuses/$status_code" => json => $status_with_updated_field )
244 ->status_is(200)->json_is( '/name' => 'Master Ploo Koon' );
246 # Authorized attempt to write invalid data
247 my $status_with_invalid_field = {
249 name => "Master Mace Windu",
253 $t->put_ok( "//$userid:$password@/api/v1/illbatchstatuses/$status_code" => json => $status_with_invalid_field )
254 ->status_is(400)->json_is(
257 message => "Properties not allowed: doh.",
263 my $status_to_delete = $builder->build_object( { class => 'Koha::IllbatchStatuses' } );
264 my $non_existent_code = $status_to_delete->code;
265 $status_to_delete->delete;
268 "//$userid:$password@/api/v1/illbatchstatuses/$non_existent_code" => json => $status_with_updated_field )
271 $schema->storage->txn_rollback;
274 subtest 'delete() tests' => sub {
278 $schema->storage->txn_begin;
280 my $librarian = $builder->build_object(
282 class => 'Koha::Patrons',
283 value => { flags => 2**22 } # 22 => ill
286 my $password = 's1th43v3r!';
287 $librarian->set_password( { password => $password, skip_validation => 1 } );
288 my $userid = $librarian->userid;
290 my $patron = $builder->build_object(
292 class => 'Koha::Patrons',
293 value => { flags => 0 }
297 $patron->set_password( { password => $password, skip_validation => 1 } );
298 my $unauth_userid = $patron->userid;
300 my $non_system_status = $builder->build_object(
302 class => 'Koha::IllbatchStatuses',
303 value => { is_system => 0 }
307 my $system_status = $builder->build_object(
309 class => 'Koha::IllbatchStatuses',
310 value => { is_system => 1 }
314 # Unauthorized attempt to delete
315 $t->delete_ok( "//$unauth_userid:$password@/api/v1/illbatchstatuses/" . $non_system_status->code )->status_is(403);
317 $t->delete_ok( "//$userid:$password@/api/v1/illbatchstatuses/" . $non_system_status->code )->status_is(204);
319 $t->delete_ok( "//$userid:$password@/api/v1/illbatchstatuses/" . $non_system_status->code )->status_is(404);
321 $t->delete_ok( "//$userid:$password@/api/v1/illbatchstatuses/" . $system_status->code )->status_is(400)
322 ->json_is( "/errors" => [ { message => "ILL batch status cannot be deleted" } ] );
324 $schema->storage->txn_rollback;