test_locations_api.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. """Integration tests for /inventory/locations (#1004)."""
  2. import pytest
  3. from httpx import AsyncClient
  4. from sqlalchemy.ext.asyncio import AsyncSession
  5. from backend.app.models.location import Location
  6. from backend.app.services.location_service import assign_location_name
  7. @pytest.mark.asyncio
  8. @pytest.mark.integration
  9. async def test_locations_crud_and_spool_link(async_client: AsyncClient, db_session: AsyncSession):
  10. create_resp = await async_client.post("/api/v1/inventory/locations", json={"name": "Shelf A"})
  11. assert create_resp.status_code == 201
  12. loc = create_resp.json()
  13. assert loc["name"] == "Shelf A"
  14. assert loc["spool_count"] == 0
  15. dup_resp = await async_client.post("/api/v1/inventory/locations", json={"name": "shelf a"})
  16. assert dup_resp.status_code == 409
  17. spool_resp = await async_client.post(
  18. "/api/v1/inventory/spools",
  19. json={"material": "PLA", "location_id": loc["id"]},
  20. )
  21. assert spool_resp.status_code == 200
  22. spool = spool_resp.json()
  23. assert spool["location_id"] == loc["id"]
  24. assert spool["storage_location"] == "Shelf A"
  25. list_resp = await async_client.get("/api/v1/inventory/locations")
  26. assert list_resp.status_code == 200
  27. listed = {item["id"]: item for item in list_resp.json()}
  28. assert listed[loc["id"]]["spool_count"] == 1
  29. delete_resp = await async_client.delete(f"/api/v1/inventory/locations/{loc['id']}")
  30. assert delete_resp.status_code == 409
  31. clear_resp = await async_client.patch(
  32. f"/api/v1/inventory/spools/{spool['id']}",
  33. json={"location_id": None},
  34. )
  35. assert clear_resp.status_code == 200
  36. delete_resp2 = await async_client.delete(f"/api/v1/inventory/locations/{loc['id']}")
  37. assert delete_resp2.status_code == 200
  38. @pytest.mark.asyncio
  39. @pytest.mark.integration
  40. async def test_rename_location_updates_spool_count(async_client: AsyncClient):
  41. create_resp = await async_client.post("/api/v1/inventory/locations", json={"name": "Old Name"})
  42. loc = create_resp.json()
  43. await async_client.post(
  44. "/api/v1/inventory/spools",
  45. json={"material": "PLA", "location_id": loc["id"]},
  46. )
  47. list_before = await async_client.get("/api/v1/inventory/locations")
  48. by_id = {item["id"]: item for item in list_before.json()}
  49. assert by_id[loc["id"]]["spool_count"] == 1
  50. rename_resp = await async_client.patch(
  51. f"/api/v1/inventory/locations/{loc['id']}",
  52. json={"name": "New Name"},
  53. )
  54. assert rename_resp.status_code == 200
  55. assert rename_resp.json()["name"] == "New Name"
  56. assert rename_resp.json()["spool_count"] == 1
  57. @pytest.mark.asyncio
  58. @pytest.mark.integration
  59. async def test_rename_location_collision_returns_409(async_client: AsyncClient):
  60. first = await async_client.post("/api/v1/inventory/locations", json={"name": "Shelf A"})
  61. second = await async_client.post("/api/v1/inventory/locations", json={"name": "Shelf B"})
  62. assert first.status_code == 201
  63. assert second.status_code == 201
  64. collision = await async_client.patch(
  65. f"/api/v1/inventory/locations/{second.json()['id']}",
  66. json={"name": "Shelf A"},
  67. )
  68. assert collision.status_code == 409
  69. assert collision.json()["detail"] == "A location with this name already exists"
  70. @pytest.mark.asyncio
  71. @pytest.mark.integration
  72. async def test_create_location_duplicate_after_commit_returns_409(async_client: AsyncClient):
  73. """Second create with the same name_key must return 409, not 500."""
  74. first = await async_client.post("/api/v1/inventory/locations", json={"name": "Race Shelf"})
  75. second = await async_client.post("/api/v1/inventory/locations", json={"name": "race shelf"})
  76. assert first.status_code == 201
  77. assert second.status_code == 409
  78. assert second.json()["detail"] == "A location with this name already exists"
  79. @pytest.mark.asyncio
  80. @pytest.mark.integration
  81. async def test_list_locations_is_read_only(async_client: AsyncClient, db_session: AsyncSession):
  82. """GET /locations is a pure read — no catalog rows appear without explicit writes."""
  83. from sqlalchemy import func, select
  84. loc = Location()
  85. assign_location_name(loc, "Local Only")
  86. db_session.add(loc)
  87. await db_session.commit()
  88. before = await db_session.scalar(select(func.count()).select_from(Location))
  89. resp = await async_client.get("/api/v1/inventory/locations")
  90. after = await db_session.scalar(select(func.count()).select_from(Location))
  91. assert resp.status_code == 200
  92. assert len(resp.json()) == 1
  93. assert before == after == 1
  94. @pytest.mark.asyncio
  95. @pytest.mark.integration
  96. async def test_update_location_404_on_unknown_id(async_client: AsyncClient):
  97. resp = await async_client.patch(
  98. "/api/v1/inventory/locations/99999",
  99. json={"name": "Ghost"},
  100. )
  101. assert resp.status_code == 404
  102. assert resp.json()["detail"] == "Location not found"
  103. @pytest.mark.asyncio
  104. @pytest.mark.integration
  105. async def test_delete_location_404_on_unknown_id(async_client: AsyncClient):
  106. resp = await async_client.delete("/api/v1/inventory/locations/99999")
  107. assert resp.status_code == 404
  108. assert resp.json()["detail"] == "Location not found"
  109. @pytest.mark.asyncio
  110. @pytest.mark.integration
  111. async def test_locations_routes_require_auth_when_enabled(async_client: AsyncClient):
  112. """All five /locations endpoints must return 401 when auth is enabled and
  113. no credentials are presented. Mirror of the pattern from
  114. test_queue_start_user_attribution._enable_auth_with_admin — required by
  115. project policy: every permission-gated route gets a fail-closed test on
  116. first ship, no follow-ups (the two CVSS 9.8/9.9 advisories shipped from
  117. this exact gap)."""
  118. await async_client.post(
  119. "/api/v1/auth/setup",
  120. json={
  121. "auth_enabled": True,
  122. "admin_username": "locations1505admin",
  123. "admin_password": "AdminPass1!",
  124. },
  125. )
  126. # GET /locations — read-gated
  127. list_resp = await async_client.get("/api/v1/inventory/locations")
  128. assert list_resp.status_code == 401, list_resp.text
  129. # POST /locations — write-gated
  130. create_resp = await async_client.post("/api/v1/inventory/locations", json={"name": "Locked"})
  131. assert create_resp.status_code == 401, create_resp.text
  132. # PATCH /locations/{id} — write-gated. Use a synthetic id; the auth gate
  133. # runs before the not-found check, so 401 is the correct expectation even
  134. # when the id doesn't exist.
  135. patch_resp = await async_client.patch("/api/v1/inventory/locations/99999", json={"name": "Locked2"})
  136. assert patch_resp.status_code == 401, patch_resp.text
  137. # DELETE /locations/{id} — write-gated
  138. delete_resp = await async_client.delete("/api/v1/inventory/locations/99999")
  139. assert delete_resp.status_code == 401, delete_resp.text