trapped in the rabbit hole of the LLM nonsens
Co-Authored-by: iGor milhit <igor@milhit.ch>
parent
67213da237
commit
891ea17675
13
config.py
13
config.py
|
|
@ -10,12 +10,23 @@ class Settings(BaseSettings):
|
|||
)
|
||||
|
||||
# CORS
|
||||
cors_origins: str = "http://localhost:5173"
|
||||
cors_origins_str: str = "http://localhost:5173"
|
||||
|
||||
# Autres configurations futures
|
||||
app_name: str = "Dough Calculator"
|
||||
debug: bool = False
|
||||
|
||||
@property
|
||||
def cors_origins(self) -> list[str]:
|
||||
"""Convert a string coma separated into list."""
|
||||
if isinstance(self.cors_origins_str, str):
|
||||
return [
|
||||
item.strip()
|
||||
for item in self.cors_origins_str.split(",")
|
||||
if item.strip()
|
||||
]
|
||||
return []
|
||||
|
||||
@lru_cache
|
||||
def get_settings() -> Settings:
|
||||
return Settings()
|
||||
|
|
|
|||
4
main.py
4
main.py
|
|
@ -8,11 +8,9 @@ from config import get_settings
|
|||
app = FastAPI()
|
||||
settings = get_settings()
|
||||
|
||||
origins = [origin.strip() for origin in settings.cors_origins.split(",")]
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=origins,
|
||||
allow_origins=settings.cors_origins,
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
|
|
|
|||
|
|
@ -2,55 +2,63 @@ from config import Settings, get_settings
|
|||
|
||||
|
||||
def test_settings_default_values():
|
||||
"""Vérifie les valeurs par défaut"""
|
||||
"""Tests the default values."""
|
||||
settings = Settings()
|
||||
assert settings.cors_origins == "http://localhost:5173"
|
||||
assert settings.cors_origins == ["http://localhost:5173"]
|
||||
assert settings.app_name == "Dough Calculator"
|
||||
assert settings.debug is False
|
||||
|
||||
def test_settings_from_env(monkeypatch):
|
||||
"""Vérifie le chargement depuis variables d'environnement"""
|
||||
monkeypatch.setenv("CORS_ORIGINS", "https://example.com,https://test.com")
|
||||
"""Tests loading from environment variable."""
|
||||
monkeypatch.setenv( "CORS_ORIGINS", "https://example.com,https://test.com")
|
||||
monkeypatch.setenv("APP_NAME", "Test App")
|
||||
monkeypatch.setenv("DEBUG", "true")
|
||||
|
||||
# Vider le cache pour forcer le rechargement
|
||||
get_settings.cache_clear()
|
||||
# Clear cache to force reloading
|
||||
# get_settings.cache_clear()
|
||||
|
||||
settings = get_settings()
|
||||
assert settings.cors_origins == "https://example.com,https://test.com"
|
||||
settings = Settings()
|
||||
assert settings.cors_origins == ["https://example.com","https://test.com"]
|
||||
assert settings.app_name == "Test App"
|
||||
assert settings.debug is True
|
||||
|
||||
def test_cors_origins_parsing():
|
||||
"""Vérifie que les origines CORS sont correctement parsées"""
|
||||
settings = Settings(cors_origins="http://localhost:3000,https://prod.com")
|
||||
origins = [origin.strip() for origin in settings.cors_origins.split(",")]
|
||||
"""Tests the parsing of CORS origins."""
|
||||
settings = Settings(
|
||||
cors_origins=["http://localhost:3000","https://prod.com"]
|
||||
)
|
||||
origins = settings.cors_origins
|
||||
|
||||
assert len(origins) == 2
|
||||
assert "http://localhost:3000" in origins
|
||||
assert "https://prod.com" in origins
|
||||
|
||||
def test_settings_cache():
|
||||
"""Vérifie que le cache fonctionne"""
|
||||
"""Tests that the cache is working as expected."""
|
||||
get_settings.cache_clear()
|
||||
|
||||
settings1 = get_settings()
|
||||
settings2 = get_settings()
|
||||
|
||||
# Même instance en mémoire grâce au cache
|
||||
# Same instance in memory thanks to the cache
|
||||
assert settings1 is settings2
|
||||
|
||||
def test_env_file_loading(tmp_path, monkeypatch):
|
||||
"""Vérifie le chargement depuis fichier .env"""
|
||||
# Créer un fichier .env temporaire
|
||||
"""Tests variable loading from an .env file."""
|
||||
# Create a temporary .env file
|
||||
env_file = tmp_path / ".env"
|
||||
env_file.write_text("CORS_ORIGINS=https://from-file.com\nDEBUG=true")
|
||||
env_file.write_text(
|
||||
"CORS_ORIGINS=https://from-file.com\n"
|
||||
"DEBUG=true"
|
||||
)
|
||||
|
||||
# Changer le répertoire de travail
|
||||
# Change working directory
|
||||
monkeypatch.chdir(tmp_path)
|
||||
get_settings.cache_clear()
|
||||
|
||||
settings = Settings()
|
||||
assert settings.cors_origins == "https://from-file.com"
|
||||
|
||||
assert isinstance(settings.cors_origins, list)
|
||||
assert settings.cors_origins == ["https://from-file.com"]
|
||||
assert settings.debug is True
|
||||
|
||||
|
|
|
|||
|
|
@ -14,7 +14,10 @@ def test_cors_headers():
|
|||
"Access-Control-Request-Method": "GET"
|
||||
}
|
||||
)
|
||||
expected_origin = "http://localhost:5173"
|
||||
assert response.status_code == 200
|
||||
assert "access-control-allow-origin" in response.headers
|
||||
assert response.headers["access-control-allow-origin"] == expected_origin
|
||||
|
||||
def test_settings_injection():
|
||||
"""Tests that the settings are available."""
|
||||
|
|
|
|||
Loading…
Reference in New Issue