Files
DeepTutor/tests/api/test_cors_settings.py
Bingxi Zhao (Frank) 6c024857b7 release: v1.4.1 — security & stability patch
Security: lock down the TutorBot tool sandbox (shell exec is opt-in, all
filesystem/shell access confined to the bot workspace) and isolate per-user
resources, closing #518, #517, #516, #515, #514 and #506 (first hardened in
#507).

Bug fixes: chat input disabled after the first turn (#520), KB embedding
failure on long documents (#521 / #509), profile creation under Docker
(#512 / #513), Qwen reasoning models failing native tool calling (#527 / #528),
the GPT-5 init-wizard token parameter (#508), and oversized session-event
truncation (#524).

Features: HTTP/SSE API for multi-turn chat with a specific TutorBot (#511),
multimodal image fallback for vision-capable providers without a capability
entry, safe ZIP knowledge upload, and a /settings/network page with model
fetching (community PRs #522 and #523 reimplemented locally).

Also bumps __version__ to 1.4.1, adds the v1.4.1 release notes, updates the
README Releases section, and ships the Astro + Starlight docs site under site/.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-27 23:16:00 +08:00

54 lines
1.9 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
def test_cors_normalizes_common_origin_input_mistakes(monkeypatch) -> None:
monkeypatch.setenv("AUTH_ENABLED", "true")
monkeypatch.setenv(
"CORS_ORIGIN",
"172.26.0.10:3782; https://learn.example.com/app/",
)
monkeypatch.setenv("CORS_ORIGINS", "http://localhost:3000;api.example.com")
settings = api_main._build_cors_settings()
assert settings["allow_origin_regex"] is None
assert "http://172.26.0.10:3782" in settings["allow_origins"]
assert "https://learn.example.com" in settings["allow_origins"]
assert "http://api.example.com" in settings["allow_origins"]