365 lines
15 KiB
Python
365 lines
15 KiB
Python
|
|
import pytest
|
||
|
|
from sqlalchemy import select
|
||
|
|
|
||
|
|
|
||
|
|
async def folder(client, name, parent_id=None):
|
||
|
|
response = await client.post("/api/folders", json=dict(name=name, parent_id=parent_id))
|
||
|
|
assert response.status_code == 201, response.text
|
||
|
|
return response.json()
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.asyncio
|
||
|
|
async def test_tree_unique_cycle_and_isolation(system, other_client):
|
||
|
|
c = system.client
|
||
|
|
root = await folder(c, "root")
|
||
|
|
child = await folder(c, "child", root["id"])
|
||
|
|
assert (await c.post("/api/folders", json={"name": "root"})).status_code == 409
|
||
|
|
assert (
|
||
|
|
await c.patch("/api/folders/" + root["id"], json={"parent_id": child["id"]})
|
||
|
|
).status_code == 409
|
||
|
|
assert (
|
||
|
|
await other_client.post("/api/folders", json={"name": "bad", "parent_id": root["id"]})
|
||
|
|
).status_code == 404
|
||
|
|
assert (await other_client.get("/api/folders")).json() == {"items": []}
|
||
|
|
assert (await c.patch("/api/folders/" + child["id"], json={"parent_id": None})).json()[
|
||
|
|
"parent_id"
|
||
|
|
] is None
|
||
|
|
assert (await c.patch("/api/folders/" + child["id"], json={"name": "root"})).status_code == 409
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.asyncio
|
||
|
|
async def test_asset_membership_atomic_and_delete_preserves_content(
|
||
|
|
system, other_client, png_bytes
|
||
|
|
):
|
||
|
|
from app.models import Asset
|
||
|
|
|
||
|
|
c = system.client
|
||
|
|
root = await folder(c, "root")
|
||
|
|
child = await folder(c, "child", root["id"])
|
||
|
|
foreign = await folder(other_client, "foreign")
|
||
|
|
asset = (await c.post("/api/assets", files={"file": ("a.png", png_bytes, "image/png")})).json()
|
||
|
|
foreign_asset = (
|
||
|
|
await other_client.post("/api/assets", files={"file": ("b.png", png_bytes, "image/png")})
|
||
|
|
).json()
|
||
|
|
body = dict(kind="assets", ids=[asset["id"]], folder_id=root["id"])
|
||
|
|
assert (await c.post("/api/folders/assign", json=body)).status_code == 200
|
||
|
|
body.update(folder_id=child["id"], mode="add")
|
||
|
|
assert (await c.post("/api/folders/assign", json=body)).status_code == 200
|
||
|
|
assert set((await c.get("/api/assets/" + asset["id"])).json()["folder_ids"]) == {
|
||
|
|
root["id"],
|
||
|
|
child["id"],
|
||
|
|
}
|
||
|
|
body.update(ids=[asset["id"], foreign_asset["id"]], mode="move")
|
||
|
|
assert (await c.post("/api/folders/assign", json=body)).status_code == 404
|
||
|
|
body.update(ids=[asset["id"]], folder_id=foreign["id"])
|
||
|
|
assert (await c.post("/api/folders/assign", json=body)).status_code == 404
|
||
|
|
async with system.app.state.db.session() as s:
|
||
|
|
record = await s.get(Asset, asset["id"])
|
||
|
|
path = system.settings.asset_root / record.storage_key
|
||
|
|
assert path.exists()
|
||
|
|
assert (await c.delete("/api/folders/" + root["id"])).status_code == 204
|
||
|
|
assert (await c.get("/api/assets/" + asset["id"])).json()["folder_ids"] == []
|
||
|
|
assert path.exists()
|
||
|
|
assert (await c.get("/api/assets/" + foreign_asset["id"])).status_code == 404
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.asyncio
|
||
|
|
async def test_helpers_no_commit_sync_dto_missing_and_foreign(system, other_client):
|
||
|
|
from app.models import Asset, User
|
||
|
|
from app.services.folders import create_folder, assign_asset_folders, add_asset_folder
|
||
|
|
from app.services.snapshots import asset_dto
|
||
|
|
|
||
|
|
foreign = await folder(other_client, "foreign")
|
||
|
|
async with system.app.state.db.session() as s:
|
||
|
|
owner = await s.scalar(select(User.id).where(User.username == "admin"))
|
||
|
|
f = await create_folder(s, owner, "story")
|
||
|
|
f2 = await create_folder(s, owner, "story", unique_name=True)
|
||
|
|
assert f.name != f2.name
|
||
|
|
asset = Asset(
|
||
|
|
owner_id=owner,
|
||
|
|
name="a",
|
||
|
|
storage_key="stable.png",
|
||
|
|
kind="image",
|
||
|
|
mime_type="image/png",
|
||
|
|
size_bytes=1,
|
||
|
|
sha256="a" * 64,
|
||
|
|
)
|
||
|
|
s.add(asset)
|
||
|
|
await s.flush()
|
||
|
|
assert asset_dto(asset)["folder_ids"] == []
|
||
|
|
await assign_asset_folders(s, asset, [f.id])
|
||
|
|
await add_asset_folder(s, asset, f2.id)
|
||
|
|
assert set(asset_dto(asset)["folder_ids"]) == {f.id, f2.id}
|
||
|
|
await add_asset_folder(s, asset, "missing", allow_missing=True)
|
||
|
|
from fastapi import HTTPException
|
||
|
|
|
||
|
|
with pytest.raises(HTTPException):
|
||
|
|
await add_asset_folder(s, asset, foreign["id"], allow_missing=True)
|
||
|
|
await s.rollback()
|
||
|
|
assert (await system.client.get("/api/folders")).json()["items"] == []
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.asyncio
|
||
|
|
async def test_classification_versions_output_freeze_and_delete_business_rows(system):
|
||
|
|
from app.models import Job, WorkflowDraft, WorkflowRun, User
|
||
|
|
|
||
|
|
c = system.client
|
||
|
|
target = await folder(c, "target")
|
||
|
|
moved = await folder(c, "moved")
|
||
|
|
async with system.app.state.db.session() as s:
|
||
|
|
owner = await s.scalar(select(User.id).where(User.username == "admin"))
|
||
|
|
job = Job(
|
||
|
|
owner_id=owner,
|
||
|
|
task_type="image.generate",
|
||
|
|
state="queued",
|
||
|
|
phase="queued",
|
||
|
|
input_json={},
|
||
|
|
parameters={},
|
||
|
|
request_hash="a" * 64,
|
||
|
|
upstream_key="unique",
|
||
|
|
folder_id=target["id"],
|
||
|
|
output_folder_id=target["id"],
|
||
|
|
)
|
||
|
|
draft = WorkflowDraft(owner_id=owner, name="story", folder_id=target["id"])
|
||
|
|
s.add_all([job, draft])
|
||
|
|
await s.flush()
|
||
|
|
run = WorkflowRun(
|
||
|
|
owner_id=owner,
|
||
|
|
draft_id=draft.id,
|
||
|
|
draft_revision=1,
|
||
|
|
spec_snapshot={"shots": []},
|
||
|
|
spec_hash="b" * 64,
|
||
|
|
folder_id=target["id"],
|
||
|
|
output_folder_id=target["id"],
|
||
|
|
)
|
||
|
|
s.add(run)
|
||
|
|
await s.commit()
|
||
|
|
ids = {"jobs": job.id, "drafts": draft.id, "runs": run.id}
|
||
|
|
for kind, record_id in ids.items():
|
||
|
|
assert (
|
||
|
|
await c.post(
|
||
|
|
"/api/folders/assign", json=dict(kind=kind, ids=[record_id], folder_id=moved["id"])
|
||
|
|
)
|
||
|
|
).status_code == 200
|
||
|
|
async with system.app.state.db.session() as s:
|
||
|
|
for model, key in [(Job, "jobs"), (WorkflowDraft, "drafts"), (WorkflowRun, "runs")]:
|
||
|
|
row = await s.get(model, ids[key])
|
||
|
|
assert row.folder_id == moved["id"]
|
||
|
|
assert row.version == 2
|
||
|
|
if model != WorkflowDraft:
|
||
|
|
assert row.output_folder_id == target["id"]
|
||
|
|
assert (await c.delete("/api/folders/" + target["id"])).status_code == 204
|
||
|
|
assert (await c.delete("/api/folders/" + moved["id"])).status_code == 204
|
||
|
|
async with system.app.state.db.session() as s:
|
||
|
|
for model, key in [(Job, "jobs"), (WorkflowDraft, "drafts"), (WorkflowRun, "runs")]:
|
||
|
|
row = await s.get(model, ids[key])
|
||
|
|
assert row is not None and row.folder_id is None
|
||
|
|
assert row.version == (3 if model == WorkflowDraft else 4)
|
||
|
|
if model != WorkflowDraft:
|
||
|
|
assert row.output_folder_id is None
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.asyncio
|
||
|
|
async def test_folder_filter_and_request_validation(system, png_bytes):
|
||
|
|
from app.models import Asset, User
|
||
|
|
from app.services.folders import folder_filter
|
||
|
|
|
||
|
|
c = system.client
|
||
|
|
target = await folder(c, "target")
|
||
|
|
asset = (await c.post("/api/assets", files={"file": ("a.png", png_bytes, "image/png")})).json()
|
||
|
|
body = dict(kind="assets", ids=[asset["id"]], folder_id=target["id"])
|
||
|
|
assert (await c.post("/api/folders/assign", json=body)).status_code == 200
|
||
|
|
async with system.app.state.db.session() as s:
|
||
|
|
owner = await s.scalar(select(User.id).where(User.username == "admin"))
|
||
|
|
for selected, expected in [
|
||
|
|
("", [asset["id"]]),
|
||
|
|
("unclassified", []),
|
||
|
|
(target["id"], [asset["id"]]),
|
||
|
|
]:
|
||
|
|
condition = await folder_filter(s, Asset, owner, selected)
|
||
|
|
assert list((await s.scalars(select(Asset.id).where(condition))).all()) == expected
|
||
|
|
assert (await c.post("/api/folders", json={"name": " "})).status_code == 422
|
||
|
|
assert (await c.patch("/api/folders/" + target["id"], json={"name": None})).status_code == 422
|
||
|
|
body.update(kind="jobs", mode="add")
|
||
|
|
assert (await c.post("/api/folders/assign", json=body)).status_code == 422
|
||
|
|
body.update(ids=["x"] * 201)
|
||
|
|
assert (await c.post("/api/folders/assign", json=body)).status_code == 422
|
||
|
|
headers = dict(c.headers)
|
||
|
|
del c.headers["X-CSRF-Token"]
|
||
|
|
assert (await c.delete("/api/folders/" + target["id"])).status_code == 403
|
||
|
|
c.headers.update(headers)
|
||
|
|
(system.settings.state_root / "maintenance").write_text("on")
|
||
|
|
assert (await c.post("/api/folders", json={"name": "blocked"})).status_code == 503
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.asyncio
|
||
|
|
async def test_assignment_run_event_keeps_existing_workflow_contract(system):
|
||
|
|
from app.models import User, WorkflowDraft, WorkflowRun, Event
|
||
|
|
|
||
|
|
c = system.client
|
||
|
|
target = await folder(c, "target")
|
||
|
|
async with system.app.state.db.session() as s:
|
||
|
|
owner = await s.scalar(select(User.id).where(User.username == "admin"))
|
||
|
|
draft = WorkflowDraft(owner_id=owner, name="draft")
|
||
|
|
s.add(draft)
|
||
|
|
await s.flush()
|
||
|
|
run = WorkflowRun(
|
||
|
|
owner_id=owner,
|
||
|
|
draft_id=draft.id,
|
||
|
|
draft_revision=1,
|
||
|
|
spec_snapshot={"shots": []},
|
||
|
|
spec_hash="a" * 64,
|
||
|
|
)
|
||
|
|
s.add(run)
|
||
|
|
await s.commit()
|
||
|
|
run_id = run.id
|
||
|
|
assert (
|
||
|
|
await c.post(
|
||
|
|
"/api/folders/assign", json=dict(kind="runs", ids=[run_id], folder_id=target["id"])
|
||
|
|
)
|
||
|
|
).status_code == 200
|
||
|
|
async with system.app.state.db.session() as s:
|
||
|
|
event = await s.scalar(
|
||
|
|
select(Event)
|
||
|
|
.where(Event.owner_id == owner, Event.event_type == "workflow.updated")
|
||
|
|
.order_by(Event.id.desc())
|
||
|
|
)
|
||
|
|
assert event.event_type == "workflow.updated"
|
||
|
|
assert event.data["run"]["folder_id"] == target["id"]
|
||
|
|
assert event.data["run"]["version"] == 2
|
||
|
|
assert "steps" in event.data and "jobs" in event.data
|
||
|
|
|
||
|
|
|
||
|
|
async def saved_events(system, after=0):
|
||
|
|
from app.models import Event
|
||
|
|
|
||
|
|
async with system.app.state.db.session() as s:
|
||
|
|
return list(
|
||
|
|
(await s.scalars(select(Event).where(Event.id > after).order_by(Event.id))).all()
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.asyncio
|
||
|
|
async def test_folder_crud_and_assignment_emit_owner_scoped_events(system, other_client, png_bytes):
|
||
|
|
c = system.client
|
||
|
|
foreign = await folder(other_client, "foreign")
|
||
|
|
baseline = max((e.id for e in await saved_events(system)), default=0)
|
||
|
|
parent = await folder(c, "parent")
|
||
|
|
child = await folder(c, "child", parent["id"])
|
||
|
|
assert (
|
||
|
|
await c.patch("/api/folders/" + parent["id"], json={"name": "renamed"})
|
||
|
|
).status_code == 200
|
||
|
|
asset = (await c.post("/api/assets", files={"file": ("a.png", png_bytes, "image/png")})).json()
|
||
|
|
assert (
|
||
|
|
await c.post(
|
||
|
|
"/api/folders/assign",
|
||
|
|
json=dict(kind="assets", ids=[asset["id"]], folder_id=child["id"]),
|
||
|
|
)
|
||
|
|
).status_code == 200
|
||
|
|
assert (await c.delete("/api/folders/" + parent["id"])).status_code == 204
|
||
|
|
events = await saved_events(system, baseline)
|
||
|
|
created = [e for e in events if e.event_type == "folder.created"]
|
||
|
|
assert {e.data["folder"]["id"] for e in created} == {parent["id"], child["id"]}
|
||
|
|
owner = created[0].owner_id
|
||
|
|
assert all(e.owner_id == owner for e in events)
|
||
|
|
assert foreign["id"] not in str([e.data for e in events])
|
||
|
|
assert any(
|
||
|
|
e.event_type == "folder.updated" and e.data["folder"]["name"] == "renamed" for e in events
|
||
|
|
)
|
||
|
|
assert any(e.event_type == "folder.contents_changed" for e in events)
|
||
|
|
deleted = [e for e in events if e.event_type == "folder.deleted"]
|
||
|
|
assert {e.data["folder_id"] for e in deleted} == {parent["id"], child["id"]}
|
||
|
|
updates = [
|
||
|
|
e
|
||
|
|
for e in events
|
||
|
|
if e.event_type == "asset.updated" and e.data["asset"]["id"] == asset["id"]
|
||
|
|
]
|
||
|
|
assert updates[-1].data["asset"]["folder_ids"] == []
|
||
|
|
assert "storage_key" not in str([e.data for e in events])
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.asyncio
|
||
|
|
async def test_story_classification_emits_story_contract_without_revision_edit(system):
|
||
|
|
from test_story_studio import story_spec
|
||
|
|
|
||
|
|
c = system.client
|
||
|
|
created = await c.post(
|
||
|
|
"/api/stories", json={"name": "story", "spec": story_spec(), "create_folder": False}
|
||
|
|
)
|
||
|
|
assert created.status_code == 201, created.text
|
||
|
|
story = created.json()
|
||
|
|
target = await folder(c, "target")
|
||
|
|
baseline = max((e.id for e in await saved_events(system)), default=0)
|
||
|
|
assert (
|
||
|
|
await c.post(
|
||
|
|
"/api/folders/assign",
|
||
|
|
json=dict(kind="drafts", ids=[story["id"]], folder_id=target["id"]),
|
||
|
|
)
|
||
|
|
).status_code == 200
|
||
|
|
events = await saved_events(system, baseline)
|
||
|
|
updates = [e for e in events if e.event_type == "story.updated"]
|
||
|
|
assert len(updates) == 1
|
||
|
|
value = updates[0].data["story"]
|
||
|
|
assert value["id"] == story["id"] and value["folder_id"] == target["id"]
|
||
|
|
assert value["version"] == story["version"] + 1
|
||
|
|
assert value["revision"] == story["revision"]
|
||
|
|
assert not any(e.event_type == "draft.updated" for e in events)
|
||
|
|
baseline = max(e.id for e in events)
|
||
|
|
assert (await c.delete("/api/folders/" + target["id"])).status_code == 204
|
||
|
|
events = await saved_events(system, baseline)
|
||
|
|
value = next(e.data["story"] for e in events if e.event_type == "story.updated")
|
||
|
|
assert value["folder_id"] is None and value["version"] == story["version"] + 2
|
||
|
|
assert value["revision"] == story["revision"]
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.asyncio
|
||
|
|
async def test_folder_delete_emits_job_draft_run_current_versions(system):
|
||
|
|
from app.models import Job, WorkflowDraft, WorkflowRun, User
|
||
|
|
|
||
|
|
c = system.client
|
||
|
|
parent = await folder(c, "parent")
|
||
|
|
target = await folder(c, "child", parent["id"])
|
||
|
|
async with system.app.state.db.session() as s:
|
||
|
|
owner = await s.scalar(select(User.id).where(User.username == "admin"))
|
||
|
|
job = Job(
|
||
|
|
owner_id=owner,
|
||
|
|
task_type="image.generate",
|
||
|
|
state="queued",
|
||
|
|
phase="queued",
|
||
|
|
input_json={},
|
||
|
|
parameters={},
|
||
|
|
request_hash="a" * 64,
|
||
|
|
upstream_key="delete-event",
|
||
|
|
output_folder_id=target["id"],
|
||
|
|
)
|
||
|
|
draft = WorkflowDraft(owner_id=owner, name="draft", folder_id=target["id"])
|
||
|
|
s.add_all([job, draft])
|
||
|
|
await s.flush()
|
||
|
|
run = WorkflowRun(
|
||
|
|
owner_id=owner,
|
||
|
|
draft_id=draft.id,
|
||
|
|
draft_revision=1,
|
||
|
|
spec_snapshot={"shots": []},
|
||
|
|
spec_hash="b" * 64,
|
||
|
|
output_folder_id=target["id"],
|
||
|
|
)
|
||
|
|
s.add(run)
|
||
|
|
await s.commit()
|
||
|
|
job_id, draft_id, run_id = job.id, draft.id, run.id
|
||
|
|
baseline = max((e.id for e in await saved_events(system)), default=0)
|
||
|
|
assert (await c.delete("/api/folders/" + parent["id"])).status_code == 204
|
||
|
|
events = await saved_events(system, baseline)
|
||
|
|
values = {e.event_type: e.data for e in events}
|
||
|
|
job_dto = values["job.updated"]["job"]
|
||
|
|
assert (
|
||
|
|
job_dto["id"] == job_id and job_dto["output_folder_id"] is None and job_dto["version"] == 2
|
||
|
|
)
|
||
|
|
draft_dto = values["draft.updated"]["draft"]
|
||
|
|
assert (
|
||
|
|
draft_dto["id"] == draft_id and draft_dto["folder_id"] is None and draft_dto["version"] == 2
|
||
|
|
)
|
||
|
|
run_dto = values["workflow.updated"]["run"]
|
||
|
|
assert (
|
||
|
|
run_dto["id"] == run_id and run_dto["output_folder_id"] is None and run_dto["version"] == 2
|
||
|
|
)
|