test_ownership_permissions.py 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  1. """Integration tests for ownership-based permission system.
  2. Tests the ownership permission model where users can have:
  3. - *_all permissions: can modify any item
  4. - *_own permissions: can only modify items they created
  5. - Ownerless items (created_by_id = null) require *_all permission
  6. """
  7. import pytest
  8. from httpx import AsyncClient
  9. class TestOwnershipPermissionsSetup:
  10. """Helper fixture class for ownership permission tests."""
  11. @pytest.fixture
  12. async def auth_setup(self, async_client: AsyncClient):
  13. """Setup auth with admin, create test users with different permission levels."""
  14. # Enable auth with admin user
  15. await async_client.post(
  16. "/api/v1/auth/setup",
  17. json={
  18. "auth_enabled": True,
  19. "admin_username": "ownershipadmin",
  20. "admin_password": "AdminPass1!",
  21. },
  22. )
  23. # Login as admin
  24. admin_login = await async_client.post(
  25. "/api/v1/auth/login",
  26. json={"username": "ownershipadmin", "password": "AdminPass1!"},
  27. )
  28. admin_token = admin_login.json()["access_token"]
  29. admin_user = admin_login.json()["user"]
  30. # Get group IDs
  31. groups_response = await async_client.get(
  32. "/api/v1/groups/",
  33. headers={"Authorization": f"Bearer {admin_token}"},
  34. )
  35. groups = groups_response.json()
  36. operators_group = next(g for g in groups if g["name"] == "Operators")
  37. viewers_group = next(g for g in groups if g["name"] == "Viewers")
  38. # Create operator user (has *_own permissions)
  39. operator_response = await async_client.post(
  40. "/api/v1/users/",
  41. headers={"Authorization": f"Bearer {admin_token}"},
  42. json={
  43. "username": "operator1",
  44. "password": "Operatorpass1!",
  45. "group_ids": [operators_group["id"]],
  46. },
  47. )
  48. operator_user = operator_response.json()
  49. # Login as operator
  50. operator_login = await async_client.post(
  51. "/api/v1/auth/login",
  52. json={"username": "operator1", "password": "Operatorpass1!"},
  53. )
  54. operator_token = operator_login.json()["access_token"]
  55. # Create second operator (for cross-user tests)
  56. operator2_response = await async_client.post(
  57. "/api/v1/users/",
  58. headers={"Authorization": f"Bearer {admin_token}"},
  59. json={
  60. "username": "operator2",
  61. "password": "Operatorpass1!",
  62. "group_ids": [operators_group["id"]],
  63. },
  64. )
  65. operator2_user = operator2_response.json()
  66. operator2_login = await async_client.post(
  67. "/api/v1/auth/login",
  68. json={"username": "operator2", "password": "Operatorpass1!"},
  69. )
  70. operator2_token = operator2_login.json()["access_token"]
  71. # Create viewer user (has no update/delete permissions)
  72. await async_client.post(
  73. "/api/v1/users/",
  74. headers={"Authorization": f"Bearer {admin_token}"},
  75. json={
  76. "username": "viewer1",
  77. "password": "Viewerpass1!",
  78. "group_ids": [viewers_group["id"]],
  79. },
  80. )
  81. viewer_login = await async_client.post(
  82. "/api/v1/auth/login",
  83. json={"username": "viewer1", "password": "Viewerpass1!"},
  84. )
  85. viewer_token = viewer_login.json()["access_token"]
  86. return {
  87. "admin_token": admin_token,
  88. "admin_user": admin_user,
  89. "operator_token": operator_token,
  90. "operator_user": operator_user,
  91. "operator2_token": operator2_token,
  92. "operator2_user": operator2_user,
  93. "viewer_token": viewer_token,
  94. }
  95. class TestArchiveOwnershipPermissions(TestOwnershipPermissionsSetup):
  96. """Tests for archive ownership-based permissions."""
  97. # ========================================================================
  98. # DELETE permissions
  99. # ========================================================================
  100. @pytest.mark.asyncio
  101. @pytest.mark.integration
  102. async def test_admin_can_delete_any_archive(
  103. self, async_client: AsyncClient, auth_setup, archive_factory, printer_factory, db_session
  104. ):
  105. """Admin with *_all permissions can delete any archive."""
  106. printer = await printer_factory()
  107. # Create archive owned by operator
  108. archive = await archive_factory(
  109. printer.id,
  110. print_name="Operator Archive",
  111. created_by_id=auth_setup["operator_user"]["id"],
  112. )
  113. # Admin deletes it
  114. response = await async_client.delete(
  115. f"/api/v1/archives/{archive.id}",
  116. headers={"Authorization": f"Bearer {auth_setup['admin_token']}"},
  117. )
  118. assert response.status_code == 200
  119. @pytest.mark.asyncio
  120. @pytest.mark.integration
  121. async def test_operator_can_delete_own_archive(
  122. self, async_client: AsyncClient, auth_setup, archive_factory, printer_factory, db_session
  123. ):
  124. """Operator with *_own permissions can delete their own archive."""
  125. printer = await printer_factory()
  126. archive = await archive_factory(
  127. printer.id,
  128. print_name="My Archive",
  129. created_by_id=auth_setup["operator_user"]["id"],
  130. )
  131. response = await async_client.delete(
  132. f"/api/v1/archives/{archive.id}",
  133. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  134. )
  135. assert response.status_code == 200
  136. @pytest.mark.asyncio
  137. @pytest.mark.integration
  138. async def test_operator_cannot_delete_others_archive(
  139. self, async_client: AsyncClient, auth_setup, archive_factory, printer_factory, db_session
  140. ):
  141. """Operator with *_own permissions cannot delete another user's archive."""
  142. printer = await printer_factory()
  143. # Archive created by operator2
  144. archive = await archive_factory(
  145. printer.id,
  146. print_name="Other's Archive",
  147. created_by_id=auth_setup["operator2_user"]["id"],
  148. )
  149. # operator1 tries to delete it
  150. response = await async_client.delete(
  151. f"/api/v1/archives/{archive.id}",
  152. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  153. )
  154. assert response.status_code == 403
  155. assert "your own" in response.json()["detail"].lower()
  156. @pytest.mark.asyncio
  157. @pytest.mark.integration
  158. async def test_operator_cannot_delete_ownerless_archive(
  159. self, async_client: AsyncClient, auth_setup, archive_factory, printer_factory, db_session
  160. ):
  161. """Operator with *_own permissions cannot delete ownerless archive."""
  162. printer = await printer_factory()
  163. # Archive with no owner (legacy data)
  164. archive = await archive_factory(
  165. printer.id,
  166. print_name="Ownerless Archive",
  167. created_by_id=None,
  168. )
  169. response = await async_client.delete(
  170. f"/api/v1/archives/{archive.id}",
  171. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  172. )
  173. assert response.status_code == 403
  174. @pytest.mark.asyncio
  175. @pytest.mark.integration
  176. async def test_viewer_cannot_delete_archive(
  177. self, async_client: AsyncClient, auth_setup, archive_factory, printer_factory, db_session
  178. ):
  179. """Viewer with no delete permissions cannot delete any archive."""
  180. printer = await printer_factory()
  181. archive = await archive_factory(printer.id, print_name="Any Archive")
  182. response = await async_client.delete(
  183. f"/api/v1/archives/{archive.id}",
  184. headers={"Authorization": f"Bearer {auth_setup['viewer_token']}"},
  185. )
  186. assert response.status_code == 403
  187. # ========================================================================
  188. # UPDATE permissions
  189. # ========================================================================
  190. @pytest.mark.asyncio
  191. @pytest.mark.integration
  192. async def test_admin_can_update_any_archive(
  193. self, async_client: AsyncClient, auth_setup, archive_factory, printer_factory, db_session
  194. ):
  195. """Admin can update any archive."""
  196. printer = await printer_factory()
  197. archive = await archive_factory(
  198. printer.id,
  199. print_name="Original Name",
  200. created_by_id=auth_setup["operator_user"]["id"],
  201. )
  202. response = await async_client.patch(
  203. f"/api/v1/archives/{archive.id}",
  204. headers={"Authorization": f"Bearer {auth_setup['admin_token']}"},
  205. json={"print_name": "Admin Updated"},
  206. )
  207. assert response.status_code == 200
  208. assert response.json()["print_name"] == "Admin Updated"
  209. @pytest.mark.asyncio
  210. @pytest.mark.integration
  211. async def test_operator_can_update_own_archive(
  212. self, async_client: AsyncClient, auth_setup, archive_factory, printer_factory, db_session
  213. ):
  214. """Operator can update their own archive."""
  215. printer = await printer_factory()
  216. archive = await archive_factory(
  217. printer.id,
  218. print_name="Original Name",
  219. created_by_id=auth_setup["operator_user"]["id"],
  220. )
  221. response = await async_client.patch(
  222. f"/api/v1/archives/{archive.id}",
  223. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  224. json={"print_name": "Operator Updated"},
  225. )
  226. assert response.status_code == 200
  227. assert response.json()["print_name"] == "Operator Updated"
  228. @pytest.mark.asyncio
  229. @pytest.mark.integration
  230. async def test_operator_cannot_update_others_archive(
  231. self, async_client: AsyncClient, auth_setup, archive_factory, printer_factory, db_session
  232. ):
  233. """Operator cannot update another user's archive."""
  234. printer = await printer_factory()
  235. archive = await archive_factory(
  236. printer.id,
  237. print_name="Other's Archive",
  238. created_by_id=auth_setup["operator2_user"]["id"],
  239. )
  240. response = await async_client.patch(
  241. f"/api/v1/archives/{archive.id}",
  242. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  243. json={"print_name": "Attempted Update"},
  244. )
  245. assert response.status_code == 403
  246. # ========================================================================
  247. # Legacy reprint endpoint
  248. # ========================================================================
  249. @pytest.mark.asyncio
  250. @pytest.mark.integration
  251. async def test_reprint_endpoint_is_gone_for_all_callers(
  252. self, async_client: AsyncClient, auth_setup, archive_factory, printer_factory, db_session
  253. ):
  254. """Direct archive reprint no longer exists; callers must use the queue."""
  255. printer = await printer_factory()
  256. archive = await archive_factory(
  257. printer.id,
  258. created_by_id=auth_setup["operator2_user"]["id"],
  259. )
  260. response = await async_client.post(
  261. f"/api/v1/archives/{archive.id}/reprint?printer_id={printer.id}",
  262. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  263. )
  264. assert response.status_code == 410
  265. # ========================================================================
  266. # Queue route — archives:reprint_* gate (#1625)
  267. # ========================================================================
  268. # The unified /queue/ route replaced the legacy /reprint endpoint; the
  269. # reprint permission gate must move with it. Without these checks a
  270. # caller with QUEUE_CREATE + ARCHIVES_READ_OWN could reprint their own
  271. # archives even if explicitly denied ARCHIVES_REPRINT_OWN.
  272. @pytest.mark.asyncio
  273. @pytest.mark.integration
  274. async def test_queue_route_operator_can_reprint_own_archive(
  275. self, async_client: AsyncClient, auth_setup, archive_factory, printer_factory, db_session
  276. ):
  277. """Operator with REPRINT_OWN can queue their own archive."""
  278. printer = await printer_factory()
  279. archive = await archive_factory(
  280. printer.id,
  281. created_by_id=auth_setup["operator_user"]["id"],
  282. )
  283. response = await async_client.post(
  284. "/api/v1/queue/",
  285. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  286. json={"printer_id": printer.id, "archive_id": archive.id},
  287. )
  288. assert response.status_code == 200
  289. @pytest.mark.asyncio
  290. @pytest.mark.integration
  291. async def test_queue_route_user_without_reprint_gets_403(
  292. self, async_client: AsyncClient, auth_setup, archive_factory, printer_factory, db_session
  293. ):
  294. """User with QUEUE_CREATE + ARCHIVES_READ_OWN but no reprint perm → 403.
  295. Custom group mirrors a real operator policy where someone is allowed
  296. to enqueue freshly-uploaded library files but explicitly NOT allowed
  297. to re-run completed archives.
  298. """
  299. # Create custom group with queue:create + archives:read_own but no reprint perm.
  300. admin_headers = {"Authorization": f"Bearer {auth_setup['admin_token']}"}
  301. group_resp = await async_client.post(
  302. "/api/v1/groups/",
  303. headers=admin_headers,
  304. json={
  305. "name": "QueueOnlyNoReprint",
  306. "description": "Test group: can queue library files but not reprint",
  307. "permissions": [
  308. "queue:create",
  309. "queue:read_own",
  310. "archives:read_own",
  311. "library:read_own",
  312. "library:upload",
  313. "printers:read",
  314. ],
  315. },
  316. )
  317. assert group_resp.status_code in (200, 201)
  318. group_id = group_resp.json()["id"]
  319. await async_client.post(
  320. "/api/v1/users/",
  321. headers=admin_headers,
  322. json={
  323. "username": "noreprint_user",
  324. "password": "NoreprintPass1!",
  325. "group_ids": [group_id],
  326. },
  327. )
  328. login = await async_client.post(
  329. "/api/v1/auth/login",
  330. json={"username": "noreprint_user", "password": "NoreprintPass1!"},
  331. )
  332. token = login.json()["access_token"]
  333. user_id = login.json()["user"]["id"]
  334. # Archive owned by the no-reprint user.
  335. printer = await printer_factory()
  336. archive = await archive_factory(printer.id, created_by_id=user_id)
  337. response = await async_client.post(
  338. "/api/v1/queue/",
  339. headers={"Authorization": f"Bearer {token}"},
  340. json={"printer_id": printer.id, "archive_id": archive.id},
  341. )
  342. assert response.status_code == 403
  343. assert "reprint" in response.json()["detail"].lower()
  344. @pytest.mark.asyncio
  345. @pytest.mark.integration
  346. async def test_queue_route_ownerless_archive_requires_reprint_all(
  347. self, async_client: AsyncClient, auth_setup, archive_factory, printer_factory, db_session
  348. ):
  349. """Ownerless archive (created_by_id=null) requires REPRINT_ALL.
  350. Pre-IDOR-fix legacy data has no creator; an operator with
  351. REPRINT_OWN can't fall back to "I own this" — fail-closed.
  352. The existing IDOR check returns 404 first (operator lacks
  353. READ_ALL and doesn't own the row), so this is also a regression
  354. guard against accidentally surfacing 403-instead-of-404 if the
  355. IDOR check is ever loosened.
  356. """
  357. printer = await printer_factory()
  358. archive = await archive_factory(printer.id, created_by_id=None)
  359. response = await async_client.post(
  360. "/api/v1/queue/",
  361. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  362. json={"printer_id": printer.id, "archive_id": archive.id},
  363. )
  364. # IDOR returns 404 before the new gate fires for this operator.
  365. assert response.status_code == 404
  366. class TestQueueOwnershipPermissions(TestOwnershipPermissionsSetup):
  367. """Tests for print queue ownership-based permissions."""
  368. @pytest.fixture
  369. async def queue_item_factory(self, db_session, printer_factory, archive_factory):
  370. """Factory to create test queue items."""
  371. async def _create_item(**kwargs):
  372. from backend.app.models.print_queue import PrintQueueItem
  373. printer = await printer_factory()
  374. # Create an archive to link to the queue item
  375. archive = await archive_factory(printer.id)
  376. defaults = {
  377. "printer_id": printer.id,
  378. "archive_id": archive.id,
  379. "status": "pending",
  380. "position": 0,
  381. }
  382. defaults.update(kwargs)
  383. item = PrintQueueItem(**defaults)
  384. db_session.add(item)
  385. await db_session.commit()
  386. await db_session.refresh(item)
  387. return item
  388. return _create_item
  389. @pytest.mark.asyncio
  390. @pytest.mark.integration
  391. async def test_admin_can_delete_any_queue_item(self, async_client: AsyncClient, auth_setup, queue_item_factory):
  392. """Admin can delete any queue item."""
  393. item = await queue_item_factory(created_by_id=auth_setup["operator_user"]["id"])
  394. response = await async_client.delete(
  395. f"/api/v1/queue/{item.id}",
  396. headers={"Authorization": f"Bearer {auth_setup['admin_token']}"},
  397. )
  398. assert response.status_code == 200
  399. @pytest.mark.asyncio
  400. @pytest.mark.integration
  401. async def test_operator_can_delete_own_queue_item(self, async_client: AsyncClient, auth_setup, queue_item_factory):
  402. """Operator can delete their own queue item."""
  403. item = await queue_item_factory(created_by_id=auth_setup["operator_user"]["id"])
  404. response = await async_client.delete(
  405. f"/api/v1/queue/{item.id}",
  406. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  407. )
  408. assert response.status_code == 200
  409. @pytest.mark.asyncio
  410. @pytest.mark.integration
  411. async def test_operator_cannot_delete_others_queue_item(
  412. self, async_client: AsyncClient, auth_setup, queue_item_factory
  413. ):
  414. """Operator cannot delete another user's queue item."""
  415. item = await queue_item_factory(created_by_id=auth_setup["operator2_user"]["id"])
  416. response = await async_client.delete(
  417. f"/api/v1/queue/{item.id}",
  418. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  419. )
  420. assert response.status_code == 403
  421. @pytest.mark.asyncio
  422. @pytest.mark.integration
  423. async def test_operator_can_update_own_queue_item(self, async_client: AsyncClient, auth_setup, queue_item_factory):
  424. """Operator can update their own queue item."""
  425. item = await queue_item_factory(created_by_id=auth_setup["operator_user"]["id"])
  426. response = await async_client.patch(
  427. f"/api/v1/queue/{item.id}",
  428. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  429. json={"position": 10},
  430. )
  431. assert response.status_code == 200
  432. @pytest.mark.asyncio
  433. @pytest.mark.integration
  434. async def test_operator_cannot_update_others_queue_item(
  435. self, async_client: AsyncClient, auth_setup, queue_item_factory
  436. ):
  437. """Operator cannot update another user's queue item."""
  438. item = await queue_item_factory(created_by_id=auth_setup["operator2_user"]["id"])
  439. response = await async_client.patch(
  440. f"/api/v1/queue/{item.id}",
  441. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  442. json={"position": 10},
  443. )
  444. assert response.status_code == 403
  445. @pytest.mark.asyncio
  446. @pytest.mark.integration
  447. async def test_operator_cannot_cancel_others_queue_item(
  448. self, async_client: AsyncClient, auth_setup, queue_item_factory
  449. ):
  450. """Operator cannot cancel another user's queue item."""
  451. item = await queue_item_factory(created_by_id=auth_setup["operator2_user"]["id"])
  452. response = await async_client.post(
  453. f"/api/v1/queue/{item.id}/cancel",
  454. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  455. )
  456. assert response.status_code == 403
  457. # ========================================================================
  458. # Start / Stop ownership gates (#1625-followup)
  459. # ========================================================================
  460. # Pre-fix /stop required QUEUE_UPDATE_ALL (admin-only) — operators saw the
  461. # Stop button in the queue UI but got 403 on click. /start required
  462. # QUEUE_UPDATE_OWN with no ownership check — operators could start anyone's
  463. # queue items via direct API. Both now use require_ownership_permission.
  464. @pytest.mark.asyncio
  465. @pytest.mark.integration
  466. async def test_operator_can_start_own_queue_item(self, async_client: AsyncClient, auth_setup, queue_item_factory):
  467. """Operator can start their own staged queue item."""
  468. item = await queue_item_factory(
  469. created_by_id=auth_setup["operator_user"]["id"],
  470. manual_start=True,
  471. )
  472. response = await async_client.post(
  473. f"/api/v1/queue/{item.id}/start?skip_filament_check=true",
  474. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  475. )
  476. assert response.status_code == 200
  477. @pytest.mark.asyncio
  478. @pytest.mark.integration
  479. async def test_operator_cannot_start_others_queue_item(
  480. self, async_client: AsyncClient, auth_setup, queue_item_factory
  481. ):
  482. """Operator cannot start another user's queue item."""
  483. item = await queue_item_factory(
  484. created_by_id=auth_setup["operator2_user"]["id"],
  485. manual_start=True,
  486. )
  487. response = await async_client.post(
  488. f"/api/v1/queue/{item.id}/start?skip_filament_check=true",
  489. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  490. )
  491. assert response.status_code == 403
  492. @pytest.mark.asyncio
  493. @pytest.mark.integration
  494. async def test_operator_can_start_unowned_queue_item(
  495. self, async_client: AsyncClient, auth_setup, queue_item_factory, db_session
  496. ):
  497. """Operator can start a NULL-owner queue item (VP-uploaded, #1670)
  498. and claims ownership in the process.
  499. Stop and Cancel reject unowned items for _OWN holders (destructive,
  500. no "I own it" claim available), but Start is the entry point for the
  501. VP-import flow where attribution happens at click-time.
  502. """
  503. from backend.app.models.print_queue import PrintQueueItem
  504. item = await queue_item_factory(created_by_id=None, manual_start=True)
  505. response = await async_client.post(
  506. f"/api/v1/queue/{item.id}/start?skip_filament_check=true",
  507. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  508. )
  509. assert response.status_code == 200
  510. # Ownership claimed: operator is now the item's owner.
  511. await db_session.refresh(item)
  512. refetch = await db_session.get(PrintQueueItem, item.id)
  513. assert refetch.created_by_id == auth_setup["operator_user"]["id"]
  514. @pytest.mark.asyncio
  515. @pytest.mark.integration
  516. async def test_operator_can_stop_own_queue_item(self, async_client: AsyncClient, auth_setup, queue_item_factory):
  517. """Operator can stop their own currently-printing queue item."""
  518. item = await queue_item_factory(
  519. created_by_id=auth_setup["operator_user"]["id"],
  520. status="printing",
  521. )
  522. response = await async_client.post(
  523. f"/api/v1/queue/{item.id}/stop",
  524. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  525. )
  526. assert response.status_code == 200
  527. @pytest.mark.asyncio
  528. @pytest.mark.integration
  529. async def test_operator_cannot_stop_others_queue_item(
  530. self, async_client: AsyncClient, auth_setup, queue_item_factory
  531. ):
  532. """Operator cannot stop another user's printing queue item."""
  533. item = await queue_item_factory(
  534. created_by_id=auth_setup["operator2_user"]["id"],
  535. status="printing",
  536. )
  537. response = await async_client.post(
  538. f"/api/v1/queue/{item.id}/stop",
  539. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  540. )
  541. assert response.status_code == 403
  542. @pytest.mark.asyncio
  543. @pytest.mark.integration
  544. async def test_operator_cannot_stop_unowned_queue_item(
  545. self, async_client: AsyncClient, auth_setup, queue_item_factory
  546. ):
  547. """Operator cannot stop a NULL-owner printing queue item — stop mirrors
  548. cancel (destructive, no claim semantics). Admins with _ALL can still stop it.
  549. """
  550. item = await queue_item_factory(created_by_id=None, status="printing")
  551. response = await async_client.post(
  552. f"/api/v1/queue/{item.id}/stop",
  553. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  554. )
  555. assert response.status_code == 403
  556. @pytest.mark.asyncio
  557. @pytest.mark.integration
  558. async def test_admin_can_stop_any_queue_item(self, async_client: AsyncClient, auth_setup, queue_item_factory):
  559. """Admin with _ALL can stop any printing queue item including unowned."""
  560. item = await queue_item_factory(created_by_id=None, status="printing")
  561. response = await async_client.post(
  562. f"/api/v1/queue/{item.id}/stop",
  563. headers={"Authorization": f"Bearer {auth_setup['admin_token']}"},
  564. )
  565. assert response.status_code == 200
  566. @pytest.mark.asyncio
  567. @pytest.mark.integration
  568. async def test_bulk_update_skips_non_owned_items(self, async_client: AsyncClient, auth_setup, queue_item_factory):
  569. """Bulk update only updates items the user owns."""
  570. # Create items owned by different users
  571. own_item = await queue_item_factory(
  572. created_by_id=auth_setup["operator_user"]["id"],
  573. )
  574. other_item = await queue_item_factory(
  575. created_by_id=auth_setup["operator2_user"]["id"],
  576. )
  577. response = await async_client.patch(
  578. "/api/v1/queue/bulk",
  579. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  580. json={
  581. "item_ids": [own_item.id, other_item.id],
  582. "manual_start": True,
  583. },
  584. )
  585. assert response.status_code == 200
  586. result = response.json()
  587. # Should only update the owned item
  588. assert result["updated_count"] == 1
  589. assert result["skipped_count"] == 1
  590. class TestLibraryOwnershipPermissions(TestOwnershipPermissionsSetup):
  591. """Tests for library file ownership-based permissions."""
  592. @pytest.fixture
  593. async def library_file_factory(self, db_session):
  594. """Factory to create test library files."""
  595. _counter = [0]
  596. async def _create_file(**kwargs):
  597. from backend.app.models.library import LibraryFile
  598. _counter[0] += 1
  599. defaults = {
  600. "filename": f"test_{_counter[0]}.3mf",
  601. "file_path": f"library/test_{_counter[0]}.3mf",
  602. "file_type": "3mf",
  603. "file_size": 1024,
  604. }
  605. defaults.update(kwargs)
  606. file = LibraryFile(**defaults)
  607. db_session.add(file)
  608. await db_session.commit()
  609. await db_session.refresh(file)
  610. return file
  611. return _create_file
  612. @pytest.fixture
  613. async def library_folder_factory(self, db_session):
  614. """Factory to create test library folders."""
  615. _counter = [0]
  616. async def _create_folder(**kwargs):
  617. from backend.app.models.library import LibraryFolder
  618. _counter[0] += 1
  619. defaults = {
  620. "name": f"TestFolder_{_counter[0]}",
  621. }
  622. defaults.update(kwargs)
  623. folder = LibraryFolder(**defaults)
  624. db_session.add(folder)
  625. await db_session.commit()
  626. await db_session.refresh(folder)
  627. return folder
  628. return _create_folder
  629. @pytest.mark.asyncio
  630. @pytest.mark.integration
  631. async def test_admin_can_delete_any_library_file(self, async_client: AsyncClient, auth_setup, library_file_factory):
  632. """Admin can delete any library file."""
  633. file = await library_file_factory(created_by_id=auth_setup["operator_user"]["id"])
  634. response = await async_client.delete(
  635. f"/api/v1/library/files/{file.id}",
  636. headers={"Authorization": f"Bearer {auth_setup['admin_token']}"},
  637. )
  638. assert response.status_code == 200
  639. @pytest.mark.asyncio
  640. @pytest.mark.integration
  641. async def test_operator_can_delete_own_library_file(
  642. self, async_client: AsyncClient, auth_setup, library_file_factory
  643. ):
  644. """Operator can delete their own library file."""
  645. file = await library_file_factory(created_by_id=auth_setup["operator_user"]["id"])
  646. response = await async_client.delete(
  647. f"/api/v1/library/files/{file.id}",
  648. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  649. )
  650. assert response.status_code == 200
  651. @pytest.mark.asyncio
  652. @pytest.mark.integration
  653. async def test_operator_cannot_delete_others_library_file(
  654. self, async_client: AsyncClient, auth_setup, library_file_factory
  655. ):
  656. """Operator cannot delete another user's library file."""
  657. file = await library_file_factory(created_by_id=auth_setup["operator2_user"]["id"])
  658. response = await async_client.delete(
  659. f"/api/v1/library/files/{file.id}",
  660. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  661. )
  662. assert response.status_code == 403
  663. @pytest.mark.asyncio
  664. @pytest.mark.integration
  665. async def test_operator_can_update_own_library_file(
  666. self, async_client: AsyncClient, auth_setup, library_file_factory
  667. ):
  668. """Operator can update their own library file."""
  669. file = await library_file_factory(created_by_id=auth_setup["operator_user"]["id"])
  670. response = await async_client.put(
  671. f"/api/v1/library/files/{file.id}",
  672. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  673. json={"filename": "renamed.3mf"},
  674. )
  675. assert response.status_code == 200
  676. @pytest.mark.asyncio
  677. @pytest.mark.integration
  678. async def test_operator_cannot_update_others_library_file(
  679. self, async_client: AsyncClient, auth_setup, library_file_factory
  680. ):
  681. """Operator cannot update another user's library file."""
  682. file = await library_file_factory(created_by_id=auth_setup["operator2_user"]["id"])
  683. response = await async_client.put(
  684. f"/api/v1/library/files/{file.id}",
  685. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  686. json={"filename": "renamed.3mf"},
  687. )
  688. assert response.status_code == 403
  689. @pytest.mark.asyncio
  690. @pytest.mark.integration
  691. async def test_folders_require_all_permission(self, async_client: AsyncClient, auth_setup, library_folder_factory):
  692. """Folders require *_all permission (no ownership tracking on folders)."""
  693. folder = await library_folder_factory(name="TestFolder")
  694. # Operator cannot delete folder (needs *_all)
  695. response = await async_client.delete(
  696. f"/api/v1/library/folders/{folder.id}",
  697. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  698. )
  699. assert response.status_code == 403
  700. @pytest.mark.asyncio
  701. @pytest.mark.integration
  702. async def test_bulk_delete_skips_non_owned_files(self, async_client: AsyncClient, auth_setup, library_file_factory):
  703. """Bulk delete only deletes files the user owns."""
  704. own_file = await library_file_factory(
  705. filename="own.3mf",
  706. created_by_id=auth_setup["operator_user"]["id"],
  707. )
  708. other_file = await library_file_factory(
  709. filename="other.3mf",
  710. created_by_id=auth_setup["operator2_user"]["id"],
  711. )
  712. response = await async_client.post(
  713. "/api/v1/library/bulk-delete",
  714. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  715. json={"file_ids": [own_file.id, other_file.id], "folder_ids": []},
  716. )
  717. assert response.status_code == 200
  718. result = response.json()
  719. # Should only delete the owned file; other_file is skipped (but skipped count not in response)
  720. assert result["deleted_files"] == 1
  721. class TestAuthDisabledPermissions:
  722. """Tests that verify all operations are allowed when auth is disabled."""
  723. @pytest.mark.asyncio
  724. @pytest.mark.integration
  725. async def test_delete_archive_without_auth(
  726. self, async_client: AsyncClient, archive_factory, printer_factory, db_session
  727. ):
  728. """When auth is disabled, anyone can delete archives."""
  729. printer = await printer_factory()
  730. archive = await archive_factory(printer.id)
  731. response = await async_client.delete(f"/api/v1/archives/{archive.id}")
  732. assert response.status_code == 200
  733. @pytest.mark.asyncio
  734. @pytest.mark.integration
  735. async def test_update_archive_without_auth(
  736. self, async_client: AsyncClient, archive_factory, printer_factory, db_session
  737. ):
  738. """When auth is disabled, anyone can update archives."""
  739. printer = await printer_factory()
  740. archive = await archive_factory(printer.id)
  741. response = await async_client.patch(
  742. f"/api/v1/archives/{archive.id}",
  743. json={"print_name": "Updated Name"},
  744. )
  745. assert response.status_code == 200
  746. class TestUserItemsCountAndDeletion(TestOwnershipPermissionsSetup):
  747. """Tests for user items count endpoint and deletion with items."""
  748. @pytest.mark.asyncio
  749. @pytest.mark.integration
  750. async def test_get_user_items_count(
  751. self, async_client: AsyncClient, auth_setup, archive_factory, printer_factory, db_session
  752. ):
  753. """Verify items count endpoint returns correct counts."""
  754. printer = await printer_factory()
  755. user_id = auth_setup["operator_user"]["id"]
  756. # Create some items for the operator
  757. await archive_factory(printer.id, created_by_id=user_id)
  758. await archive_factory(printer.id, created_by_id=user_id)
  759. response = await async_client.get(
  760. f"/api/v1/users/{user_id}/items-count",
  761. headers={"Authorization": f"Bearer {auth_setup['admin_token']}"},
  762. )
  763. assert response.status_code == 200
  764. counts = response.json()
  765. assert counts["archives"] >= 2
  766. assert "queue_items" in counts
  767. assert "library_files" in counts
  768. @pytest.mark.asyncio
  769. @pytest.mark.integration
  770. async def test_delete_user_keeps_items(
  771. self, async_client: AsyncClient, auth_setup, archive_factory, printer_factory, db_session
  772. ):
  773. """Verify deleting user without delete_items keeps items (ownerless)."""
  774. printer = await printer_factory()
  775. user_id = auth_setup["operator2_user"]["id"]
  776. # Create archive for operator2
  777. archive = await archive_factory(printer.id, created_by_id=user_id)
  778. archive_id = archive.id
  779. # Delete user without deleting items
  780. response = await async_client.delete(
  781. f"/api/v1/users/{user_id}?delete_items=false",
  782. headers={"Authorization": f"Bearer {auth_setup['admin_token']}"},
  783. )
  784. assert response.status_code == 204
  785. # Verify archive still exists but is now ownerless
  786. archive_response = await async_client.get(
  787. f"/api/v1/archives/{archive_id}",
  788. headers={"Authorization": f"Bearer {auth_setup['admin_token']}"},
  789. )
  790. assert archive_response.status_code == 200
  791. assert archive_response.json()["created_by_id"] is None
  792. @pytest.mark.asyncio
  793. @pytest.mark.integration
  794. async def test_delete_user_with_items(
  795. self, async_client: AsyncClient, auth_setup, archive_factory, printer_factory, db_session
  796. ):
  797. """Verify deleting user with delete_items=true removes their items."""
  798. printer = await printer_factory()
  799. # Create a new user with items
  800. create_response = await async_client.post(
  801. "/api/v1/users/",
  802. headers={"Authorization": f"Bearer {auth_setup['admin_token']}"},
  803. json={
  804. "username": "deletewithitems",
  805. "password": "Password123!",
  806. },
  807. )
  808. user_id = create_response.json()["id"]
  809. # Create archive for this user
  810. archive = await archive_factory(printer.id, created_by_id=user_id)
  811. archive_id = archive.id
  812. # Delete user WITH deleting items
  813. response = await async_client.delete(
  814. f"/api/v1/users/{user_id}?delete_items=true",
  815. headers={"Authorization": f"Bearer {auth_setup['admin_token']}"},
  816. )
  817. assert response.status_code == 204
  818. # Verify archive was deleted
  819. archive_response = await async_client.get(
  820. f"/api/v1/archives/{archive_id}",
  821. headers={"Authorization": f"Bearer {auth_setup['admin_token']}"},
  822. )
  823. assert archive_response.status_code == 404
  824. class TestReadIDORClosure(TestOwnershipPermissionsSetup):
  825. """Regression tests pinning maziggy/bambuddy-security #2 — IDOR on
  826. archives / library / queue read paths.
  827. Before the fix, ARCHIVES_READ / LIBRARY_READ / QUEUE_READ were flat
  828. "see everything" permissions even though the write side was split into
  829. OWN/ALL. An operator with only ARCHIVES_READ could read, download, and
  830. queue any user's archive via direct id reference. These tests pin the
  831. bambuddy_archive_idor.py and bambuddy_archive_viewer_idor.py PoC paths
  832. so the IDOR can't regress silently.
  833. """
  834. @pytest.mark.asyncio
  835. @pytest.mark.integration
  836. async def test_operator_get_others_archive_returns_404_not_200(
  837. self, async_client: AsyncClient, auth_setup, archive_factory, printer_factory, db_session
  838. ):
  839. """PoC #2 read path. operator1 GET /archives/{id} where id is admin's
  840. archive must NOT leak the row. 404 (not 403) so the operator can't
  841. enumerate which ids exist — same shape as a nonexistent id."""
  842. printer = await printer_factory()
  843. archive = await archive_factory(
  844. printer.id,
  845. print_name="Admin Archive",
  846. created_by_id=auth_setup["admin_user"]["id"],
  847. )
  848. response = await async_client.get(
  849. f"/api/v1/archives/{archive.id}",
  850. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  851. )
  852. assert response.status_code == 404
  853. @pytest.mark.asyncio
  854. @pytest.mark.integration
  855. async def test_operator_download_others_archive_returns_404(
  856. self, async_client: AsyncClient, auth_setup, archive_factory, printer_factory, db_session
  857. ):
  858. """Viewer-IDOR PoC path: GET /archives/{id}/download on admin's archive.
  859. Before the fix this streamed the 3MF body straight to a viewer-tier
  860. token."""
  861. printer = await printer_factory()
  862. archive = await archive_factory(
  863. printer.id,
  864. print_name="Admin Archive 2",
  865. created_by_id=auth_setup["admin_user"]["id"],
  866. )
  867. response = await async_client.get(
  868. f"/api/v1/archives/{archive.id}/download",
  869. headers={"Authorization": f"Bearer {auth_setup['viewer_token']}"},
  870. )
  871. assert response.status_code == 404
  872. @pytest.mark.asyncio
  873. @pytest.mark.integration
  874. async def test_operator_list_archives_excludes_others(
  875. self, async_client: AsyncClient, auth_setup, archive_factory, printer_factory, db_session
  876. ):
  877. """GET /archives/ must filter to own archives only for OWN-level callers."""
  878. printer = await printer_factory()
  879. own = await archive_factory(
  880. printer.id, print_name="Operator's Own", created_by_id=auth_setup["operator_user"]["id"]
  881. )
  882. others = await archive_factory(printer.id, print_name="Admin's", created_by_id=auth_setup["admin_user"]["id"])
  883. response = await async_client.get(
  884. "/api/v1/archives/",
  885. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  886. )
  887. assert response.status_code == 200
  888. returned_ids = {a["id"] for a in response.json()}
  889. assert own.id in returned_ids
  890. assert others.id not in returned_ids
  891. @pytest.mark.asyncio
  892. @pytest.mark.integration
  893. async def test_admin_list_archives_includes_all(
  894. self, async_client: AsyncClient, auth_setup, archive_factory, printer_factory, db_session
  895. ):
  896. """ARCHIVES_READ_ALL → admin sees own + every user's archives."""
  897. printer = await printer_factory()
  898. admin_archive = await archive_factory(
  899. printer.id, print_name="Admin's", created_by_id=auth_setup["admin_user"]["id"]
  900. )
  901. operator_archive = await archive_factory(
  902. printer.id, print_name="Operator's", created_by_id=auth_setup["operator_user"]["id"]
  903. )
  904. response = await async_client.get(
  905. "/api/v1/archives/",
  906. headers={"Authorization": f"Bearer {auth_setup['admin_token']}"},
  907. )
  908. assert response.status_code == 200
  909. returned_ids = {a["id"] for a in response.json()}
  910. assert admin_archive.id in returned_ids
  911. assert operator_archive.id in returned_ids
  912. @pytest.mark.asyncio
  913. @pytest.mark.integration
  914. async def test_operator_cannot_queue_others_archive(
  915. self, async_client: AsyncClient, auth_setup, archive_factory, printer_factory, db_session
  916. ):
  917. """PoC #2 queue path. POST /queue/ with admin's archive_id as
  918. operator1 must return 404, not create a queue item. Before the fix
  919. this returned 201 and queued the admin archive (Landon's CONFIRMED
  920. line in the PoC)."""
  921. printer = await printer_factory()
  922. archive = await archive_factory(
  923. printer.id,
  924. print_name="Admin Archive (queue-target)",
  925. created_by_id=auth_setup["admin_user"]["id"],
  926. )
  927. response = await async_client.post(
  928. "/api/v1/queue/",
  929. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  930. json={"archive_id": archive.id, "printer_id": printer.id, "quantity": 1},
  931. )
  932. assert response.status_code == 404
  933. @pytest.mark.asyncio
  934. @pytest.mark.integration
  935. async def test_admin_can_queue_others_archive(
  936. self, async_client: AsyncClient, auth_setup, archive_factory, printer_factory, db_session
  937. ):
  938. """Belt-and-suspenders for the ALL path: admin (ARCHIVES_READ_ALL) can
  939. queue a user's archive on their behalf — common workshop pattern."""
  940. printer = await printer_factory()
  941. archive = await archive_factory(
  942. printer.id,
  943. print_name="Operator's archive (queue by admin)",
  944. created_by_id=auth_setup["operator_user"]["id"],
  945. )
  946. response = await async_client.post(
  947. "/api/v1/queue/",
  948. headers={"Authorization": f"Bearer {auth_setup['admin_token']}"},
  949. json={"archive_id": archive.id, "printer_id": printer.id, "quantity": 1},
  950. )
  951. assert response.status_code == 200
  952. @pytest.mark.asyncio
  953. @pytest.mark.integration
  954. async def test_operator_get_others_library_file_returns_404(
  955. self, async_client: AsyncClient, auth_setup, db_session
  956. ):
  957. """Library IDOR closure (same shape as archives — closed in the same PR
  958. per maziggy/bambuddy-security #2)."""
  959. from backend.app.models.library import LibraryFile
  960. admin_file = LibraryFile(
  961. filename="admin_secret.3mf",
  962. file_path="library/admin_secret.3mf",
  963. file_type="3mf",
  964. file_size=2048,
  965. created_by_id=auth_setup["admin_user"]["id"],
  966. )
  967. db_session.add(admin_file)
  968. await db_session.commit()
  969. await db_session.refresh(admin_file)
  970. response = await async_client.get(
  971. f"/api/v1/library/files/{admin_file.id}",
  972. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  973. )
  974. assert response.status_code == 404
  975. @pytest.mark.asyncio
  976. @pytest.mark.integration
  977. async def test_operator_list_library_files_excludes_others(self, async_client: AsyncClient, auth_setup, db_session):
  978. from backend.app.models.library import LibraryFile
  979. own = LibraryFile(
  980. filename="my_file.3mf",
  981. file_path="library/my_file.3mf",
  982. file_type="3mf",
  983. file_size=1024,
  984. created_by_id=auth_setup["operator_user"]["id"],
  985. )
  986. others = LibraryFile(
  987. filename="admin_file.3mf",
  988. file_path="library/admin_file.3mf",
  989. file_type="3mf",
  990. file_size=1024,
  991. created_by_id=auth_setup["admin_user"]["id"],
  992. )
  993. db_session.add_all([own, others])
  994. await db_session.commit()
  995. await db_session.refresh(own)
  996. await db_session.refresh(others)
  997. response = await async_client.get(
  998. "/api/v1/library/files",
  999. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  1000. )
  1001. assert response.status_code == 200
  1002. returned_ids = {f["id"] for f in response.json()}
  1003. assert own.id in returned_ids
  1004. assert others.id not in returned_ids
  1005. @pytest.mark.asyncio
  1006. @pytest.mark.integration
  1007. async def test_operator_queue_list_excludes_others_items(
  1008. self, async_client: AsyncClient, auth_setup, archive_factory, printer_factory, db_session
  1009. ):
  1010. """GET /queue/ must filter to own queue items only for OWN callers —
  1011. same shape as the archive list."""
  1012. from backend.app.models.print_queue import PrintQueueItem
  1013. printer = await printer_factory()
  1014. archive = await archive_factory(printer.id, print_name="A", created_by_id=auth_setup["operator_user"]["id"])
  1015. own_item = PrintQueueItem(
  1016. archive_id=archive.id,
  1017. printer_id=printer.id,
  1018. status="pending",
  1019. position=1,
  1020. created_by_id=auth_setup["operator_user"]["id"],
  1021. )
  1022. admin_item = PrintQueueItem(
  1023. archive_id=archive.id,
  1024. printer_id=printer.id,
  1025. status="pending",
  1026. position=2,
  1027. created_by_id=auth_setup["admin_user"]["id"],
  1028. )
  1029. db_session.add_all([own_item, admin_item])
  1030. await db_session.commit()
  1031. await db_session.refresh(own_item)
  1032. await db_session.refresh(admin_item)
  1033. response = await async_client.get(
  1034. "/api/v1/queue/",
  1035. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  1036. )
  1037. assert response.status_code == 200
  1038. returned_ids = {q["id"] for q in response.json()}
  1039. assert own_item.id in returned_ids
  1040. assert admin_item.id not in returned_ids
  1041. @pytest.mark.asyncio
  1042. @pytest.mark.integration
  1043. async def test_operator_get_others_queue_item_returns_404(
  1044. self, async_client: AsyncClient, auth_setup, archive_factory, printer_factory, db_session
  1045. ):
  1046. """Direct-id queue item access — same enumeration risk as archive get."""
  1047. from backend.app.models.print_queue import PrintQueueItem
  1048. printer = await printer_factory()
  1049. archive = await archive_factory(printer.id, print_name="A", created_by_id=auth_setup["admin_user"]["id"])
  1050. admin_item = PrintQueueItem(
  1051. archive_id=archive.id,
  1052. printer_id=printer.id,
  1053. status="pending",
  1054. position=1,
  1055. created_by_id=auth_setup["admin_user"]["id"],
  1056. )
  1057. db_session.add(admin_item)
  1058. await db_session.commit()
  1059. await db_session.refresh(admin_item)
  1060. response = await async_client.get(
  1061. f"/api/v1/queue/{admin_item.id}",
  1062. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  1063. )
  1064. assert response.status_code == 404
  1065. @pytest.mark.asyncio
  1066. @pytest.mark.integration
  1067. async def test_auth_disabled_preserves_single_tenant_read_all(
  1068. self, async_client: AsyncClient, archive_factory, printer_factory
  1069. ):
  1070. """With auth disabled, ARCHIVES_READ resolves to read-all (can_modify_all=True
  1071. in require_ownership_permission's auth-disabled branch). Existing
  1072. single-user installs see no behavior change."""
  1073. printer = await printer_factory()
  1074. archive = await archive_factory(printer.id, print_name="Anonymous", created_by_id=None)
  1075. # No Authorization header — auth-disabled mode.
  1076. response = await async_client.get(f"/api/v1/archives/{archive.id}")
  1077. # Either 200 (auth disabled in this test session) or 401 (auth enabled
  1078. # from a prior test) — both are acceptable; the IDOR closure does not
  1079. # change auth-enable/disable behavior. Pin not-404 to avoid masking a
  1080. # regression where auth-disabled callers would lose access.
  1081. assert response.status_code in (200, 401)