From 871e4a0491cca921b9661157f7b58fd9de3cea89 Mon Sep 17 00:00:00 2001 From: Maryse Simard Date: Mon, 5 Feb 2024 20:33:57 -0500 Subject: [PATCH] Bug 34755: Unit tests --- t/db_dependent/api/v1/idp.t | 68 ++++++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 27 deletions(-) diff --git a/t/db_dependent/api/v1/idp.t b/t/db_dependent/api/v1/idp.t index c67b5124e0..eceb054819 100755 --- a/t/db_dependent/api/v1/idp.t +++ b/t/db_dependent/api/v1/idp.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 2; +use Test::More tests => 3; use Test::Mojo; use Test::Warn; use Mojo::JWT; @@ -263,43 +263,57 @@ subtest 'domain endpoint tests' => sub { $schema->storage->txn_rollback; }; -# subtest 'oauth login tests' => sub { -# plan tests => 4; +subtest 'oauth login tests' => sub { + plan tests => 6; -# $schema->storage->txn_begin; + $schema->storage->txn_begin; + + Koha::Auth::Identity::Provider::Domains->delete; + Koha::Auth::Identity::Providers->delete; + + my ( $borrowernumber, $session_id ) = create_user_and_session({ authorized => 1 }); + + my $t = Test::Mojo->new('Koha::REST::V1'); + + # Build provider + my $tx = $t->ua->build_tx( POST => "/api/v1/auth/identity_providers", json => $oauth_provider_data ); + $tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); + $tx->req->env( { REMOTE_ADDR => $remote_address } ); -# Koha::Auth::Identity::Provider::Domains->delete; -# Koha::Auth::Identity::Providers->delete; + $t->request_ok($tx); + my $provider_id = $t->tx->res->json->{identity_provider_id}; -# my ( $borrowernumber, $session_id ) = create_user_and_session({ authorized => 1 }); + # Build domain + $tx = $t->ua->build_tx( POST => "/api/v1/auth/identity_providers/$provider_id/domains", json => $domain_not_matching ); + $tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); + $tx->req->env( { REMOTE_ADDR => $remote_address } ); -# my $t = Test::Mojo->new('Koha::REST::V1'); + $t->request_ok($tx); -# # Build provider -# my $tx = $t->ua->build_tx( POST => "/api/v1/auth/identity_providers", json => $oauth_provider_data ); -# $tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); -# $tx->req->env( { REMOTE_ADDR => $remote_address } ); + t::lib::Mocks::mock_preference( 'RESTPublicAPI', 1 ); -# $t->request_ok($tx); -# my $provider_id = $t->tx->res->json->{identity_provider_id}; + # Simulate server restart + $t = Test::Mojo->new('Koha::REST::V1'); -# # Build domain -# $tx = $t->ua->build_tx( POST => "/api/v1/auth/identity_providers/$provider_id/domains", json => $domain_not_matching ); -# $tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); -# $tx->req->env( { REMOTE_ADDR => $remote_address } ); + $tx = $t->ua->build_tx(GET => "/api/v1/public/oauth/login/oauth_test/opac"); + $tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); + $t->request_ok($tx) + ->status_is(302); -# $t->request_ok($tx); + my $state = URI->new($t->tx->res->headers->location)->query_param("state"); -# t::lib::Mocks::mock_preference( 'RESTPublicAPI', 1 ); + C4::Context->_new_userenv('DUMMY SESSION'); + C4::Context->set_userenv(0,42,0,'firstname','surname', 'CPL', 'Library 1', 0, ''); -# # Simulate server restart -# $t = Test::Mojo->new('Koha::REST::V1'); + $tx = $t->ua->build_tx(GET => "/api/v1/public/oauth/login/oauth_test/opac?code=code&state=$state"); + $tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); + $t->request_ok($tx); + + my $error = URI->new($t->tx->res->headers->location)->query_param("auth_error"); + isnt($error, "wrong_csrf_token", "CRSF token is recognized even when another user is logged in."); -# #$t->ua->max_redirects(10); -# $t->get_ok("/api/v1/public/oauth/login/oauth_test/opac") -# ->status_is(302); -# $schema->storage->txn_rollback; -# }; + $schema->storage->txn_rollback; +}; sub create_user_and_session { -- 2.34.1