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 <tomascohen@theke.io>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
This commit is contained in:
Martin Renvoize 2024-06-24 16:04:01 +01:00 committed by Katrin Fischer
parent baa6814753
commit dca5e04165
Signed by: kfischer
GPG key ID: 0EF6E2C03357A834
2 changed files with 84 additions and 1 deletions

View file

@ -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;

View file

@ -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;
};