From 36a86d747830cbeba8126b618bb8b6a040c931b4 Mon Sep 17 00:00:00 2001 From: Agustin Moyano Date: Wed, 7 Dec 2022 12:05:35 -0300 Subject: [PATCH] Bug 31793: Add REST endpoint to delete authorities To test: 1. Apply patch 2. Set RESTBasicAuth preference to true 3. Get the id of an authority 4. Make a DELETE request to /api/v1/authorities/{authid} 5. Check that the authority was deleted 6. Sign off Signed-off-by: David Nind Signed-off-by: Marcel de Rooy Signed-off-by: Tomas Cohen Arazi --- Koha/REST/V1/Authorities.pm | 37 ++++++++++++++++++ api/v1/swagger/paths/authorities.yaml | 42 +++++++++++++++++++++ t/db_dependent/api/v1/authorities.t | 54 ++++++++++++++++++++++++++- 3 files changed, 132 insertions(+), 1 deletion(-) diff --git a/Koha/REST/V1/Authorities.pm b/Koha/REST/V1/Authorities.pm index 5266fab705..136e3134e6 100644 --- a/Koha/REST/V1/Authorities.pm +++ b/Koha/REST/V1/Authorities.pm @@ -20,6 +20,7 @@ use Modern::Perl; use Mojo::Base 'Mojolicious::Controller'; use Koha::Authorities; +use C4::AuthoritiesMarc qw( DelAuthority ); use List::MoreUtils qw( any ); use MARC::Record::MiJ; @@ -99,4 +100,40 @@ sub get { }; } +=head3 delete + +Controller function that handles deleting an authority object + +=cut + +sub delete { + my $c = shift->openapi->valid_input or return; + + my $authority = Koha::Authorities->find( { authid => $c->validation->param('authority_id') } ); + + if ( not defined $authority ) { + return $c->render( + status => 404, + openapi => { error => "Object not found" } + ); + } + + return try { + my $error = DelAuthority( { authid => $authority->authid } ); + + if ($error) { + return $c->render( + status => 409, + openapi => { error => $error } + ); + } + else { + return $c->render( status => 204, openapi => "" ); + } + } + catch { + $c->unhandled_exception($_); + }; +} + 1; diff --git a/api/v1/swagger/paths/authorities.yaml b/api/v1/swagger/paths/authorities.yaml index c5f1044b72..0b47d41389 100644 --- a/api/v1/swagger/paths/authorities.yaml +++ b/api/v1/swagger/paths/authorities.yaml @@ -50,3 +50,45 @@ x-koha-authorization: permissions: catalogue: "1" + delete: + x-mojo-to: Authorities#delete + operationId: deleteAuthority + tags: + - authorities + summary: Delete authority + parameters: + - $ref: "../swagger.yaml#/parameters/authority_id_pp" + produces: + - application/json + responses: + "204": + description: Authority deleted + schema: + type: string + "401": + description: Authentication required + schema: + $ref: "../swagger.yaml#/definitions/error" + "403": + description: Access forbidden + schema: + $ref: "../swagger.yaml#/definitions/error" + "404": + description: Biblio not found + schema: + $ref: "../swagger.yaml#/definitions/error" + "409": + description: Unable to perform action on biblio + schema: + $ref: "../swagger.yaml#/definitions/error" + "500": + description: Internal error + schema: + $ref: "../swagger.yaml#/definitions/error" + "503": + description: Under maintenance + schema: + $ref: "../swagger.yaml#/definitions/error" + x-koha-authorization: + permissions: + editcatalogue: edit_catalogue diff --git a/t/db_dependent/api/v1/authorities.t b/t/db_dependent/api/v1/authorities.t index 5edc612838..13e101f951 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 => 1; +use Test::More tests => 2; use Test::MockModule; use Test::Mojo; use Test::Warn; @@ -108,5 +108,57 @@ subtest 'get() tests' => sub { ->status_is(404) ->json_is( '/error', 'Object not found.' ); + $schema->storage->txn_rollback; +}; + +subtest 'delete() tests' => sub { + + plan tests => 7; + + $schema->storage->txn_begin; + + my $patron = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 0 } # no permissions + } + ); + my $password = 'thePassword123'; + $patron->set_password( { password => $password, skip_validation => 1 } ); + my $userid = $patron->userid; + + my $authority = $builder->build_object({ 'class' => 'Koha::Authorities', value => { + marcxml => q| + + 1001 + + 102 + My Corporation + +| + } }); + + $t->delete_ok("//$userid:$password@/api/v1/authorities/".$authority->authid) + ->status_is(403, 'Not enough permissions makes it return the right code'); + + # Add permissions + $builder->build( + { + source => 'UserPermission', + value => { + borrowernumber => $patron->borrowernumber, + module_bit => 9, + code => 'edit_catalogue' + } + } + ); + + $t->delete_ok("//$userid:$password@/api/v1/authorities/".$authority->authid) + ->status_is(204, 'SWAGGER3.2.4') + ->content_is('', 'SWAGGER3.3.4'); + + $t->delete_ok("//$userid:$password@/api/v1/authorities/".$authority->authid) + ->status_is(404); + $schema->storage->txn_rollback; }; \ No newline at end of file -- 2.39.5