From 9b2f2f938d8e70b543e4a17f537a0663ff575035 Mon Sep 17 00:00:00 2001 From: Agustin Moyano Date: Tue, 28 Feb 2023 11:05:34 -0300 Subject: [PATCH] Bug 32735: Add endpoint to list authorities --- Koha/REST/V1/Authorities.pm | 67 +++++++++++++++++++++++ api/v1/swagger/paths/authorities.yaml | 57 ++++++++++++++++++++ t/db_dependent/api/v1/authorities.t | 78 ++++++++++++++++++++++++++- 3 files changed, 200 insertions(+), 2 deletions(-) diff --git a/Koha/REST/V1/Authorities.pm b/Koha/REST/V1/Authorities.pm index d4236861b1..8566a8f189 100644 --- a/Koha/REST/V1/Authorities.pm +++ b/Koha/REST/V1/Authorities.pm @@ -271,4 +271,71 @@ sub update { $c->unhandled_exception($_); }; } + +=head3 list + +Controller function that handles retrieving a list of authorities + +=cut + +sub list { + my $c = shift->openapi->valid_input or return; + + my $authorities = $c->objects->search_rs( Koha::Authorities->new ); + + return try { + + if ( $c->req->headers->accept =~ m/application\/json(;.*)?$/ ) { + return $c->render( + status => 200, + json => $authorities->to_api + ); + } + elsif ( + $c->req->headers->accept =~ m/application\/marcxml\+xml(;.*)?$/ ) + { + $c->res->headers->add( 'Content-Type', 'application/marcxml+xml' ); + return $c->render( + status => 200, + text => $authorities->print_collection('marcxml') + ); + } + elsif ( + $c->req->headers->accept =~ m/application\/marc-in-json(;.*)?$/ ) + { + $c->res->headers->add( 'Content-Type', 'application/marc-in-json' ); + return $c->render( + status => 200, + data => $authorities->print_collection('mij') + ); + } + elsif ( $c->req->headers->accept =~ m/application\/marc(;.*)?$/ ) { + $c->res->headers->add( 'Content-Type', 'application/marc' ); + return $c->render( + status => 200, + text => $authorities->print_collection('marc') + ); + } + elsif ( $c->req->headers->accept =~ m/text\/plain(;.*)?$/ ) { + return $c->render( + status => 200, + text => $authorities->print_collection('txt') + ); + } + else { + return $c->render( + status => 406, + openapi => [ + "application/json", "application/marcxml+xml", + "application/marc-in-json", "application/marc", + "text/plain" + ] + ); + } + } + catch { + $c->unhandled_exception($_); + }; +} + 1; diff --git a/api/v1/swagger/paths/authorities.yaml b/api/v1/swagger/paths/authorities.yaml index d754e49b3b..da71156942 100644 --- a/api/v1/swagger/paths/authorities.yaml +++ b/api/v1/swagger/paths/authorities.yaml @@ -1,5 +1,62 @@ --- "/authorities": + get: + x-mojo-to: Authorities#list + operationId: listAuthorities + tags: + - authorities + summary: List authorities + parameters: + - $ref: "../swagger.yaml#/parameters/page" + - $ref: "../swagger.yaml#/parameters/per_page" + - $ref: "../swagger.yaml#/parameters/match" + - $ref: "../swagger.yaml#/parameters/order_by" + - $ref: "../swagger.yaml#/parameters/q_param" + - $ref: "../swagger.yaml#/parameters/q_body" + - $ref: "../swagger.yaml#/parameters/q_header" + - $ref: "../swagger.yaml#/parameters/request_id_header" + produces: + - application/json + - application/marcxml+xml + - application/marc-in-json + - application/marc + - text/plain + responses: + "200": + description: A list of authorities + "401": + description: Authentication required + schema: + $ref: "../swagger.yaml#/definitions/error" + "403": + description: Access forbidden + schema: + $ref: "../swagger.yaml#/definitions/error" + "404": + description: Authority not found + schema: + $ref: "../swagger.yaml#/definitions/error" + "406": + description: Not acceptable + schema: + type: array + description: Accepted content-types + items: + type: string + "500": + description: | + Internal server error. Possible `error_code` attribute values: + + * `internal_server_error` + schema: + $ref: "../swagger.yaml#/definitions/error" + "503": + description: Under maintenance + schema: + $ref: "../swagger.yaml#/definitions/error" + x-koha-authorization: + permissions: + catalogue: "1" post: x-mojo-to: Authorities#add operationId: addAuthority diff --git a/t/db_dependent/api/v1/authorities.t b/t/db_dependent/api/v1/authorities.t index 13ec22df63..fa4c8f9bf0 100755 --- a/t/db_dependent/api/v1/authorities.t +++ b/t/db_dependent/api/v1/authorities.t @@ -20,7 +20,7 @@ use Modern::Perl; use utf8; use Encode; -use Test::More tests => 4; +use Test::More tests => 5; use Test::MockModule; use Test::Mojo; use Test::Warn; @@ -345,4 +345,78 @@ subtest 'put() tests' => sub { is($subfield_a, 'USMARCFormated'); $schema->storage->txn_rollback; -}; \ No newline at end of file +}; + + +subtest 'list() tests' => sub { + plan tests => 14; + + $schema->storage->txn_begin; + + my $patron = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 0 } + } + ); + my $password = 'thePassword123'; + $patron->set_password( { password => $password, skip_validation => 1 } ); + $patron->discard_changes; + my $userid = $patron->userid; + + my $authid1 = $builder->build_object({ 'class' => 'Koha::Authorities', value => { + marcxml => q| + + 1001 + + 102 + My Corporation + +| + } })->authid; + + my $authid2 = $builder->build_object({ 'class' => 'Koha::Authorities', value => { + marcxml => q| + + 1001 + + 102 + My Corporation + +| + } })->authid; + + my $search = +"[{\"authid\": \"$authid1\"}, {\"authid\": \"$authid2\"}]"; + $t->get_ok( + "//$userid:$password@/api/v1/authorities/" => { 'x-koha-query' => $search } + )->status_is(403); + + $patron->flags(4)->store; + + $t->get_ok( "//$userid:$password@/api/v1/authorities/" => + { Accept => 'application/weird+format', 'x-koha-query' => $search } ) + ->status_is(400); + + $t->get_ok( "//$userid:$password@/api/v1/authorities/" => + { Accept => 'application/json', 'x-koha-query' => $search } ) + ->status_is(200); + + $t->get_ok( "//$userid:$password@/api/v1/authorities/" => + { Accept => 'application/marcxml+xml', 'x-koha-query' => $search } ) + ->status_is(200); + + $t->get_ok( "//$userid:$password@/api/v1/authorities/" => + { Accept => 'application/marc-in-json', 'x-koha-query' => $search } ) + ->status_is(200); + + $t->get_ok( "//$userid:$password@/api/v1/authorities/" => + { Accept => 'application/marc', 'x-koha-query' => $search } ) + ->status_is(200); + + $t->get_ok( "//$userid:$password@/api/v1/authorities/" => + { Accept => 'text/plain', 'x-koha-query' => $search } ) + ->status_is(200); + + $schema->storage->txn_rollback; +}; -- 2.25.1