Merge branch 'dev' into test/oversized-test-split-plan-3983

This commit is contained in:
Alexandre Teixeira
2026-06-11 22:00:27 +01:00
committed by GitHub
19 changed files with 705 additions and 18 deletions
+37
View File
@@ -0,0 +1,37 @@
"""Cookbook HF token persistence and lookup."""
import json
import os
import pytest
from routes.cookbook_helpers import load_stored_hf_token
from src.secret_storage import encrypt
def test_load_stored_hf_token_reads_encrypted_state(tmp_path, monkeypatch):
monkeypatch.setenv("DATA_DIR", str(tmp_path))
state_path = tmp_path / "cookbook_state.json"
state_path.write_text(
json.dumps({"env": {"hfToken": encrypt("hf_test_token_12345")}}),
encoding="utf-8",
)
assert load_stored_hf_token() == "hf_test_token_12345"
assert load_stored_hf_token(state_path=state_path) == "hf_test_token_12345"
def test_load_stored_hf_token_falls_back_to_env_when_state_missing(tmp_path, monkeypatch):
monkeypatch.setenv("DATA_DIR", str(tmp_path))
monkeypatch.setenv("HF_TOKEN", "hf_from_env")
assert load_stored_hf_token() == "hf_from_env"
def test_load_stored_hf_token_prefers_state_over_env(tmp_path, monkeypatch):
monkeypatch.setenv("DATA_DIR", str(tmp_path))
monkeypatch.setenv("HF_TOKEN", "hf_from_env")
state_path = tmp_path / "cookbook_state.json"
state_path.write_text(
json.dumps({"env": {"hfToken": encrypt("hf_from_state")}}),
encoding="utf-8",
)
assert load_stored_hf_token() == "hf_from_state"
@@ -0,0 +1,26 @@
"""load_features() must degrade to defaults if features.json is unreadable.
load_settings() already catches PermissionError, but load_features() did not, so
an unreadable data/features.json (e.g. root-owned after a deploy) raised instead
of falling back to DEFAULT_FEATURES, taking down GET /api/auth/features.
"""
import builtins
import src.settings as settings
def test_load_features_degrades_on_permission_error(monkeypatch):
# Ensure the cache does not short-circuit the read.
monkeypatch.setattr(settings, "_features_cache", None, raising=False)
real_open = builtins.open
def deny(path, *args, **kwargs):
if str(path) == str(settings.FEATURES_FILE):
raise PermissionError("denied")
return real_open(path, *args, **kwargs)
monkeypatch.setattr(builtins, "open", deny)
result = settings.load_features()
assert result == dict(settings.DEFAULT_FEATURES)
+1
View File
@@ -83,6 +83,7 @@ def test_is_wsl_true_when_proc_version_mentions_microsoft(monkeypatch):
def fake_open(path, mode="r", *args, **kwargs):
assert path == "/proc/version"
assert mode == "r"
assert kwargs == {"encoding": "utf-8", "errors": "ignore"}
return io.StringIO("Linux version 6.6.0 microsoft standard")
monkeypatch.setattr("builtins.open", fake_open)