Spaces:
Sleeping
Sleeping
| """Missing /media/... paths serve a bundled placeholder unless KINK_MEDIA_STRICT=1.""" | |
| from __future__ import annotations | |
| import shutil | |
| import sys | |
| from pathlib import Path | |
| import pytest | |
| from fastapi.testclient import TestClient | |
| _SEED = Path(__file__).resolve().parent.parent / "deploy/hf/seed/hf_bundled_store.db" | |
| def media_fallback_app(tmp_path, monkeypatch): | |
| assert _SEED.is_file(), f"Missing seed DB: {_SEED}" | |
| dest = tmp_path / "media_fb_store.db" | |
| shutil.copyfile(_SEED, dest) | |
| monkeypatch.setenv("KINK_STORE_PATH", str(dest)) | |
| monkeypatch.setenv("KINK_SKIP_HEAVY_WARM", "1") | |
| monkeypatch.setenv("KINK_HF_REQUIRE_FULL_CATALOG", "0") | |
| monkeypatch.delenv("KINK_MEDIA_STRICT", raising=False) | |
| sys.modules.pop("api", None) | |
| import api as api_mod | |
| api_mod._backend_impl = None | |
| api_mod._get_backend() | |
| return api_mod | |
| def test_missing_fetlife_media_returns_placeholder_with_header(media_fallback_app) -> None: | |
| client = TestClient(media_fallback_app.app) | |
| r = client.get("/media/fetlife_fetishes/999999/0000000000001.jpg") | |
| assert r.status_code == 200 | |
| assert r.headers.get("x-kink-media-fallback") == "1" | |
| assert r.headers.get("content-type", "").startswith("image/") | |
| assert len(r.content) > 1000 | |
| def test_missing_media_strict_returns_404(tmp_path, monkeypatch) -> None: | |
| assert _SEED.is_file(), f"Missing seed DB: {_SEED}" | |
| dest = tmp_path / "strict_store.db" | |
| shutil.copyfile(_SEED, dest) | |
| monkeypatch.setenv("KINK_STORE_PATH", str(dest)) | |
| monkeypatch.setenv("KINK_SKIP_HEAVY_WARM", "1") | |
| monkeypatch.setenv("KINK_HF_REQUIRE_FULL_CATALOG", "0") | |
| monkeypatch.setenv("KINK_MEDIA_STRICT", "1") | |
| sys.modules.pop("api", None) | |
| import api as api_mod | |
| api_mod._backend_impl = None | |
| api_mod._get_backend() | |
| client = TestClient(api_mod.app) | |
| r = client.get("/media/fetlife_fetishes/999999/0000000000001.jpg") | |
| assert r.status_code == 404 | |
| def test_missing_media_redirects_to_public_media_base(tmp_path, monkeypatch) -> None: | |
| assert _SEED.is_file(), f"Missing seed DB: {_SEED}" | |
| dest = tmp_path / "remote_media_store.db" | |
| shutil.copyfile(_SEED, dest) | |
| monkeypatch.setenv("KINK_STORE_PATH", str(dest)) | |
| monkeypatch.setenv("KINK_SKIP_HEAVY_WARM", "1") | |
| monkeypatch.setenv("KINK_HF_REQUIRE_FULL_CATALOG", "1") | |
| monkeypatch.setenv("KINK_HF_STALE_MAX_BYTES", "0") | |
| monkeypatch.setenv("KINK_MEDIA_PUBLIC_BASE_URL", "https://cdn.example.test/kink-media") | |
| monkeypatch.delenv("KINK_MEDIA_STRICT", raising=False) | |
| sys.modules.pop("api", None) | |
| import api as api_mod | |
| api_mod._backend_impl = None | |
| api_mod._get_backend() | |
| client = TestClient(api_mod.app) | |
| r = client.get("/media/fetlife_fetishes/999999/0000000000001.jpg", follow_redirects=False) | |
| assert r.status_code == 307 | |
| assert r.headers["location"] == "https://cdn.example.test/kink-media/fetlife_fetishes/999999/0000000000001.jpg" | |
| h = client.get("/health").json() | |
| assert h.get("media", {}).get("remote_mode") == "public_base" | |
| assert h.get("media", {}).get("remote_configured") is True | |
| def test_favicon_serves_placeholder(media_fallback_app) -> None: | |
| client = TestClient(media_fallback_app.app) | |
| r = client.get("/favicon.ico") | |
| assert r.status_code == 200 | |
| assert r.headers.get("content-type", "").startswith("image/") | |
| def test_hf_space_image_honors_media_strict_for_missing_tiles(tmp_path, monkeypatch) -> None: | |
| """Published HF image marker must not override an explicit Space ``KINK_MEDIA_STRICT=1``.""" | |
| assert _SEED.is_file(), f"Missing seed DB: {_SEED}" | |
| dest = tmp_path / "media_hf_strict.db" | |
| shutil.copyfile(_SEED, dest) | |
| monkeypatch.setenv("KINK_STORE_PATH", str(dest)) | |
| monkeypatch.setenv("KINK_SKIP_HEAVY_WARM", "1") | |
| monkeypatch.setenv("KINK_HF_REQUIRE_FULL_CATALOG", "0") | |
| monkeypatch.setenv("KINK_MEDIA_STRICT", "1") | |
| monkeypatch.setenv("KINK_HF_SPACE_IMAGE", "1") | |
| sys.modules.pop("api", None) | |
| import api as api_mod | |
| api_mod._backend_impl = None | |
| api_mod._get_backend() | |
| client = TestClient(api_mod.app) | |
| r = client.get("/media/fetlife_fetishes/999999/0000000000001.jpg") | |
| assert r.status_code == 404 | |
| h = client.get("/health").json() | |
| assert h.get("media", {}).get("hf_space_image") is True | |
| assert h.get("media", {}).get("strict_env") is True | |
| assert h.get("media", {}).get("strict_missing_assets") is True | |
| def test_full_catalog_mode_defaults_to_strict_missing_media(tmp_path, monkeypatch) -> None: | |
| assert _SEED.is_file(), f"Missing seed DB: {_SEED}" | |
| dest = tmp_path / "media_full_catalog.db" | |
| shutil.copyfile(_SEED, dest) | |
| monkeypatch.setenv("KINK_STORE_PATH", str(dest)) | |
| monkeypatch.setenv("KINK_SKIP_HEAVY_WARM", "1") | |
| monkeypatch.setenv("KINK_HF_REQUIRE_FULL_CATALOG", "1") | |
| monkeypatch.setenv("KINK_HF_STALE_MAX_BYTES", "0") | |
| monkeypatch.delenv("KINK_MEDIA_STRICT", raising=False) | |
| sys.modules.pop("api", None) | |
| import api as api_mod | |
| api_mod._backend_impl = None | |
| api_mod._get_backend() | |
| client = TestClient(api_mod.app) | |
| r = client.get("/media/fetlife_fetishes/999999/0000000000001.jpg") | |
| assert r.status_code == 404 | |
| h = client.get("/health").json() | |
| assert h.get("media", {}).get("strict_env") is False | |
| assert h.get("media", {}).get("strict_missing_assets") is True | |
| def test_invalid_kink_media_fallback_override_still_serves_bundled_demo(tmp_path, monkeypatch) -> None: | |
| """Broken ``KINK_MEDIA_FALLBACK_PATH`` must not disable the default placeholder.""" | |
| assert _SEED.is_file(), f"Missing seed DB: {_SEED}" | |
| dest = tmp_path / "media_fb_bad_override.db" | |
| shutil.copyfile(_SEED, dest) | |
| monkeypatch.setenv("KINK_STORE_PATH", str(dest)) | |
| monkeypatch.setenv("KINK_SKIP_HEAVY_WARM", "1") | |
| monkeypatch.setenv("KINK_HF_REQUIRE_FULL_CATALOG", "0") | |
| monkeypatch.delenv("KINK_MEDIA_STRICT", raising=False) | |
| monkeypatch.setenv("KINK_MEDIA_FALLBACK_PATH", str(tmp_path / "nonexistent_fallback.jpg")) | |
| sys.modules.pop("api", None) | |
| import api as api_mod | |
| api_mod._backend_impl = None | |
| api_mod._get_backend() | |
| client = TestClient(api_mod.app) | |
| r = client.get("/media/fetlife_fetishes/999999/0000000000001.jpg") | |
| assert r.status_code == 200 | |
| assert r.headers.get("x-kink-media-fallback") == "1" | |
| h = client.get("/health").json() | |
| assert h.get("media", {}).get("fallback_ready") is True | |
| assert h.get("media", {}).get("strict_missing_assets") is False | |