Test the get_msf_console context manager.
| 209 | |
| 210 | |
| 211 | class TestGetMsfConsole: |
| 212 | """Test the get_msf_console context manager.""" |
| 213 | |
| 214 | @pytest.fixture |
| 215 | def mock_client(self): |
| 216 | """Fixture providing a mock MSF client.""" |
| 217 | client = Mock() |
| 218 | with patch('MetasploitMCP.get_msf_client', return_value=client): |
| 219 | yield client |
| 220 | |
| 221 | @pytest.mark.asyncio |
| 222 | async def test_get_msf_console_success(self, mock_client): |
| 223 | """Test successful console creation and cleanup.""" |
| 224 | mock_console = MockMsfConsole('test-console-123') |
| 225 | mock_client.consoles.console.return_value = mock_console |
| 226 | mock_client.consoles.destroy.return_value = 'destroyed' |
| 227 | |
| 228 | # Mock the global client instance for cleanup |
| 229 | with patch('MetasploitMCP._msf_client_instance', mock_client): |
| 230 | async with get_msf_console() as console: |
| 231 | assert console is mock_console |
| 232 | assert console.cid == 'test-console-123' |
| 233 | |
| 234 | # Verify cleanup was called |
| 235 | mock_client.consoles.destroy.assert_called_once_with('test-console-123') |
| 236 | |
| 237 | @pytest.mark.asyncio |
| 238 | async def test_get_msf_console_creation_error(self, mock_client): |
| 239 | """Test console creation error handling.""" |
| 240 | mock_client.consoles.console.side_effect = MockMsfRpcError("Console creation failed") |
| 241 | |
| 242 | with pytest.raises(MockMsfRpcError, match="Console creation failed"): |
| 243 | async with get_msf_console() as console: |
| 244 | pass |
| 245 | |
| 246 | @pytest.mark.asyncio |
| 247 | async def test_get_msf_console_cleanup_error(self, mock_client): |
| 248 | """Test that cleanup errors don't propagate.""" |
| 249 | mock_console = MockMsfConsole('test-console-123') |
| 250 | mock_client.consoles.console.return_value = mock_console |
| 251 | mock_client.consoles.destroy.side_effect = Exception("Cleanup failed") |
| 252 | |
| 253 | # Should not raise exception even if cleanup fails |
| 254 | async with get_msf_console() as console: |
| 255 | assert console is mock_console |
| 256 | |
| 257 | |
| 258 | class TestRunCommandSafely: |
nothing calls this directly
no outgoing calls
no test coverage detected