Improve error handling in entry edit mode and update sudo request tests for clarity and accuracy
This commit is contained in:
		
							parent
							
								
									0041ded402
								
							
						
					
					
						commit
						8b8c02c6da
					
				
					 3 changed files with 30 additions and 12 deletions
				
			
		| 
						 | 
					@ -402,8 +402,12 @@ class HostsManagerApp(App):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def watch_entry_edit_mode(self, entry_edit_mode: bool) -> None:
 | 
					    def watch_entry_edit_mode(self, entry_edit_mode: bool) -> None:
 | 
				
			||||||
        """Update the right pane border title when entry edit mode changes."""
 | 
					        """Update the right pane border title when entry edit mode changes."""
 | 
				
			||||||
 | 
					        try:
 | 
				
			||||||
            right_pane = self.query_one(".right-pane")
 | 
					            right_pane = self.query_one(".right-pane")
 | 
				
			||||||
            if entry_edit_mode:
 | 
					            if entry_edit_mode:
 | 
				
			||||||
                right_pane.border_title = "Edit Entry"
 | 
					                right_pane.border_title = "Edit Entry"
 | 
				
			||||||
            else:
 | 
					            else:
 | 
				
			||||||
                right_pane.border_title = "Entry Details"
 | 
					                right_pane.border_title = "Entry Details"
 | 
				
			||||||
 | 
					        except Exception:
 | 
				
			||||||
 | 
					            # App not fully initialized yet, ignore
 | 
				
			||||||
 | 
					            pass
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -44,14 +44,14 @@ class TestPermissionManager:
 | 
				
			||||||
    @patch("subprocess.run")
 | 
					    @patch("subprocess.run")
 | 
				
			||||||
    def test_request_sudo_prompt_success(self, mock_run):
 | 
					    def test_request_sudo_prompt_success(self, mock_run):
 | 
				
			||||||
        """Test requesting sudo with password prompt success."""
 | 
					        """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_run.side_effect = [
 | 
				
			||||||
            Mock(returncode=1),  # sudo -n true fails
 | 
					            Mock(returncode=1),  # sudo -n true fails
 | 
				
			||||||
            Mock(returncode=0),  # sudo -v succeeds
 | 
					            Mock(returncode=0),  # sudo -S -v succeeds
 | 
				
			||||||
        ]
 | 
					        ]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        pm = PermissionManager()
 | 
					        pm = PermissionManager()
 | 
				
			||||||
        success, message = pm.request_sudo()
 | 
					        success, message = pm.request_sudo("testpassword")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        assert success
 | 
					        assert success
 | 
				
			||||||
        assert "access granted" in message
 | 
					        assert "access granted" in message
 | 
				
			||||||
| 
						 | 
					@ -60,17 +60,31 @@ class TestPermissionManager:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        assert mock_run.call_count == 2
 | 
					        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")
 | 
					    @patch("subprocess.run")
 | 
				
			||||||
    def test_request_sudo_denied(self, mock_run):
 | 
					    def test_request_sudo_denied(self, mock_run):
 | 
				
			||||||
        """Test requesting sudo when access is denied."""
 | 
					        """Test requesting sudo when access is denied."""
 | 
				
			||||||
        # Both calls fail
 | 
					        # Both calls fail
 | 
				
			||||||
        mock_run.side_effect = [
 | 
					        mock_run.side_effect = [
 | 
				
			||||||
            Mock(returncode=1),  # sudo -n true fails
 | 
					            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()
 | 
					        pm = PermissionManager()
 | 
				
			||||||
        success, message = pm.request_sudo()
 | 
					        success, message = pm.request_sudo("testpassword")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        assert not success
 | 
					        assert not success
 | 
				
			||||||
        assert "denied" in message
 | 
					        assert "denied" in message
 | 
				
			||||||
| 
						 | 
					@ -234,7 +248,7 @@ class TestHostsManager:
 | 
				
			||||||
            success, message = manager.enter_edit_mode()
 | 
					            success, message = manager.enter_edit_mode()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            assert not success
 | 
					            assert not success
 | 
				
			||||||
            assert "Cannot enter edit mode" in message
 | 
					            assert message == "Denied"
 | 
				
			||||||
            assert not manager.edit_mode
 | 
					            assert not manager.edit_mode
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def test_enter_edit_mode_permission_validation_failure(self):
 | 
					    def test_enter_edit_mode_permission_validation_failure(self):
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue