From 6d1064d835aa4833656f0b0f1e22d93369bbbf78 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 20 Apr 2021 14:23:30 -0300 Subject: [PATCH] Bug 28157: Regression tests --- .../api/v1/auth_authenticate_api_request.t | 68 ++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/api/v1/auth_authenticate_api_request.t b/t/db_dependent/api/v1/auth_authenticate_api_request.t index 5bb68d25700..dddf93ca4a6 100755 --- a/t/db_dependent/api/v1/auth_authenticate_api_request.t +++ b/t/db_dependent/api/v1/auth_authenticate_api_request.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 3; +use Test::More tests => 4; use Test::Mojo; use Module::Load::Conditional qw(can_load); @@ -192,6 +192,72 @@ subtest 'anonymous requests to public API' => sub { $schema->storage->txn_rollback; }; +subtest 'x-koha-library tests' => sub { + + plan tests => 10; + + $schema->storage->txn_begin; + + my $stash; + my $userenv; + + $t->app->hook(after_dispatch => sub { + $stash = shift->stash; + $userenv = C4::Context->userenv; + }); + + t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); + my $superlibrarian = $builder->build_object({ + class => 'Koha::Patrons', + value => { flags => 1 } + }); + my $password = 'thePassword123'; + $superlibrarian->set_password({ password => $password, skip_validation => 1 }); + my $superlibrarian_userid = $superlibrarian->userid; + + my $unprivileged = $builder->build_object({ + class => 'Koha::Patrons', + value => { flags => undef } + }); + $unprivileged->set_password({ password => $password, skip_validation => 1 }); + my $unprivileged_userid = $unprivileged->userid; + + my $library = $builder->build_object( { class => 'Koha::Libraries' } ); + + ## Independent branches tests + t::lib::Mocks::mock_preference('IndependentBranches', 1); + + $t->get_ok( + "//$unprivileged_userid:$password@/api/v1/cities", + { 'x-koha-library' => $unprivileged->branchcode } + ); + + is( $userenv->{branch}, $unprivileged->branchcode, 'branch set correctly' ); + + $t->get_ok( "//$unprivileged_userid:$password@/api/v1/cities" => + { 'x-koha-library' => $library->id } )->status_is(403) + ->json_is( + '/error' => 'Unauthorized attempt to set library to ' . $library->id ); + + $t->get_ok( "//$superlibrarian_userid:$password@/api/v1/cities" => + { 'x-koha-library' => $library->id } )->status_is(200); + + is( $userenv->{branch}, $library->id, 'branch set correctly' ); + + ## !Independent branches tests + t::lib::Mocks::mock_preference('IndependentBranches', 1); + $t->get_ok( + "//$unprivileged_userid:$password@/api/v1/cities", + { 'x-koha-library' => $unprivileged->branchcode } + ); + $t->get_ok( + "//$unprivileged_userid:$password@/api/v1/cities", + { 'x-koha-library' => $library->id } + ); + + $schema->storage->txn_rollback; +}; + sub create_user_and_session { my $args = shift; -- 2.31.1