From 67cafa719bac557d15e4447507ff5ef6e4063c17 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 This patch adds an endpoint to list authorities To test: 1. apply patch 2. enable basic auth 3. call to GET /api/v1/authorities with the following Accept headers: * application/json * application/marcxml+xml * application/marc-in-json * application/marc * text/plain 4. notice how data changes with each Accept header 5. prove t/db_dependent/api/v1/authorities.t 6. sign off Signed-off-by: David Nind Signed-off-by: Nick Clemens Signed-off-by: Tomas Cohen Arazi --- Koha/REST/V1/Authorities.pm | 66 +++++++++++++++++++++++ api/v1/swagger/paths/authorities.yaml | 57 ++++++++++++++++++++ t/db_dependent/api/v1/authorities.t | 77 ++++++++++++++++++++++++++- 3 files changed, 199 insertions(+), 1 deletion(-) diff --git a/Koha/REST/V1/Authorities.pm b/Koha/REST/V1/Authorities.pm index 2bbab6a6a3..52244d025d 100644 --- a/Koha/REST/V1/Authorities.pm +++ b/Koha/REST/V1/Authorities.pm @@ -253,4 +253,70 @@ sub update { }; } +=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 b2a3b7f334..2de38a5a36 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 9524d53614..ba6eaaf186 100644 --- 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; @@ -314,3 +314,78 @@ subtest 'put() tests' => sub { $schema->storage->txn_rollback; }; + + + +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.39.5