""" Upload-validation and configuration-hardening regression tests. The audit reproduced two crashes here: an empty file and a text file renamed to .pdf both returned 500. Malformed input is a client error and must never take a worker down or pollute error monitoring. """ from __future__ import annotations import io import pytest from tests.conftest import auth_header, register_and_login @pytest.mark.parametrize( "name,payload,label", [ ("empty.pdf", b"", "empty file"), ("text.pdf", b"just plain text, definitely not a pdf", "renamed text file"), ("corrupt.pdf", b"%PDF-1.7\n< 0 assert body["coverage"]["coverage_pct"] == 100.0 # ─── Configuration hardening (C2, H6) ──────────────────────────────────────── def test_production_requires_explicit_cors_allowlist(monkeypatch): """AUDIT C2: wildcard CORS must be impossible in production.""" from api.settings import ApiSettings, ConfigurationError monkeypatch.setenv("ENVIRONMENT", "production") monkeypatch.setenv("JWT_SECRET", "x" * 48) monkeypatch.setenv("CORS_ORIGINS", "") with pytest.raises(ConfigurationError): ApiSettings().validate_runtime() monkeypatch.setenv("CORS_ORIGINS", "*") with pytest.raises(ConfigurationError): ApiSettings().cors_origins_list monkeypatch.setenv("CORS_ORIGINS", "https://app.example.com") assert ApiSettings().cors_origins_list == ["https://app.example.com"] def test_production_requires_strong_jwt_secret(monkeypatch): from api.settings import ApiSettings, ConfigurationError monkeypatch.setenv("ENVIRONMENT", "production") monkeypatch.setenv("CORS_ORIGINS", "https://app.example.com") monkeypatch.setenv("JWT_SECRET", "") with pytest.raises(ConfigurationError): ApiSettings().validate_runtime() monkeypatch.setenv("JWT_SECRET", "tooshort") with pytest.raises(ConfigurationError): ApiSettings().validate_runtime() monkeypatch.setenv("JWT_SECRET", "x" * 48) ApiSettings().validate_runtime() # must not raise def test_docs_hidden_in_production(monkeypatch): from api.settings import ApiSettings monkeypatch.setenv("ENVIRONMENT", "production") monkeypatch.setenv("JWT_SECRET", "x" * 48) monkeypatch.setenv("CORS_ORIGINS", "https://app.example.com") monkeypatch.setenv("EXPOSE_DOCS", "false") assert ApiSettings().docs_url is None