From 4bbb0645ab7cdf7f62e65760745deb5ac15b3c32 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 17 Nov 2021 14:30:11 -0300 Subject: [PATCH] [21.05.x] Bug 29506: Regression tests Signed-off-by: Tomas Cohen Arazi Signed-off-by: Martin Renvoize Signed-off-by: Nick Clemens --- t/db_dependent/Koha/REST/Plugin/Objects.t | 108 +++++++++++++++++++++- 1 file changed, 107 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Koha/REST/Plugin/Objects.t b/t/db_dependent/Koha/REST/Plugin/Objects.t index e85a57f78d..8deee9da7d 100755 --- a/t/db_dependent/Koha/REST/Plugin/Objects.t +++ b/t/db_dependent/Koha/REST/Plugin/Objects.t @@ -20,6 +20,9 @@ use Modern::Perl; use Koha::Acquisition::Orders; use Koha::Cities; use Koha::Biblios; +use Koha::Patrons; + +use Mojo::JSON qw(encode_json); # Dummy app for testing the plugin use Mojolicious::Lite; @@ -79,8 +82,42 @@ 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 ] }, + { order_by => 'branchname' } + ); + my $libraries = $c->objects->search( $libraries_rs ); + + $c->render( + status => 200, + json => $libraries + ); +}; + +get '/my_patrons' => sub { + + my $c = shift; + + my $patrons = $c->objects->search( scalar Koha::Patrons->search( {}, { order_by => 'borrowernumber' }) ); + + $c->render( + status => 200, + json => $patrons + ); +}; + # The tests -use Test::More tests => 12; +use Test::More tests => 14; use Test::Mojo; use t::lib::Mocks; @@ -512,3 +549,72 @@ 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', value => { branchname => 'A' } }); + my $library_2 = $builder->build_object({ class => 'Koha::Libraries', value => { branchname => 'B' } }); + + 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; +}; + +subtest 'objects.search helper, search_limited() tests' => sub { + + plan tests => 9; + + $schema->storage->txn_begin; + + my $library_1 = $builder->build_object({ class => 'Koha::Libraries' }); + my $library_2 = $builder->build_object({ class => 'Koha::Libraries' }); + + my $patron_1 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $library_1->id } }); + my $patron_2 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $library_1->id } }); + my $patron_3 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $library_2->id } }); + + my @libraries_where_can_see_patrons = ( $library_1->id, $library_2->id ); + + my $t = Test::Mojo->new; + + my $mocked_patron = Test::MockModule->new('Koha::Patron'); + $mocked_patron->mock( 'libraries_where_can_see_patrons', sub + { + return @libraries_where_can_see_patrons; + } + ); + + my $patron = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 2**4 } # borrowers flag = 4 + } + ); + + t::lib::Mocks::mock_userenv({ patron => $patron }); + + $t->get_ok( "/my_patrons?q=" . encode_json( { library_id => [ $library_1->id, $library_2->id ] } ) ) + ->status_is(200) + ->json_is( '/0/patron_id' => $patron_1->id ) + ->json_is( '/1/patron_id' => $patron_2->id ) + ->json_is( '/2/patron_id' => $patron_3->id ); + + @libraries_where_can_see_patrons = ( $library_2->id ); + + my $res = $t->get_ok( "/my_patrons?q=" . encode_json( { library_id => [ $library_1->id, $library_2->id ] } ) ) + ->status_is(200) + ->json_is( '/0/patron_id' => $patron_3->id, 'Returns the only allowed patron' ) + ->tx->res->json; + + is( scalar @{$res}, 1, 'Only one patron returned' ); + + $schema->storage->txn_rollback; +}; -- 2.32.0