removed non-needed code, fix tests, +1 new test

This commit is contained in:
bigcat88 2025-09-09 20:54:11 +03:00
parent 964de8a8ad
commit a9096f6c97
No known key found for this signature in database
GPG Key ID: 1F0BF0EC3CF22721
3 changed files with 46 additions and 49 deletions

View File

@ -66,19 +66,13 @@ async def list_assets(request: web.Request) -> web.Response:
@ROUTES.get(f"/api/assets/{{id:{UUID_RE}}}/content")
async def download_asset_content(request: web.Request) -> web.Response:
asset_info_id_raw = request.match_info.get("id", "")
try:
asset_info_id = str(uuid.UUID(asset_info_id_raw))
except Exception:
return _error_response(400, "INVALID_ID", f"AssetInfo id '{asset_info_id_raw}' is not a valid UUID.")
disposition = request.query.get("disposition", "attachment").lower().strip()
if disposition not in {"inline", "attachment"}:
disposition = "attachment"
try:
abs_path, content_type, filename = await assets_manager.resolve_asset_content_for_download(
asset_info_id=asset_info_id,
asset_info_id=str(uuid.UUID(request.match_info["id"])),
owner_id=UserManager.get_request_user_id(request),
)
except ValueError as ve:
@ -300,12 +294,7 @@ async def upload_asset(request: web.Request) -> web.Response:
@ROUTES.get(f"/api/assets/{{id:{UUID_RE}}}")
async def get_asset(request: web.Request) -> web.Response:
asset_info_id_raw = request.match_info.get("id", "")
try:
asset_info_id = str(uuid.UUID(asset_info_id_raw))
except Exception:
return _error_response(400, "INVALID_ID", f"AssetInfo id '{asset_info_id_raw}' is not a valid UUID.")
asset_info_id = str(uuid.UUID(request.match_info["id"]))
try:
result = await assets_manager.get_asset(
asset_info_id=asset_info_id,
@ -320,12 +309,7 @@ async def get_asset(request: web.Request) -> web.Response:
@ROUTES.put(f"/api/assets/{{id:{UUID_RE}}}")
async def update_asset(request: web.Request) -> web.Response:
asset_info_id_raw = request.match_info.get("id", "")
try:
asset_info_id = str(uuid.UUID(asset_info_id_raw))
except Exception:
return _error_response(400, "INVALID_ID", f"AssetInfo id '{asset_info_id_raw}' is not a valid UUID.")
asset_info_id = str(uuid.UUID(request.match_info["id"]))
try:
body = schemas_in.UpdateAssetBody.model_validate(await request.json())
except ValidationError as ve:
@ -350,12 +334,7 @@ async def update_asset(request: web.Request) -> web.Response:
@ROUTES.delete(f"/api/assets/{{id:{UUID_RE}}}")
async def delete_asset(request: web.Request) -> web.Response:
asset_info_id_raw = request.match_info.get("id", "")
try:
asset_info_id = str(uuid.UUID(asset_info_id_raw))
except Exception:
return _error_response(400, "INVALID_ID", f"AssetInfo id '{asset_info_id_raw}' is not a valid UUID.")
asset_info_id = str(uuid.UUID(request.match_info["id"]))
delete_content = request.query.get("delete_content")
delete_content = True if delete_content is None else delete_content.lower() not in {"0", "false", "no"}
@ -398,12 +377,7 @@ async def get_tags(request: web.Request) -> web.Response:
@ROUTES.post(f"/api/assets/{{id:{UUID_RE}}}/tags")
async def add_asset_tags(request: web.Request) -> web.Response:
asset_info_id_raw = request.match_info.get("id", "")
try:
asset_info_id = str(uuid.UUID(asset_info_id_raw))
except Exception:
return _error_response(400, "INVALID_ID", f"AssetInfo id '{asset_info_id_raw}' is not a valid UUID.")
asset_info_id = str(uuid.UUID(request.match_info["id"]))
try:
payload = await request.json()
data = schemas_in.TagsAdd.model_validate(payload)
@ -429,12 +403,7 @@ async def add_asset_tags(request: web.Request) -> web.Response:
@ROUTES.delete(f"/api/assets/{{id:{UUID_RE}}}/tags")
async def delete_asset_tags(request: web.Request) -> web.Response:
asset_info_id_raw = request.match_info.get("id", "")
try:
asset_info_id = str(uuid.UUID(asset_info_id_raw))
except Exception:
return _error_response(400, "INVALID_ID", f"AssetInfo id '{asset_info_id_raw}' is not a valid UUID.")
asset_info_id = str(uuid.UUID(request.match_info["id"]))
try:
payload = await request.json()
data = schemas_in.TagsRemove.model_validate(payload)

View File

@ -30,16 +30,21 @@ async def test_download_missing_file_returns_404(
):
# Remove the underlying file then attempt download.
# We initialize fixture without additional tags to know exactly the asset file path.
aid = seeded_asset["id"]
async with http.get(f"{api_base}/api/assets/{aid}") as rg:
detail = await rg.json()
assert rg.status == 200
rel_inside_category = detail["name"]
abs_path = comfy_tmp_base_dir / "models" / "checkpoints" / rel_inside_category
if abs_path.exists():
abs_path.unlink()
try:
aid = seeded_asset["id"]
async with http.get(f"{api_base}/api/assets/{aid}") as rg:
detail = await rg.json()
assert rg.status == 200
rel_inside_category = detail["name"]
abs_path = comfy_tmp_base_dir / "models" / "checkpoints" / rel_inside_category
if abs_path.exists():
abs_path.unlink()
async with http.get(f"{api_base}/api/assets/{aid}/content") as r2:
body = await r2.json()
assert r2.status == 404
assert body["error"]["code"] == "FILE_NOT_FOUND"
async with http.get(f"{api_base}/api/assets/{aid}/content") as r2:
body = await r2.json()
assert r2.status == 404
assert body["error"]["code"] == "FILE_NOT_FOUND"
finally:
# We created asset without the "unit-tests" tag(see `autoclean_unit_test_assets`), we need to clear it manually.
async with http.delete(f"{api_base}/api/assets/{aid}") as dr:
await dr.read()

View File

@ -0,0 +1,23 @@
import aiohttp
import pytest
@pytest.mark.asyncio
async def test_get_update_download_bad_ids(http: aiohttp.ClientSession, api_base: str):
# All endpoints should be not found, as we UUID regex directly in the route definition.
bad_id = "not-a-uuid"
async with http.get(f"{api_base}/api/assets/{bad_id}") as r1:
assert r1.status == 404
async with http.get(f"{api_base}/api/assets/{bad_id}/content") as r3:
assert r3.status == 404
@pytest.mark.asyncio
async def test_update_requires_at_least_one_field(http: aiohttp.ClientSession, api_base: str, seeded_asset: dict):
aid = seeded_asset["id"]
async with http.put(f"{api_base}/api/assets/{aid}", json={}) as r:
body = await r.json()
assert r.status == 400
assert body["error"]["code"] == "INVALID_BODY"