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

(-)a/Koha/REST/V1/Authorities.pm (+67 lines)
Lines 271-274 sub update { Link Here
271
        $c->unhandled_exception($_);
271
        $c->unhandled_exception($_);
272
    };
272
    };
273
}
273
}
274
275
=head3 list
276
277
Controller function that handles retrieving a list of authorities
278
279
=cut
280
281
sub list {
282
    my $c = shift->openapi->valid_input or return;
283
284
    my $authorities = $c->objects->search_rs( Koha::Authorities->new );
285
286
    return try {
287
288
        if ( $c->req->headers->accept =~ m/application\/json(;.*)?$/ ) {
289
            return $c->render(
290
                status => 200,
291
                json   => $authorities->to_api
292
            );
293
        }
294
        elsif (
295
            $c->req->headers->accept =~ m/application\/marcxml\+xml(;.*)?$/ )
296
        {
297
            $c->res->headers->add( 'Content-Type', 'application/marcxml+xml' );
298
            return $c->render(
299
                status => 200,
300
                text   => $authorities->print_collection('marcxml')
301
            );
302
        }
303
        elsif (
304
            $c->req->headers->accept =~ m/application\/marc-in-json(;.*)?$/ )
305
        {
306
            $c->res->headers->add( 'Content-Type', 'application/marc-in-json' );
307
            return $c->render(
308
                status => 200,
309
                data   => $authorities->print_collection('mij')
310
            );
311
        }
312
        elsif ( $c->req->headers->accept =~ m/application\/marc(;.*)?$/ ) {
313
            $c->res->headers->add( 'Content-Type', 'application/marc' );
314
            return $c->render(
315
                status => 200,
316
                text   => $authorities->print_collection('marc')
317
            );
318
        }
319
        elsif ( $c->req->headers->accept =~ m/text\/plain(;.*)?$/ ) {
320
            return $c->render(
321
                status => 200,
322
                text   => $authorities->print_collection('txt')
323
            );
324
        }
325
        else {
326
            return $c->render(
327
                status  => 406,
328
                openapi => [
329
                    "application/json",         "application/marcxml+xml",
330
                    "application/marc-in-json", "application/marc",
331
                    "text/plain"
332
                ]
333
            );
334
        }
335
    }
336
    catch {
337
        $c->unhandled_exception($_);
338
    };
339
}
340
274
1;
341
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 (-3 / +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 345-348 subtest 'put() tests' => sub { Link Here
345
    is($subfield_a, 'USMARCFormated');
345
    is($subfield_a, 'USMARCFormated');
346
346
347
    $schema->storage->txn_rollback;
347
    $schema->storage->txn_rollback;
348
};
348
};
349
350
351
subtest 'list() tests' => sub {
352
    plan tests => 14;
353
354
    $schema->storage->txn_begin;
355
356
    my $patron = $builder->build_object(
357
        {
358
            class => 'Koha::Patrons',
359
            value => { flags => 0 }
360
        }
361
    );
362
    my $password = 'thePassword123';
363
    $patron->set_password( { password => $password, skip_validation => 1 } );
364
    $patron->discard_changes;
365
    my $userid = $patron->userid;
366
367
    my $authid1 = $builder->build_object({ 'class' => 'Koha::Authorities', value => {
368
      marcxml => q|<?xml version="1.0" encoding="UTF-8"?>
369
<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">
370
    <controlfield tag="001">1001</controlfield>
371
    <datafield tag="110" ind1=" " ind2=" ">
372
        <subfield code="9">102</subfield>
373
        <subfield code="a">My Corporation</subfield>
374
    </datafield>
375
</record>|
376
    } })->authid;
377
378
    my $authid2 = $builder->build_object({ 'class' => 'Koha::Authorities', value => {
379
      marcxml => q|<?xml version="1.0" encoding="UTF-8"?>
380
<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">
381
    <controlfield tag="001">1001</controlfield>
382
    <datafield tag="110" ind1=" " ind2=" ">
383
        <subfield code="9">102</subfield>
384
        <subfield code="a">My Corporation</subfield>
385
    </datafield>
386
</record>|
387
    } })->authid;
388
389
    my $search =
390
"[{\"authid\": \"$authid1\"}, {\"authid\": \"$authid2\"}]";
391
    $t->get_ok(
392
        "//$userid:$password@/api/v1/authorities/" => { 'x-koha-query' => $search }
393
    )->status_is(403);
394
395
    $patron->flags(4)->store;
396
397
    $t->get_ok( "//$userid:$password@/api/v1/authorities/" =>
398
          { Accept => 'application/weird+format', 'x-koha-query' => $search } )
399
      ->status_is(400);
400
401
    $t->get_ok( "//$userid:$password@/api/v1/authorities/" =>
402
          { Accept => 'application/json', 'x-koha-query' => $search } )
403
      ->status_is(200);
404
405
    $t->get_ok( "//$userid:$password@/api/v1/authorities/" =>
406
          { Accept => 'application/marcxml+xml', 'x-koha-query' => $search } )
407
      ->status_is(200);
408
409
    $t->get_ok( "//$userid:$password@/api/v1/authorities/" =>
410
          { Accept => 'application/marc-in-json', 'x-koha-query' => $search } )
411
      ->status_is(200);
412
413
    $t->get_ok( "//$userid:$password@/api/v1/authorities/" =>
414
          { Accept => 'application/marc', 'x-koha-query' => $search } )
415
      ->status_is(200);
416
417
    $t->get_ok( "//$userid:$password@/api/v1/authorities/" =>
418
          { Accept => 'text/plain', 'x-koha-query' => $search } )
419
      ->status_is(200);
420
421
    $schema->storage->txn_rollback;
422
};
349
- 

Return to bug 32735