From b7a2fc114f8ce9a9720703de0444a9565c1c9f2c 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 --- Koha/REST/Plugin/Objects.pm | 4 ++- t/db_dependent/Koha/REST/Plugin/Objects.t | 40 +++++++++++++++++++++-- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/Koha/REST/Plugin/Objects.pm b/Koha/REST/Plugin/Objects.pm index 0f08a2d37a..9be577515e 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'); @@ -138,7 +140,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 cbec228ce9..370b2aa81d 100755 --- a/t/db_dependent/Koha/REST/Plugin/Objects.t +++ b/t/db_dependent/Koha/REST/Plugin/Objects.t @@ -75,9 +75,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; @@ -423,4 +441,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.30.0