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 <david@davidnind.com>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Agustin Moyano 2022-12-07 12:05:35 -03:00 committed by Tomas Cohen Arazi
parent 35dd17735f
commit 36a86d7478
3 changed files with 132 additions and 1 deletions

View File

@ -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;

View File

@ -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

View File

@ -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|<?xml version="1.0" encoding="UTF-8"?>
<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">
<controlfield tag="001">1001</controlfield>
<datafield tag="110" ind1=" " ind2=" ">
<subfield code="9">102</subfield>
<subfield code="a">My Corporation</subfield>
</datafield>
</record>|
} });
$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;
};