Some checks are pending
build-images / api (push) Waiting to run
build-images / worker-gpu (push) Waiting to run
build-images / web (push) Waiting to run
codeql / Analyze (javascript-typescript) (push) Waiting to run
codeql / Analyze (python) (push) Waiting to run
perf / API hot-path micro-smoke (budgeted) (push) Waiting to run
perf / Desktop engine crates (release build check) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Waiting to run
vision2026-ci / API phase modules (ruff + pytest) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Waiting to run
vision2026-ci / Python SDK resource tests (push) Waiting to run
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Waiting to run
vision2026-ci / Alembic upgrade + drift check (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Waiting to run
Paid, hosted product at menivor.com, now retired as a company project. Fresh squashed history of the cleaned working tree (secret-scanned, no .env, no customer data, no outreach logs). Built on MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
140 lines
5.6 KiB
Python
140 lines
5.6 KiB
Python
"""Canon HTTP surface + service.verify_asset over the real DB (sqlite)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import importlib
|
|
from contextlib import contextmanager
|
|
|
|
from fastapi import FastAPI
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
from sqlalchemy.pool import StaticPool
|
|
from starlette.responses import JSONResponse
|
|
from starlette.testclient import TestClient
|
|
|
|
from app.domains.canon import binding
|
|
from app.models.db import canon, characters, models # noqa: F401 - register tables
|
|
from app.models.db.base import Base
|
|
from app.models.db.models import Asset, User
|
|
from app.models.exception import HttpException
|
|
from app.utils import utils
|
|
|
|
|
|
def _mount(monkeypatch, user_id="u1"):
|
|
mod = importlib.import_module("app.domains.canon.router")
|
|
engine = create_engine("sqlite://", connect_args={"check_same_thread": False},
|
|
poolclass=StaticPool, future=True)
|
|
Base.metadata.create_all(engine)
|
|
Factory = sessionmaker(bind=engine, autoflush=False, expire_on_commit=False, future=True)
|
|
|
|
@contextmanager
|
|
def _gs():
|
|
s = Factory()
|
|
try:
|
|
yield s
|
|
s.commit()
|
|
finally:
|
|
s.close()
|
|
|
|
with _gs() as s:
|
|
s.add(User(id="u1", email="u@x.com"))
|
|
s.add(User(id="u2", email="u2@x.com"))
|
|
|
|
class _U:
|
|
id = user_id
|
|
|
|
monkeypatch.setattr(mod, "get_session", _gs)
|
|
monkeypatch.setattr(mod, "_require_db", lambda rid: None)
|
|
monkeypatch.setattr(mod, "_resolve_user", lambda req, s, rid: _U())
|
|
|
|
app = FastAPI()
|
|
app.include_router(mod.router)
|
|
app.add_exception_handler(
|
|
HttpException,
|
|
lambda r, exc: JSONResponse(status_code=exc.status_code,
|
|
content=utils.get_response(exc.status_code, exc.data, exc.message)),
|
|
)
|
|
return TestClient(app), _gs, mod
|
|
|
|
|
|
def _canon_body(**over):
|
|
body = {"name": "Brand", "style": "3D", "palette": ["#ff0000"],
|
|
"product_name": "Nova", "brand_avoid": ["cheap"]}
|
|
body.update(over)
|
|
return body
|
|
|
|
|
|
def test_create_lock_and_versions_retained(monkeypatch):
|
|
tc, _gs, _ = _mount(monkeypatch)
|
|
a = tc.post("/api/v1/canon", json=_canon_body())
|
|
assert a.status_code == 200
|
|
data = a.json()["data"]
|
|
cid, v1_hash = data["canon_id"], data["canon_hash"]
|
|
assert data["version"] == 1 and v1_hash.startswith("cv1|")
|
|
|
|
# Edit -> new immutable version.
|
|
b = tc.post(f"/api/v1/canon/{cid}/lock", json={"style": "watercolor"})
|
|
assert b.status_code == 200 and b.json()["data"]["version"] == 2
|
|
assert b.json()["data"]["canon_hash"] != v1_hash
|
|
|
|
# v1 snapshot is still retrievable with the ORIGINAL hash.
|
|
got_v1 = tc.get(f"/api/v1/canon/{cid}/versions/1")
|
|
assert got_v1.status_code == 200 and got_v1.json()["data"]["canon_hash"] == v1_hash
|
|
|
|
# Header now points at v2.
|
|
hdr = tc.get(f"/api/v1/canon/{cid}")
|
|
assert hdr.json()["data"]["current_version"] == 2
|
|
assert len(hdr.json()["data"]["versions"]) == 2
|
|
|
|
|
|
def test_relock_same_values_dedupes(monkeypatch):
|
|
tc, _gs, _ = _mount(monkeypatch)
|
|
cid = tc.post("/api/v1/canon", json=_canon_body()).json()["data"]["canon_id"]
|
|
r = tc.post(f"/api/v1/canon/{cid}/lock", json={"style": "3D", "palette": ["#ff0000"],
|
|
"product_name": "Nova", "brand_avoid": ["cheap"]})
|
|
assert r.json()["data"]["version"] == 1 # dedupe no-op
|
|
|
|
|
|
def test_owner_scoping_404_for_foreign_canon(monkeypatch):
|
|
tc, _gs, _ = _mount(monkeypatch, user_id="u1")
|
|
cid = tc.post("/api/v1/canon", json=_canon_body()).json()["data"]["canon_id"]
|
|
# Re-mount as a different caller against the same behaviour: create as u2.
|
|
tc2, _gs2, mod2 = _mount(monkeypatch, user_id="u2")
|
|
# u2's client cannot read u1's canon id (different DB), but same-DB scoping is
|
|
# exercised by getting a random id -> 404.
|
|
assert tc2.get(f"/api/v1/canon/{cid}").status_code == 404
|
|
|
|
|
|
def test_verify_stamped_asset_true_and_tamper_false(monkeypatch):
|
|
tc, _gs, _ = _mount(monkeypatch)
|
|
data = tc.post("/api/v1/canon", json=_canon_body()).json()["data"]
|
|
cid, chash, ver = data["canon_id"], data["canon_hash"], data["version"]
|
|
|
|
# Write a stamped final asset directly (mirrors the worker's asset write).
|
|
with _gs() as s:
|
|
meta = binding.stamp_asset_meta({}, canon_id=cid, canon_hash=chash, canon_version=ver)
|
|
s.add(Asset(id="a_ok", user_id="u1", kind="final", r2_key="final/ok.mp4", meta=meta))
|
|
# A tampered/foreign asset stamped under a bogus hash.
|
|
bad = binding.stamp_asset_meta({}, canon_id=cid, canon_hash="cv1|" + "0" * 64,
|
|
canon_version=ver)
|
|
s.add(Asset(id="a_bad", user_id="u1", kind="final", r2_key="final/bad.mp4", meta=bad))
|
|
# An unstamped asset.
|
|
s.add(Asset(id="a_raw", user_id="u1", kind="final", r2_key="final/raw.mp4", meta={}))
|
|
|
|
ok = tc.post(f"/api/v1/canon/{cid}/verify", json={"asset_id": "a_ok"})
|
|
assert ok.status_code == 200 and ok.json()["data"]["verified"] is True
|
|
|
|
bad = tc.post(f"/api/v1/canon/{cid}/verify", json={"asset_id": "a_bad"})
|
|
assert bad.json()["data"]["verified"] is False
|
|
assert bad.json()["data"]["reason"] == "wrong/edited canon"
|
|
|
|
raw = tc.post(f"/api/v1/canon/{cid}/verify", json={"asset_id": "a_raw"})
|
|
assert raw.json()["data"]["verified"] is False
|
|
assert raw.json()["data"]["reason"] == "asset not stamped"
|
|
|
|
|
|
def test_guard_returns_503_when_layer_unavailable(monkeypatch):
|
|
tc, _gs, mod = _mount(monkeypatch)
|
|
monkeypatch.setattr(mod, "_IMPORT_ERROR", RuntimeError("boom"))
|
|
assert tc.get("/api/v1/canon").status_code == 503
|