From dca5e04165dc071822cc17dd26d7ddc63d09a30e 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 Signed-off-by: Katrin Fischer --- 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 ed3f767362..3b39ac1379 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 5acf2340b4..575fc33a0c 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.39.5