From ac0de64f5537cd1d3e7415623d94bb37d2d5298a Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Mon, 24 Jun 2024 16:04:01 +0100 Subject: [PATCH] Bug 36641: Introduce render_invalid_parameter_value helper This patch introduces a new 'render_invalid_parameter_value' helper method that accepts 'path' and 'values' parameters to denote which field has failed validation and where the end user can get valid options. Signed-off-by: Tomas Cohen Arazi Signed-off-by: Kyle M Hall --- Koha/REST/Plugin/Responses.pm | 30 +++++++++++++++++ t/db_dependent/api/v1/responses.t | 55 ++++++++++++++++++++++++++++++- 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/Koha/REST/Plugin/Responses.pm b/Koha/REST/Plugin/Responses.pm index ed3f7673629..3b39ac13797 100644 --- a/Koha/REST/Plugin/Responses.pm +++ b/Koha/REST/Plugin/Responses.pm @@ -77,6 +77,36 @@ Provides a generic method rendering the standard response for resource not found ); } ); + +=head3 render_invalid_parameter_value + + $c->render_invalid_parameter_value + +Provides a generic method rendering the standard response for invalid parameter value passed. + +=cut + + $app->helper( + 'render_invalid_parameter_value' => sub { + my ( $c, $opts ) = @_; + my $path = $opts->{path}; + my $values = $opts->{values}; + + $c->render( + status => 400, + openapi => { + error => "Invalid parameter value", + error_code => 'invalid_parameter_value', + path => $path, + ( + $values + ? ( values => { uri => $values->{uri}, field => $values->{field} } ) + : () + ) + }, + ); + } + ); } 1; diff --git a/t/db_dependent/api/v1/responses.t b/t/db_dependent/api/v1/responses.t index 5acf2340b4e..575fc33a0cf 100755 --- a/t/db_dependent/api/v1/responses.t +++ b/t/db_dependent/api/v1/responses.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 2; +use Test::More tests => 3; use Test::MockModule; use Test::Mojo; @@ -115,3 +115,56 @@ subtest 'render_resource_deleted() tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'render_invalid_parameter_value() 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 $path = '/query/library'; + my $uri = '/api/v1/libraries'; + my $field = 'library_id'; + my $mock_cities = Test::MockModule->new('Koha::REST::V1::CirculationRules'); + $mock_cities->mock( + 'list_effective_rules', + sub { + my $c = shift->openapi->valid_input or return; + return $c->render_invalid_parameter_value( + { + path => $path, + values => { + uri => $uri, + field => $field + } + } + ); + } + ); + + my $t = Test::Mojo->new('Koha::REST::V1'); + + $t->get_ok("//$userid:$password@/api/v1/circulation_rules?library=SOMETHING")->status_is('400')->json_is( + { + error => 'Invalid parameter value', + error_code => 'invalid_parameter_value', + path => $path, + values => { + uri => $uri, + field => $field + } + } + ); + + $schema->storage->txn_rollback; +}; -- 2.45.2