clipwright/tests/test_api_keys.py
juancarlos.cavero f10655fd1c feat(agents): the pipeline as a platform — API keys, MCP server, webhooks
Agents don't forget to come back; humans do. Make the whole pipeline
drivable without the dashboard:

- API keys (osk_...): sha256-stored tokens minted from the account page.
  get_current_user_optional resolves them like a session, so metering,
  entitlement, priority and job ownership apply to agents with zero
  endpoint changes. Key management refuses API-key auth (a leaked key
  cannot mint replacements).
- MCP server at /mcp: stateless Streamable-HTTP JSON-RPC, no SDK
  dependency. Six tools (process_video, get_job_status, list_clips,
  get_quota, add_subtitles, publish_clip) that call back into this same
  app in-process via ASGITransport with the caller's auth headers, so
  MCP can never drift from REST behavior. Cloud requires a key;
  self-host stays BYOK-open.
- Completion webhooks: webhook_url (+ optional HMAC webhook_secret) on
  /api/process, SSRF-checked at submit AND delivery, fired once after
  the R2 archive so payloads carry durable links, survives redeploys
  via the resume manifest.
- Dashboard: API keys card in the account page (create once-shown key,
  list, revoke).
- Tests pin BILLING_ENABLED=0 in conftest so the suite no longer
  depends on the developer's personal .env.
2026-08-03 22:33:16 +02:00

42 lines
1.6 KiB
Python

"""API-key token semantics (cloud/api_keys.py).
The raw ``osk_`` value is shown once and only its sha256 is stored, so the
format and hashing rules are load-bearing: a drifting prefix would break
``looks_like_key`` routing in cloud/auth.py, and a non-deterministic hash would
lock every issued key out of the resolver.
"""
from cloud.api_keys import KEY_PREFIX, generate_key, hash_key, looks_like_key
class TestKeyGeneration:
def test_raw_key_carries_the_prefix(self):
raw, _hash, _prefix = generate_key()
assert raw.startswith(KEY_PREFIX)
def test_hash_matches_rehashing_the_raw_value(self):
raw, token_hash, _prefix = generate_key()
assert hash_key(raw) == token_hash
def test_display_prefix_is_a_prefix_of_the_raw_key(self):
raw, _hash, prefix = generate_key()
assert raw.startswith(prefix)
# Long enough to tell keys apart, short enough to be useless to guess.
assert len(prefix) < len(raw) / 3
def test_keys_are_unique(self):
assert generate_key()[0] != generate_key()[0]
def test_raw_key_has_enough_entropy(self):
raw, _hash, _prefix = generate_key()
assert len(raw) >= len(KEY_PREFIX) + 40 # token_urlsafe(32) ≈ 43 chars
class TestLooksLikeKey:
def test_accepts_generated_keys(self):
assert looks_like_key(generate_key()[0])
def test_rejects_jwts_and_junk(self):
assert not looks_like_key("eyJhbGciOiJIUzI1NiJ9.e30.abc")
assert not looks_like_key("")
assert not looks_like_key(None)
assert not looks_like_key(123)