From 6e83fdd451c6fe1b586e361ba2b859f5f553e8d7 Mon Sep 17 00:00:00 2001 From: Scott Walsh Date: Fri, 1 Mar 2024 17:22:29 -0400 Subject: [PATCH] Switch tests to caplog --- test/unit/test_auth_webbrowser.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) mode change 100644 => 100755 test/unit/test_auth_webbrowser.py diff --git a/test/unit/test_auth_webbrowser.py b/test/unit/test_auth_webbrowser.py old mode 100644 new mode 100755 index 8a138d8f9..58970cfba --- a/test/unit/test_auth_webbrowser.py +++ b/test/unit/test_auth_webbrowser.py @@ -6,6 +6,7 @@ from __future__ import annotations import base64 +import logging import socket from unittest import mock from unittest.mock import MagicMock, Mock, PropertyMock, patch @@ -248,7 +249,7 @@ def test_auth_webbrowser_post(_, disable_console_login): ) @patch("secrets.token_bytes", return_value=PROOF_KEY) def test_auth_webbrowser_fail_webbrowser( - _, capsys, input_text, expected_error, disable_console_login + _, caplog, input_text, expected_error, disable_console_login ): """Authentication by WebBrowser with failed to start web browser case.""" rest = _init_rest( @@ -265,6 +266,9 @@ def test_auth_webbrowser_fail_webbrowser( mock_webbrowser = MagicMock() mock_webbrowser.open_new.return_value = False + # capture the logging output + caplog.set_level(logging.INFO) + auth = AuthByWebBrowser( application=APPLICATION, webbrowser_pkg=mock_webbrowser, @@ -279,15 +283,16 @@ def test_auth_webbrowser_fail_webbrowser( user=USER, password=PASSWORD, ) - captured = capsys.readouterr() - assert captured.out == ( + print(f"this is the caplog.text {caplog.messages}") + assert ( "Initiating login request with your identity provider. A browser window " "should have opened for you to complete the login. If you can't see it, " "check existing browser windows, or your OS settings. Press CTRL+C to " f"abort and try again...\nGoing to open: {REF_SSO_URL if disable_console_login else REF_CONSOLE_LOGIN_SSO_URL} to authenticate...\nWe were unable to open a browser window for " "you, please open the url above manually then paste the URL you " "are redirected to into the terminal.\n" - ) + ) in caplog.text + if expected_error: assert rest._connection.errorhandler.called # an error assert auth.assertion_content is None @@ -303,8 +308,10 @@ def test_auth_webbrowser_fail_webbrowser( @pytest.mark.parametrize("disable_console_login", [True, False]) @patch("secrets.token_bytes", return_value=PROOF_KEY) -def test_auth_webbrowser_fail_webserver(_, capsys, disable_console_login): +def test_auth_webbrowser_fail_webserver(_, caplog, disable_console_login): """Authentication by WebBrowser with failed to start web browser case.""" + # capture the logging output + caplog.set_level(logging.INFO) rest = _init_rest( REF_SSO_URL, REF_PROOF_KEY, disable_console_login=disable_console_login ) @@ -324,6 +331,7 @@ def test_auth_webbrowser_fail_webserver(_, capsys, disable_console_login): with mock.patch( "select.select", return_value=([mock_socket_pkg.return_value], [], []) ): + # case 1: invalid HTTP request auth = AuthByWebBrowser( application=APPLICATION, @@ -338,13 +346,14 @@ def test_auth_webbrowser_fail_webserver(_, capsys, disable_console_login): user=USER, password=PASSWORD, ) - captured = capsys.readouterr() - assert captured.out == ( + print(f"this is the caplog.text {caplog.messages}") + assert ( "Initiating login request with your identity provider. A browser window " "should have opened for you to complete the login. If you can't see it, " "check existing browser windows, or your OS settings. Press CTRL+C to " f"abort and try again...\nGoing to open: {REF_SSO_URL if disable_console_login else REF_CONSOLE_LOGIN_SSO_URL} to authenticate...\n" - ) + ) in caplog.text + assert rest._connection.errorhandler.called # an error assert auth.assertion_content is None