From cad562bb425cc154c05763665712d4e6b91ea6bc Mon Sep 17 00:00:00 2001 From: Ioannis Bonatakis Date: Fri, 27 Sep 2024 09:59:32 +0200 Subject: [PATCH] Extend test coverage of OpenID.pm Cover setup_needed handling of OpenID respond. Signed-off-by: Ioannis Bonatakis --- lib/OpenQA/WebAPI/Auth/OpenID.pm | 4 ++-- t/03-auth-openid.t | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/OpenQA/WebAPI/Auth/OpenID.pm b/lib/OpenQA/WebAPI/Auth/OpenID.pm index 6247b061cfa1..f05b7154fa21 100644 --- a/lib/OpenQA/WebAPI/Auth/OpenID.pm +++ b/lib/OpenQA/WebAPI/Auth/OpenID.pm @@ -4,7 +4,7 @@ package OpenQA::WebAPI::Auth::OpenID; use Mojo::Base -base, -signatures; -use OpenQA::Log qw(log_error log_warning); +use OpenQA::Log qw(log_error log_warning log_debug); use LWP::UserAgent; use Net::OpenID::Consumer; use MIME::Base64 qw(encode_base64url decode_base64url); @@ -116,7 +116,7 @@ sub auth_response ($c) { setup_needed => sub ($setup_url) { # Redirect the user to $setup_url $setup_url = URI::Escape::uri_unescape($setup_url); - $c->app->log->debug(qq{setup_url[$setup_url]}); + log_debug(qq{setup_url[$setup_url]}); return (redirect => $setup_url, error => 0); }, diff --git a/t/03-auth-openid.t b/t/03-auth-openid.t index 12c16c28706a..f82c917a3a3b 100644 --- a/t/03-auth-openid.t +++ b/t/03-auth-openid.t @@ -38,4 +38,22 @@ stderr_like { } qr/Invalid OpenID/, 'warning about invalid OpenID is displayed'; +$c->set_always( + req => Test::MockObject->new->set_always(params => Test::MockObject->new->set_always(pairs => [1, 2])) + ->set_always(url => Test::MockObject->new->set_always(base => 'openqa')) + ->set_always(log => Test::MockObject->new->set_true('error', 'debug'))->set_true('flash')); +my $mock_openid_consumer = Test::MockModule->new('Net::OpenID::Consumer'); +$mock_openid_consumer->redefine('handle_server_response', sub { + my ($self, %res_handlers) = @_; + if ($res_handlers{setup_needed}) { + return $res_handlers{setup_needed}->("https://www.opensuse.org/openid/setup"); + } +}); + +stdout_like { + my $ret = OpenQA::WebAPI::Auth::OpenID::auth_response($c); + is_deeply($ret, 0, 'can handle setup_needed response'); +} +qr/setup_url\[https:\/\/www.opensuse.org\/openid\/setup\]/, 'debug log is displayed when setup_needed'; + done_testing;