From 84a86d3d5718730b16c1efea777b116d9dbda06d Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Thu, 7 Jan 2021 13:18:27 -0300 Subject: [PATCH] Bug 27358: Teach objects.search about public requests Signed-off-by: Martin Renvoize --- Koha/REST/Plugin/Objects.pm | 4 ++- t/db_dependent/Koha/REST/Plugin/Objects.t | 41 +++++++++++++++++++++-- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/Koha/REST/Plugin/Objects.pm b/Koha/REST/Plugin/Objects.pm index 246aece704..bf558a96c3 100644 --- a/Koha/REST/Plugin/Objects.pm +++ b/Koha/REST/Plugin/Objects.pm @@ -53,6 +53,8 @@ sub register { # Extract reserved params my ( $filtered_params, $reserved_params, $path_params ) = $c->extract_reserved_params($args); + # Privileged reques? + my $is_public = $c->stash('is_public'); # Look for embeds my $embed = $c->stash('koha.embed'); @@ -124,7 +126,7 @@ sub register { } ); - return $objects->to_api({ embed => $embed }); + return $objects->to_api({ embed => $embed, public => $is_public }); } ); } diff --git a/t/db_dependent/Koha/REST/Plugin/Objects.t b/t/db_dependent/Koha/REST/Plugin/Objects.t index 60c9f81192..e1e9e9a14c 100755 --- a/t/db_dependent/Koha/REST/Plugin/Objects.t +++ b/t/db_dependent/Koha/REST/Plugin/Objects.t @@ -64,8 +64,27 @@ get '/biblios' => sub { $c->render( status => 200, json => {count => scalar(@$biblios), biblios => $biblios} ); }; +get '/items/:item_id_1/:item_id_2' => sub { + + my $c = shift; + + # Emulate a public route by stashing the is_public value + $c->stash( 'is_public' => 1 ); + + my $item_id_1 = $c->param('item_id_1'); + my $item_id_2 = $c->param('item_id_2'); + + my $items_rs = Koha::Items->search({ itemnumber => [ $item_id_1, $item_id_2 ] }); + my $items = $c->objects->search( $items_rs ); + + $c->render( + status => 200, + json => $items + ); +}; + # The tests -use Test::More tests => 10; +use Test::More tests => 11; use Test::Mojo; use t::lib::Mocks; @@ -412,4 +431,22 @@ subtest 'object.search helper order by embedded columns' => sub { ->json_is('/biblios/1/biblio_id' => $biblio1->biblionumber, 'Biblio 1 should be second'); $schema->storage->txn_begin; -} +}; + +subtest 'objects.search helper, public requests' => sub { + + plan tests => 3; + + $schema->storage->txn_begin; + + my $item_1 = $builder->build_sample_item; + my $item_2 = $builder->build_sample_item; + + my $t = Test::Mojo->new; + + $t->get_ok( '/items/'.$item_1->id.'/'.$item_2->id ) + ->json_is('/0' => $item_1->to_api({ public => 1 }), 'Public representation of $item_1 is retrieved') + ->json_is('/1' => $item_2->to_api({ public => 1 }), 'Public representation of $item_2 is retrieved'); + + $schema->storage->txn_rollback; +}; -- 2.20.1