test_queue_filament_scope_3522.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. """Queueing a job refuses filaments the user's groups don't allow (#3522)."""
  2. from __future__ import annotations
  3. import zipfile
  4. from pathlib import Path
  5. from unittest.mock import AsyncMock, patch
  6. import pytest
  7. from httpx import AsyncClient
  8. from sqlalchemy import update
  9. from backend.app.core.config import settings
  10. from backend.app.models.library import LibraryFile
  11. from backend.app.models.print_queue import PrintQueueItem
  12. pytestmark = [pytest.mark.asyncio, pytest.mark.integration]
  13. QUEUE_PERMISSIONS = [
  14. "printers:read",
  15. "queue:read_all",
  16. "queue:create",
  17. "archives:read_all",
  18. "archives:reprint_all",
  19. "library:read_all",
  20. ]
  21. def _auth(jwt: str) -> dict[str, str]:
  22. return {"Authorization": f"Bearer {jwt}"}
  23. @pytest.fixture(autouse=True)
  24. def _base_dir(tmp_path, monkeypatch):
  25. # Archives resolve under settings.base_dir, which is the repo root in tests.
  26. monkeypatch.setattr(settings, "base_dir", tmp_path)
  27. PETG_MATTE = '<filament id="1" used_g="5" type="PETG" tray_info_idx="GFG03" color="#FFFFFF"/>'
  28. PLA = '<filament id="1" used_g="5" type="PLA" tray_info_idx="GFA00" color="#FFFFFF"/>'
  29. def _write_3mf(rel: str, filaments: str) -> None:
  30. path = Path(settings.base_dir) / rel
  31. path.parent.mkdir(parents=True, exist_ok=True)
  32. with zipfile.ZipFile(path, "w") as zf:
  33. # A plate G-code member, or the queue refuses the file as unsliced (#3435).
  34. zf.writestr("Metadata/plate_1.gcode", "; gcode\n")
  35. zf.writestr(
  36. "Metadata/slice_info.config",
  37. f'<config><plate><metadata key="index" value="1"/>{filaments}</plate></config>',
  38. )
  39. async def _admin_token(async_client: AsyncClient) -> str:
  40. await async_client.post(
  41. "/api/v1/auth/setup",
  42. json={"auth_enabled": True, "admin_username": "qadmin", "admin_password": "AdminPass1!"},
  43. )
  44. login = await async_client.post("/api/v1/auth/login", json={"username": "qadmin", "password": "AdminPass1!"})
  45. return login.json()["access_token"]
  46. async def _friend(async_client: AsyncClient, admin: str, rules: list[dict], mode: str = "block") -> str:
  47. perms = await async_client.post(
  48. "/api/v1/groups/", headers=_auth(admin), json={"name": "perms", "permissions": QUEUE_PERMISSIONS}
  49. )
  50. friends = await async_client.post(
  51. "/api/v1/groups/",
  52. headers=_auth(admin),
  53. json={"name": "friends", "restrict_filaments": True, "filament_rule_mode": mode, "filament_rules": rules},
  54. )
  55. created = await async_client.post(
  56. "/api/v1/users/",
  57. headers=_auth(admin),
  58. json={"username": "friend", "password": "UserPass1!", "group_ids": [perms.json()["id"], friends.json()["id"]]},
  59. )
  60. assert created.status_code in (200, 201), created.text
  61. login = await async_client.post("/api/v1/auth/login", json={"username": "friend", "password": "UserPass1!"})
  62. return login.json()["access_token"]
  63. BLOCK_PETG_MATTE = [{"match": "filament_id", "value": "GFG03"}]
  64. async def _printer_and_archive(printer_factory, archive_factory, name: str, filaments: str):
  65. printer = await printer_factory(name="H3C")
  66. rel = f"archives/fil/{name}.gcode.3mf"
  67. _write_3mf(rel, filaments)
  68. return printer, await archive_factory(printer.id, file_path=rel)
  69. def _library_file(db_session, name: str, filaments: str, metadata: dict | None = None) -> LibraryFile:
  70. rel = f"library/{name}.gcode.3mf"
  71. _write_3mf(rel, filaments)
  72. row = LibraryFile(
  73. filename=f"{name}.gcode.3mf", file_path=rel, file_size=100, file_type="gcode.3mf", file_metadata=metadata or {}
  74. )
  75. db_session.add(row)
  76. return row
  77. async def _queue(async_client: AsyncClient, jwt: str, **job):
  78. return await async_client.post("/api/v1/queue/", headers=_auth(jwt), json=job)
  79. class TestQueueFilamentScope:
  80. async def test_blocked_filament_is_refused_with_its_name(self, async_client, printer_factory, archive_factory):
  81. printer, archive = await _printer_and_archive(printer_factory, archive_factory, "matte", PETG_MATTE)
  82. friend = await _friend(async_client, await _admin_token(async_client), BLOCK_PETG_MATTE)
  83. refused = await _queue(async_client, friend, archive_id=archive.id, printer_id=printer.id)
  84. assert refused.status_code == 403, refused.text
  85. assert "PETG (GFG03)" in refused.json()["detail"]
  86. async def test_allowed_filament_is_queued(self, async_client, printer_factory, archive_factory):
  87. printer, archive = await _printer_and_archive(printer_factory, archive_factory, "pla", PLA)
  88. friend = await _friend(async_client, await _admin_token(async_client), [{"match": "material", "value": "PETG"}])
  89. ok = await _queue(async_client, friend, archive_id=archive.id, printer_id=printer.id)
  90. assert ok.status_code == 200, ok.text
  91. async def test_admin_is_never_refused(self, async_client, printer_factory, archive_factory):
  92. printer, archive = await _printer_and_archive(printer_factory, archive_factory, "admin", PETG_MATTE)
  93. admin = await _admin_token(async_client)
  94. await _friend(async_client, admin, BLOCK_PETG_MATTE)
  95. # The admin is in the restricted group too: being an admin is what lets them through.
  96. me = (await async_client.get("/api/v1/auth/me", headers=_auth(admin))).json()
  97. groups = (await async_client.get("/api/v1/groups/", headers=_auth(admin))).json()
  98. friends_id = next(g["id"] for g in groups if g["name"] == "friends")
  99. joined = await async_client.patch(
  100. f"/api/v1/users/{me['id']}",
  101. headers=_auth(admin),
  102. json={"group_ids": [g["id"] for g in me["groups"]] + [friends_id]},
  103. )
  104. assert joined.status_code == 200, joined.text
  105. assert "friends" in [g["name"] for g in joined.json()["groups"]]
  106. ok = await _queue(async_client, admin, archive_id=archive.id, printer_id=printer.id)
  107. assert ok.status_code == 200, ok.text
  108. async def test_refused_before_the_queue_is_locked(self, async_client, printer_factory, archive_factory):
  109. """The 3MF is read before any lock or write (#3522)."""
  110. printer, archive = await _printer_and_archive(printer_factory, archive_factory, "early", PETG_MATTE)
  111. friend = await _friend(async_client, await _admin_token(async_client), BLOCK_PETG_MATTE)
  112. lock = AsyncMock()
  113. with patch("backend.app.api.routes.print_queue.lock_queue_positions", lock):
  114. refused = await _queue(async_client, friend, archive_id=archive.id, printer_id=printer.id, quantity=2)
  115. assert refused.status_code == 403, refused.text
  116. lock.assert_not_awaited()
  117. async def test_a_library_file_is_judged_too(self, async_client, printer_factory, db_session):
  118. printer = await printer_factory(name="H3C", model="X1C")
  119. library_file = _library_file(db_session, "matte", PETG_MATTE)
  120. await db_session.commit()
  121. friend = await _friend(async_client, await _admin_token(async_client), BLOCK_PETG_MATTE)
  122. refused = await _queue(async_client, friend, library_file_id=library_file.id, printer_id=printer.id)
  123. assert refused.status_code == 403, refused.text
  124. assert "PETG (GFG03)" in refused.json()["detail"]
  125. async def test_each_cross_model_variant_is_judged(self, async_client, printer_factory, db_session):
  126. """A job offered to several models is refused if any of its files is (#3522)."""
  127. await printer_factory(name="S", model="H2S")
  128. await printer_factory(name="C", model="H2C")
  129. files = [
  130. _library_file(db_session, model, filaments, {"sliced_for_model": model})
  131. for model, filaments in (("H2S", PLA), ("H2C", PETG_MATTE))
  132. ]
  133. await db_session.commit()
  134. friend = await _friend(async_client, await _admin_token(async_client), BLOCK_PETG_MATTE)
  135. refused = await _queue(async_client, friend, variants=[{"library_file_id": f.id} for f in files])
  136. assert refused.status_code == 403, refused.text
  137. assert "PETG (GFG03)" in refused.json()["detail"]
  138. class TestOverridesAreJudgedAsTheSchedulerJudgesThem:
  139. """The 403 applies the job's overrides the way the scheduler does, so both answer alike (#3522)."""
  140. async def test_a_colour_swap_is_left_to_the_tray_matcher(self, async_client, printer_factory, archive_factory):
  141. printer, archive = await _printer_and_archive(printer_factory, archive_factory, "swap", PETG_MATTE)
  142. friend = await _friend(async_client, await _admin_token(async_client), BLOCK_PETG_MATTE)
  143. ok = await _queue(
  144. async_client,
  145. friend,
  146. archive_id=archive.id,
  147. printer_id=printer.id,
  148. filament_overrides=[{"slot_id": 1, "type": "PETG", "color": "#000000", "force_color_match": False}],
  149. )
  150. assert ok.status_code == 200, ok.text
  151. async def test_a_forced_colour_naming_a_blocked_id_is_refused(self, async_client, printer_factory, archive_factory):
  152. printer, archive = await _printer_and_archive(printer_factory, archive_factory, "forced", PLA)
  153. friend = await _friend(async_client, await _admin_token(async_client), BLOCK_PETG_MATTE)
  154. refused = await _queue(
  155. async_client,
  156. friend,
  157. archive_id=archive.id,
  158. printer_id=printer.id,
  159. filament_overrides=[
  160. {"slot_id": 1, "type": "PETG", "color": "#FFFFFF", "tray_info_idx": "GFG03", "force_color_match": True}
  161. ],
  162. )
  163. assert refused.status_code == 403, refused.text
  164. assert "PETG (GFG03)" in refused.json()["detail"]
  165. class TestDeletingALimitedUser:
  166. async def test_their_kept_jobs_wait_for_an_admin(self, async_client, printer_factory, archive_factory, db_session):
  167. # Ownerless jobs are unrestricted, so a job held, or about to be, by
  168. # the deleted user's filament rules is staged rather than released (#3522).
  169. printer, archive = await _printer_and_archive(printer_factory, archive_factory, "kept", PLA)
  170. admin = await _admin_token(async_client)
  171. friend = await _friend(async_client, admin, BLOCK_PETG_MATTE)
  172. queued = await _queue(async_client, friend, archive_id=archive.id, printer_id=printer.id)
  173. assert queued.status_code == 200, queued.text
  174. # Released from review, as an admin would: the job is due to run.
  175. await db_session.execute(
  176. update(PrintQueueItem).where(PrintQueueItem.id == queued.json()["id"]).values(manual_start=False)
  177. )
  178. await db_session.commit()
  179. friend_id = (await async_client.get("/api/v1/auth/me", headers=_auth(friend))).json()["id"]
  180. deleted = await async_client.delete(f"/api/v1/users/{friend_id}", headers=_auth(admin))
  181. assert deleted.status_code in (200, 204), deleted.text
  182. item = (await async_client.get(f"/api/v1/queue/{queued.json()['id']}", headers=_auth(admin))).json()
  183. assert (item["created_by_id"], item["manual_start"]) == (None, True)
  184. async def test_a_deactivated_user_without_filament_rules_stages_nothing(
  185. self, async_client, printer_factory, archive_factory, db_session
  186. ):
  187. # A deactivated user's jobs are held for that, not by a filament rule:
  188. # with no rule on any of their groups, deleting them stages nothing.
  189. printer, archive = await _printer_and_archive(printer_factory, archive_factory, "kept", PLA)
  190. admin = await _admin_token(async_client)
  191. perms = await async_client.post(
  192. "/api/v1/groups/", headers=_auth(admin), json={"name": "perms", "permissions": QUEUE_PERMISSIONS}
  193. )
  194. created = await async_client.post(
  195. "/api/v1/users/",
  196. headers=_auth(admin),
  197. json={"username": "leaver", "password": "UserPass1!", "group_ids": [perms.json()["id"]]},
  198. )
  199. assert created.status_code in (200, 201), created.text
  200. leaver_id = created.json()["id"]
  201. login = await async_client.post("/api/v1/auth/login", json={"username": "leaver", "password": "UserPass1!"})
  202. queued = await _queue(async_client, login.json()["access_token"], archive_id=archive.id, printer_id=printer.id)
  203. assert queued.status_code == 200, queued.text
  204. await db_session.execute(
  205. update(PrintQueueItem).where(PrintQueueItem.id == queued.json()["id"]).values(manual_start=False)
  206. )
  207. await db_session.commit()
  208. deactivated = await async_client.patch(
  209. f"/api/v1/users/{leaver_id}", headers=_auth(admin), json={"is_active": False}
  210. )
  211. assert deactivated.status_code == 200, deactivated.text
  212. deleted = await async_client.delete(f"/api/v1/users/{leaver_id}", headers=_auth(admin))
  213. assert deleted.status_code in (200, 204), deleted.text
  214. item = (await async_client.get(f"/api/v1/queue/{queued.json()['id']}", headers=_auth(admin))).json()
  215. assert (item["created_by_id"], item["manual_start"]) == (None, False)