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:
parent
baa6814753
commit
dca5e04165
2 changed files with 84 additions and 1 deletions
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue