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;
28 use Koha::Illrequests;
29 use Koha::IllbatchStatuses;
32 my $schema = Koha::Database->new->schema;
33 my $builder = t::lib::TestBuilder->new;
35 my $t = Test::Mojo->new('Koha::REST::V1');
36 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
38 subtest 'list() tests' => sub {
42 $schema->storage->txn_begin;
44 Koha::Illbatches->search->delete;
46 my $librarian = $builder->build_object(
48 class => 'Koha::Patrons',
50 flags => 2**22 # 22 => ill
55 my $branch = $builder->build_object( { class => 'Koha::Libraries' } );
57 my $password = 'sheev_is_da_boss!';
58 $librarian->set_password( { password => $password, skip_validation => 1 } );
59 my $userid = $librarian->userid;
61 ## Authorized user tests
62 # No batches, so empty array should be returned
63 $t->get_ok("//$userid:$password@/api/v1/ill/batches")->status_is(200)->json_is( [] );
65 my $batch = $builder->build_object(
67 class => 'Koha::Illbatches',
69 name => "PapaPalpatine",
71 borrowernumber => $librarian->borrowernumber,
72 branchcode => $branch->branchcode
77 my $illrq = $builder->build(
79 source => 'Illrequest',
81 borrowernumber => $librarian->borrowernumber,
82 batch_id => $batch->id
87 # One batch created, should get returned
88 $t->get_ok("//$userid:$password@/api/v1/ill/batches")->status_is(200)->json_has( '/0/batch_id', 'Batch ID' )
89 ->json_has( '/0/name', 'Batch name' )->json_has( '/0/backend', 'Backend name' )
90 ->json_has( '/0/patron_id', 'Borrowernumber' )->json_has( '/0/library_id', 'Branchcode' )
91 ->json_has( '/0/patron', 'patron embedded' )->json_has( '/0/branch', 'branch embedded' )
92 ->json_has( '/0/requests_count', 'request count' );
94 # Try to create a second batch with the same name, this should fail
95 my $another_batch = $builder->build_object( { class => 'Koha::Illbatches', value => { name => $batch->name } } );
97 # Create a second batch with a different name
98 my $batch_with_another_name = $builder->build_object( { class => 'Koha::Illbatches' } );
100 # Two batches created, they should both be returned
101 $t->get_ok("//$userid:$password@/api/v1/ill/batches")->status_is(200)->json_has( '/0', 'has first batch' )
102 ->json_has( '/1', 'has second batch' );
104 my $patron = $builder->build_object(
106 class => 'Koha::Patrons',
114 $patron->set_password( { password => $password, skip_validation => 1 } );
115 my $unauth_userid = $patron->userid;
117 # Unauthorized access
118 $t->get_ok("//$unauth_userid:$password@/api/v1/ill/batches")->status_is(403);
120 $schema->storage->txn_rollback;
123 subtest 'get() tests' => sub {
127 $schema->storage->txn_begin;
129 my $librarian = $builder->build_object(
131 class => 'Koha::Patrons',
132 value => { flags => 2**22 } # 22 => ill
135 my $password = 'Rebelz4DaWin';
136 $librarian->set_password( { password => $password, skip_validation => 1 } );
137 my $userid = $librarian->userid;
139 my $patron = $builder->build_object(
141 class => 'Koha::Patrons',
142 value => { flags => 0 }
146 my $branch = $builder->build_object( { class => 'Koha::Libraries' } );
148 my $batch = $builder->build_object(
150 class => 'Koha::Illbatches',
152 name => "LeiaOrgana",
154 borrowernumber => $librarian->borrowernumber,
155 branchcode => $branch->branchcode
160 $patron->set_password( { password => $password, skip_validation => 1 } );
161 my $unauth_userid = $patron->userid;
163 $t->get_ok( "//$userid:$password@/api/v1/ill/batches/" . $batch->id )->status_is(200)
164 ->json_has( '/batch_id', 'Batch ID' )->json_has( '/name', 'Batch name' )
165 ->json_has( '/backend', 'Backend name' )->json_has( '/patron_id', 'Borrowernumber' )
166 ->json_has( '/library_id', 'Branchcode' )->json_has( '/patron', 'patron embedded' )
167 ->json_has( '/branch', 'branch embedded' )->json_has( '/requests_count', 'request count' );
169 $t->get_ok( "//$unauth_userid:$password@/api/v1/ill/batches/" . $batch->id )->status_is(403);
171 my $batch_to_delete = $builder->build_object( { class => 'Koha::Illbatches' } );
172 my $non_existent_id = $batch_to_delete->id;
173 $batch_to_delete->delete;
175 $t->get_ok("//$userid:$password@/api/v1/ill/batches/$non_existent_id")->status_is(404)
176 ->json_is( '/error' => 'ILL batch not found' );
178 $schema->storage->txn_rollback;
181 subtest 'add() tests' => sub {
185 $schema->storage->txn_begin;
187 my $librarian = $builder->build_object(
189 class => 'Koha::Patrons',
190 value => { flags => 2**22 } # 22 => ill
193 my $password = 'v4d3rRox';
194 $librarian->set_password( { password => $password, skip_validation => 1 } );
195 my $userid = $librarian->userid;
197 my $patron = $builder->build_object(
199 class => 'Koha::Patrons',
200 value => { flags => 0 }
204 $patron->set_password( { password => $password, skip_validation => 1 } );
205 my $unauth_userid = $patron->userid;
207 my $branch = $builder->build_object( { class => 'Koha::Libraries' } );
209 my $batch_status = $builder->build_object( { class => 'Koha::IllbatchStatuses' } );
211 my $batch_metadata = {
212 name => "Anakin's requests",
214 cardnumber => $librarian->cardnumber,
215 library_id => $branch->branchcode,
216 statuscode => $batch_status->code
219 # Unauthorized attempt to write
220 $t->post_ok( "//$unauth_userid:$password@/api/v1/ill/batches" => json => $batch_metadata )->status_is(403);
222 # Authorized attempt to write invalid data
223 my $batch_with_invalid_field = {
228 $t->post_ok( "//$userid:$password@/api/v1/ill/batches" => json => $batch_with_invalid_field )->status_is(400)
232 message => "Properties not allowed: doh.",
238 # Authorized attempt to write
240 $t->post_ok( "//$userid:$password@/api/v1/ill/batches" => json => $batch_metadata )->status_is(201)
241 ->json_is( '/name' => $batch_metadata->{name} )->json_is( '/backend' => $batch_metadata->{backend} )
242 ->json_is( '/patron_id' => $librarian->borrowernumber )
243 ->json_is( '/library_id' => $batch_metadata->{library_id} )->json_is( '/statuscode' => $batch_status->code )
244 ->json_has('/patron')->json_has('/status')->json_has('/requests_count')->json_has('/branch');
246 # Authorized attempt to create with null id
247 $batch_metadata->{id} = undef;
248 $t->post_ok( "//$userid:$password@/api/v1/ill/batches" => json => $batch_metadata )->status_is(400)
249 ->json_has('/errors');
251 $schema->storage->txn_rollback;
254 subtest 'update() tests' => sub {
258 $schema->storage->txn_begin;
260 my $librarian = $builder->build_object(
262 class => 'Koha::Patrons',
263 value => { flags => 2**22 } # 22 => ill
266 my $password = 'aw3s0m3y0d41z';
267 $librarian->set_password( { password => $password, skip_validation => 1 } );
268 my $userid = $librarian->userid;
270 my $patron = $builder->build_object(
272 class => 'Koha::Patrons',
273 value => { flags => 0 }
277 $patron->set_password( { password => $password, skip_validation => 1 } );
278 my $unauth_userid = $patron->userid;
280 my $branch = $builder->build_object( { class => 'Koha::Libraries' } );
282 my $batch_id = $builder->build_object( { class => 'Koha::Illbatches' } )->id;
284 # Unauthorized attempt to update
285 $t->put_ok( "//$unauth_userid:$password@/api/v1/ill/batches/$batch_id" => json =>
286 { name => 'These are not the droids you are looking for' } )->status_is(403);
288 my $batch_status = $builder->build_object( { class => 'Koha::IllbatchStatuses' } );
290 # Attempt partial update on a PUT
291 my $batch_with_missing_field = {
293 patron_id => $librarian->borrowernumber,
294 library_id => $branch->branchcode,
295 statuscode => $batch_status->code
298 $t->put_ok( "//$userid:$password@/api/v1/ill/batches/$batch_id" => json => $batch_with_missing_field )
299 ->status_is(400)->json_is( "/errors" => [ { message => "Missing property.", path => "/body/name" } ] );
301 # Full object update on PUT
302 my $batch_with_updated_field = {
303 name => "Master Ploo Koon",
305 patron_id => $librarian->borrowernumber,
306 library_id => $branch->branchcode,
307 statuscode => $batch_status->code
310 $t->put_ok( "//$userid:$password@/api/v1/ill/batches/$batch_id" => json => $batch_with_updated_field )
311 ->status_is(200)->json_is( '/name' => 'Master Ploo Koon' );
313 # Authorized attempt to write invalid data
314 my $batch_with_invalid_field = {
316 name => "Master Mace Windu",
320 $t->put_ok( "//$userid:$password@/api/v1/ill/batches/$batch_id" => json => $batch_with_invalid_field )
321 ->status_is(400)->json_is(
324 message => "Properties not allowed: doh.",
330 my $batch_to_delete = $builder->build_object( { class => 'Koha::Cities' } );
331 my $non_existent_id = $batch_to_delete->id;
332 $batch_to_delete->delete;
334 $t->put_ok( "//$userid:$password@/api/v1/ill/batches/$non_existent_id" => json => $batch_with_updated_field )
337 # Wrong method (POST)
338 $batch_with_updated_field->{id} = 2;
340 $t->post_ok( "//$userid:$password@/api/v1/ill/batches/$batch_id" => json => $batch_with_updated_field )
343 $schema->storage->txn_rollback;
346 subtest 'delete() tests' => sub {
350 $schema->storage->txn_begin;
352 my $librarian = $builder->build_object(
354 class => 'Koha::Patrons',
355 value => { flags => 2**22 } # 22 => ill
358 my $password = 's1th43v3r!';
359 $librarian->set_password( { password => $password, skip_validation => 1 } );
360 my $userid = $librarian->userid;
362 my $patron = $builder->build_object(
364 class => 'Koha::Patrons',
365 value => { flags => 0 }
369 $patron->set_password( { password => $password, skip_validation => 1 } );
370 my $unauth_userid = $patron->userid;
372 my $batch_id = $builder->build_object( { class => 'Koha::Illbatches' } )->id;
374 # Unauthorized attempt to delete
375 $t->delete_ok("//$unauth_userid:$password@/api/v1/ill/batches/$batch_id")->status_is(403);
377 $t->delete_ok("//$userid:$password@/api/v1/ill/batches/$batch_id")->status_is(204);
379 $t->delete_ok("//$userid:$password@/api/v1/ill/batches/$batch_id")->status_is(404);
381 $schema->storage->txn_rollback;