diff --git a/backend/main.py b/backend/main.py index a434e29..a507509 100644 --- a/backend/main.py +++ b/backend/main.py @@ -91,6 +91,13 @@ def clean_empty_strings(data: dict, *keys: str) -> dict: return data +def require_row(result, label: str): + """Raise 404 when a PATCH/DELETE matched no row (PostgREST returns ``[]``).""" + if result == []: + raise HTTPException(status_code=404, detail=f"{label} not found") + return result + + def model_data(model: BaseModel) -> dict: return model.model_dump() @@ -142,12 +149,12 @@ def create_customer(c: Customer): @app.put("/customers/{id}") def update_customer(id: str, c: Customer): - return db_patch("customers", id, model_data(c)) + return require_row(db_patch("customers", id, model_data(c)), "Customer") @app.delete("/customers/{id}") def delete_customer(id: str): - return db_delete("customers", id) + return require_row(db_delete("customers", id), "Customer") # --- Devices --- @@ -163,7 +170,9 @@ def create_device(d: Device): @app.put("/devices/{id}") def update_device(id: str, d: Device): - return db_patch("devices", id, clean_empty_strings(model_data(d), "customer_id")) + return require_row( + db_patch("devices", id, clean_empty_strings(model_data(d), "customer_id")), "Device" + ) # --- Tickets --- @@ -235,7 +244,7 @@ def create_ticket(t: Ticket): def update_ticket(id: str, t: Ticket): data = clean_empty_strings(model_data(t), "customer_id", "device_id", "assigned_user_id") old = db_get("tickets", f"id=eq.{escape_filter_value(id)}&select=status") - result = db_patch("tickets", id, data) + result = require_row(db_patch("tickets", id, data), "Ticket") if old and old[0]["status"] != data["status"]: db_post( "ticket_history", @@ -279,7 +288,7 @@ def create_rma(r: RMA): @app.put("/rmas/{id}") def update_rma(id: str, r: RMA): - return db_patch("rmas", id, model_data(r)) + return require_row(db_patch("rmas", id, model_data(r)), "RMA") # --- Dashboard --- diff --git a/backend/test_main.py b/backend/test_main.py index ebd5e80..c04af06 100644 --- a/backend/test_main.py +++ b/backend/test_main.py @@ -559,3 +559,61 @@ def test_ai_suggest_maps_errors_to_http_responses(client, error, expected_status res = test_client.post("/ai/suggest", json={"ticket_id": "t1"}) assert res.status_code == expected_status + + +@pytest.mark.parametrize( + ("method", "path", "body", "patched", "label"), + [ + ("put", "/customers/{id}", {"name": "A"}, "db_patch", "Customer"), + ("delete", "/customers/{id}", None, "db_delete", "Customer"), + ("put", "/devices/{id}", {"serial_number": "S"}, "db_patch", "Device"), + ( + "put", + "/rmas/{id}", + {"ticket_id": "00000000-0000-4000-8000-0000000000a1", "rma_number": "R"}, + "db_patch", + "RMA", + ), + ("put", "/tickets/{id}", {"title": "T"}, "db_patch", "Ticket"), + ], +) +def test_update_or_delete_missing_row_returns_404( + client, monkeypatch, method, path, body, patched, label +): + test_client, main = client + missing_id = "00000000-0000-4000-8000-00000000ffff" + monkeypatch.setattr(main, "db_get", lambda *a, **k: []) + mutation = MagicMock(return_value=[]) + history_write = MagicMock() + monkeypatch.setattr(main, patched, mutation) + monkeypatch.setattr(main, "db_post", history_write) + + response = getattr(test_client, method)( + path.format(id=missing_id), **({"json": body} if body else {}) + ) + + assert response.status_code == 404 + assert response.json() == {"detail": f"{label} not found"} + mutation.assert_called_once() + history_write.assert_not_called() + + +def test_update_ticket_deleted_after_lookup_returns_404_without_history(client, monkeypatch): + test_client, main = client + ticket_id = "00000000-0000-4000-8000-0000000000a1" + lookup = MagicMock(return_value=[{"status": "Open"}]) + mutation = MagicMock(return_value=[]) + history_write = MagicMock() + monkeypatch.setattr(main, "db_get", lookup) + monkeypatch.setattr(main, "db_patch", mutation) + monkeypatch.setattr(main, "db_post", history_write) + + response = test_client.put( + f"/tickets/{ticket_id}", json={"title": "Printer fixed", "status": "Closed"} + ) + + assert response.status_code == 404 + assert response.json() == {"detail": "Ticket not found"} + lookup.assert_called_once_with("tickets", f"id=eq.{ticket_id}&select=status") + mutation.assert_called_once() + history_write.assert_not_called()