Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle other exceptions in the popup window #11

Open
wants to merge 3 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/pyoncatqt/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 25 additions & 4 deletions tests/test_login_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
Loading