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 JSON qw(encode_json);
29 use Koha::ILL::Batches;
30 use Koha::Illrequests;
31 use Koha::ILL::Batch::Statuses;
34 my $schema = Koha::Database->new->schema;
35 my $builder = t::lib::TestBuilder->new;
37 my $t = Test::Mojo->new('Koha::REST::V1');
38 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
40 subtest 'list() tests' => sub {
44 $schema->storage->txn_begin;
46 my $librarian = $builder->build_object(
48 class => 'Koha::Patrons',
50 flags => 2**22 # 22 => ill
55 my $library = $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 my $batch_to_delete = $builder->build_object( { class => 'Koha::ILL::Batches' } );
62 my $deleted_batch_id = $batch_to_delete->id;
63 $batch_to_delete->delete;
65 my $query = { ill_batch_id => [$deleted_batch_id] };
67 ## Authorized user tests
68 # No batches, so empty array should be returned
69 $t->get_ok( "//$userid:$password@/api/v1/ill/batches?q=" . encode_json($query) )->status_is(200)->json_is( [] );
71 my $batch_1 = $builder->build_object(
73 class => 'Koha::ILL::Batches',
76 patron_id => $librarian->id,
77 library_id => $library->id,
82 my $illrq = $builder->build_object(
84 class => 'Koha::Illrequests',
86 batch_id => $batch_1->id,
87 borrowernumber => $librarian->id,
92 $query = { ill_batch_id => [ $batch_1->id ] };
94 # One batch created, should get returned
95 $t->get_ok( "//$userid:$password@/api/v1/ill/batches?q="
96 . encode_json($query) => { 'x-koha-embed' => '+strings,requests+count,patron,library' } )->status_is(200)
97 ->json_has( '/0/ill_batch_id', 'Batch ID' )->json_has( '/0/name', 'Batch name' )
98 ->json_has( '/0/backend', 'Backend name' )->json_has( '/0/patron_id', 'Borrowernumber' )
99 ->json_has( '/0/library_id', 'Branchcode' )->json_has( '/0/patron', 'patron embedded' )
100 ->json_has( '/0/library', 'branch embedded' )->json_has( '/0/requests_count', 'request count' );
102 # Create a second batch with a different name
103 my $batch_2 = $builder->build_object( { class => 'Koha::ILL::Batches' } );
105 $query = { ill_batch_id => [ $batch_1->id, $batch_2->id ] };
107 # Two batches created, they should both be returned
108 $t->get_ok( "//$userid:$password@/api/v1/ill/batches?q=" . encode_json($query) )->status_is(200)
109 ->json_has( '/0', 'has first batch' )->json_is( '/0/ill_batch_id', $batch_1->id )
110 ->json_has( '/1', 'has second batch' )->json_is( '/1/ill_batch_id', $batch_2->id );
112 my $patron = $builder->build_object(
114 class => 'Koha::Patrons',
122 $patron->set_password( { password => $password, skip_validation => 1 } );
123 my $unauth_userid = $patron->userid;
125 # Unauthorized access
126 $t->get_ok("//$unauth_userid:$password@/api/v1/ill/batches")->status_is(403);
128 $schema->storage->txn_rollback;
131 subtest 'get() tests' => sub {
135 $schema->storage->txn_begin;
137 my $librarian = $builder->build_object(
139 class => 'Koha::Patrons',
140 value => { flags => 2**22 } # 22 => ill
143 my $password = 'Rebelz4DaWin';
144 $librarian->set_password( { password => $password, skip_validation => 1 } );
145 my $userid = $librarian->userid;
147 my $patron = $builder->build_object(
149 class => 'Koha::Patrons',
150 value => { flags => 0 }
154 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
156 my $batch = $builder->build_object(
158 class => 'Koha::ILL::Batches',
161 patron_id => $librarian->id,
162 library_id => $library->id,
167 $patron->set_password( { password => $password, skip_validation => 1 } );
168 my $unauth_userid = $patron->userid;
170 $t->get_ok( "//$userid:$password@/api/v1/ill/batches/"
171 . $batch->id => { 'x-koha-embed' => '+strings,requests+count,patron,library' } )->status_is(200)
172 ->json_has( '/ill_batch_id', 'Batch ID' )->json_has( '/name', 'Batch name' )
173 ->json_has( '/backend', 'Backend name' )->json_has( '/patron_id', 'Borrowernumber' )
174 ->json_has( '/library_id', 'Branchcode' )->json_has( '/patron', 'patron embedded' )
175 ->json_has( '/library', 'library embedded' )->json_has( '/requests_count', 'request count' );
177 $t->get_ok( "//$unauth_userid:$password@/api/v1/ill/batches/" . $batch->id )->status_is(403);
179 my $batch_to_delete = $builder->build_object( { class => 'Koha::ILL::Batches' } );
180 my $non_existent_id = $batch_to_delete->id;
181 $batch_to_delete->delete;
183 $t->get_ok("//$userid:$password@/api/v1/ill/batches/$non_existent_id")->status_is(404)
184 ->json_is( '/error' => 'ILL batch not found' );
186 $schema->storage->txn_rollback;
189 subtest 'add() tests' => sub {
193 $schema->storage->txn_begin;
195 my $librarian = $builder->build_object(
197 class => 'Koha::Patrons',
198 value => { flags => 2**22 } # 22 => ill
201 my $password = 'v4d3rRox';
202 $librarian->set_password( { password => $password, skip_validation => 1 } );
203 my $userid = $librarian->userid;
205 my $patron = $builder->build_object(
207 class => 'Koha::Patrons',
208 value => { flags => 0 }
212 $patron->set_password( { password => $password, skip_validation => 1 } );
213 my $unauth_userid = $patron->userid;
215 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
217 my $batch_status = $builder->build_object( { class => 'Koha::ILL::Batch::Statuses' } );
219 my $batch_metadata = {
220 name => "Anakin's requests",
222 cardnumber => $librarian->cardnumber,
223 library_id => $library->branchcode,
224 status_code => $batch_status->code
227 # Unauthorized attempt to write
228 $t->post_ok( "//$unauth_userid:$password@/api/v1/ill/batches" => json => $batch_metadata )->status_is(403);
230 # Authorized attempt to write invalid data
231 my $batch_with_invalid_field = {
236 $t->post_ok( "//$userid:$password@/api/v1/ill/batches" => json => $batch_with_invalid_field )->status_is(400)
240 message => "Properties not allowed: doh.",
246 # Authorized attempt to write
249 "//$userid:$password@/api/v1/ill/batches" => { 'x-koha-embed' => '+strings,requests+count,patron,library' } =>
250 json => $batch_metadata )->status_is(201)->json_is( '/name' => $batch_metadata->{name} )
251 ->json_is( '/backend' => $batch_metadata->{backend} )->json_is( '/patron_id' => $librarian->borrowernumber )
252 ->json_is( '/library_id' => $batch_metadata->{library_id} )->json_is( '/status_code' => $batch_status->code )
253 ->json_has('/patron')->json_has('/_strings/status_code')->json_has('/_strings/library_id')
254 ->json_has('/requests_count')->json_has('/library');
256 # Authorized attempt to create with null id
257 $batch_metadata->{id} = undef;
258 $t->post_ok( "//$userid:$password@/api/v1/ill/batches" => json => $batch_metadata )->status_is(400)
259 ->json_has('/errors');
261 $schema->storage->txn_rollback;
264 subtest 'update() tests' => sub {
268 $schema->storage->txn_begin;
270 my $librarian = $builder->build_object(
272 class => 'Koha::Patrons',
273 value => { flags => 2**22 } # 22 => ill
276 my $password = 'aw3s0m3y0d41z';
277 $librarian->set_password( { password => $password, skip_validation => 1 } );
278 my $userid = $librarian->userid;
280 my $patron = $builder->build_object(
282 class => 'Koha::Patrons',
283 value => { flags => 0 }
287 $patron->set_password( { password => $password, skip_validation => 1 } );
288 my $unauth_userid = $patron->userid;
290 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
291 my $batch_id = $builder->build_object( { class => 'Koha::ILL::Batches' } )->id;
293 # Unauthorized attempt to update
294 $t->put_ok( "//$unauth_userid:$password@/api/v1/ill/batches/$batch_id" => json =>
295 { name => 'These are not the droids you are looking for' } )->status_is(403);
297 my $batch_status = $builder->build_object( { class => 'Koha::ILL::Batch::Statuses' } );
299 # Attempt partial update on a PUT
300 my $batch_with_missing_field = {
302 patron_id => $librarian->borrowernumber,
303 library_id => $library->branchcode,
304 status_code => $batch_status->code
307 $t->put_ok( "//$userid:$password@/api/v1/ill/batches/$batch_id" => json => $batch_with_missing_field )
308 ->status_is(400)->json_is( "/errors" => [ { message => "Missing property.", path => "/body/name" } ] );
310 # Full object update on PUT
311 my $batch_with_updated_field = {
312 name => "Master Ploo Koon",
314 patron_id => $librarian->borrowernumber,
315 library_id => $library->branchcode,
316 status_code => $batch_status->code
319 $t->put_ok( "//$userid:$password@/api/v1/ill/batches/$batch_id" => json => $batch_with_updated_field )
320 ->status_is(200)->json_is( '/name' => 'Master Ploo Koon' );
322 # Authorized attempt to write invalid data
323 my $batch_with_invalid_field = {
325 name => "Master Mace Windu",
329 $t->put_ok( "//$userid:$password@/api/v1/ill/batches/$batch_id" => json => $batch_with_invalid_field )
330 ->status_is(400)->json_is(
333 message => "Properties not allowed: doh.",
339 my $batch_to_delete = $builder->build_object( { class => 'Koha::ILL::Batches' } );
340 my $non_existent_id = $batch_to_delete->id;
341 $batch_to_delete->delete;
343 $t->put_ok( "//$userid:$password@/api/v1/ill/batches/$non_existent_id" => json => $batch_with_updated_field )
346 # Wrong method (POST)
347 $batch_with_updated_field->{id} = 2;
349 $t->post_ok( "//$userid:$password@/api/v1/ill/batches/$batch_id" => json => $batch_with_updated_field )
352 $schema->storage->txn_rollback;
355 subtest 'delete() tests' => sub {
359 $schema->storage->txn_begin;
361 my $librarian = $builder->build_object(
363 class => 'Koha::Patrons',
364 value => { flags => 2**22 } # 22 => ill
367 my $password = 's1th43v3r!';
368 $librarian->set_password( { password => $password, skip_validation => 1 } );
369 my $userid = $librarian->userid;
371 my $patron = $builder->build_object(
373 class => 'Koha::Patrons',
374 value => { flags => 0 }
378 $patron->set_password( { password => $password, skip_validation => 1 } );
379 my $unauth_userid = $patron->userid;
381 my $batch_id = $builder->build_object( { class => 'Koha::ILL::Batches' } )->id;
383 # Unauthorized attempt to delete
384 $t->delete_ok("//$unauth_userid:$password@/api/v1/ill/batches/$batch_id")->status_is(403);
386 $t->delete_ok("//$userid:$password@/api/v1/ill/batches/$batch_id")->status_is(204);
388 $t->delete_ok("//$userid:$password@/api/v1/ill/batches/$batch_id")->status_is(404);
390 $schema->storage->txn_rollback;