Aniworld/tests/integration/test_frontend_integration_smoke.py
Lukas 0957a6e183 feat: Complete frontend-backend integration with JWT authentication
Implemented full JWT-based authentication integration between frontend and backend:

Frontend Changes:
- Updated login.html to store JWT tokens in localStorage after successful login
- Updated setup.html to use correct API payload format (master_password)
- Modified app.js and queue.js to include Bearer tokens in all authenticated requests
- Updated makeAuthenticatedRequest() to add Authorization header with JWT token
- Enhanced checkAuthentication() to verify token and redirect on 401 responses
- Updated logout() to clear tokens from localStorage

API Endpoint Updates:
- Mapped queue API endpoints to new backend structure
- /api/queue/clear → /api/queue/completed (DELETE) for clearing completed
- /api/queue/remove → /api/queue/{item_id} (DELETE) for single removal
- /api/queue/retry payload changed to {item_ids: []} array format
- /api/download/pause|resume|cancel → /api/queue/pause|resume|stop

Testing:
- Created test_frontend_integration_smoke.py with JWT token validation tests
- Verified login returns access_token, token_type, and expires_at
- Tested Bearer token authentication on protected endpoints
- Smoke tests passing for authentication flow

Documentation:
- Updated infrastructure.md with JWT authentication implementation details
- Documented token storage, API endpoint changes, and response formats
- Marked Frontend Integration task as completed in instructions.md
- Added frontend integration testing section

WebSocket:
- Verified WebSocket integration with new backend (already functional)
- Dual event handlers support both old and new message types
- Room-based subscriptions working correctly

This completes Task 7: Frontend Integration from the development instructions.
2025-10-17 19:27:52 +02:00

98 lines
3.1 KiB
Python

"""
Smoke tests for frontend-backend integration.
These tests verify that key authentication and API changes work correctly
with the frontend's expectations for JWT tokens.
"""
import pytest
from httpx import ASGITransport, AsyncClient
from src.server.fastapi_app import app
from src.server.services.auth_service import auth_service
@pytest.fixture(autouse=True)
def reset_auth():
"""Reset authentication state."""
auth_service._hash = None
auth_service._failed.clear()
yield
auth_service._hash = None
auth_service._failed.clear()
@pytest.fixture
async def client():
"""Create async test client."""
transport = ASGITransport(app=app)
async with AsyncClient(transport=transport, base_url="http://test") as ac:
yield ac
class TestFrontendIntegration:
"""Test frontend integration with JWT authentication."""
async def test_login_returns_jwt_token(self, client):
"""Test that login returns JWT token in expected format."""
# Setup
await client.post(
"/api/auth/setup",
json={"master_password": "StrongP@ss123"}
)
# Login
response = await client.post(
"/api/auth/login",
json={"password": "StrongP@ss123"}
)
assert response.status_code == 200
data = response.json()
# Frontend expects these fields
assert "access_token" in data
assert "token_type" in data
assert data["token_type"] == "bearer"
async def test_authenticated_endpoints_require_bearer_token(self, client):
"""Test that authenticated endpoints require Bearer token."""
# Setup and login
await client.post(
"/api/auth/setup",
json={"master_password": "StrongP@ss123"}
)
login_resp = await client.post(
"/api/auth/login",
json={"password": "StrongP@ss123"}
)
token = login_resp.json()["access_token"]
# Test without token - should fail
response = await client.get("/api/v1/anime")
assert response.status_code == 401
# Test with Bearer token in header - should work or return 503
headers = {"Authorization": f"Bearer {token}"}
response = await client.get("/api/v1/anime", headers=headers)
# May return 503 if anime directory not configured
assert response.status_code in [200, 503]
async def test_queue_endpoints_accessible_with_token(self, client):
"""Test queue endpoints work with JWT token."""
# Setup and login
await client.post(
"/api/auth/setup",
json={"master_password": "StrongP@ss123"}
)
login_resp = await client.post(
"/api/auth/login",
json={"password": "StrongP@ss123"}
)
token = login_resp.json()["access_token"]
headers = {"Authorization": f"Bearer {token}"}
# Test queue status endpoint
response = await client.get("/api/queue/status", headers=headers)
# Should work or return 503 if service not configured
assert response.status_code in [200, 503]