From 88a0fbc21667c2a4b5d30847ab515b2665d784db Mon Sep 17 00:00:00 2001 From: Kyle Qianli Ma <41385391+KyleQianliMa@users.noreply.github.com> Date: Mon, 3 Mar 2025 09:35:26 -0500 Subject: [PATCH] handle other exceptions in the popup window (#11) --- src/pyoncatqt/login.py | 4 ++++ tests/test_login_dialog.py | 29 +++++++++++++++++++++++++---- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/pyoncatqt/login.py b/src/pyoncatqt/login.py index a17c0d6..3c2258c 100644 --- a/src/pyoncatqt/login.py +++ b/src/pyoncatqt/login.py @@ -138,6 +138,10 @@ def accept(self: QDialog) -> None: self.show_message("A username and/or password was not provided when logging in.") self.user_pwd.setText("") return + except Exception as e: # noqa: BLE001 + self.show_message(f"The following exception occured: {e}") + self.user_pwd.setText("") + return self.login_status.emit(True) # close dialog diff --git a/tests/test_login_dialog.py b/tests/test_login_dialog.py index 9767864..adc7919 100644 --- a/tests/test_login_dialog.py +++ b/tests/test_login_dialog.py @@ -168,8 +168,8 @@ def test_login_dialog_no_password(qtbot: pytest.fixture) -> None: assert dialog.user_pwd.text() == "" assert dialog.button_login.isEnabled() is False qtbot.mouseClick(dialog.button_login, QtCore.Qt.LeftButton) - assert mock_agent.login.called_once_with(os.getlogin(), "") - assert dialog.show_message.called_once_with("A username and/or password was not provided when logging in.") + mock_agent.call_count == 0 + assert dialog.show_message.call_count == 0 def test_login_dialog_bad_password(qtbot: pytest.fixture) -> None: @@ -183,8 +183,29 @@ def test_login_dialog_bad_password(qtbot: pytest.fixture) -> None: qtbot.wait(2000) assert dialog.user_pwd.text() == "bad_password" qtbot.mouseClick(dialog.button_login, QtCore.Qt.LeftButton) - assert mock_agent.login.called_once_with(os.getlogin(), "bad_password") - assert dialog.show_message.called_once_with("Invalid username or password. Please try again.") + mock_agent.login.assert_called_once_with(os.getlogin(), "bad_password") + dialog.show_message.assert_called_once_with("Invalid username or password. Please try again.") + + +def test_login_dialog_other_exceptions(qtbot: pytest.fixture) -> None: + import urllib3 + + mock_agent = MagicMock(spec=pyoncat.ONCat) + mock_agent.login.side_effect = urllib3.exceptions.NameResolutionError + dialog = ONCatLoginDialog(agent=mock_agent) + dialog.show_message = MagicMock() + qtbot.addWidget(dialog) + dialog.show() + qtbot.keyClicks(dialog.user_pwd, "bad_password") + qtbot.wait(2000) + assert dialog.user_pwd.text() == "bad_password" + qtbot.mouseClick(dialog.button_login, QtCore.Qt.LeftButton) + mock_agent.login.assert_called_once_with(os.getlogin(), "bad_password") + # This is the error message that crashed Shiver when there is no internet connection + dialog.show_message.assert_called_once_with( + "The following exception occured: NameResolutionError.__init__() missing \ +3 required positional arguments: 'host', 'conn', and 'reason'" + ) def test_login_dialog_no_agent(qtbot: pytest.fixture) -> None: