From 7647d5147861ad3b0c83ea9fabeba6ad837b9f0c Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Thu, 7 Jan 2021 13:18:27 -0300 Subject: [PATCH] Bug 28948: Teach objects.search about public requests Signed-off-by: Martin Renvoize Signed-off-by: Kyle M Hall Signed-off-by: Jonathan Druart --- Koha/REST/Plugin/Objects.pm | 4 ++- t/db_dependent/Koha/REST/Plugin/Objects.t | 39 ++++++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/Koha/REST/Plugin/Objects.pm b/Koha/REST/Plugin/Objects.pm index 7ccc1c4cf4..ddf2194530 100644 --- a/Koha/REST/Plugin/Objects.pm +++ b/Koha/REST/Plugin/Objects.pm @@ -91,6 +91,8 @@ for API rendering. # 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'); @@ -162,7 +164,7 @@ for API rendering. } ); - 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 e85a57f78d..47a953a834 100755 --- a/t/db_dependent/Koha/REST/Plugin/Objects.t +++ b/t/db_dependent/Koha/REST/Plugin/Objects.t @@ -79,8 +79,27 @@ get '/biblios' => sub { $c->render( status => 200, json => {count => scalar(@$biblios), biblios => $biblios} ); }; +get '/libraries/:library_id_1/:library_id_2' => sub { + + my $c = shift; + + # Emulate a public route by stashing the is_public value + $c->stash( 'is_public' => 1 ); + + my $library_id_1 = $c->param('library_id_1'); + my $library_id_2 = $c->param('library_id_2'); + + my $libraries_rs = Koha::Libraries->search({ branchcode => [ $library_id_1, $library_id_2 ] }); + my $libraries = $c->objects->search( $libraries_rs ); + + $c->render( + status => 200, + json => $libraries + ); +}; + # The tests -use Test::More tests => 12; +use Test::More tests => 13; use Test::Mojo; use t::lib::Mocks; @@ -512,3 +531,21 @@ subtest 'objects.find helper, embed' => sub { $schema->storage->txn_rollback; }; + +subtest 'objects.search helper, public requests' => sub { + + plan tests => 3; + + $schema->storage->txn_begin; + + my $library_1 = $builder->build_object({ class => 'Koha::Libraries' }); + my $library_2 = $builder->build_object({ class => 'Koha::Libraries' }); + + my $t = Test::Mojo->new; + + $t->get_ok( '/libraries/'.$library_1->id.'/'.$library_2->id ) + ->json_is('/0' => $library_1->to_api({ public => 1 }), 'Public representation of $library_1 is retrieved') + ->json_is('/1' => $library_2->to_api({ public => 1 }), 'Public representation of $library_2 is retrieved'); + + $schema->storage->txn_rollback; +}; -- 2.39.5