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

(-)a/Koha/REST/V1/Authorities.pm (+66 lines)
Lines 253-256 sub update { Link Here
253
    };
253
    };
254
}
254
}
255
255
256
=head3 list
257
258
Controller function that handles retrieving a list of authorities
259
260
=cut
261
262
sub list {
263
    my $c = shift->openapi->valid_input or return;
264
265
    my $authorities = $c->objects->search_rs( Koha::Authorities->new );
266
267
    return try {
268
269
        if ( $c->req->headers->accept =~ m/application\/json(;.*)?$/ ) {
270
            return $c->render(
271
                status => 200,
272
                json   => $authorities->to_api
273
            );
274
        }
275
        elsif (
276
            $c->req->headers->accept =~ m/application\/marcxml\+xml(;.*)?$/ )
277
        {
278
            $c->res->headers->add( 'Content-Type', 'application/marcxml+xml' );
279
            return $c->render(
280
                status => 200,
281
                text   => $authorities->print_collection('marcxml')
282
            );
283
        }
284
        elsif (
285
            $c->req->headers->accept =~ m/application\/marc-in-json(;.*)?$/ )
286
        {
287
            $c->res->headers->add( 'Content-Type', 'application/marc-in-json' );
288
            return $c->render(
289
                status => 200,
290
                data   => $authorities->print_collection('mij')
291
            );
292
        }
293
        elsif ( $c->req->headers->accept =~ m/application\/marc(;.*)?$/ ) {
294
            $c->res->headers->add( 'Content-Type', 'application/marc' );
295
            return $c->render(
296
                status => 200,
297
                text   => $authorities->print_collection('marc')
298
            );
299
        }
300
        elsif ( $c->req->headers->accept =~ m/text\/plain(;.*)?$/ ) {
301
            return $c->render(
302
                status => 200,
303
                text   => $authorities->print_collection('txt')
304
            );
305
        }
306
        else {
307
            return $c->render(
308
                status  => 406,
309
                openapi => [
310
                    "application/json",         "application/marcxml+xml",
311
                    "application/marc-in-json", "application/marc",
312
                    "text/plain"
313
                ]
314
            );
315
        }
316
    }
317
    catch {
318
        $c->unhandled_exception($_);
319
    };
320
}
321
256
1;
322
1;
(-)a/api/v1/swagger/paths/authorities.yaml (+57 lines)
Lines 1-5 Link Here
1
---
1
---
2
"/authorities":
2
"/authorities":
3
  get:
4
    x-mojo-to: Authorities#list
5
    operationId: listAuthorities
6
    tags:
7
      - authorities
8
    summary: List authorities
9
    parameters:
10
      - $ref: "../swagger.yaml#/parameters/page"
11
      - $ref: "../swagger.yaml#/parameters/per_page"
12
      - $ref: "../swagger.yaml#/parameters/match"
13
      - $ref: "../swagger.yaml#/parameters/order_by"
14
      - $ref: "../swagger.yaml#/parameters/q_param"
15
      - $ref: "../swagger.yaml#/parameters/q_body"
16
      - $ref: "../swagger.yaml#/parameters/q_header"
17
      - $ref: "../swagger.yaml#/parameters/request_id_header"
18
    produces:
19
      - application/json
20
      - application/marcxml+xml
21
      - application/marc-in-json
22
      - application/marc
23
      - text/plain
24
    responses:
25
      "200":
26
        description: A list of authorities
27
      "401":
28
        description: Authentication required
29
        schema:
30
          $ref: "../swagger.yaml#/definitions/error"
31
      "403":
32
        description: Access forbidden
33
        schema:
34
          $ref: "../swagger.yaml#/definitions/error"
35
      "404":
36
        description: Authority not found
37
        schema:
38
          $ref: "../swagger.yaml#/definitions/error"
39
      "406":
40
        description: Not acceptable
41
        schema:
42
          type: array
43
          description: Accepted content-types
44
          items:
45
            type: string
46
      "500":
47
        description: |
48
          Internal server error. Possible `error_code` attribute values:
49
50
          * `internal_server_error`
51
        schema:
52
          $ref: "../swagger.yaml#/definitions/error"
53
      "503":
54
        description: Under maintenance
55
        schema:
56
          $ref: "../swagger.yaml#/definitions/error"
57
    x-koha-authorization:
58
      permissions:
59
        catalogue: "1"
3
  post:
60
  post:
4
    x-mojo-to: Authorities#add
61
    x-mojo-to: Authorities#add
5
    operationId: addAuthority
62
    operationId: addAuthority
(-)a/t/db_dependent/api/v1/authorities.t (-2 / +76 lines)
Lines 20-26 use Modern::Perl; Link Here
20
use utf8;
20
use utf8;
21
use Encode;
21
use Encode;
22
22
23
use Test::More tests => 4;
23
use Test::More tests => 5;
24
use Test::MockModule;
24
use Test::MockModule;
25
use Test::Mojo;
25
use Test::Mojo;
26
use Test::Warn;
26
use Test::Warn;
Lines 314-316 subtest 'put() tests' => sub { Link Here
314
314
315
    $schema->storage->txn_rollback;
315
    $schema->storage->txn_rollback;
316
};
316
};
317
- 
317
318
319
320
subtest 'list() tests' => sub {
321
    plan tests => 14;
322
323
    $schema->storage->txn_begin;
324
325
    my $patron = $builder->build_object(
326
        {
327
            class => 'Koha::Patrons',
328
            value => { flags => 0 }
329
        }
330
    );
331
    my $password = 'thePassword123';
332
    $patron->set_password( { password => $password, skip_validation => 1 } );
333
    $patron->discard_changes;
334
    my $userid = $patron->userid;
335
336
    my $authid1 = $builder->build_object({ 'class' => 'Koha::Authorities', value => {
337
      marcxml => q|<?xml version="1.0" encoding="UTF-8"?>
338
<record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.loc.gov/MARC21/slim" xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd">
339
    <controlfield tag="001">1001</controlfield>
340
    <datafield tag="110" ind1=" " ind2=" ">
341
        <subfield code="9">102</subfield>
342
        <subfield code="a">My Corporation</subfield>
343
    </datafield>
344
</record>|
345
    } })->authid;
346
347
    my $authid2 = $builder->build_object({ 'class' => 'Koha::Authorities', value => {
348
      marcxml => q|<?xml version="1.0" encoding="UTF-8"?>
349
<record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.loc.gov/MARC21/slim" xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd">
350
    <controlfield tag="001">1001</controlfield>
351
    <datafield tag="110" ind1=" " ind2=" ">
352
        <subfield code="9">102</subfield>
353
        <subfield code="a">My Corporation</subfield>
354
    </datafield>
355
</record>|
356
    } })->authid;
357
358
    my $search =
359
"[{\"authid\": \"$authid1\"}, {\"authid\": \"$authid2\"}]";
360
    $t->get_ok(
361
        "//$userid:$password@/api/v1/authorities/" => { 'x-koha-query' => $search }
362
    )->status_is(403);
363
364
    $patron->flags(4)->store;
365
366
    $t->get_ok( "//$userid:$password@/api/v1/authorities/" =>
367
          { Accept => 'application/weird+format', 'x-koha-query' => $search } )
368
      ->status_is(400);
369
370
    $t->get_ok( "//$userid:$password@/api/v1/authorities/" =>
371
          { Accept => 'application/json', 'x-koha-query' => $search } )
372
      ->status_is(200);
373
374
    $t->get_ok( "//$userid:$password@/api/v1/authorities/" =>
375
          { Accept => 'application/marcxml+xml', 'x-koha-query' => $search } )
376
      ->status_is(200);
377
378
    $t->get_ok( "//$userid:$password@/api/v1/authorities/" =>
379
          { Accept => 'application/marc-in-json', 'x-koha-query' => $search } )
380
      ->status_is(200);
381
382
    $t->get_ok( "//$userid:$password@/api/v1/authorities/" =>
383
          { Accept => 'application/marc', 'x-koha-query' => $search } )
384
      ->status_is(200);
385
386
    $t->get_ok( "//$userid:$password@/api/v1/authorities/" =>
387
          { Accept => 'text/plain', 'x-koha-query' => $search } )
388
      ->status_is(200);
389
390
    $schema->storage->txn_rollback;
391
};

Return to bug 32735