use Mojo::Base 'Mojolicious::Controller';
use Koha::Authorities;
+use C4::AuthoritiesMarc qw( DelAuthority );
use List::MoreUtils qw( any );
use MARC::Record::MiJ;
};
}
+=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;
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
use utf8;
use Encode;
-use Test::More tests => 1;
+use Test::More tests => 2;
use Test::MockModule;
use Test::Mojo;
use Test::Warn;
->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;
};
\ No newline at end of file