Bugzilla – Attachment 164983 Details for
Bug 36495
Add render_resource_not_found() and render_resource_deleted() helpers
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 36495: Add render_resource_not_found() and render_resource_deleted() helpers
Bug-36495-Add-renderresourcenotfound-and-renderres.patch (text/plain), 7.33 KB, created by
Martin Renvoize (ashimema)
on 2024-04-17 09:40:11 UTC
(
hide
)
Description:
Bug 36495: Add render_resource_not_found() and render_resource_deleted() helpers
Filename:
MIME Type:
Creator:
Martin Renvoize (ashimema)
Created:
2024-04-17 09:40:11 UTC
Size:
7.33 KB
patch
obsolete
>From a5b1c765a3514ff1f2bc3c6566bd1e8dfa3dd64d Mon Sep 17 00:00:00 2001 >From: Tomas Cohen Arazi <tomascohen@theke.io> >Date: Tue, 2 Apr 2024 15:18:16 -0300 >Subject: [PATCH] Bug 36495: Add render_resource_not_found() and > render_resource_deleted() helpers > >This patch introduces two helpers to be used in controllers. > >To test: >1. Apply this patch >2. Run: > $ ktd --shell > k$ prove t/db_dependent/api/v1/responses.t >=> SUCCESS: Tests pass! >3. Sign off :-D > >Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org> >Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com> >--- > 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 > >diff --git a/Koha/REST/Plugin/Responses.pm b/Koha/REST/Plugin/Responses.pm >new file mode 100644 >index 00000000000..ed3f7673629 >--- /dev/null >+++ b/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 <http://www.gnu.org/licenses>. >+ >+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; >diff --git a/Koha/REST/V1.pm b/Koha/REST/V1.pm >index 59f8f88e341..53a67e32472 100644 >--- a/Koha/REST/V1.pm >+++ b/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 ); > } > >diff --git a/t/db_dependent/api/v1/responses.t b/t/db_dependent/api/v1/responses.t >new file mode 100755 >index 00000000000..5acf2340b4e >--- /dev/null >+++ b/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 <http://www.gnu.org/licenses>. >+ >+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; >+}; >-- >2.44.0
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 36495
:
164291
|
164297
|
164301
|
164377
|
164378
|
164380
|
164382
|
164383
|
164384
|
164971
|
164972
|
164973
| 164983 |
164984
|
164985
|
165696