Files
romm/backend/tests/endpoints/test_cors.py
Sam Dornan bd16648c9a chore(mypy): switch to strict mode with listed exceptions (#4817)
* chore(mypy): switch to strict mode with listed exceptions

Replace the hand-picked flags with `strict = True`, turning off only the
four flags that aren't clean yet (disallow_any_generics,
warn_return_any, disallow_untyped_calls, disallow_untyped_decorators).
Any other strict flag, including those future mypy releases add to the
preset, is on by default.

That turns on no_implicit_reexport, strict_equality and extra_checks:
- Import each name from the module that defines it. About 240 test
  sites patched a dependency through the module that imported it
  (`scan_module.fs_rom_handler`); they now import the same object from
  its source, so every patch targets what it did before. Tests that
  replace a module attribute are unchanged.
- handler.streaming.memory_cards re-exports store_memory_card_version
  explicitly, since endpoints call it there and tests patch it there.
- mutagen gets implicit_reexport, as it re-exports without __all__.
- Test comparisons build typed values (CoverResult, a typed expected
  dict, enum .value) and the CORS lookup casts for its identity check.
- One ss_handler ** spread keeps a coded ignore: extra_checks wants its
  NotRequired dump_* keys spelled out, and the media dict never has them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PLzXae5up2pYXRoBjfjn2G

* test(upload): assert cleanup against the patched upload directory

The 409 test checked config's ROM_UPLOAD_TMP_BASE while the fixture patches
the endpoint's copy to tmp_path/uploads, so it passed even if cleanup failed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PLzXae5up2pYXRoBjfjn2G

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 15:45:37 -05:00

89 lines
2.9 KiB
Python

"""Guards the cross-origin defaults the API is served with."""
from collections.abc import Sequence
from typing import Any, cast
import pytest
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.testclient import TestClient
from main import app
from starlette.middleware import Middleware
from config import cors_allow_credentials
FOREIGN_ORIGIN = "https://evil.example"
LISTED_ORIGIN = "https://romm.example"
def _cors_middleware() -> Middleware:
return next(m for m in app.user_middleware if cast(object, m.cls) is CORSMiddleware)
def _scratch_app(**kwargs: Any) -> TestClient:
scratch = FastAPI()
scratch.add_middleware(CORSMiddleware, **kwargs)
@scratch.get("/ping")
async def ping() -> dict[str, bool]:
return {"ok": True}
return TestClient(scratch)
def test_a_wildcard_origin_is_never_paired_with_credentials() -> None:
cors = _cors_middleware()
origins = cors.kwargs["allow_origins"]
assert isinstance(origins, Sequence)
assert not (cors.kwargs["allow_credentials"] and "*" in origins), (
"Starlette answers a wildcard with the caller's own Origin and "
"Access-Control-Allow-Credentials, granting every site credentialed access"
)
def test_a_foreign_origin_gets_no_cors_grant(client: TestClient) -> None:
response = client.get("/api/heartbeat", headers={"Origin": FOREIGN_ORIGIN})
assert response.status_code == 200
# Starlette ships the credentials header either way; with no Allow-Origin to
# pair it with, the browser drops the response.
assert "access-control-allow-origin" not in response.headers
def test_a_listed_origin_is_granted_with_credentials() -> None:
"""Listing an origin is what turns cross-origin access on, credentials included."""
client = _scratch_app(
**{**_cors_middleware().kwargs, "allow_origins": [LISTED_ORIGIN]}
)
response = client.get("/ping", headers={"Origin": LISTED_ORIGIN})
assert response.headers["access-control-allow-origin"] == LISTED_ORIGIN
assert response.headers["access-control-allow-credentials"] == "true"
def test_an_explicit_wildcard_is_served_without_credentials() -> None:
"""An operator-set `*` stays open, but is not paired with the session cookie."""
client = _scratch_app(
allow_origins=["*"],
allow_credentials=cors_allow_credentials(["*"]),
)
response = client.get("/ping", headers={"Origin": FOREIGN_ORIGIN})
assert response.headers["access-control-allow-origin"] == "*"
assert "access-control-allow-credentials" not in response.headers
@pytest.mark.parametrize(
("origins", "expected"),
[
(["*"], False),
([], True),
([LISTED_ORIGIN], True),
(["*", LISTED_ORIGIN], False),
],
)
def test_credentials_follow_the_wildcard(origins: list[str], expected: bool) -> None:
assert cors_allow_credentials(origins) is expected