fix: detect HuggingFace token when downloading cookbook models (#3459)

Co-authored-by: Alexandre Teixeira <111787685+alteixeira20@users.noreply.github.com>
This commit is contained in:
muhamed hamed
2026-06-11 23:53:16 +03:00
committed by GitHub
parent f5c1eb4b9d
commit 3b3c0d6254
5 changed files with 68 additions and 15 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"