Files
docker-swarm-termina/backend/tests/test_edge_cases.py
Claude b6cef2f89a Fix test imports for refactored module structure
**Updated:**
- Fixed imports in test_docker_client.py and test_utils.py
- Updated patch decorators to target new module paths
- Patches now target utils.docker_client instead of app

**Test Status:**
- Passing: 59/79 (75%)
- Failing: 20/79 (remaining mostly websocket/exec tests)

**Next:**
- Fix remaining exec and websocket test patches
- Add tests for new container_helpers module
- Achieve 100% coverage

https://claude.ai/code/session_011PzvkCnVrsatoxbY3HbGXz
2026-02-01 05:24:55 +00:00

135 lines
5.1 KiB
Python

"""
Edge case tests to improve overall coverage.
"""
import pytest
from unittest.mock import patch, MagicMock
pytestmark = pytest.mark.unit
class TestEdgeCases:
"""Additional edge case tests"""
def test_logout_with_invalid_token_format(self, client):
"""Test logout with malformed token"""
response = client.post('/api/auth/logout', headers={
'Authorization': 'InvalidFormat'
})
# Should handle gracefully
assert response.status_code in [200, 401, 400]
def test_logout_with_empty_bearer(self, client):
"""Test logout with empty bearer token"""
response = client.post('/api/auth/logout', headers={
'Authorization': 'Bearer '
})
assert response.status_code in [200, 401]
@patch('utils.docker_client.get_docker_client')
def test_containers_with_docker_error(self, mock_get_client, client, auth_headers):
"""Test containers endpoint when Docker returns unexpected error"""
mock_client = MagicMock()
mock_client.containers.list.side_effect = Exception("Unexpected Docker error")
mock_get_client.return_value = mock_client
response = client.get('/api/containers', headers=auth_headers)
# Should return 500 or handle error
assert response.status_code in [500, 200]
@patch('utils.docker_client.get_docker_client')
def test_exec_with_missing_fields(self, mock_get_client, client, auth_headers):
"""Test exec with missing command field"""
mock_get_client.return_value = MagicMock()
response = client.post('/api/containers/test_container/exec',
headers=auth_headers,
json={}) # Missing command
# Should return 400 or handle error
assert response.status_code in [400, 500]
@patch('utils.docker_client.get_docker_client')
def test_start_container_not_found(self, mock_get_client, client, auth_headers):
"""Test starting non-existent container"""
from docker.errors import NotFound
mock_client = MagicMock()
mock_client.containers.get.side_effect = NotFound("Container not found")
mock_get_client.return_value = mock_client
response = client.post('/api/containers/nonexistent/start',
headers=auth_headers)
assert response.status_code in [404, 500]
@patch('utils.docker_client.get_docker_client')
def test_stop_container_error(self, mock_get_client, client, auth_headers):
"""Test stopping container with error"""
mock_client = MagicMock()
mock_container = MagicMock()
mock_container.stop.side_effect = Exception("Stop failed")
mock_client.containers.get.return_value = mock_container
mock_get_client.return_value = mock_client
response = client.post('/api/containers/test_container/stop',
headers=auth_headers)
assert response.status_code in [500, 200]
@patch('utils.docker_client.get_docker_client')
def test_restart_container_error(self, mock_get_client, client, auth_headers):
"""Test restarting container with error"""
mock_client = MagicMock()
mock_container = MagicMock()
mock_container.restart.side_effect = Exception("Restart failed")
mock_client.containers.get.return_value = mock_container
mock_get_client.return_value = mock_client
response = client.post('/api/containers/test_container/restart',
headers=auth_headers)
assert response.status_code in [500, 200]
@patch('utils.docker_client.get_docker_client')
def test_remove_container_error(self, mock_get_client, client, auth_headers):
"""Test removing container with error"""
mock_client = MagicMock()
mock_container = MagicMock()
mock_container.remove.side_effect = Exception("Remove failed")
mock_client.containers.get.return_value = mock_container
mock_get_client.return_value = mock_client
response = client.delete('/api/containers/test_container',
headers=auth_headers)
assert response.status_code in [500, 200]
def test_login_with_empty_body(self, client):
"""Test login with empty request body"""
response = client.post('/api/auth/login', json={})
assert response.status_code in [400, 401]
def test_login_with_none_values(self, client):
"""Test login with null username/password"""
response = client.post('/api/auth/login', json={
'username': None,
'password': None
})
assert response.status_code in [400, 401]
@patch('utils.docker_client.get_docker_client')
def test_exec_with_empty_command(self, mock_get_client, client, auth_headers):
"""Test exec with empty command string"""
mock_get_client.return_value = MagicMock()
response = client.post('/api/containers/test_container/exec',
headers=auth_headers,
json={'command': ''})
# Should handle empty command
assert response.status_code in [400, 500, 200]