Files
inventory/backend/tests/test_objects.py
jze9 f99949559c Add inventory audit mode with camera QR scanning
Lets an employee walk the premises, scan each object's QR to confirm
it's present and record where it was found (defaulting to the last
picked location), and track found/not-found progress for the walk.
Scanning a box's QR shows its contents read-only without affecting
the found/not-found counts.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-22 14:04:00 +05:00

85 lines
3.3 KiB
Python

import uuid
from httpx import AsyncClient
async def test_create_read_update_delete_object(client: AsyncClient, auth_headers: dict[str, str]) -> None:
inventory_number = f"TEST-{uuid.uuid4().hex[:8]}"
create_res = await client.post(
"/api/v1/objects",
headers=auth_headers,
json={"inventory_number": inventory_number, "name": "Test drill", "description": "for tests"},
)
assert create_res.status_code == 201
object_id = create_res.json()["id"]
public_res = await client.get(f"/api/v1/objects/{object_id}")
assert public_res.status_code == 200
assert public_res.json()["inventory_number"] == inventory_number
assert public_res.json()["photos"] == []
update_res = await client.put(
f"/api/v1/objects/{object_id}", headers=auth_headers, json={"name": "Renamed drill"}
)
assert update_res.status_code == 200
assert update_res.json()["name"] == "Renamed drill"
delete_res = await client.delete(f"/api/v1/objects/{object_id}", headers=auth_headers)
assert delete_res.status_code == 204
missing_res = await client.get(f"/api/v1/objects/{object_id}")
assert missing_res.status_code == 404
async def test_duplicate_inventory_number_conflicts(client: AsyncClient, auth_headers: dict[str, str]) -> None:
inventory_number = f"TEST-{uuid.uuid4().hex[:8]}"
payload = {"inventory_number": inventory_number, "name": "First"}
first = await client.post("/api/v1/objects", headers=auth_headers, json=payload)
assert first.status_code == 201
dup = await client.post(
"/api/v1/objects", headers=auth_headers, json={**payload, "name": "Second"}
)
assert dup.status_code == 409
await client.delete(f"/api/v1/objects/{first.json()['id']}", headers=auth_headers)
async def test_scan_marks_object_found_and_moves_it(client: AsyncClient, auth_headers: dict[str, str]) -> None:
inventory_number = f"TEST-{uuid.uuid4().hex[:8]}"
create_res = await client.post(
"/api/v1/objects", headers=auth_headers, json={"inventory_number": inventory_number, "name": "Scan test"}
)
object_id = create_res.json()["id"]
assert create_res.json()["last_seen_at"] is None
box_res = await client.post("/api/v1/boxes", headers=auth_headers, json={"name": "Audit room"})
box_id = box_res.json()["id"]
scan_res = await client.post(
f"/api/v1/objects/{object_id}/scan", headers=auth_headers, json={"box_id": box_id}
)
assert scan_res.status_code == 200
assert scan_res.json()["box_id"] == box_id
assert scan_res.json()["last_seen_at"] is not None
await client.delete(f"/api/v1/objects/{object_id}", headers=auth_headers)
await client.delete(f"/api/v1/boxes/{box_id}", headers=auth_headers)
async def test_object_qrcode(client: AsyncClient, auth_headers: dict[str, str]) -> None:
inventory_number = f"TEST-{uuid.uuid4().hex[:8]}"
create_res = await client.post(
"/api/v1/objects", headers=auth_headers, json={"inventory_number": inventory_number, "name": "QR test"}
)
object_id = create_res.json()["id"]
qr_res = await client.get(f"/api/v1/objects/{object_id}/qrcode.png")
assert qr_res.status_code == 200
assert qr_res.headers["content-type"] == "image/png"
assert qr_res.content[:8] == b"\x89PNG\r\n\x1a\n"
await client.delete(f"/api/v1/objects/{object_id}", headers=auth_headers)