Browse Source

Bug 28948: Teach objects.search about public requests

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
21.11.x
Tomas Cohen Arazi 1 year ago
committed by Jonathan Druart
parent
commit
7647d51478
  1. 4
      Koha/REST/Plugin/Objects.pm
  2. 39
      t/db_dependent/Koha/REST/Plugin/Objects.t

4
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 });
}
);
}

39
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;
};

Loading…
Cancel
Save