audio-processor / test_jwt_auth_simple.py
tedowski's picture
n8n-improvements (#1)
dbe78dd verified
"""Simplified unit tests for JWT authentication components."""
import pytest
import base64
import json
from unittest.mock import Mock
from fastapi import HTTPException
# Direct import of JWT validation service
import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from infrastructure.services.jwt_validation_service import JWTValidationService
class TestJWTAuthenticationIntegration:
"""Integration tests for JWT authentication components."""
def setup_method(self):
"""Set up test fixtures."""
self.jwt_service = JWTValidationService()
# Create a valid JWT token for testing
header = {"alg": "HS256", "typ": "JWT"}
payload = {"sub": "1234567890", "name": "John Doe", "iat": 1516239022}
header_b64 = base64.urlsafe_b64encode(
json.dumps(header).encode()
).decode().rstrip('=')
payload_b64 = base64.urlsafe_b64encode(
json.dumps(payload).encode()
).decode().rstrip('=')
self.valid_token = f"{header_b64}.{payload_b64}.test_signature"
self.valid_auth_header = f"Bearer {self.valid_token}"
def test_jwt_validation_service_integration(self):
"""Test the JWT validation service works correctly."""
# Test valid token
assert self.jwt_service.validate_structure(self.valid_token) is True
# Test invalid tokens
assert self.jwt_service.validate_structure("invalid.token") is False
assert self.jwt_service.validate_structure("") is False
assert self.jwt_service.validate_structure(None) is False
# Test claims extraction
claims = self.jwt_service.extract_claims(self.valid_token)
assert claims is not None
assert claims["sub"] == "1234567890"
assert claims["name"] == "John Doe"
def test_bearer_token_validation_logic(self):
"""Test the bearer token validation logic manually."""
def validate_bearer_token_logic(authorization: str) -> str:
"""Simulate the bearer token validation logic."""
if not authorization:
raise ValueError("Missing Authorization header")
if not authorization.startswith("Bearer "):
raise ValueError("Invalid Authorization header format")
token = authorization[7:] # Remove "Bearer " prefix
if not token:
raise ValueError("Empty bearer token")
# Validate JWT structure
jwt_service = JWTValidationService()
if not jwt_service.validate_structure(token):
raise ValueError("Invalid JWT token structure")
return token
# Test successful validation
result = validate_bearer_token_logic(self.valid_auth_header)
assert result == self.valid_token
# Test various failure cases
with pytest.raises(ValueError, match="Missing Authorization header"):
validate_bearer_token_logic(None)
with pytest.raises(ValueError, match="Invalid Authorization header format"):
validate_bearer_token_logic("Basic dXNlcjpwYXNz")
with pytest.raises(ValueError, match="Empty bearer token"):
validate_bearer_token_logic("Bearer ")
with pytest.raises(ValueError, match="Invalid JWT token structure"):
validate_bearer_token_logic("Bearer invalid.token")
def test_end_to_end_authentication_flow(self):
"""Test the complete authentication flow."""
# 1. Client sends request with valid token
auth_header = f"Bearer {self.valid_token}"
# 2. Extract token from header
assert auth_header.startswith("Bearer ")
token = auth_header[7:]
# 3. Validate token structure
assert self.jwt_service.validate_structure(token) is True
# 4. Extract claims (optional)
claims = self.jwt_service.extract_claims(token)
assert claims is not None
assert "sub" in claims
# This represents a successful authentication flow
print(f"✅ Authentication successful for user: {claims['sub']}")
def test_configuration_flags(self):
"""Test that authentication can be enabled/disabled via configuration."""
# Simulate configuration flags
enforce_authentication = True
enable_external_job_ids = True
jwt_validation_strict = False
# When authentication is enforced
if enforce_authentication:
# Token validation should be required
assert self.jwt_service.validate_structure(self.valid_token) is True
# When external job IDs are enabled
if enable_external_job_ids:
# Should accept external job ID parameter
external_job_id = "test-job-123"
assert len(external_job_id) > 0
# JWT validation strictness
if not jwt_validation_strict:
# Only structure validation, no signature verification
assert self.jwt_service.validate_structure(self.valid_token) is True
print("✅ Configuration flags working correctly")