Browse Source

Bug 32713: Unit tests

This patch adds unit tests to prove we return a 400 and an appropriate
error message when calling an endpoint that is not defined to support
x-koha-embed whilst passing an x-koha-embed header.

Test plan:
1) Run test prior to applying second patch and confirm it fails
2) Run test after applying second patch and confirm it passes

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
23.05.x
Martin Renvoize 1 year ago
committed by Tomas Cohen Arazi
parent
commit
2202f7f2c2
Signed by: tomascohen GPG Key ID: 0A272EA1B2F3C15F
  1. 79
      t/Koha/REST/Plugin/Query.t
  2. 10
      t/db_dependent/api/v1/query.t

79
t/Koha/REST/Plugin/Query.t

@ -209,17 +209,68 @@ get '/build_query' => sub {
get '/stash_embed' => sub {
my $c = shift;
$c->stash_embed();
my $embed = $c->stash('koha.embed');
my $strings = $c->stash('koha.strings');
try {
$c->stash_embed(
{
spec => {
'parameters' => [
{
'in' => 'header',
'name' => 'x-koha-embed',
'items' => {
'enum' => [
'checkouts', 'checkouts.item',
'library', 'holds+count'
]
}
}
]
}
}
);
$c->render(
status => 200,
json => {
strings => $strings,
embed => $embed,
}
);
my $embed = $c->stash('koha.embed');
my $strings = $c->stash('koha.strings');
$c->render(
status => 200,
json => {
strings => $strings,
embed => $embed
}
);
}
catch {
$c->render(
status => 400,
json => { error => "$_" }
);
};
};
get '/stash_embed_no_spec' => sub {
my $c = shift;
try {
$c->stash_embed( { spec => {} } );
my $embed = $c->stash('koha.embed');
my $strings = $c->stash('koha.strings');
$c->render(
status => 200,
json => {
strings => $strings,
embed => $embed
}
);
}
catch {
$c->render(
status => 400,
json => { error => "$_" }
);
};
};
get '/stash_overrides' => sub {
@ -433,7 +484,7 @@ subtest '_build_query_params_from_api' => sub {
subtest 'stash_embed() tests' => sub {
plan tests => 16;
plan tests => 19;
my $t = Test::Mojo->new;
@ -468,6 +519,12 @@ subtest 'stash_embed() tests' => sub {
patron => { }
})
->json_is( '/strings' => 1 );
$t->get_ok( '/stash_embed_no_spec' => { 'x-koha-embed' => 'checkouts,checkouts.item,patron' } )
->status_is(400)
->json_is( '/error' =>
qq{Exception 'Koha::Exceptions::BadParameter' thrown 'Embedding objects is not allowed on this endpoint.'\n}
);
};
subtest 'stash_overrides() tests' => sub {

10
t/db_dependent/api/v1/query.t

@ -108,7 +108,7 @@ subtest 'q handling tests' => sub {
subtest 'x-koha-embed tests' => sub {
plan tests => 5;
plan tests => 8;
$schema->storage->txn_begin;
@ -138,5 +138,13 @@ subtest 'x-koha-embed tests' => sub {
}
)->status_is(400);
$res = $t->get_ok(
"//$userid:$password@/api/v1/cities" => {
'x-koha-embed' => 'any_embed'
}
)->status_is(400)->tx->res->json;
is($res, 'Embedding objects is not allowed on this endpoint.', 'Correct error message is returned');
$schema->storage->txn_rollback;
};

Loading…
Cancel
Save