Files
docker-swarm-termina/backend/tests/test_auth.py
Claude 2a79d782be Refactor tests to use parameterized patterns and improve coverage
Frontend improvements:
- Refactor useSimpleTerminal tests with it.each for empty/whitespace commands
- Add test for missing workdir in API response (100% branch coverage)
- Refactor DashboardHeader tests to parameterize container count variations
- Refactor LoginForm tests to parameterize input field changes
- Refactor ContainerCard tests to parameterize status border colors
- Add TerminalModal tests for FallbackNotification and isMobile dimensions
- Total: 254 passing tests, 76.94% coverage

Backend improvements:
- Refactor auth tests with pytest.parametrize for missing/empty fields
- Refactor container action tests with pytest.parametrize for start/stop/restart
- Maintains 100% backend coverage across all modules
- Total: 120 passing tests, 100% coverage

Benefits of parameterized tests:
- Reduced code duplication
- Easier to add new test cases
- Better test coverage with less code
- More maintainable test suite

https://claude.ai/code/session_mmQs0
2026-02-01 16:14:17 +00:00

66 lines
2.2 KiB
Python

import pytest
from datetime import datetime
class TestAuthentication:
"""Test authentication endpoints"""
def test_login_success(self, client):
"""Test successful login"""
response = client.post('/api/auth/login', json={
'username': 'admin',
'password': 'admin123'
})
assert response.status_code == 200
data = response.get_json()
assert data['success'] is True
assert 'token' in data
assert data['username'] == 'admin'
def test_login_invalid_credentials(self, client):
"""Test login with invalid credentials"""
response = client.post('/api/auth/login', json={
'username': 'admin',
'password': 'wrongpassword'
})
assert response.status_code == 401
data = response.get_json()
assert data['success'] is False
assert 'message' in data
@pytest.mark.parametrize("payload,description", [
({'password': 'admin123'}, 'missing username'),
({'username': 'admin'}, 'missing password'),
({}, 'missing both username and password'),
({'username': ''}, 'empty username'),
({'password': ''}, 'empty password'),
({'username': '', 'password': ''}, 'both fields empty'),
])
def test_login_missing_or_empty_fields(self, client, payload, description):
"""Test login with missing or empty fields"""
response = client.post('/api/auth/login', json=payload)
assert response.status_code == 401
data = response.get_json()
assert data['success'] is False
def test_logout_success(self, client, auth_token):
"""Test successful logout"""
response = client.post('/api/auth/logout', headers={
'Authorization': f'Bearer {auth_token}'
})
assert response.status_code == 200
data = response.get_json()
assert data['success'] is True
def test_logout_without_token(self, client):
"""Test logout without token"""
response = client.post('/api/auth/logout')
assert response.status_code == 200
data = response.get_json()
assert data['success'] is True