From 2de83991ce810a3cbf59ebc131d4efc0fc83d8ad Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Thu, 2 Mar 2023 09:37:14 +0000 Subject: [PATCH] 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 --- t/Koha/REST/Plugin/Query.t | 79 ++++++++++++++++++++++++++++++----- t/db_dependent/api/v1/query.t | 10 ++++- 2 files changed, 77 insertions(+), 12 deletions(-) diff --git a/t/Koha/REST/Plugin/Query.t b/t/Koha/REST/Plugin/Query.t index 9da2590e48..ef704d56ac 100755 --- a/t/Koha/REST/Plugin/Query.t +++ b/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 => 14; + plan tests => 17; my $t = Test::Mojo->new; @@ -465,6 +516,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 { diff --git a/t/db_dependent/api/v1/query.t b/t/db_dependent/api/v1/query.t index 3b8be18f8a..3a9bc1bebe 100755 --- a/t/db_dependent/api/v1/query.t +++ b/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; }; -- 2.39.2