From e63d5e936ce5c753130468a765eca5e50559a508 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Fri, 5 Jun 2020 09:15:19 -0300 Subject: [PATCH] Bug 24003: Regression tests This patch adds regression tests for the different authentication mechanisms Koha supports. It highlights the fact that Koha expects userenv to be set on authentication. Signed-off-by: Tomas Cohen Arazi --- .../api/v1/auth_authenticate_api_request.t | 35 +++++++++++++++++-- t/db_dependent/api/v1/auth_basic.t | 14 ++++++-- 2 files changed, 44 insertions(+), 5 deletions(-) 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 de8578bfe05..5734b14da47 100755 --- a/t/db_dependent/api/v1/auth_authenticate_api_request.t +++ b/t/db_dependent/api/v1/auth_authenticate_api_request.t @@ -29,9 +29,9 @@ use Koha::Patrons; use t::lib::Mocks; use t::lib::TestBuilder; -my $t = Test::Mojo->new('Koha::REST::V1'); my $schema = Koha::Database->new->schema; my $builder = t::lib::TestBuilder->new(); +my $t = Test::Mojo->new('Koha::REST::V1'); my $remote_address = '127.0.0.1'; my $tx; @@ -44,7 +44,7 @@ t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' ); subtest 'token-based tests' => sub { if ( can_load( modules => { 'Net::OAuth2::AuthorizationServer' => undef } ) ) { - plan tests => 12; + plan tests => 14; } else { plan skip_all => 'Net::OAuth2::AuthorizationServer not available'; @@ -95,12 +95,26 @@ subtest 'token-based tests' => sub { ok( defined $embed, 'The embed hashref is generated and stashed' ); is_deeply( $embed, { fund => {} }, 'The embed data structure is correct' ); + my $mock = Test::MockModule->new('Koha::REST::V1::Patrons'); + $mock->mock( list => sub { + my $c = shift; + return $c->render( + status => 200, + json => { userenv => C4::Context->userenv() } + ); + }); + + $tx = $t->ua->build_tx(GET => '/api/v1/patrons'); + $tx->req->headers->authorization("Bearer $access_token"); + $t->request_ok($tx) + ->json_is('/userenv/number' => $patron->borrowernumber ); + $schema->storage->txn_rollback; }; subtest 'cookie-based tests' => sub { - plan tests => 6; + plan tests => 8; $schema->storage->txn_begin; @@ -119,6 +133,21 @@ subtest 'cookie-based tests' => sub { is( ref($user), 'Koha::Patron', 'Stashed koha.user object type is Koha::Patron') and is( $user->borrowernumber, $borrowernumber, 'The stashed user is the right one' ); + my $mock = Test::MockModule->new('Koha::REST::V1::Patrons'); + $mock->mock( list => sub { + my $c = shift; + return $c->render( + status => 200, + json => { userenv => C4::Context->userenv() } + ); + }); + + $tx = $t->ua->build_tx(GET => '/api/v1/patrons'); + $tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); + $tx->req->env( { REMOTE_ADDR => $remote_address } ); + $t->request_ok($tx) + ->json_is('/userenv/number' => $borrowernumber ); + subtest 'logged-out tests' => sub { plan tests => 3; diff --git a/t/db_dependent/api/v1/auth_basic.t b/t/db_dependent/api/v1/auth_basic.t index ca5bb672144..1073bfd0bb0 100644 --- a/t/db_dependent/api/v1/auth_basic.t +++ b/t/db_dependent/api/v1/auth_basic.t @@ -30,7 +30,7 @@ my $t = Test::Mojo->new('Koha::REST::V1'); subtest 'success tests' => sub { - plan tests => 5; + plan tests => 6; $schema->storage->txn_begin; @@ -43,8 +43,18 @@ subtest 'success tests' => sub { $patron->set_password({ password => $password }); my $userid = $patron->userid; + my $mock = Test::MockModule->new('Koha::REST::V1::Patrons'); + $mock->mock( list => sub { + my $c = shift; + return $c->render( + status => 200, + json => { userenv => C4::Context->userenv() } + ); + }); + $t->get_ok("//$userid:$password@/api/v1/patrons") - ->status_is( 200, 'Successful authentication and permissions check' ); + ->status_is( 200, 'Successful authentication and permissions check' ) + ->json_is('/userenv/number' => $patron->borrowernumber ); $patron->flags(undef)->store; -- 2.27.0