|
|
import sys |
|
|
import types |
|
|
|
|
|
|
|
|
def mock_package(name): |
|
|
m = types.ModuleType(name) |
|
|
sys.modules[name] = m |
|
|
return m |
|
|
|
|
|
mock_package("torch") |
|
|
mock_package("torch.serialization") |
|
|
mock_package("torchaudio") |
|
|
mock_package("numpy") |
|
|
mock_package("pyannote") |
|
|
mock_package("pyannote.audio") |
|
|
m_prom = mock_package("prometheus_fastapi_instrumentator") |
|
|
from unittest.mock import MagicMock |
|
|
m_prom.Instrumentator = MagicMock() |
|
|
|
|
|
|
|
|
g = mock_package("google") |
|
|
gc = mock_package("google.cloud") |
|
|
gcs = mock_package("google.cloud.speech") |
|
|
gct = mock_package("google.cloud.texttospeech") |
|
|
gcl = mock_package("google.cloud.language") |
|
|
|
|
|
m_gcs = mock_package("google.cloud.speech_v1") |
|
|
m_gcs.types = MagicMock() |
|
|
|
|
|
m_gct = mock_package("google.cloud.texttospeech_v1") |
|
|
m_gct.types = MagicMock() |
|
|
|
|
|
m_gcl = mock_package("google.cloud.language_v1") |
|
|
m_gcl.types = MagicMock() |
|
|
|
|
|
mock_package("edge_tts") |
|
|
mock_package("librosa") |
|
|
mock_package("soundfile") |
|
|
mock_package("faster_whisper") |
|
|
mock_package("transformers") |
|
|
mock_package("TTS") |
|
|
mock_package("melotts") |
|
|
mock_package("ffmpeg") |
|
|
mock_package("pydub") |
|
|
mock_package("pydantic_settings") |
|
|
mock_package("dotenv") |
|
|
mock_package("passlib") |
|
|
mock_package("passlib.context") |
|
|
mock_package("jose") |
|
|
mock_package("multipart") |
|
|
|
|
|
|
|
|
|
|
|
from fastapi.testclient import TestClient |
|
|
from app.main import app |
|
|
|
|
|
client = TestClient(app) |
|
|
|
|
|
def test_security_headers(): |
|
|
print("Testing Security Headers...") |
|
|
response = client.get("/") |
|
|
|
|
|
headers = response.headers |
|
|
|
|
|
|
|
|
assert headers.get("X-Frame-Options") == "DENY", "X-Frame-Options missing or incorrect" |
|
|
assert headers.get("X-Content-Type-Options") == "nosniff", "X-Content-Type-Options missing or incorrect" |
|
|
assert "default-src 'self'" in headers.get("Content-Security-Policy", ""), "CSP missing or incorrect" |
|
|
assert "max-age=31536000" in headers.get("Strict-Transport-Security", ""), "HSTS missing or incorrect" |
|
|
|
|
|
print("✅ All security headers present and correct.") |
|
|
print(f"CSP: {headers.get('Content-Security-Policy')}") |
|
|
|
|
|
if __name__ == "__main__": |
|
|
try: |
|
|
test_security_headers() |
|
|
except Exception as e: |
|
|
print(f"❌ Test Failed: {e}") |
|
|
exit(1) |
|
|
|