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

(-)a/Koha/REST/V1/Items.pm (+26 lines)
Lines 58-63 sub list { Link Here
58
    };
58
    };
59
}
59
}
60
60
61
=head3 list_public
62
63
Controller function that handles listing Koha::Item objects available to the opac
64
65
=cut
66
67
sub list_public {
68
    my $c = shift->openapi->valid_input or return;
69
70
    return try {
71
        my $patron = $c->stash('koha.user');
72
73
        my $items_set =
74
          Koha::Items->filter_by_visible_in_opac( { patron => $patron } );
75
        my $items = $c->objects->search($items_set);
76
77
        return $c->render(
78
            status  => 200,
79
            openapi => $items
80
        );
81
    }
82
    catch {
83
        $c->unhandled_exception($_);
84
    };
85
}
86
61
=head3 get
87
=head3 get
62
88
63
Controller function that handles retrieving a single Koha::Item
89
Controller function that handles retrieving a single Koha::Item
(-)a/api/v1/swagger/paths/biblios.yaml (+10 lines)
Lines 525-530 Link Here
525
      - $ref: "../swagger.yaml#/parameters/q_param"
525
      - $ref: "../swagger.yaml#/parameters/q_param"
526
      - $ref: "../swagger.yaml#/parameters/q_body"
526
      - $ref: "../swagger.yaml#/parameters/q_body"
527
      - $ref: "../swagger.yaml#/parameters/q_header"
527
      - $ref: "../swagger.yaml#/parameters/q_header"
528
      - name: x-koha-embed
529
        in: header
530
        required: false
531
        description: Embed list sent as a request header
532
        type: array
533
        items:
534
          type: string
535
          enum:
536
            - +strings
537
        collectionFormat: csv
528
    consumes:
538
    consumes:
529
      - application/json
539
      - application/json
530
    produces:
540
    produces:
(-)a/api/v1/swagger/paths/items.yaml (+61 lines)
Lines 396-398 Link Here
396
    x-koha-authorization:
396
    x-koha-authorization:
397
      permissions:
397
      permissions:
398
        reserveforothers: place_holds
398
        reserveforothers: place_holds
399
"/public/items":
400
  get:
401
    x-mojo-to: Items#list_public
402
    operationId: listItemsPublic
403
    tags:
404
      - items
405
    summary: List items publically visible
406
    parameters:
407
      - name: external_id
408
        in: query
409
        description: Search on the item's barcode
410
        required: false
411
        type: string
412
      - $ref: "../swagger.yaml#/parameters/match"
413
      - $ref: "../swagger.yaml#/parameters/order_by"
414
      - $ref: "../swagger.yaml#/parameters/page"
415
      - $ref: "../swagger.yaml#/parameters/per_page"
416
      - $ref: "../swagger.yaml#/parameters/q_param"
417
      - $ref: "../swagger.yaml#/parameters/q_body"
418
      - $ref: "../swagger.yaml#/parameters/q_header"
419
      - $ref: "../swagger.yaml#/parameters/request_id_header"
420
      - name: x-koha-embed
421
        in: header
422
        required: false
423
        description: Embed list sent as a request header
424
        type: array
425
        items:
426
          type: string
427
          enum:
428
            - +strings
429
        collectionFormat: csv
430
    consumes:
431
      - application/json
432
    produces:
433
      - application/json
434
    responses:
435
      "200":
436
        description: A list of item
437
        schema:
438
          type: array
439
          items:
440
            $ref: "../swagger.yaml#/definitions/item"
441
      "401":
442
        description: Authentication required
443
        schema:
444
          $ref: "../swagger.yaml#/definitions/error"
445
      "403":
446
        description: Access forbidden
447
        schema:
448
          $ref: "../swagger.yaml#/definitions/error"
449
      "500":
450
        description: |
451
          Internal server error. Possible `error_code` attribute values:
452
453
          * `internal_server_error`
454
        schema:
455
          $ref: "../swagger.yaml#/definitions/error"
456
      "503":
457
        description: Under maintenance
458
        schema:
459
          $ref: "../swagger.yaml#/definitions/error"
(-)a/api/v1/swagger/swagger.yaml (+2 lines)
Lines 291-296 paths: Link Here
291
    $ref: "./paths/patrons_password.yaml#/~1patrons~1{patron_id}~1password~1expiration_date"
291
    $ref: "./paths/patrons_password.yaml#/~1patrons~1{patron_id}~1password~1expiration_date"
292
  "/public/biblios/{biblio_id}":
292
  "/public/biblios/{biblio_id}":
293
    $ref: "./paths/biblios.yaml#/~1public~1biblios~1{biblio_id}"
293
    $ref: "./paths/biblios.yaml#/~1public~1biblios~1{biblio_id}"
294
  "/public/items":
295
    $ref: "./paths/items.yaml#/~1public~1items"
294
  "/public/biblios/{biblio_id}/items":
296
  "/public/biblios/{biblio_id}/items":
295
    $ref: "./paths/biblios.yaml#/~1public~1biblios~1{biblio_id}~1items"
297
    $ref: "./paths/biblios.yaml#/~1public~1biblios~1{biblio_id}~1items"
296
  "/public/biblios/{biblio_id}/ratings":
298
  "/public/biblios/{biblio_id}/ratings":
(-)a/t/db_dependent/api/v1/items.t (-14 / +22 lines)
Lines 27-32 use Test::Warn; Link Here
27
use t::lib::TestBuilder;
27
use t::lib::TestBuilder;
28
use t::lib::Mocks;
28
use t::lib::Mocks;
29
29
30
use Mojo::JSON qw(encode_json);
31
30
use C4::Auth;
32
use C4::Auth;
31
use Koha::Items;
33
use Koha::Items;
32
use Koha::Database;
34
use Koha::Database;
Lines 105-119 subtest 'list_public() tests' => sub { Link Here
105
107
106
    $schema->storage->txn_begin;
108
    $schema->storage->txn_begin;
107
109
108
    # Clean out all demo items
110
    my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
109
    Koha::Items->delete();
110
111
111
    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
112
    my $mocked_category = Test::MockModule->new('Koha::Patron::Category');
113
    my $exception = 1;
112
    my $exception = 1;
114
    $mocked_category->mock( 'override_hidden_items', sub {
113
115
        return $exception;
114
    my $mocked_category = Test::MockModule->new('Koha::Patron::Category');
116
    });
115
    $mocked_category->mock(
116
        'override_hidden_items',
117
        sub {
118
            return $exception;
119
        }
120
    );
117
121
118
    my $password = 'thePassword123';
122
    my $password = 'thePassword123';
119
    $patron->set_password( { password => $password, skip_validation => 1 } );
123
    $patron->set_password( { password => $password, skip_validation => 1 } );
Lines 186-213 subtest 'list_public() tests' => sub { Link Here
186
        return $rules;
190
        return $rules;
187
    });
191
    });
188
192
193
    my $query = encode_json({ item_id => [ $item_1->id, $item_2->id, $item_3->id, $item_4->id, $item_5->id, $item_6->id ] });
194
195
    t::lib::Mocks::mock_preference( 'RESTPublicAPI', 1 );
189
    subtest 'anonymous access' => sub {
196
    subtest 'anonymous access' => sub {
190
        plan tests => 21;
197
        plan tests => 21;
198
        
199
        t::lib::Mocks::mock_preference( 'RESTPublicAnonymousRequests', 1 );
191
200
192
        t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
201
        t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
193
        my $res = $t->get_ok( "/api/v1/public/items" )->status_is(200)->tx->res->json;
202
        my $res = $t->get_ok( "/api/v1/public/items?q=" . $query )->status_is(200)->tx->res->json;
194
        is( scalar @{ $res }, 6, 'No rules set, hidelostitems unset, all items returned');
203
        is( scalar @{ $res }, 6, 'No rules set, hidelostitems unset, all items returned');
195
204
196
        t::lib::Mocks::mock_preference( 'hidelostitems', 1 );
205
        t::lib::Mocks::mock_preference( 'hidelostitems', 1 );
197
        $res = $t->get_ok( "/api/v1/public/items" )->status_is(200)->tx->res->json;
206
        $res = $t->get_ok( "/api/v1/public/items?q=" . $query )->status_is(200)->tx->res->json;
198
        is( scalar @{ $res }, 3, 'No rules set, hidelostitems set, 3 items hidden');
207
        is( scalar @{ $res }, 3, 'No rules set, hidelostitems set, 3 items hidden');
199
208
200
        t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
209
        t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
201
        $rules = { biblionumber => [ $biblio->biblionumber ] };
210
        $rules = { biblionumber => [ $biblio->biblionumber ] };
202
        $res = $t->get_ok( "/api/v1/public/items" )->status_is(200)->tx->res->json;
211
        $res = $t->get_ok( "/api/v1/public/items?q=" . $query )->status_is(200)->tx->res->json;
203
        is( scalar @{ $res }, 0, 'Biblionumber rule set, hidelostitems unset, all items hidden');
212
        is( scalar @{ $res }, 0, 'Biblionumber rule set, hidelostitems unset, all items hidden');
204
213
205
        $rules = { withdrawn => [ 1, 2 ] };
214
        $rules = { withdrawn => [ 1, 2 ] };
206
        $res = $t->get_ok( "/api/v1/public/items" )->status_is(200)->tx->res->json;
215
        $res = $t->get_ok( "/api/v1/public/items?q=" . $query )->status_is(200)->tx->res->json;
207
        is( scalar @{ $res }, 4, 'Withdrawn rule set, hidelostitems unset, 2 items hidden');
216
        is( scalar @{ $res }, 4, 'Withdrawn rule set, hidelostitems unset, 2 items hidden');
208
217
209
        $rules = { itype => [ $itype_1->itemtype ] };
218
        $rules = { itype => [ $itype_1->itemtype ] };
210
        $res = $t->get_ok( "/api/v1/public/items" )->status_is(200)->tx->res->json;
219
        $res = $t->get_ok( "/api/v1/public/items?q=" . $query )->status_is(200)->tx->res->json;
211
        is( scalar @{ $res }, 2, 'Itype rule set, hidelostitems unset, 4 items hidden');
220
        is( scalar @{ $res }, 2, 'Itype rule set, hidelostitems unset, 4 items hidden');
212
221
213
        $rules = { withdrawn => [ 1 ] };
222
        $rules = { withdrawn => [ 1 ] };
Lines 228-234 subtest 'list_public() tests' => sub { Link Here
228
237
229
        t::lib::Mocks::mock_preference( 'hidelostitems', 1 );
238
        t::lib::Mocks::mock_preference( 'hidelostitems', 1 );
230
        $rules = { withdrawn => [ 1, 2 ] };
239
        $rules = { withdrawn => [ 1, 2 ] };
231
        my $res = $t->get_ok("//$userid:$password@/api/v1/public/items")
240
        my $res = $t->get_ok("//$userid:$password@/api/v1/public/items?q=" . $query)
232
          ->status_is(200)->tx->res->json;
241
          ->status_is(200)->tx->res->json;
233
        is(
242
        is(
234
            scalar @{$res},
243
            scalar @{$res},
235
- 

Return to bug 33146