Files
unslothai--unsloth/studio/backend/tests/test_preview_token.py
T
wehub-resource-sync e93507a09c
Lockfile supply-chain audit / lockfile supply-chain audit (push) Has been cancelled
Windows Studio GGUF CI / GPU prebuilt resolves without Visual Studio (push) Has been cancelled
Windows Studio GGUF CI / setup.ps1 unit tests (VS 2026 / CMake guard) (push) Has been cancelled
Windows Studio GGUF CI / real-VS detection (VS 2022) (push) Has been cancelled
Windows Studio GGUF CI / real-VS detection (VS 2026) (push) Has been cancelled
Windows Studio GGUF CI / VC++ runtime detect + install round-trip (windows-2025-vs2026) (push) Has been cancelled
Windows Studio GGUF CI / VC++ runtime detect + install round-trip (windows-latest) (push) Has been cancelled
Windows Studio Update CI / Studio Updating Tests (push) Has been cancelled
Wheel CI / Wheel build + content sanity + import smoke (push) Has been cancelled
Lint CI / Source lint (Python + shell + YAML + JSON + safety nets) (push) Has been cancelled
MLX CI on Mac M1 / dispatch (push) Has been cancelled
Security audit / advisory audit (pip + npm + cargo) (push) Has been cancelled
Security audit / pip scan-packages :: extras (push) Has been cancelled
Security audit / pip scan-packages :: studio (push) Has been cancelled
Security audit / pip scan-packages :: hf-stack (push) Has been cancelled
Security audit / npm scan-packages (Studio frontend tarballs) (push) Has been cancelled
Security audit / workflow-trigger lint (pull_request_target / cache-poisoning) (push) Has been cancelled
Security audit / pytest tests/security (push) Has been cancelled
Security audit / npm provenance + new install-script diff (push) Has been cancelled
Studio API CI / Studio API & Auth Tests (push) Has been cancelled
Backend CI / (Python 3.10) (push) Has been cancelled
Backend CI / (Python 3.11) (push) Has been cancelled
Backend CI / (Python 3.12) (push) Has been cancelled
Backend CI / (Python 3.13) (push) Has been cancelled
Backend CI / Repo tests (CPU) (push) Has been cancelled
Frontend CI / Frontend build + bundle sanity (push) Has been cancelled
Studio GGUF CI / OpenAI, Anthropic API tests (push) Has been cancelled
Studio GGUF CI / Tool calling Tests (push) Has been cancelled
Studio GGUF CI / JSON, images (push) Has been cancelled
Mac Studio GGUF CI / OpenAI, Anthropic API tests (push) Has been cancelled
Mac Studio GGUF CI / Tool calling Tests (push) Has been cancelled
Mac Studio GGUF CI / JSON, images (push) Has been cancelled
Mac Studio Install Matrix CI / Install + load (macos-14) (push) Has been cancelled
Mac Studio Install Matrix CI / Install + load (macos-15) (push) Has been cancelled
Mac Studio Install Matrix CI / Install + load (macos-26) (push) Has been cancelled
Mac Studio Install Matrix CI / Install + load (macos-15-intel) (push) Has been cancelled
Mac Studio API CI / Studio API & Auth Tests (push) Has been cancelled
Mac Studio Install Matrix CI / Install + load (macos-26-intel) (push) Has been cancelled
Mac Studio UI CI / Chat UI Tests (push) Has been cancelled
Studio Tauri CI / Tauri Linux debug build (no codesign) (push) Has been cancelled
Mac Studio Update CI / Studio Updating Tests (push) Has been cancelled
Studio UI CI / Chat UI Tests (push) Has been cancelled
Windows Studio API CI / Studio API & Auth Tests (push) Has been cancelled
Windows Studio UI CI / Chat UI Tests (push) Has been cancelled
Studio Update CI / Studio Updating Tests (push) Has been cancelled
Core / Core (HF=default + TRL=default) (push) Has been cancelled
Core / Core (HF=4.57.6 + TRL<1) (push) Has been cancelled
Core / Core (HF=latest + TRL=latest) (push) Has been cancelled
Core / llama.cpp build + smoke (push) Has been cancelled
Windows Studio GGUF CI / OpenAI, Anthropic API tests (push) Has been cancelled
Windows Studio GGUF CI / Tool calling Tests (push) Has been cancelled
Windows Studio GGUF CI / JSON, images (push) Has been cancelled
Windows Studio GGUF CI / Studio install + inference without Visual Studio (push) Has been cancelled
Studio export capability / capability (macos-latest) (push) Has been cancelled
Studio export capability / capability (ubuntu-latest) (push) Has been cancelled
Studio export capability / capability (windows-latest) (push) Has been cancelled
Cross-platform parity / parity (macos-latest) (push) Has been cancelled
Cross-platform parity / parity (windows-latest) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
Studio load-orchestrator CI / test (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:59:56 +08:00

76 lines
3.3 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
"""Unit + rotation coverage for `/p` preview capability tokens.
The token turns a guessable preview ref into an unguessable bearer capability:
it must round-trip for the ref it was signed for, reject tampering / wrong refs,
and stop verifying once the signing secret is rotated (link revocation).
"""
from pathlib import Path
import sys
import types as _types
_BACKEND_DIR = str(Path(__file__).resolve().parent.parent)
if _BACKEND_DIR not in sys.path:
sys.path.insert(0, _BACKEND_DIR)
# Mirror the other preview tests: avoid the heavy real `loggers` handlers.
_loggers_stub = _types.ModuleType("loggers")
_loggers_stub.get_logger = lambda name: __import__("logging").getLogger(name)
sys.modules.setdefault("loggers", _loggers_stub)
import auth.storage as storage
import utils.preview_token as preview_token
_S1 = b"secret-one-aaaaaaaaaaaaaaaaaaaaaaaa"
_S2 = b"secret-two-bbbbbbbbbbbbbbbbbbbbbbbb"
def test_sign_verify_roundtrip(monkeypatch):
monkeypatch.setattr(preview_token, "get_or_create_preview_link_secret", lambda: _S1)
token = preview_token.sign_preview_ref("run/checkpoint-1")
assert preview_token.verify_preview_ref("run/checkpoint-1", token)
# URL-safe, unpadded, and high-entropy (SHA-256 -> 43 base64url chars).
assert "=" not in token and "/" not in token and "+" not in token
assert len(token) >= 40
def test_missing_or_tampered_token_rejected(monkeypatch):
monkeypatch.setattr(preview_token, "get_or_create_preview_link_secret", lambda: _S1)
token = preview_token.sign_preview_ref("demorun")
assert not preview_token.verify_preview_ref("demorun", None)
assert not preview_token.verify_preview_ref("demorun", "")
flipped = token[:-1] + ("A" if token[-1] != "A" else "B")
assert not preview_token.verify_preview_ref("demorun", flipped)
# A token minted for one ref does not unlock another.
assert not preview_token.verify_preview_ref("otherrun", token)
# A non-ASCII token is invalid, not a crash (the route would 500 otherwise).
assert not preview_token.verify_preview_ref("demorun", "tøken-é")
def test_secret_change_invalidates_token(monkeypatch):
monkeypatch.setattr(preview_token, "get_or_create_preview_link_secret", lambda: _S1)
token = preview_token.sign_preview_ref("demorun")
monkeypatch.setattr(preview_token, "get_or_create_preview_link_secret", lambda: _S2)
assert not preview_token.verify_preview_ref("demorun", token)
def test_rotation_revokes_links(tmp_path, monkeypatch):
# Exercise the real storage helpers against a throwaway auth.db.
monkeypatch.setattr(storage, "DB_PATH", tmp_path / "auth.db")
monkeypatch.setattr(storage, "_preview_link_secret_cache", None)
token = preview_token.sign_preview_ref("demorun")
assert preview_token.verify_preview_ref("demorun", token)
# Secret persists across calls (the link keeps working until rotated).
assert preview_token.verify_preview_ref("demorun", token)
storage.rotate_preview_link_secret()
# Old shared link is revoked; a freshly minted one works.
assert not preview_token.verify_preview_ref("demorun", token)
assert preview_token.verify_preview_ref("demorun", preview_token.sign_preview_ref("demorun"))