@@ -, +, @@ render_resource_deleted() helpers $ ktd --shell k$ prove t/db_dependent/api/v1/responses.t --- Koha/REST/Plugin/Responses.pm | 82 +++++++++++++++++++++ Koha/REST/V1.pm | 11 +-- t/db_dependent/api/v1/responses.t | 117 ++++++++++++++++++++++++++++++ 3 files changed, 205 insertions(+), 5 deletions(-) create mode 100644 Koha/REST/Plugin/Responses.pm create mode 100755 t/db_dependent/api/v1/responses.t --- a/Koha/REST/Plugin/Responses.pm +++ a/Koha/REST/Plugin/Responses.pm @@ -0,0 +1,82 @@ +package Koha::REST::Plugin::Responses; + +# 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, see . + +use Modern::Perl; + +use Mojo::Base 'Mojolicious::Plugin'; + +=head1 NAME + +Koha::REST::Plugin::Responses + +=head1 API + +=head2 Helper methods + +=cut + +sub register { + my ( $self, $app ) = @_; + +=head3 render_resource_deleted + + $c->render_resource_deleted + +Provides a generic method rendering the standard response for resource deletion. + +=cut + + $app->helper( + 'render_resource_deleted' => sub { + my ($c) = @_; + + $c->render( + status => 204, + openapi => q{}, + ); + } + ); + +=head3 render_resource_not_found + + $c->render_resource_not_found + +Provides a generic method rendering the standard response for resource not found. + +=cut + + $app->helper( + 'render_resource_not_found' => sub { + my ( $c, $name ) = @_; + + my $message = + ($name) + ? "$name not found" + : "Resource not found"; + + $c->render( + status => 404, + openapi => { + error => $message, + error_code => 'not_found', + }, + ); + } + ); +} + +1; --- a/Koha/REST/V1.pm +++ a/Koha/REST/V1.pm @@ -150,11 +150,12 @@ sub startup { $self->app->log->warn( "Warning: Failed to fetch oauth configuration: " . $_ ); }; - $self->plugin( 'Koha::REST::Plugin::Pagination' ); - $self->plugin( 'Koha::REST::Plugin::Query' ); - $self->plugin( 'Koha::REST::Plugin::Objects' ); - $self->plugin( 'Koha::REST::Plugin::Exceptions' ); - $self->plugin( 'Koha::REST::Plugin::Auth::IdP' ); + $self->plugin('Koha::REST::Plugin::Pagination'); + $self->plugin('Koha::REST::Plugin::Query'); + $self->plugin('Koha::REST::Plugin::Objects'); + $self->plugin('Koha::REST::Plugin::Exceptions'); + $self->plugin('Koha::REST::Plugin::Responses'); + $self->plugin('Koha::REST::Plugin::Auth::IdP'); $self->plugin( 'Mojolicious::Plugin::OAuth2' => $oauth_configuration ); } --- a/t/db_dependent/api/v1/responses.t +++ a/t/db_dependent/api/v1/responses.t @@ -0,0 +1,117 @@ +#!/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, see . + +use Modern::Perl; + +use Test::More tests => 2; +use Test::MockModule; +use Test::Mojo; + +use t::lib::Mocks; +use t::lib::TestBuilder; + +use Koha::Database; +use Koha::Exceptions; + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); + +subtest 'render_resource_not_found() tests' => sub { + + plan tests => 6; + + $schema->storage->txn_begin; + + my $authorized_patron = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 1 }, + } + ); + my $password = 'thePassword123'; + $authorized_patron->set_password( { password => $password, skip_validation => 1 } ); + my $userid = $authorized_patron->userid; + + my $mock_cities = Test::MockModule->new('Koha::REST::V1::Cities'); + $mock_cities->mock( + 'get', + sub { + my $c = shift->openapi->valid_input or return; + $c->render_resource_not_found; + } + ); + + my $t = Test::Mojo->new('Koha::REST::V1'); + + $t->get_ok("//$userid:$password@/api/v1/cities/1")->status_is('404')->json_is( + { + error => 'Resource not found', + error_code => 'not_found', + } + ); + + $mock_cities->mock( + 'get', + sub { + my $c = shift->openapi->valid_input or return; + $c->render_resource_not_found("Thing"); + } + ); + + $t->get_ok("//$userid:$password@/api/v1/cities/1")->status_is('404')->json_is( + { + error => 'Thing not found', + error_code => 'not_found', + } + ); + + $schema->storage->txn_rollback; +}; + +subtest 'render_resource_deleted() tests' => sub { + + plan tests => 3; + + $schema->storage->txn_begin; + + my $authorized_patron = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 1 }, + } + ); + my $password = 'thePassword123'; + $authorized_patron->set_password( { password => $password, skip_validation => 1 } ); + my $userid = $authorized_patron->userid; + + my $mock_cities = Test::MockModule->new('Koha::REST::V1::Cities'); + $mock_cities->mock( + 'delete', + sub { + my $c = shift->openapi->valid_input or return; + return $c->render_resource_deleted; + } + ); + + my $t = Test::Mojo->new('Koha::REST::V1'); + + $t->delete_ok("//$userid:$password@/api/v1/cities/1")->status_is('204')->content_is(q{}); + + $schema->storage->txn_rollback; +}; --