From 7ae1af202d44e77f6c65b38c763db54d341bc4d1 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Thu, 11 Apr 2019 10:05:14 -0400 Subject: [PATCH] Bug 15496: Add API endoint for deleting a bib --- Koha/REST/V1/Biblios.pm | 76 +++++++++++++++++ api/v1/swagger/parameters.json | 3 + api/v1/swagger/parameters/biblio.json | 9 ++ api/v1/swagger/paths.json | 3 + api/v1/swagger/paths/biblios.json | 64 ++++++++++++++ t/db_dependent/api/v1/biblios.t | 118 ++++++++++++++++++++++++++ 6 files changed, 273 insertions(+) create mode 100644 Koha/REST/V1/Biblios.pm create mode 100644 api/v1/swagger/parameters/biblio.json create mode 100644 api/v1/swagger/paths/biblios.json create mode 100644 t/db_dependent/api/v1/biblios.t diff --git a/Koha/REST/V1/Biblios.pm b/Koha/REST/V1/Biblios.pm new file mode 100644 index 0000000000..d5e5564b0c --- /dev/null +++ b/Koha/REST/V1/Biblios.pm @@ -0,0 +1,76 @@ +package Koha::REST::V1::Biblios; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +use Mojo::Base 'Mojolicious::Controller'; + +use Koha::Biblios; +use C4::Biblio qw(DelBiblio); + +use Try::Tiny; + +=head1 API + +=head2 Class Methods + +=head3 delete + +=cut + +sub delete { + my $c = shift->openapi->valid_input or return; + + my $biblio = Koha::Biblios->find( $c->validation->param('biblio_id') ); + + if ( not defined $biblio ) { + return $c->render( + status => 404, + openapi => { error => "Object not found" } + ); + } + + return try { + my $error = DelBiblio( $biblio->id ); + + if ($error) { + return $c->render( + status => 409, + openapi => { error => $error } + ); + } + else { + return $c->render( status => 200, openapi => "" ); + } + } + catch { + if ( $_->isa('DBIx::Class::Exception') ) { + return $c->render( + status => 500, + openapi => { error => $_->{msg} } + ); + } + else { + return $c->render( + status => 500, + openapi => { error => "Something went wrong, check the logs." } + ); + } + }; +} + +1; diff --git a/api/v1/swagger/parameters.json b/api/v1/swagger/parameters.json index 11ce57cd9a..fce13be958 100644 --- a/api/v1/swagger/parameters.json +++ b/api/v1/swagger/parameters.json @@ -1,4 +1,7 @@ { + "biblio_id_pp": { + "$ref": "parameters/biblio.json#/biblio_id_pp" + }, "patron_id_pp": { "$ref": "parameters/patron.json#/patron_id_pp" }, diff --git a/api/v1/swagger/parameters/biblio.json b/api/v1/swagger/parameters/biblio.json new file mode 100644 index 0000000000..95b8ffcb31 --- /dev/null +++ b/api/v1/swagger/parameters/biblio.json @@ -0,0 +1,9 @@ +{ + "biblio_id_pp": { + "name": "biblio_id", + "in": "path", + "description": "Record internal identifier", + "required": true, + "type": "integer" + } +} diff --git a/api/v1/swagger/paths.json b/api/v1/swagger/paths.json index d0c27338d0..33cfd3aa5a 100644 --- a/api/v1/swagger/paths.json +++ b/api/v1/swagger/paths.json @@ -26,6 +26,9 @@ "/cities/{city_id}": { "$ref": "paths/cities.json#/~1cities~1{city_id}" }, + "/biblios/{biblio_id}": { + "$ref": "paths/biblios.json#/~1biblios~1{biblio_id}" + }, "/holds": { "$ref": "paths/holds.json#/~1holds" }, diff --git a/api/v1/swagger/paths/biblios.json b/api/v1/swagger/paths/biblios.json new file mode 100644 index 0000000000..73ace63c07 --- /dev/null +++ b/api/v1/swagger/paths/biblios.json @@ -0,0 +1,64 @@ +{ + "/biblios/{biblio_id}": { + "delete": { + "x-mojo-to": "Biblios#delete", + "operationId": "deleteBiblio", + "tags": ["biblios"], + "parameters": [{ + "$ref": "../parameters.json#/biblio_id_pp" + }], + "produces": [ + "application/json" + ], + "responses": { + "200": { + "description": "Biblio deleted", + "schema": { + "type": "string" + } + }, + "401": { + "description": "Authentication required", + "schema": { + "$ref": "../definitions.json#/error" + } + }, + "403": { + "description": "Access forbidden", + "schema": { + "$ref": "../definitions.json#/error" + } + }, + "404": { + "description": "Biblio not found", + "schema": { + "$ref": "../definitions.json#/error" + } + }, + "409": { + "description": "Unable to perform action on biblio", + "schema": { + "$ref": "../definitions.json#/error" + } + }, + "500": { + "description": "Internal error", + "schema": { + "$ref": "../definitions.json#/error" + } + }, + "503": { + "description": "Under maintenance", + "schema": { + "$ref": "../definitions.json#/error" + } + } + }, + "x-koha-authorization": { + "permissions": { + "parameters": "edit_catalogue" + } + } + } + } +} diff --git a/t/db_dependent/api/v1/biblios.t b/t/db_dependent/api/v1/biblios.t new file mode 100644 index 0000000000..bbc4587003 --- /dev/null +++ b/t/db_dependent/api/v1/biblios.t @@ -0,0 +1,118 @@ +#!/usr/bin/env perl + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +use Test::More tests => 1; +use Test::Mojo; +use Test::Warn; + +use t::lib::Mocks; +use t::lib::TestBuilder; + +use C4::Auth; +use Koha::Biblios; +use Koha::Database; + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +# FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling +# this affects the other REST api tests +t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' ); + +my $remote_address = '127.0.0.1'; +my $t = Test::Mojo->new('Koha::REST::V1'); + +subtest 'delete() tests' => sub { + + plan tests => 9; + + $schema->storage->txn_begin; + + my ( $unauthorized_borrowernumber, $unauthorized_session_id ) = + create_user_and_session( { authorized => 0 } ); + my ( $authorized_borrowernumber, $authorized_session_id ) = + create_user_and_session( { authorized => 1 } ); + + my $biblio_id = $builder->build_object({ class => 'Koha::Biblios' })->id; + my $item = $builder->build_object( { class => 'Koha::Items', value => { biblionumber => $biblio_id } } ); + + # Unauthorized attempt to delete + my $tx = $t->ua->build_tx( DELETE => "/api/v1/biblios/$biblio_id" ); + $tx->req->cookies({ name => 'CGISESSID', value => $unauthorized_session_id }); + $tx->req->env({ REMOTE_ADDR => $remote_address }); + $t->request_ok($tx) + ->status_is(403); + + $tx = $t->ua->build_tx( DELETE => "/api/v1/biblios/$biblio_id" ); + $tx->req->cookies({ name => 'CGISESSID', value => $authorized_session_id }); + $tx->req->env({ REMOTE_ADDR => $remote_address }); + $t->request_ok($tx) + ->status_is(409); + + $item->delete(); + + $tx = $t->ua->build_tx( DELETE => "/api/v1/biblios/$biblio_id" ); + $tx->req->cookies({ name => 'CGISESSID', value => $authorized_session_id }); + $tx->req->env({ REMOTE_ADDR => $remote_address }); + $t->request_ok($tx) + ->status_is(200) + ->content_is('""'); + + $tx = $t->ua->build_tx( DELETE => "/api/v1/biblios/$biblio_id" ); + $tx->req->cookies({ name => 'CGISESSID', value => $authorized_session_id }); + $tx->req->env({ REMOTE_ADDR => $remote_address }); + $t->request_ok($tx) + ->status_is(404); + + $schema->storage->txn_rollback; +}; + +sub create_user_and_session { + + my $args = shift; + my $flags = ( $args->{authorized} ) ? $args->{authorized} : 0; + my $dbh = C4::Context->dbh; + + my $user = $builder->build( + { + source => 'Borrower', + value => { + flags => $flags + } + } + ); + + # Create a session for the authorized user + my $session = C4::Auth::get_session(''); + $session->param( 'number', $user->{borrowernumber} ); + $session->param( 'id', $user->{userid} ); + $session->param( 'ip', '127.0.0.1' ); + $session->param( 'lasttime', time() ); + $session->flush; + + if ( $args->{authorized} ) { + $dbh->do( " + INSERT INTO user_permissions (borrowernumber,module_bit,code) + VALUES (?,3,'parameters_remaining_permissions')", undef, + $user->{borrowernumber} ); + } + + return ( $user->{borrowernumber}, $session->id ); +} + -- 2.20.1 (Apple Git-117)