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")
63 my $status = $builder->build_object({
64 class => 'Koha::IllbatchStatuses',
72 # One batch created, should get returned
73 $t->get_ok("//$userid:$password@/api/v1/illbatchstatuses")
75 ->json_has( '/0/id', 'ID' )
76 ->json_has( '/0/name', 'Name' )
77 ->json_has( '/0/code', 'Code' )
78 ->json_has( '/0/is_system', 'is_system' );
80 $schema->storage->txn_rollback;
83 subtest 'get() tests' => sub {
87 $schema->storage->txn_begin;
89 my $librarian = $builder->build_object(
91 class => 'Koha::Patrons',
92 value => { flags => 2**22 } # 22 => ill
95 my $password = 'Rebelz4DaWin';
96 $librarian->set_password( { password => $password, skip_validation => 1 } );
97 my $userid = $librarian->userid;
99 my $status = $builder->build_object({
100 class => 'Koha::IllbatchStatuses',
109 my $patron = $builder->build_object(
111 class => 'Koha::Patrons',
112 value => { flags => 0 }
115 $patron->set_password( { password => $password, skip_validation => 1 } );
116 my $unauth_userid = $patron->userid;
118 $t->get_ok( "//$userid:$password@/api/v1/illbatchstatuses/" . $status->code )
120 ->json_has( '/id', 'ID' )
121 ->json_has( '/name', 'Name' )
122 ->json_has( '/code', 'Code' )
123 ->json_has( '/is_system', 'is_system' );
125 $t->get_ok( "//$unauth_userid:$password@/api/v1/illbatchstatuses/" . $status->id )
128 my $status_to_delete = $builder->build_object({ class => 'Koha::IllbatchStatuses' });
129 my $non_existent_code = $status_to_delete->code;
130 $status_to_delete->delete;
132 $t->get_ok( "//$userid:$password@/api/v1/illbatchstatuses/$non_existent_code" )
134 ->json_is( '/error' => 'ILL batch status not found' );
136 $schema->storage->txn_rollback;
139 subtest 'add() tests' => sub {
143 $schema->storage->txn_begin;
145 my $librarian = $builder->build_object(
147 class => 'Koha::Patrons',
148 value => { flags => 2**22 } # 22 => ill
151 my $password = '3poRox';
152 $librarian->set_password( { password => $password, skip_validation => 1 } );
153 my $userid = $librarian->userid;
155 my $patron = $builder->build_object(
157 class => 'Koha::Patrons',
158 value => { flags => 0 }
161 $patron->set_password( { password => $password, skip_validation => 1 } );
162 my $unauth_userid = $patron->userid;
164 my $status_metadata = {
165 name => "In a bacta tank",
170 # Unauthorized attempt to write
171 $t->post_ok("//$unauth_userid:$password@/api/v1/illbatchstatuses" => json => $status_metadata)
174 # Authorized attempt to write invalid data
175 my $status_with_invalid_field = {
180 $t->post_ok( "//$userid:$password@/api/v1/illbatchstatuses" => json => $status_with_invalid_field )
185 message => "Properties not allowed: doh.",
191 # Authorized attempt to write
193 $t->post_ok( "//$userid:$password@/api/v1/illbatchstatuses" => json => $status_metadata )
195 ->json_has( '/id', 'ID' )
196 ->json_has( '/name', 'Name' )
197 ->json_has( '/code', 'Code' )
198 ->json_has( '/is_system', 'is_system' );
200 # Authorized attempt to create with null id
201 $status_metadata->{id} = undef;
202 $t->post_ok( "//$userid:$password@/api/v1/illbatchstatuses" => json => $status_metadata )
204 ->json_has('/errors');
206 $schema->storage->txn_rollback;
209 subtest 'update() tests' => sub {
213 $schema->storage->txn_begin;
215 my $librarian = $builder->build_object(
217 class => 'Koha::Patrons',
218 value => { flags => 2**22 } # 22 => ill
221 my $password = 'aw3s0m3y0d41z';
222 $librarian->set_password( { password => $password, skip_validation => 1 } );
223 my $userid = $librarian->userid;
225 my $patron = $builder->build_object(
227 class => 'Koha::Patrons',
228 value => { flags => 0 }
231 $patron->set_password( { password => $password, skip_validation => 1 } );
232 my $unauth_userid = $patron->userid;
234 my $status_code = $builder->build_object({ class => 'Koha::IllbatchStatuses' } )->code;
236 # Unauthorized attempt to update
237 $t->put_ok( "//$unauth_userid:$password@/api/v1/illbatchstatuses/$status_code" => json => { name => 'These are not the droids you are looking for' } )
240 # Attempt partial update on a PUT
241 my $status_with_missing_field = {
242 code => $status_code,
246 $t->put_ok( "//$userid:$password@/api/v1/illbatchstatuses/$status_code" => json => $status_with_missing_field )
248 ->json_is( "/errors" =>
249 [ { message => "Missing property.", path => "/body/name" } ]
252 # Full object update on PUT
253 my $status_with_updated_field = {
254 name => "Master Ploo Koon",
255 code => $status_code,
259 $t->put_ok( "//$userid:$password@/api/v1/illbatchstatuses/$status_code" => json => $status_with_updated_field )
261 ->json_is( '/name' => 'Master Ploo Koon' );
263 # Authorized attempt to write invalid data
264 my $status_with_invalid_field = {
266 name => "Master Mace Windu",
270 $t->put_ok( "//$userid:$password@/api/v1/illbatchstatuses/$status_code" => json => $status_with_invalid_field )
275 message => "Properties not allowed: doh.",
281 my $status_to_delete = $builder->build_object({ class => 'Koha::IllbatchStatuses' });
282 my $non_existent_code = $status_to_delete->code;
283 $status_to_delete->delete;
285 $t->put_ok( "//$userid:$password@/api/v1/illbatchstatuses/$non_existent_code" => json => $status_with_updated_field )
288 $schema->storage->txn_rollback;
291 subtest 'delete() tests' => sub {
295 $schema->storage->txn_begin;
297 my $librarian = $builder->build_object(
299 class => 'Koha::Patrons',
300 value => { flags => 2**22 } # 22 => ill
303 my $password = 's1th43v3r!';
304 $librarian->set_password( { password => $password, skip_validation => 1 } );
305 my $userid = $librarian->userid;
307 my $patron = $builder->build_object(
309 class => 'Koha::Patrons',
310 value => { flags => 0 }
314 $patron->set_password( { password => $password, skip_validation => 1 } );
315 my $unauth_userid = $patron->userid;
317 my $non_system_status = $builder->build_object({
318 class => 'Koha::IllbatchStatuses',
324 my $system_status = $builder->build_object({
325 class => 'Koha::IllbatchStatuses',
331 # Unauthorized attempt to delete
332 $t->delete_ok( "//$unauth_userid:$password@/api/v1/illbatchstatuses/" . $non_system_status->code )
335 $t->delete_ok("//$userid:$password@/api/v1/illbatchstatuses/" . $non_system_status->code )
338 $t->delete_ok("//$userid:$password@/api/v1/illbatchstatuses/" . $non_system_status->code )
341 $t->delete_ok("//$userid:$password@/api/v1/illbatchstatuses/" . $system_status->code )
346 message => "ILL batch status cannot be deleted"
351 $schema->storage->txn_rollback;