Improve error handling in entry edit mode and update sudo request tests for clarity and accuracy

This commit is contained in:
Philip Henning 2025-08-14 18:01:20 +02:00
parent 0041ded402
commit 8b8c02c6da
3 changed files with 30 additions and 12 deletions

View file

@ -402,8 +402,12 @@ class HostsManagerApp(App):
def watch_entry_edit_mode(self, entry_edit_mode: bool) -> None:
"""Update the right pane border title when entry edit mode changes."""
right_pane = self.query_one(".right-pane")
if entry_edit_mode:
right_pane.border_title = "Edit Entry"
else:
right_pane.border_title = "Entry Details"
try:
right_pane = self.query_one(".right-pane")
if entry_edit_mode:
right_pane.border_title = "Edit Entry"
else:
right_pane.border_title = "Entry Details"
except Exception:
# App not fully initialized yet, ignore
pass

View file

@ -113,7 +113,7 @@ class PasswordModal(ModalScreen):
self.action_submit()
elif event.button.id == "cancel-button":
self.action_cancel()
def on_input_submitted(self, event: Input.Submitted) -> None:
"""Handle Enter key in password input field."""
if event.input.id == "password-input":

View file

@ -44,14 +44,14 @@ class TestPermissionManager:
@patch("subprocess.run")
def test_request_sudo_prompt_success(self, mock_run):
"""Test requesting sudo with password prompt success."""
# First call (sudo -n true) fails, second call (sudo -v) succeeds
# First call (sudo -n true) fails, second call (sudo -S -v) succeeds
mock_run.side_effect = [
Mock(returncode=1), # sudo -n true fails
Mock(returncode=0), # sudo -v succeeds
Mock(returncode=0), # sudo -S -v succeeds
]
pm = PermissionManager()
success, message = pm.request_sudo()
success, message = pm.request_sudo("testpassword")
assert success
assert "access granted" in message
@ -60,17 +60,31 @@ class TestPermissionManager:
assert mock_run.call_count == 2
@patch("subprocess.run")
def test_request_sudo_no_password(self, mock_run):
"""Test requesting sudo when no password is provided."""
# sudo -n true fails (password needed)
mock_run.return_value = Mock(returncode=1)
pm = PermissionManager()
success, message = pm.request_sudo()
assert not success
assert "Password required" in message
assert not pm.has_sudo
assert not pm._sudo_validated
@patch("subprocess.run")
def test_request_sudo_denied(self, mock_run):
"""Test requesting sudo when access is denied."""
# Both calls fail
mock_run.side_effect = [
Mock(returncode=1), # sudo -n true fails
Mock(returncode=1), # sudo -v fails
Mock(returncode=1, stderr="access denied"), # sudo -S -v fails
]
pm = PermissionManager()
success, message = pm.request_sudo()
success, message = pm.request_sudo("testpassword")
assert not success
assert "denied" in message
@ -234,7 +248,7 @@ class TestHostsManager:
success, message = manager.enter_edit_mode()
assert not success
assert "Cannot enter edit mode" in message
assert message == "Denied"
assert not manager.edit_mode
def test_enter_edit_mode_permission_validation_failure(self):