test_printer_id_zero.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. """A printer id of 0 is checked like any other printer id.
  2. Smart plug create and edit and virtual printer create tested the id for
  3. truthiness, so 0 skipped the "Printer not found" check: PostgreSQL then failed
  4. on the foreign key with a server error, SQLite stored a plug or virtual
  5. printer pointing at a printer that does not exist.
  6. """
  7. from __future__ import annotations
  8. import pytest
  9. from httpx import AsyncClient
  10. pytestmark = [pytest.mark.asyncio, pytest.mark.integration]
  11. async def test_creating_a_plug_for_printer_0_is_refused(async_client: AsyncClient):
  12. response = await async_client.post(
  13. "/api/v1/smart-plugs/", json={"name": "Plug", "ip_address": "192.168.1.100", "printer_id": 0}
  14. )
  15. assert response.status_code == 400, response.text
  16. assert response.json()["detail"] == "Printer not found"
  17. assert (await async_client.get("/api/v1/smart-plugs/")).json() == []
  18. async def test_moving_a_plug_to_printer_0_is_refused(async_client: AsyncClient):
  19. created = await async_client.post("/api/v1/smart-plugs/", json={"name": "Plug", "ip_address": "192.168.1.100"})
  20. assert created.status_code == 200, created.text
  21. plug_id = created.json()["id"]
  22. response = await async_client.patch(f"/api/v1/smart-plugs/{plug_id}", json={"printer_id": 0})
  23. assert response.status_code == 400, response.text
  24. assert response.json()["detail"] == "Printer not found"
  25. assert (await async_client.get(f"/api/v1/smart-plugs/{plug_id}")).json()["printer_id"] is None
  26. async def test_creating_a_virtual_printer_for_printer_0_is_refused(async_client: AsyncClient):
  27. response = await async_client.post(
  28. "/api/v1/virtual-printers",
  29. json={"name": "VP", "mode": "queue", "access_code": "12345678", "target_printer_id": 0},
  30. )
  31. assert response.status_code == 400, response.text
  32. assert response.json()["detail"] == "Printer with ID 0 not found"
  33. listed = (await async_client.get("/api/v1/virtual-printers")).json()
  34. printers = listed if isinstance(listed, list) else listed.get("printers", [])
  35. assert printers == []