Bug 36329: Make POST /transfer_limits/batch honor BranchTransferLimitsType
[koha.git] / t / db_dependent / api / v1 / transfer_limits.t
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 => 4;
21 use Test::Mojo;
22 use Test::Warn;
23
24 use t::lib::TestBuilder;
25 use t::lib::Mocks;
26
27 use List::Util qw(min);
28
29 use Koha::Item::Transfer::Limits;
30 use Koha::Database;
31
32 my $schema  = Koha::Database->new->schema;
33 my $builder = t::lib::TestBuilder->new;
34
35 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
36
37 my $t = Test::Mojo->new('Koha::REST::V1');
38
39 subtest 'list() tests' => sub {
40     plan tests => 3;
41
42     $schema->storage->txn_begin;
43
44     Koha::Item::Transfer::Limits->delete;
45
46     my $patron = $builder->build_object({
47         class => 'Koha::Patrons',
48         value => { flags => 1 }
49     });
50     my $password = 'thePassword123';
51     $patron->set_password({ password => $password, skip_validation => 1 });
52     my $userid = $patron->userid;
53
54     my $limit = $builder->build_object({ class => 'Koha::Item::Transfer::Limits' });
55
56     $t->get_ok( "//$userid:$password@/api/v1/transfer_limits" )
57       ->status_is( 200, 'SWAGGER3.2.2' )
58       ->json_is( [$limit->to_api] );
59
60     $schema->storage->txn_rollback;
61 };
62
63 subtest 'add() tests' => sub {
64
65     plan tests => 11;
66
67     $schema->storage->txn_begin;
68
69     my $authorized_patron = $builder->build_object({
70         class => 'Koha::Patrons',
71         value => { flags => 1 }
72     });
73     my $password = 'thePassword123';
74     $authorized_patron->set_password({ password => $password, skip_validation => 1 });
75     my $auth_userid = $authorized_patron->userid;
76
77     my $unauthorized_patron = $builder->build_object({
78         class => 'Koha::Patrons',
79         value => { flags => 4 }
80     });
81     $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
82     my $unauth_userid = $unauthorized_patron->userid;
83
84     my $limit = $builder->build_object({ class => 'Koha::Item::Transfer::Limits' });
85     my $limit_hashref = $limit->to_api;
86     delete $limit_hashref->{limit_id};
87     $limit->delete;
88
89     # Unauthorized attempt to write
90     $t->post_ok( "//$unauth_userid:$password@/api/v1/transfer_limits" => json => $limit_hashref )
91       ->status_is(403);
92
93     # Authorized attempt to write invalid data
94     my $limit_with_invalid_field = {'invalid' => 'invalid'};
95
96     $t->post_ok( "//$auth_userid:$password@/api/v1/transfer_limits" => json => $limit_with_invalid_field )
97       ->status_is(400)
98       ->json_is(
99         "/errors" => [
100             {
101                 message => "Properties not allowed: invalid.",
102                 path    => "/body"
103             }
104         ]
105     );
106
107     # Authorized attempt to write
108     $t->post_ok( "//$auth_userid:$password@/api/v1/transfer_limits" => json => $limit_hashref )
109       ->status_is( 201, 'SWAGGER3.2.1' )
110       ->json_has( '' => $limit_hashref, 'SWAGGER3.3.1' );
111
112     $t->post_ok( "//$auth_userid:$password@/api/v1/transfer_limits" => json => $limit_hashref )
113       ->status_is( 409, 'Conflict creating the resource' )
114       ->json_is(
115         {
116             error => qq{Exception 'Koha::Exceptions::TransferLimit::Duplicate' thrown 'A transfer limit with the given parameters already exists!'\n}
117         }
118       );
119
120     $schema->storage->txn_rollback;
121 };
122
123 subtest 'delete() tests' => sub {
124     plan tests => 7;
125
126     $schema->storage->txn_begin;
127
128     my $authorized_patron = $builder->build_object({
129         class => 'Koha::Patrons',
130         value => { flags => 1 }
131     });
132     my $password = 'thePassword123';
133     $authorized_patron->set_password({ password => $password, skip_validation => 1 });
134     my $auth_userid = $authorized_patron->userid;
135
136     my $unauthorized_patron = $builder->build_object({
137         class => 'Koha::Patrons',
138         value => { flags => 4 }
139     });
140     $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
141     my $unauth_userid = $unauthorized_patron->userid;
142
143     my $limit = $builder->build_object({ class => 'Koha::Item::Transfer::Limits' });
144     my $limit_id = $limit->id;
145
146     # Unauthorized attempt to delete
147     $t->delete_ok( "//$unauth_userid:$password@/api/v1/transfer_limits/$limit_id" )
148       ->status_is(403);
149
150     $t->delete_ok( "//$auth_userid:$password@/api/v1/transfer_limits/$limit_id" )
151       ->status_is(204, 'SWAGGER3.2.4')
152       ->content_is('', 'SWAGGER3.3.4');
153
154     $t->delete_ok( "//$auth_userid:$password@/api/v1/transfer_limits/$limit_id" )
155       ->status_is(404);
156
157     $schema->storage->txn_rollback;
158 };
159
160 subtest 'batch_add() and batch_delete() tests' => sub {
161
162     plan tests => 38;
163
164     $schema->storage->txn_begin;
165
166     t::lib::Mocks::mock_preference( 'BranchTransferLimitsType', 'itemtype' );
167
168     Koha::Item::Transfer::Limits->delete;
169
170     #my $library = $builder->build_object({ class => 'Koha::Libraries' });
171
172     my $library  = Koha::Libraries->search->next;
173     my $itemtype = Koha::ItemTypes->search->next;
174
175     my $authorized_patron = $builder->build_object(
176         {
177             class => 'Koha::Patrons',
178             value => { flags => 1 }
179         }
180     );
181     my $password = 'thePassword123';
182     $authorized_patron->set_password( { password => $password, skip_validation => 1 } );
183     my $auth_userid = $authorized_patron->userid;
184
185     my $unauthorized_patron = $builder->build_object(
186         {
187             class => 'Koha::Patrons',
188             value => { flags => 4 }
189         }
190     );
191     $unauthorized_patron->set_password( { password => $password, skip_validation => 1 } );
192     my $unauth_userid = $unauthorized_patron->userid;
193
194     my $limit_hashref = { item_type => $itemtype->id };
195
196     # Unauthorized attempt to write
197     $t->post_ok( "//$unauth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_hashref )->status_is(403);
198
199     # Authorized attempt to write invalid data
200     my $limit_with_invalid_field = { 'invalid' => 'invalid' };
201
202     $t->post_ok( "//$auth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_with_invalid_field )
203         ->status_is(400)->json_is(
204         "/errors" => [
205             {
206                 message => "Properties not allowed: invalid.",
207                 path    => "/body"
208             }
209         ]
210         );
211
212     # Create all combinations of to/from libraries
213     $t->post_ok( "//$auth_userid:$password@/api/v1/transfer_limits/batch" => json =>
214             { item_type => 'X', collection_code => 'Y' } )->status_is(400)
215         ->json_is( '/error' => "Only one of 'item_type' and 'collecion_code' can be passed at a time" );
216
217     t::lib::Mocks::mock_preference( 'BranchTransferLimitsType', 'ccode' );
218
219     # Create all combinations of to/from libraries
220     $t->post_ok( "//$auth_userid:$password@/api/v1/transfer_limits/batch" => json => { item_type => 'X' } )
221         ->status_is(409)->json_is( '/error' => "You passed 'item_type' but configuration expects 'collection_code'" );
222
223     t::lib::Mocks::mock_preference( 'BranchTransferLimitsType', 'itemtype' );
224
225     # Create all combinations of to/from libraries
226     $t->post_ok( "//$auth_userid:$password@/api/v1/transfer_limits/batch" => json => { collection_code => 'X' } )
227         ->status_is(409)->json_is( '/error' => "You passed 'collection_code' but configuration expects 'item_type'" );
228
229     # Create all combinations of to/from libraries
230     $t->post_ok( "//$auth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_hashref )
231         ->status_is( 201, 'SWAGGER3.2.1' )->json_has( '' => $limit_hashref, 'SWAGGER3.3.1' );
232
233     my $limits = Koha::Item::Transfer::Limits->search;
234
235     my $libraries_count = Koha::Libraries->search->count;
236     is( $limits->count, $libraries_count * ( $libraries_count - 1 ), "Created the correct number of limits" );
237
238     # Delete all combinations of to/from libraries
239     $t->delete_ok( "//$auth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_hashref )
240         ->status_is( 204, 'SWAGGER3.2.4' )->content_is( '', 'SWAGGER3.3.4' );
241
242     $limits = Koha::Item::Transfer::Limits->search;
243
244     is( $limits->count, 0, "Deleted the correct number of limits" );
245
246     # Create all combinations of 'to' libraries
247     $limit_hashref->{to_library_id} = $library->id;
248     $t->post_ok( "//$auth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_hashref )
249         ->status_is( 201, 'SWAGGER3.2.1' )->json_has( '' => $limit_hashref, 'SWAGGER3.3.1' );
250
251     $limits = Koha::Item::Transfer::Limits->search;
252
253     is( $limits->count, $libraries_count - 1, "Created the correct number of limits" );
254
255     # Delete all combinations of 'to' libraries
256     $t->delete_ok( "//$auth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_hashref )
257         ->status_is( 204, 'SWAGGER3.2.4' )->content_is( '', 'SWAGGER3.3.4' );
258
259     $limits = Koha::Item::Transfer::Limits->search;
260
261     is( $limits->count, 0, "Deleted the correct number of limits" );
262
263     # Create all combinations of 'from' libraries
264     Koha::Item::Transfer::Limits->search->delete;
265
266     delete $limit_hashref->{to_library_id};
267     $limit_hashref->{from_library_id} = $library->id;
268     $t->post_ok( "//$auth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_hashref )
269         ->status_is( 201, 'SWAGGER3.2.1' )->json_has( '' => $limit_hashref, 'SWAGGER3.3.1' );
270
271     $limits = Koha::Item::Transfer::Limits->search;
272
273     $libraries_count = Koha::Libraries->search->count;
274     is( $limits->count, $libraries_count - 1, "Created the correct number of limits" );
275
276     # Delete all combinations of 'from' libraries
277     $t->delete_ok( "//$auth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_hashref )
278         ->status_is( 204, 'SWAGGER3.2.4' )->content_is( '', 'SWAGGER3.3.4' );
279
280     $limits = Koha::Item::Transfer::Limits->search;
281
282     $libraries_count = Koha::Libraries->search->count;
283     is( $limits->count, 0, "Deleted the correct number of limits" );
284
285     $schema->storage->txn_rollback;
286 };