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

(-)a/t/db_dependent/api/v1/clubs_holds.t (-27 / +130 lines)
Lines 19-24 use Modern::Perl; Link Here
19
19
20
use Test::NoWarnings;
20
use Test::NoWarnings;
21
use Test::More tests => 3;
21
use Test::More tests => 3;
22
use Test::MockModule;
22
use Test::Mojo;
23
use Test::Mojo;
23
use Test::Warn;
24
use Test::Warn;
24
25
Lines 61-67 subtest 'add() tests' => sub { Link Here
61
62
62
    subtest 'librarian access tests' => sub {
63
    subtest 'librarian access tests' => sub {
63
64
64
        plan tests => 29;
65
        plan tests => 14;
65
66
66
        $schema->storage->txn_begin;
67
        $schema->storage->txn_begin;
67
68
Lines 150-182 subtest 'add() tests' => sub { Link Here
150
            }
151
            }
151
        )->status_is(404)->json_is( '/error' => 'Item not found' );
152
        )->status_is(404)->json_is( '/error' => 'Item not found' );
152
153
153
        my $data = {
154
        my $can_be_reserved_status;
154
            biblio_id         => $item->biblionumber,
155
155
            pickup_library_id => $item->home_branch->branchcode
156
        my $c4_reserves = Test::MockModule->new('C4::Reserves');
157
        $c4_reserves->mock( 'CanBookBeReserved', sub { return { status => $can_be_reserved_status }; } );
158
        $c4_reserves->mock( 'CanItemBeReserved', sub { return { status => $can_be_reserved_status }; } );
159
160
        subtest 'biblio-level holds tests' => sub {
161
162
            plan tests => 23;
163
164
            $can_be_reserved_status = 'OK';
165
166
            my $data = {
167
                biblio_id         => $item->biblionumber,
168
                pickup_library_id => $item->home_branch->branchcode
169
            };
170
171
            $t->post_ok(
172
                "//$userid:$password@/api/v1/clubs/" . $club_without_enrollments->id . "/holds" => json => $data )
173
                ->status_is(409)->json_is( '/error' => "Cannot place a hold on a club without patrons." );
174
175
            # place a club hold with top level reserveforothers permission - should succeed
176
            $t->post_ok( "//$userid:$password@/api/v1/clubs/" . $club_with_enrollments->id . "/holds" => json => $data )
177
                ->status_is( 201, 'Created Hold' )->json_has( '/club_hold_id', 'got a club hold id' )
178
                ->json_is( '/club_id' => $club_with_enrollments->id )->json_is( '/biblio_id' => $item->biblionumber )
179
                ->header_is(
180
                      'Location' => '/api/v1/clubs/'
181
                    . $club_with_enrollments->id
182
                    . '/holds/'
183
                    . $t->tx->res->json->{club_hold_id},
184
                'REST3.4.1'
185
                );
186
187
            # place a club hold with the place_hold specific permsision - should succeed
188
            $t->post_ok(
189
                "//$userid_2:$password_2@/api/v1/clubs/" . $club_with_enrollments->id . "/holds" => json => $data )
190
                ->status_is( 201, 'Created Hold with specific place_holds permission' )
191
                ->json_has( '/club_hold_id', 'got a club hold id' )
192
                ->json_is( '/club_id' => $club_with_enrollments->id )->json_is( '/biblio_id' => $item->biblionumber );
193
194
            # place a club hold with no reserveforothers or specific permsision - should fail
195
            $t->post_ok(
196
                "//$userid_3:$password_3@/api/v1/clubs/" . $club_with_enrollments->id . "/holds" => json => $data )
197
                ->status_is( 403, 'User with no relevant permissions cannot place club holds' )
198
                ->json_is( '/error' => 'Authorization failure. Missing required permission(s).' );
199
200
            $can_be_reserved_status = 'tooManyHoldsForThisRecord';
201
202
            # place a club hold with the place_hold specific permsision - should succeed
203
            warnings_like {
204
                $t->post_ok(
205
                    "//$userid_2:$password_2@/api/v1/clubs/" . $club_with_enrollments->id . "/holds" => json => $data )
206
                    ->status_is( 201, 'Created Hold with specific place_holds permission' )
207
                    ->json_has( '/club_hold_id', 'got a club hold id' )
208
                    ->json_is( '/club_id'   => $club_with_enrollments->id )
209
                    ->json_is( '/biblio_id' => $item->biblionumber );
210
            }
211
            [
212
                qr/Hold cannot be placed. Reason: tooManyHoldsForThisRecord/,
213
                qr/Hold cannot be placed. Reason: tooManyHoldsForThisRecord/,
214
                qr/Hold cannot be placed. Reason: tooManyHoldsForThisRecord/,
215
                qr/Hold cannot be placed. Reason: tooManyHoldsForThisRecord/,
216
                qr/Hold cannot be placed. Reason: tooManyHoldsForThisRecord/,
217
                qr/Hold cannot be placed. Reason: tooManyHoldsForThisRecord/,
218
            ],
219
                "Print warning when CanBookBeReserved doesn't return 'OK'";
156
        };
220
        };
157
221
158
        $t->post_ok( "//$userid:$password@/api/v1/clubs/" . $club_without_enrollments->id . "/holds" => json => $data )
222
        subtest 'item-level holds tests' => sub {
159
            ->status_is(409)->json_is( '/error' => "Cannot place a hold on a club without patrons." );
223
160
224
            plan tests => 23;
161
        # place a club hold with top level reserveforothers permission - should succeed
225
162
        $t->post_ok( "//$userid:$password@/api/v1/clubs/" . $club_with_enrollments->id . "/holds" => json => $data )
226
            $can_be_reserved_status = 'OK';
163
            ->status_is( 201, 'Created Hold' )->json_has( '/club_hold_id', 'got a club hold id' )
227
164
            ->json_is( '/club_id' => $club_with_enrollments->id )->json_is( '/biblio_id' => $item->biblionumber )
228
            my $data = {
165
            ->header_is(
229
                biblio_id         => $item->biblionumber,
166
            'Location' => '/api/v1/clubs/' . $club_with_enrollments->id . '/holds/' . $t->tx->res->json->{club_hold_id},
230
                item_id           => $item->id,
167
            'REST3.4.1'
231
                pickup_library_id => $item->home_branch->branchcode
168
            );
232
            };
169
233
170
        # place a club hold with the place_hold specific permsision - should succeed
234
            $t->post_ok(
171
        $t->post_ok( "//$userid_2:$password_2@/api/v1/clubs/" . $club_with_enrollments->id . "/holds" => json => $data )
235
                "//$userid:$password@/api/v1/clubs/" . $club_without_enrollments->id . "/holds" => json => $data )
172
            ->status_is( 201, 'Created Hold with specific place_holds permission' )
236
                ->status_is(409)->json_is( '/error' => "Cannot place a hold on a club without patrons." );
173
            ->json_has( '/club_hold_id', 'got a club hold id' )->json_is( '/club_id' => $club_with_enrollments->id )
237
174
            ->json_is( '/biblio_id' => $item->biblionumber );
238
            # place a club hold with top level reserveforothers permission - should succeed
175
239
            $t->post_ok( "//$userid:$password@/api/v1/clubs/" . $club_with_enrollments->id . "/holds" => json => $data )
176
        # place a club hold with no reserveforothers or specific permsision - should fail
240
                ->status_is( 201, 'Created Hold' )->json_has( '/club_hold_id', 'got a club hold id' )
177
        $t->post_ok( "//$userid_3:$password_3@/api/v1/clubs/" . $club_with_enrollments->id . "/holds" => json => $data )
241
                ->json_is( '/club_id' => $club_with_enrollments->id )->json_is( '/biblio_id' => $item->biblionumber )
178
            ->status_is( 403, 'User with no relevant permissions cannot place club holds' )
242
                ->header_is(
179
            ->json_is( '/error' => 'Authorization failure. Missing required permission(s).' );
243
                      'Location' => '/api/v1/clubs/'
244
                    . $club_with_enrollments->id
245
                    . '/holds/'
246
                    . $t->tx->res->json->{club_hold_id},
247
                'REST3.4.1'
248
                );
249
250
            # place a club hold with the place_hold specific permsision - should succeed
251
            $t->post_ok(
252
                "//$userid_2:$password_2@/api/v1/clubs/" . $club_with_enrollments->id . "/holds" => json => $data )
253
                ->status_is( 201, 'Created Hold with specific place_holds permission' )
254
                ->json_has( '/club_hold_id', 'got a club hold id' )
255
                ->json_is( '/club_id' => $club_with_enrollments->id )->json_is( '/biblio_id' => $item->biblionumber );
256
257
            # place a club hold with no reserveforothers or specific permsision - should fail
258
            $t->post_ok(
259
                "//$userid_3:$password_3@/api/v1/clubs/" . $club_with_enrollments->id . "/holds" => json => $data )
260
                ->status_is( 403, 'User with no relevant permissions cannot place club holds' )
261
                ->json_is( '/error' => 'Authorization failure. Missing required permission(s).' );
262
263
            $can_be_reserved_status = 'tooManyHoldsForThisRecord';
264
265
            # place a club hold with the place_hold specific permsision - should succeed
266
            warnings_like {
267
                $t->post_ok(
268
                    "//$userid_2:$password_2@/api/v1/clubs/" . $club_with_enrollments->id . "/holds" => json => $data )
269
                    ->status_is( 201, 'Created Hold with specific place_holds permission' )
270
                    ->json_has( '/club_hold_id', 'got a club hold id' )
271
                    ->json_is( '/club_id'   => $club_with_enrollments->id )
272
                    ->json_is( '/biblio_id' => $item->biblionumber );
273
            }
274
            [
275
                qr/Hold cannot be placed. Reason: tooManyHoldsForThisRecord/,
276
                qr/Hold cannot be placed. Reason: tooManyHoldsForThisRecord/,
277
                qr/Hold cannot be placed. Reason: tooManyHoldsForThisRecord/,
278
                qr/Hold cannot be placed. Reason: tooManyHoldsForThisRecord/,
279
                qr/Hold cannot be placed. Reason: tooManyHoldsForThisRecord/,
280
                qr/Hold cannot be placed. Reason: tooManyHoldsForThisRecord/,
281
            ],
282
                "Print warning when CanItemBeReserved doesn't return 'OK'";
283
        };
180
284
181
        $schema->storage->txn_rollback;
285
        $schema->storage->txn_rollback;
182
    };
286
    };
183
- 

Return to bug 39869