View | Details | Raw Unified | Return to bug 36329
Collapse All | Expand All

(-)a/Koha/REST/V1/TransferLimits.pm (-1 / +23 lines)
Lines 128-133 sub batch_add { Link Here
128
    return try {
128
    return try {
129
        my $params = $c->req->json;
129
        my $params = $c->req->json;
130
130
131
        if ( $params->{item_type} && $params->{collection_code} ) {
132
            return $c->render(
133
                status  => 400,
134
                openapi => {
135
                    error => "Only one of 'item_type' and 'collecion_code' can be passed at a time",
136
                }
137
            );
138
        }
139
140
        if (   ( C4::Context->preference("BranchTransferLimitsType") eq 'itemtype' && $params->{collection_code} )
141
            || ( C4::Context->preference("BranchTransferLimitsType") eq 'ccode' && $params->{item_type} ) )
142
        {
143
            return $c->render(
144
                status  => 409,
145
                openapi => {
146
                    error => $params->{collection_code}
147
                    ? "You passed 'collection_code' but configuration expects 'item_type'"
148
                    : "You passed 'item_type' but configuration expects 'collection_code'"
149
                }
150
            );
151
        }
152
131
        my ( @from_branches, @to_branches );
153
        my ( @from_branches, @to_branches );
132
        if ( $params->{from_library_id} ) {
154
        if ( $params->{from_library_id} ) {
133
            @from_branches = ( $params->{from_library_id} );
155
            @from_branches = ( $params->{from_library_id} );
Lines 195-201 sub batch_delete { Link Here
195
217
196
        Koha::Item::Transfer::Limits->search($search_params)->delete;
218
        Koha::Item::Transfer::Limits->search($search_params)->delete;
197
219
198
        return $c->render( status => 204, openapi => '');
220
        return $c->render( status => 204, openapi => q{} );
199
    }
221
    }
200
    catch {
222
    catch {
201
        $c->unhandled_exception($_);
223
        $c->unhandled_exception($_);
(-)a/t/db_dependent/api/v1/transfer_limits.t (-34 / +50 lines)
Lines 158-224 subtest 'delete() tests' => sub { Link Here
158
};
158
};
159
159
160
subtest 'batch_add() and batch_delete() tests' => sub {
160
subtest 'batch_add() and batch_delete() tests' => sub {
161
    plan tests => 26;
161
162
    plan tests => 38;
162
163
163
    $schema->storage->txn_begin;
164
    $schema->storage->txn_begin;
164
165
166
    t::lib::Mocks::mock_preference( 'BranchTransferLimitsType', 'itemtype' );
167
165
    Koha::Item::Transfer::Limits->delete;
168
    Koha::Item::Transfer::Limits->delete;
166
169
167
    #my $library = $builder->build_object({ class => 'Koha::Libraries' });
170
    #my $library = $builder->build_object({ class => 'Koha::Libraries' });
168
171
169
    my $library = Koha::Libraries->search->next;
172
    my $library  = Koha::Libraries->search->next;
170
    my $itemtype = Koha::ItemTypes->search->next;
173
    my $itemtype = Koha::ItemTypes->search->next;
171
174
172
    my $authorized_patron = $builder->build_object({
175
    my $authorized_patron = $builder->build_object(
173
        class => 'Koha::Patrons',
176
        {
174
        value => { flags => 1 }
177
            class => 'Koha::Patrons',
175
    });
178
            value => { flags => 1 }
179
        }
180
    );
176
    my $password = 'thePassword123';
181
    my $password = 'thePassword123';
177
    $authorized_patron->set_password({ password => $password, skip_validation => 1 });
182
    $authorized_patron->set_password( { password => $password, skip_validation => 1 } );
178
    my $auth_userid = $authorized_patron->userid;
183
    my $auth_userid = $authorized_patron->userid;
179
184
180
    my $unauthorized_patron = $builder->build_object({
185
    my $unauthorized_patron = $builder->build_object(
181
        class => 'Koha::Patrons',
186
        {
182
        value => { flags => 4 }
187
            class => 'Koha::Patrons',
183
    });
188
            value => { flags => 4 }
184
    $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
189
        }
190
    );
191
    $unauthorized_patron->set_password( { password => $password, skip_validation => 1 } );
185
    my $unauth_userid = $unauthorized_patron->userid;
192
    my $unauth_userid = $unauthorized_patron->userid;
186
193
187
    my $limit_hashref = {
194
    my $limit_hashref = { item_type => $itemtype->id };
188
        item_type => $itemtype->id
189
    };
190
195
191
    # Unauthorized attempt to write
196
    # Unauthorized attempt to write
192
    $t->post_ok( "//$unauth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_hashref )
197
    $t->post_ok( "//$unauth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_hashref )->status_is(403);
193
        ->status_is(403);
194
198
195
    # Authorized attempt to write invalid data
199
    # Authorized attempt to write invalid data
196
    my $limit_with_invalid_field = {'invalid' => 'invalid'};
200
    my $limit_with_invalid_field = { 'invalid' => 'invalid' };
197
201
198
    $t->post_ok( "//$auth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_with_invalid_field )
202
    $t->post_ok( "//$auth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_with_invalid_field )
199
        ->status_is(400)
203
        ->status_is(400)->json_is(
200
        ->json_is(
201
        "/errors" => [
204
        "/errors" => [
202
            {
205
            {
203
                message => "Properties not allowed: invalid.",
206
                message => "Properties not allowed: invalid.",
204
                path    => "/body"
207
                path    => "/body"
205
            }
208
            }
206
        ]
209
        ]
207
    );
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'" );
208
228
209
    # Create all combinations of to/from libraries
229
    # Create all combinations of to/from libraries
210
    $t->post_ok( "//$auth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_hashref )
230
    $t->post_ok( "//$auth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_hashref )
211
        ->status_is( 201, 'SWAGGER3.2.1' )
231
        ->status_is( 201, 'SWAGGER3.2.1' )->json_has( '' => $limit_hashref, 'SWAGGER3.3.1' );
212
        ->json_has( '' => $limit_hashref, 'SWAGGER3.3.1' );
213
232
214
    my $limits = Koha::Item::Transfer::Limits->search;
233
    my $limits = Koha::Item::Transfer::Limits->search;
215
234
216
    my $libraries_count = Koha::Libraries->search->count;
235
    my $libraries_count = Koha::Libraries->search->count;
217
    is( $limits->count, $libraries_count * ($libraries_count - 1 ), "Created the correct number of limits" );
236
    is( $limits->count, $libraries_count * ( $libraries_count - 1 ), "Created the correct number of limits" );
218
237
219
    # Delete all combinations of to/from libraries
238
    # Delete all combinations of to/from libraries
220
    $t->delete_ok( "//$auth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_hashref )
239
    $t->delete_ok( "//$auth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_hashref )
221
        ->status_is( 204, 'SWAGGER3.2.1' );
240
        ->status_is( 204, 'SWAGGER3.2.4' )->content_is( '', 'SWAGGER3.3.4' );
222
241
223
    $limits = Koha::Item::Transfer::Limits->search;
242
    $limits = Koha::Item::Transfer::Limits->search;
224
243
Lines 227-242 subtest 'batch_add() and batch_delete() tests' => sub { Link Here
227
    # Create all combinations of 'to' libraries
246
    # Create all combinations of 'to' libraries
228
    $limit_hashref->{to_library_id} = $library->id;
247
    $limit_hashref->{to_library_id} = $library->id;
229
    $t->post_ok( "//$auth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_hashref )
248
    $t->post_ok( "//$auth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_hashref )
230
        ->status_is( 201, 'SWAGGER3.2.1' )
249
        ->status_is( 201, 'SWAGGER3.2.1' )->json_has( '' => $limit_hashref, 'SWAGGER3.3.1' );
231
        ->json_has( '' => $limit_hashref, 'SWAGGER3.3.1' );
232
250
233
    $limits = Koha::Item::Transfer::Limits->search;
251
    $limits = Koha::Item::Transfer::Limits->search;
234
252
235
    is( $limits->count, $libraries_count - 1 , "Created the correct number of limits" );
253
    is( $limits->count, $libraries_count - 1, "Created the correct number of limits" );
236
254
237
    # Delete all combinations of 'to' libraries
255
    # Delete all combinations of 'to' libraries
238
    $t->delete_ok( "//$auth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_hashref )
256
    $t->delete_ok( "//$auth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_hashref )
239
        ->status_is( 204, 'SWAGGER3.2.1' );
257
        ->status_is( 204, 'SWAGGER3.2.4' )->content_is( '', 'SWAGGER3.3.4' );
240
258
241
    $limits = Koha::Item::Transfer::Limits->search;
259
    $limits = Koha::Item::Transfer::Limits->search;
242
260
Lines 248-264 subtest 'batch_add() and batch_delete() tests' => sub { Link Here
248
    delete $limit_hashref->{to_library_id};
266
    delete $limit_hashref->{to_library_id};
249
    $limit_hashref->{from_library_id} = $library->id;
267
    $limit_hashref->{from_library_id} = $library->id;
250
    $t->post_ok( "//$auth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_hashref )
268
    $t->post_ok( "//$auth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_hashref )
251
        ->status_is( 201, 'SWAGGER3.2.1' )
269
        ->status_is( 201, 'SWAGGER3.2.1' )->json_has( '' => $limit_hashref, 'SWAGGER3.3.1' );
252
        ->json_has( '' => $limit_hashref, 'SWAGGER3.3.1' );
253
270
254
    $limits = Koha::Item::Transfer::Limits->search;
271
    $limits = Koha::Item::Transfer::Limits->search;
255
272
256
    $libraries_count = Koha::Libraries->search->count;
273
    $libraries_count = Koha::Libraries->search->count;
257
    is( $limits->count, $libraries_count - 1 , "Created the correct number of limits" );
274
    is( $limits->count, $libraries_count - 1, "Created the correct number of limits" );
258
275
259
    # Delete all combinations of 'from' libraries
276
    # Delete all combinations of 'from' libraries
260
    $t->delete_ok( "//$auth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_hashref )
277
    $t->delete_ok( "//$auth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_hashref )
261
        ->status_is( 204, 'SWAGGER3.2.1' );
278
        ->status_is( 204, 'SWAGGER3.2.4' )->content_is( '', 'SWAGGER3.3.4' );
262
279
263
    $limits = Koha::Item::Transfer::Limits->search;
280
    $limits = Koha::Item::Transfer::Limits->search;
264
281
265
- 

Return to bug 36329