Skip to content

Commit

Permalink
Switch tests to caplog
Browse files Browse the repository at this point in the history
  • Loading branch information
invisiblethreat committed Mar 1, 2024
1 parent be22037 commit 6e83fdd
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions test/unit/test_auth_webbrowser.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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
)
Expand All @@ -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,
Expand All @@ -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

Expand Down

0 comments on commit 6e83fdd

Please sign in to comment.