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 => 4;
24 use t::lib::TestBuilder;
27 use List::Util qw(min);
29 use Koha::Item::Transfer::Limits;
32 my $schema = Koha::Database->new->schema;
33 my $builder = t::lib::TestBuilder->new;
35 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
37 my $t = Test::Mojo->new('Koha::REST::V1');
39 subtest 'list() tests' => sub {
42 $schema->storage->txn_begin;
44 Koha::Item::Transfer::Limits->delete;
46 my $patron = $builder->build_object({
47 class => 'Koha::Patrons',
48 value => { flags => 1 }
50 my $password = 'thePassword123';
51 $patron->set_password({ password => $password, skip_validation => 1 });
52 my $userid = $patron->userid;
54 my $limit = $builder->build_object({ class => 'Koha::Item::Transfer::Limits' });
56 $t->get_ok( "//$userid:$password@/api/v1/transfer_limits" )
57 ->status_is( 200, 'SWAGGER3.2.2' )
58 ->json_is( [$limit->to_api] );
60 $schema->storage->txn_rollback;
63 subtest 'add() tests' => sub {
67 $schema->storage->txn_begin;
69 my $authorized_patron = $builder->build_object({
70 class => 'Koha::Patrons',
71 value => { flags => 1 }
73 my $password = 'thePassword123';
74 $authorized_patron->set_password({ password => $password, skip_validation => 1 });
75 my $auth_userid = $authorized_patron->userid;
77 my $unauthorized_patron = $builder->build_object({
78 class => 'Koha::Patrons',
79 value => { flags => 4 }
81 $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
82 my $unauth_userid = $unauthorized_patron->userid;
84 my $limit = $builder->build_object({ class => 'Koha::Item::Transfer::Limits' });
85 my $limit_hashref = $limit->to_api;
86 delete $limit_hashref->{limit_id};
89 # Unauthorized attempt to write
90 $t->post_ok( "//$unauth_userid:$password@/api/v1/transfer_limits" => json => $limit_hashref )
93 # Authorized attempt to write invalid data
94 my $limit_with_invalid_field = {'invalid' => 'invalid'};
96 $t->post_ok( "//$auth_userid:$password@/api/v1/transfer_limits" => json => $limit_with_invalid_field )
101 message => "Properties not allowed: invalid.",
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' );
112 $t->post_ok( "//$auth_userid:$password@/api/v1/transfer_limits" => json => $limit_hashref )
113 ->status_is( 409, 'Conflict creating the resource' )
116 error => "[A transfer limit with the given parameters already exists!]"
120 $schema->storage->txn_rollback;
123 subtest 'delete() tests' => sub {
126 $schema->storage->txn_begin;
128 my $authorized_patron = $builder->build_object({
129 class => 'Koha::Patrons',
130 value => { flags => 1 }
132 my $password = 'thePassword123';
133 $authorized_patron->set_password({ password => $password, skip_validation => 1 });
134 my $auth_userid = $authorized_patron->userid;
136 my $unauthorized_patron = $builder->build_object({
137 class => 'Koha::Patrons',
138 value => { flags => 4 }
140 $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
141 my $unauth_userid = $unauthorized_patron->userid;
143 my $limit = $builder->build_object({ class => 'Koha::Item::Transfer::Limits' });
144 my $limit_id = $limit->id;
146 # Unauthorized attempt to delete
147 $t->delete_ok( "//$unauth_userid:$password@/api/v1/transfer_limits/$limit_id" )
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');
154 $t->delete_ok( "//$auth_userid:$password@/api/v1/transfer_limits/$limit_id" )
157 $schema->storage->txn_rollback;
160 subtest 'batch_add() and batch_delete() tests' => sub {
163 $schema->storage->txn_begin;
165 Koha::Item::Transfer::Limits->delete;
167 #my $library = $builder->build_object({ class => 'Koha::Libraries' });
169 my $library = Koha::Libraries->search->next;
170 my $itemtype = Koha::ItemTypes->search->next;
172 my $authorized_patron = $builder->build_object({
173 class => 'Koha::Patrons',
174 value => { flags => 1 }
176 my $password = 'thePassword123';
177 $authorized_patron->set_password({ password => $password, skip_validation => 1 });
178 my $auth_userid = $authorized_patron->userid;
180 my $unauthorized_patron = $builder->build_object({
181 class => 'Koha::Patrons',
182 value => { flags => 4 }
184 $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
185 my $unauth_userid = $unauthorized_patron->userid;
187 my $limit_hashref = {
188 item_type => $itemtype->id
191 # Unauthorized attempt to write
192 $t->post_ok( "//$unauth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_hashref )
195 # Authorized attempt to write invalid data
196 my $limit_with_invalid_field = {'invalid' => 'invalid'};
198 $t->post_ok( "//$auth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_with_invalid_field )
203 message => "Properties not allowed: invalid.",
209 # Create all combinations of to/from libraries
210 $t->post_ok( "//$auth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_hashref )
211 ->status_is( 201, 'SWAGGER3.2.1' )
212 ->json_has( '' => $limit_hashref, 'SWAGGER3.3.1' );
214 my $limits = Koha::Item::Transfer::Limits->search;
216 my $libraries_count = Koha::Libraries->search->count;
217 is( $limits->count, $libraries_count * ($libraries_count - 1 ), "Created the correct number of limits" );
219 # Delete all combinations of to/from libraries
220 $t->delete_ok( "//$auth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_hashref )
221 ->status_is( 204, 'SWAGGER3.2.1' );
223 $limits = Koha::Item::Transfer::Limits->search;
225 is( $limits->count, 0, "Deleted the correct number of limits" );
227 # Create all combinations of 'to' libraries
228 $limit_hashref->{to_library_id} = $library->id;
229 $t->post_ok( "//$auth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_hashref )
230 ->status_is( 201, 'SWAGGER3.2.1' )
231 ->json_has( '' => $limit_hashref, 'SWAGGER3.3.1' );
233 $limits = Koha::Item::Transfer::Limits->search;
235 is( $limits->count, $libraries_count - 1 , "Created the correct number of limits" );
237 # Delete all combinations of 'to' libraries
238 $t->delete_ok( "//$auth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_hashref )
239 ->status_is( 204, 'SWAGGER3.2.1' );
241 $limits = Koha::Item::Transfer::Limits->search;
243 is( $limits->count, 0, "Deleted the correct number of limits" );
245 # Create all combinations of 'from' libraries
246 Koha::Item::Transfer::Limits->search->delete;
248 delete $limit_hashref->{to_library_id};
249 $limit_hashref->{from_library_id} = $library->id;
250 $t->post_ok( "//$auth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_hashref )
251 ->status_is( 201, 'SWAGGER3.2.1' )
252 ->json_has( '' => $limit_hashref, 'SWAGGER3.3.1' );
254 $limits = Koha::Item::Transfer::Limits->search;
256 $libraries_count = Koha::Libraries->search->count;
257 is( $limits->count, $libraries_count - 1 , "Created the correct number of limits" );
259 # Delete all combinations of 'from' libraries
260 $t->delete_ok( "//$auth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_hashref )
261 ->status_is( 204, 'SWAGGER3.2.1' );
263 $limits = Koha::Item::Transfer::Limits->search;
265 $libraries_count = Koha::Libraries->search->count;
266 is( $limits->count, 0, "Deleted the correct number of limits" );
268 $schema->storage->txn_rollback;