mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-03 16:31:17 +08:00
38 lines
1.3 KiB
Python
38 lines
1.3 KiB
Python
"""Tests for FastAPI CORS settings."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from deeptutor.api import main as api_main
|
|
|
|
|
|
def test_cors_allows_remote_http_origins_when_auth_disabled(
|
|
monkeypatch,
|
|
) -> None:
|
|
monkeypatch.delenv("AUTH_ENABLED", raising=False)
|
|
monkeypatch.delenv("CORS_ORIGIN", raising=False)
|
|
monkeypatch.delenv("CORS_ORIGINS", raising=False)
|
|
monkeypatch.setenv("FRONTEND_PORT", "3782")
|
|
|
|
settings = api_main._build_cors_settings()
|
|
|
|
assert settings["allow_origin_regex"] == r"https?://.*"
|
|
assert "http://localhost:3782" in settings["allow_origins"]
|
|
assert "http://127.0.0.1:3782" in settings["allow_origins"]
|
|
|
|
|
|
def test_cors_requires_explicit_origins_when_auth_enabled(monkeypatch) -> None:
|
|
monkeypatch.setenv("AUTH_ENABLED", "true")
|
|
monkeypatch.setenv("CORS_ORIGIN", "https://app.example.com/")
|
|
monkeypatch.setenv(
|
|
"CORS_ORIGINS",
|
|
"https://foo.example.com, https://bar.example.com\nhttps://foo.example.com",
|
|
)
|
|
|
|
settings = api_main._build_cors_settings()
|
|
|
|
assert settings["allow_origin_regex"] is None
|
|
assert "https://app.example.com" in settings["allow_origins"]
|
|
assert "https://foo.example.com" in settings["allow_origins"]
|
|
assert "https://bar.example.com" in settings["allow_origins"]
|
|
assert settings["allow_origins"].count("https://foo.example.com") == 1
|