Bug 31793: Add REST endpoint to delete authorities
authorAgustin Moyano <agustinmoyano@theke.io>
Wed, 7 Dec 2022 15:05:35 +0000 (12:05 -0300)
committerTomas Cohen Arazi <tomascohen@theke.io>
Fri, 10 Mar 2023 13:21:19 +0000 (10:21 -0300)
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>
Koha/REST/V1/Authorities.pm
api/v1/swagger/paths/authorities.yaml
t/db_dependent/api/v1/authorities.t

index 5266fab..136e313 100644 (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;
index c5f1044..0b47d41 100644 (file)
     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
index 5edc612..13e101f 100755 (executable)
@@ -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;
@@ -109,4 +109,56 @@ subtest 'get() tests' => sub {
       ->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