From c1151f2a3a5f3d551e5c93d106aa471a4c129f30 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Fri, 13 Mar 2020 11:44:03 -0300 Subject: [PATCH] Bug 24862: Regression tests This patch introduces tests for the expected behaviour on API routes that expect a logged in user, but the request is made with an anonymous session cookie. To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/db_dependent/api/v1/auth_authenticate_api_request.t => FAIL: Tests fail because the situation is not handled correctly in the code Signed-off-by: Tomas Cohen Arazi Signed-off-by: David Nind --- .../api/v1/auth_authenticate_api_request.t | 21 ++++++++++++++++++++- 1 file changed, 20 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 fbed3461a8..75348b90f5 100755 --- a/t/db_dependent/api/v1/auth_authenticate_api_request.t +++ b/t/db_dependent/api/v1/auth_authenticate_api_request.t @@ -100,7 +100,7 @@ subtest 'token-based tests' => sub { subtest 'cookie-based tests' => sub { - plan tests => 5; + plan tests => 6; $schema->storage->txn_begin; @@ -119,6 +119,25 @@ 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' ); + subtest 'logged-out tests' => sub { + plan tests => 3; + + # Generate an anonymous session + my $session = C4::Auth::get_session(''); + $session->param( 'ip', $remote_address ); + $session->param( 'lasttime', time() ); + $session->param( 'sessiontype', 'anon' ); + $session->flush; + + my $tx = $t->ua->build_tx( GET => '/api/v1/libraries' ); + $tx->req->cookies({ name => 'CGISESSID', value => $session->id }); + $tx->req->env({ REMOTE_ADDR => $remote_address }); + + $t->request_ok($tx) + ->status_is( 401, 'Anonymous session on permission protected resource returns 401' ) + ->json_is( { error => 'Authentication failure.' } ); + }; + $schema->storage->txn_rollback; }; -- 2.11.0