test_ownership_permissions.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  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. # REPRINT permissions
  248. # ========================================================================
  249. @pytest.mark.asyncio
  250. @pytest.mark.integration
  251. async def test_operator_cannot_reprint_others_archive(
  252. self, async_client: AsyncClient, auth_setup, archive_factory, printer_factory, db_session
  253. ):
  254. """Operator cannot reprint another user's archive."""
  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 == 403
  265. class TestQueueOwnershipPermissions(TestOwnershipPermissionsSetup):
  266. """Tests for print queue ownership-based permissions."""
  267. @pytest.fixture
  268. async def queue_item_factory(self, db_session, printer_factory, archive_factory):
  269. """Factory to create test queue items."""
  270. async def _create_item(**kwargs):
  271. from backend.app.models.print_queue import PrintQueueItem
  272. printer = await printer_factory()
  273. # Create an archive to link to the queue item
  274. archive = await archive_factory(printer.id)
  275. defaults = {
  276. "printer_id": printer.id,
  277. "archive_id": archive.id,
  278. "status": "pending",
  279. "position": 0,
  280. }
  281. defaults.update(kwargs)
  282. item = PrintQueueItem(**defaults)
  283. db_session.add(item)
  284. await db_session.commit()
  285. await db_session.refresh(item)
  286. return item
  287. return _create_item
  288. @pytest.mark.asyncio
  289. @pytest.mark.integration
  290. async def test_admin_can_delete_any_queue_item(self, async_client: AsyncClient, auth_setup, queue_item_factory):
  291. """Admin can delete any queue item."""
  292. item = await queue_item_factory(created_by_id=auth_setup["operator_user"]["id"])
  293. response = await async_client.delete(
  294. f"/api/v1/queue/{item.id}",
  295. headers={"Authorization": f"Bearer {auth_setup['admin_token']}"},
  296. )
  297. assert response.status_code == 200
  298. @pytest.mark.asyncio
  299. @pytest.mark.integration
  300. async def test_operator_can_delete_own_queue_item(self, async_client: AsyncClient, auth_setup, queue_item_factory):
  301. """Operator can delete their own queue item."""
  302. item = await queue_item_factory(created_by_id=auth_setup["operator_user"]["id"])
  303. response = await async_client.delete(
  304. f"/api/v1/queue/{item.id}",
  305. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  306. )
  307. assert response.status_code == 200
  308. @pytest.mark.asyncio
  309. @pytest.mark.integration
  310. async def test_operator_cannot_delete_others_queue_item(
  311. self, async_client: AsyncClient, auth_setup, queue_item_factory
  312. ):
  313. """Operator cannot delete another user's queue item."""
  314. item = await queue_item_factory(created_by_id=auth_setup["operator2_user"]["id"])
  315. response = await async_client.delete(
  316. f"/api/v1/queue/{item.id}",
  317. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  318. )
  319. assert response.status_code == 403
  320. @pytest.mark.asyncio
  321. @pytest.mark.integration
  322. async def test_operator_can_update_own_queue_item(self, async_client: AsyncClient, auth_setup, queue_item_factory):
  323. """Operator can update their own queue item."""
  324. item = await queue_item_factory(created_by_id=auth_setup["operator_user"]["id"])
  325. response = await async_client.patch(
  326. f"/api/v1/queue/{item.id}",
  327. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  328. json={"position": 10},
  329. )
  330. assert response.status_code == 200
  331. @pytest.mark.asyncio
  332. @pytest.mark.integration
  333. async def test_operator_cannot_update_others_queue_item(
  334. self, async_client: AsyncClient, auth_setup, queue_item_factory
  335. ):
  336. """Operator cannot update another user's queue item."""
  337. item = await queue_item_factory(created_by_id=auth_setup["operator2_user"]["id"])
  338. response = await async_client.patch(
  339. f"/api/v1/queue/{item.id}",
  340. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  341. json={"position": 10},
  342. )
  343. assert response.status_code == 403
  344. @pytest.mark.asyncio
  345. @pytest.mark.integration
  346. async def test_operator_cannot_cancel_others_queue_item(
  347. self, async_client: AsyncClient, auth_setup, queue_item_factory
  348. ):
  349. """Operator cannot cancel another user's queue item."""
  350. item = await queue_item_factory(created_by_id=auth_setup["operator2_user"]["id"])
  351. response = await async_client.post(
  352. f"/api/v1/queue/{item.id}/cancel",
  353. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  354. )
  355. assert response.status_code == 403
  356. @pytest.mark.asyncio
  357. @pytest.mark.integration
  358. async def test_bulk_update_skips_non_owned_items(self, async_client: AsyncClient, auth_setup, queue_item_factory):
  359. """Bulk update only updates items the user owns."""
  360. # Create items owned by different users
  361. own_item = await queue_item_factory(
  362. created_by_id=auth_setup["operator_user"]["id"],
  363. )
  364. other_item = await queue_item_factory(
  365. created_by_id=auth_setup["operator2_user"]["id"],
  366. )
  367. response = await async_client.patch(
  368. "/api/v1/queue/bulk",
  369. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  370. json={
  371. "item_ids": [own_item.id, other_item.id],
  372. "manual_start": True,
  373. },
  374. )
  375. assert response.status_code == 200
  376. result = response.json()
  377. # Should only update the owned item
  378. assert result["updated_count"] == 1
  379. assert result["skipped_count"] == 1
  380. class TestLibraryOwnershipPermissions(TestOwnershipPermissionsSetup):
  381. """Tests for library file ownership-based permissions."""
  382. @pytest.fixture
  383. async def library_file_factory(self, db_session):
  384. """Factory to create test library files."""
  385. _counter = [0]
  386. async def _create_file(**kwargs):
  387. from backend.app.models.library import LibraryFile
  388. _counter[0] += 1
  389. defaults = {
  390. "filename": f"test_{_counter[0]}.3mf",
  391. "file_path": f"library/test_{_counter[0]}.3mf",
  392. "file_type": "3mf",
  393. "file_size": 1024,
  394. }
  395. defaults.update(kwargs)
  396. file = LibraryFile(**defaults)
  397. db_session.add(file)
  398. await db_session.commit()
  399. await db_session.refresh(file)
  400. return file
  401. return _create_file
  402. @pytest.fixture
  403. async def library_folder_factory(self, db_session):
  404. """Factory to create test library folders."""
  405. _counter = [0]
  406. async def _create_folder(**kwargs):
  407. from backend.app.models.library import LibraryFolder
  408. _counter[0] += 1
  409. defaults = {
  410. "name": f"TestFolder_{_counter[0]}",
  411. }
  412. defaults.update(kwargs)
  413. folder = LibraryFolder(**defaults)
  414. db_session.add(folder)
  415. await db_session.commit()
  416. await db_session.refresh(folder)
  417. return folder
  418. return _create_folder
  419. @pytest.mark.asyncio
  420. @pytest.mark.integration
  421. async def test_admin_can_delete_any_library_file(self, async_client: AsyncClient, auth_setup, library_file_factory):
  422. """Admin can delete any library file."""
  423. file = await library_file_factory(created_by_id=auth_setup["operator_user"]["id"])
  424. response = await async_client.delete(
  425. f"/api/v1/library/files/{file.id}",
  426. headers={"Authorization": f"Bearer {auth_setup['admin_token']}"},
  427. )
  428. assert response.status_code == 200
  429. @pytest.mark.asyncio
  430. @pytest.mark.integration
  431. async def test_operator_can_delete_own_library_file(
  432. self, async_client: AsyncClient, auth_setup, library_file_factory
  433. ):
  434. """Operator can delete their own library file."""
  435. file = await library_file_factory(created_by_id=auth_setup["operator_user"]["id"])
  436. response = await async_client.delete(
  437. f"/api/v1/library/files/{file.id}",
  438. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  439. )
  440. assert response.status_code == 200
  441. @pytest.mark.asyncio
  442. @pytest.mark.integration
  443. async def test_operator_cannot_delete_others_library_file(
  444. self, async_client: AsyncClient, auth_setup, library_file_factory
  445. ):
  446. """Operator cannot delete another user's library file."""
  447. file = await library_file_factory(created_by_id=auth_setup["operator2_user"]["id"])
  448. response = await async_client.delete(
  449. f"/api/v1/library/files/{file.id}",
  450. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  451. )
  452. assert response.status_code == 403
  453. @pytest.mark.asyncio
  454. @pytest.mark.integration
  455. async def test_operator_can_update_own_library_file(
  456. self, async_client: AsyncClient, auth_setup, library_file_factory
  457. ):
  458. """Operator can update their own library file."""
  459. file = await library_file_factory(created_by_id=auth_setup["operator_user"]["id"])
  460. response = await async_client.put(
  461. f"/api/v1/library/files/{file.id}",
  462. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  463. json={"filename": "renamed.3mf"},
  464. )
  465. assert response.status_code == 200
  466. @pytest.mark.asyncio
  467. @pytest.mark.integration
  468. async def test_operator_cannot_update_others_library_file(
  469. self, async_client: AsyncClient, auth_setup, library_file_factory
  470. ):
  471. """Operator cannot update another user's library file."""
  472. file = await library_file_factory(created_by_id=auth_setup["operator2_user"]["id"])
  473. response = await async_client.put(
  474. f"/api/v1/library/files/{file.id}",
  475. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  476. json={"filename": "renamed.3mf"},
  477. )
  478. assert response.status_code == 403
  479. @pytest.mark.asyncio
  480. @pytest.mark.integration
  481. async def test_folders_require_all_permission(self, async_client: AsyncClient, auth_setup, library_folder_factory):
  482. """Folders require *_all permission (no ownership tracking on folders)."""
  483. folder = await library_folder_factory(name="TestFolder")
  484. # Operator cannot delete folder (needs *_all)
  485. response = await async_client.delete(
  486. f"/api/v1/library/folders/{folder.id}",
  487. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  488. )
  489. assert response.status_code == 403
  490. @pytest.mark.asyncio
  491. @pytest.mark.integration
  492. async def test_bulk_delete_skips_non_owned_files(self, async_client: AsyncClient, auth_setup, library_file_factory):
  493. """Bulk delete only deletes files the user owns."""
  494. own_file = await library_file_factory(
  495. filename="own.3mf",
  496. created_by_id=auth_setup["operator_user"]["id"],
  497. )
  498. other_file = await library_file_factory(
  499. filename="other.3mf",
  500. created_by_id=auth_setup["operator2_user"]["id"],
  501. )
  502. response = await async_client.post(
  503. "/api/v1/library/bulk-delete",
  504. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  505. json={"file_ids": [own_file.id, other_file.id], "folder_ids": []},
  506. )
  507. assert response.status_code == 200
  508. result = response.json()
  509. # Should only delete the owned file; other_file is skipped (but skipped count not in response)
  510. assert result["deleted_files"] == 1
  511. class TestAuthDisabledPermissions:
  512. """Tests that verify all operations are allowed when auth is disabled."""
  513. @pytest.mark.asyncio
  514. @pytest.mark.integration
  515. async def test_delete_archive_without_auth(
  516. self, async_client: AsyncClient, archive_factory, printer_factory, db_session
  517. ):
  518. """When auth is disabled, anyone can delete archives."""
  519. printer = await printer_factory()
  520. archive = await archive_factory(printer.id)
  521. response = await async_client.delete(f"/api/v1/archives/{archive.id}")
  522. assert response.status_code == 200
  523. @pytest.mark.asyncio
  524. @pytest.mark.integration
  525. async def test_update_archive_without_auth(
  526. self, async_client: AsyncClient, archive_factory, printer_factory, db_session
  527. ):
  528. """When auth is disabled, anyone can update archives."""
  529. printer = await printer_factory()
  530. archive = await archive_factory(printer.id)
  531. response = await async_client.patch(
  532. f"/api/v1/archives/{archive.id}",
  533. json={"print_name": "Updated Name"},
  534. )
  535. assert response.status_code == 200
  536. class TestUserItemsCountAndDeletion(TestOwnershipPermissionsSetup):
  537. """Tests for user items count endpoint and deletion with items."""
  538. @pytest.mark.asyncio
  539. @pytest.mark.integration
  540. async def test_get_user_items_count(
  541. self, async_client: AsyncClient, auth_setup, archive_factory, printer_factory, db_session
  542. ):
  543. """Verify items count endpoint returns correct counts."""
  544. printer = await printer_factory()
  545. user_id = auth_setup["operator_user"]["id"]
  546. # Create some items for the operator
  547. await archive_factory(printer.id, created_by_id=user_id)
  548. await archive_factory(printer.id, created_by_id=user_id)
  549. response = await async_client.get(
  550. f"/api/v1/users/{user_id}/items-count",
  551. headers={"Authorization": f"Bearer {auth_setup['admin_token']}"},
  552. )
  553. assert response.status_code == 200
  554. counts = response.json()
  555. assert counts["archives"] >= 2
  556. assert "queue_items" in counts
  557. assert "library_files" in counts
  558. @pytest.mark.asyncio
  559. @pytest.mark.integration
  560. async def test_delete_user_keeps_items(
  561. self, async_client: AsyncClient, auth_setup, archive_factory, printer_factory, db_session
  562. ):
  563. """Verify deleting user without delete_items keeps items (ownerless)."""
  564. printer = await printer_factory()
  565. user_id = auth_setup["operator2_user"]["id"]
  566. # Create archive for operator2
  567. archive = await archive_factory(printer.id, created_by_id=user_id)
  568. archive_id = archive.id
  569. # Delete user without deleting items
  570. response = await async_client.delete(
  571. f"/api/v1/users/{user_id}?delete_items=false",
  572. headers={"Authorization": f"Bearer {auth_setup['admin_token']}"},
  573. )
  574. assert response.status_code == 204
  575. # Verify archive still exists but is now ownerless
  576. archive_response = await async_client.get(
  577. f"/api/v1/archives/{archive_id}",
  578. headers={"Authorization": f"Bearer {auth_setup['admin_token']}"},
  579. )
  580. assert archive_response.status_code == 200
  581. assert archive_response.json()["created_by_id"] is None
  582. @pytest.mark.asyncio
  583. @pytest.mark.integration
  584. async def test_delete_user_with_items(
  585. self, async_client: AsyncClient, auth_setup, archive_factory, printer_factory, db_session
  586. ):
  587. """Verify deleting user with delete_items=true removes their items."""
  588. printer = await printer_factory()
  589. # Create a new user with items
  590. create_response = await async_client.post(
  591. "/api/v1/users/",
  592. headers={"Authorization": f"Bearer {auth_setup['admin_token']}"},
  593. json={
  594. "username": "deletewithitems",
  595. "password": "Password123!",
  596. },
  597. )
  598. user_id = create_response.json()["id"]
  599. # Create archive for this user
  600. archive = await archive_factory(printer.id, created_by_id=user_id)
  601. archive_id = archive.id
  602. # Delete user WITH deleting items
  603. response = await async_client.delete(
  604. f"/api/v1/users/{user_id}?delete_items=true",
  605. headers={"Authorization": f"Bearer {auth_setup['admin_token']}"},
  606. )
  607. assert response.status_code == 204
  608. # Verify archive was deleted
  609. archive_response = await async_client.get(
  610. f"/api/v1/archives/{archive_id}",
  611. headers={"Authorization": f"Bearer {auth_setup['admin_token']}"},
  612. )
  613. assert archive_response.status_code == 404