test_github_restore_api.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. """Integration tests for the Git backup restore API endpoints (#2656)."""
  2. from unittest.mock import AsyncMock, patch
  3. import pytest
  4. from httpx import AsyncClient
  5. from sqlalchemy import select
  6. from backend.tests.integration.test_ownership_permissions import TestOwnershipPermissionsSetup
  7. @pytest.fixture(autouse=True)
  8. def _mock_private_repo_check():
  9. """POST /config refuses to save unless the repo is confirmed private."""
  10. with patch(
  11. "backend.app.services.github_backup.github_backup_service.test_connection",
  12. new=AsyncMock(
  13. return_value={
  14. "success": True,
  15. "message": "Connection successful",
  16. "repo_name": "test/repo",
  17. "permissions": {"push": True},
  18. "is_private": True,
  19. }
  20. ),
  21. ) as m:
  22. yield m
  23. async def _create_config(async_client: AsyncClient) -> dict:
  24. response = await async_client.post(
  25. "/api/v1/github-backup/config",
  26. json={
  27. "repository_url": "https://github.com/test/repo",
  28. "access_token": "ghp_testtoken123",
  29. "branch": "main",
  30. "backup_kprofiles": True,
  31. "backup_spools": True,
  32. "backup_archives": True,
  33. "backup_settings": True,
  34. "enabled": True,
  35. },
  36. )
  37. assert response.status_code == 200
  38. return response.json()
  39. class TestCommitsEndpoint:
  40. @pytest.mark.asyncio
  41. @pytest.mark.integration
  42. async def test_404_when_not_configured(self, async_client: AsyncClient):
  43. response = await async_client.get("/api/v1/github-backup/commits")
  44. assert response.status_code == 404
  45. assert "Configure backup first" in response.json()["detail"]
  46. @pytest.mark.asyncio
  47. @pytest.mark.integration
  48. async def test_returns_commits_from_the_provider(self, async_client: AsyncClient):
  49. await _create_config(async_client)
  50. commits = [
  51. {"sha": "aaa1111", "message": "Bambuddy backup", "author": "Bambuddy", "date": "2026-07-02T10:00:00Z"}
  52. ]
  53. with patch(
  54. "backend.app.services.git_providers.github.GitHubBackend.list_commits",
  55. new=AsyncMock(return_value={"success": True, "message": "OK", "commits": commits}),
  56. ):
  57. response = await async_client.get("/api/v1/github-backup/commits")
  58. assert response.status_code == 200
  59. body = response.json()
  60. assert body["success"] is True
  61. assert body["branch"] == "main"
  62. assert body["commits"][0]["sha"] == "aaa1111"
  63. @pytest.mark.asyncio
  64. @pytest.mark.integration
  65. async def test_provider_failure_is_reported_not_raised(self, async_client: AsyncClient):
  66. await _create_config(async_client)
  67. with patch(
  68. "backend.app.services.git_providers.github.GitHubBackend.list_commits",
  69. new=AsyncMock(return_value={"success": False, "message": "Invalid access token", "commits": []}),
  70. ):
  71. response = await async_client.get("/api/v1/github-backup/commits")
  72. assert response.status_code == 200
  73. assert response.json()["success"] is False
  74. assert response.json()["commits"] == []
  75. @pytest.mark.asyncio
  76. @pytest.mark.integration
  77. async def test_limit_is_bounded(self, async_client: AsyncClient):
  78. await _create_config(async_client)
  79. assert (await async_client.get("/api/v1/github-backup/commits?limit=0")).status_code == 422
  80. assert (await async_client.get("/api/v1/github-backup/commits?limit=101")).status_code == 422
  81. class TestPreviewEndpoint:
  82. @pytest.mark.asyncio
  83. @pytest.mark.integration
  84. async def test_404_when_not_configured(self, async_client: AsyncClient):
  85. response = await async_client.get("/api/v1/github-backup/restore/preview")
  86. assert response.status_code == 404
  87. @pytest.mark.asyncio
  88. @pytest.mark.integration
  89. async def test_reports_available_and_missing_categories(self, async_client: AsyncClient):
  90. await _create_config(async_client)
  91. preview = {
  92. "success": True,
  93. "message": "OK",
  94. "ref": "aaa1111",
  95. "commit": None,
  96. "metadata_version": "1.0",
  97. "categories": [
  98. {"category": "kprofiles", "available": False, "item_count": 0, "detail": "Not present"},
  99. {"category": "settings", "available": True, "item_count": 12, "detail": None},
  100. {"category": "spools", "available": True, "item_count": 4, "detail": "plus 9 usage records"},
  101. {"category": "archives", "available": True, "item_count": 30, "detail": "Metadata only"},
  102. ],
  103. }
  104. with patch(
  105. "backend.app.services.github_restore.github_restore_service.preview",
  106. new=AsyncMock(return_value=preview),
  107. ):
  108. response = await async_client.get("/api/v1/github-backup/restore/preview?ref=aaa1111")
  109. assert response.status_code == 200
  110. body = response.json()
  111. assert body["metadata_version"] == "1.0"
  112. by_name = {c["category"]: c for c in body["categories"]}
  113. assert by_name["kprofiles"]["available"] is False
  114. assert by_name["spools"]["item_count"] == 4
  115. @pytest.mark.asyncio
  116. @pytest.mark.integration
  117. @pytest.mark.parametrize("ref", ["main", "abc", "../../etc/passwd", "zzzzzzz"])
  118. async def test_rejects_refs_that_are_not_object_names(self, async_client: AsyncClient, ref):
  119. await _create_config(async_client)
  120. response = await async_client.get(f"/api/v1/github-backup/restore/preview?ref={ref}")
  121. assert response.status_code == 422
  122. @pytest.mark.asyncio
  123. @pytest.mark.integration
  124. async def test_defaults_to_head(self, async_client: AsyncClient):
  125. await _create_config(async_client)
  126. mock = AsyncMock(return_value={"success": True, "message": "OK", "ref": "aaa1111", "categories": []})
  127. with patch("backend.app.services.github_restore.github_restore_service.preview", new=mock):
  128. response = await async_client.get("/api/v1/github-backup/restore/preview")
  129. assert response.status_code == 200
  130. assert mock.await_args.kwargs["ref"] == "HEAD"
  131. class TestRestoreEndpoint:
  132. @pytest.mark.asyncio
  133. @pytest.mark.integration
  134. async def test_404_when_not_configured(self, async_client: AsyncClient):
  135. response = await async_client.post("/api/v1/github-backup/restore", json={"categories": ["spools"]})
  136. assert response.status_code == 404
  137. @pytest.mark.asyncio
  138. @pytest.mark.integration
  139. async def test_applies_selected_categories(self, async_client: AsyncClient):
  140. await _create_config(async_client)
  141. outcome = {
  142. "success": True,
  143. "message": "Restored 5 item(s) from aaa1111",
  144. "log_id": 3,
  145. "ref": "aaa1111",
  146. "results": {
  147. "spools": {"restored": 4, "skipped": 1, "failed": 0, "notes": []},
  148. "settings": {"restored": 1, "skipped": 2, "failed": 0, "notes": ["1 credential-like key(s) skipped"]},
  149. },
  150. }
  151. with patch(
  152. "backend.app.services.github_restore.github_restore_service.run_restore",
  153. new=AsyncMock(return_value=outcome),
  154. ) as mock:
  155. response = await async_client.post(
  156. "/api/v1/github-backup/restore",
  157. json={"ref": "aaa1111", "categories": ["spools", "settings"], "overwrite_existing": True},
  158. )
  159. assert response.status_code == 200
  160. body = response.json()
  161. assert body["results"]["spools"]["restored"] == 4
  162. assert body["results"]["settings"]["notes"] == ["1 credential-like key(s) skipped"]
  163. assert mock.await_args.kwargs["overwrite_existing"] is True
  164. assert mock.await_args.kwargs["ref"] == "aaa1111"
  165. @pytest.mark.asyncio
  166. @pytest.mark.integration
  167. async def test_rejects_empty_category_list(self, async_client: AsyncClient):
  168. await _create_config(async_client)
  169. response = await async_client.post("/api/v1/github-backup/restore", json={"categories": []})
  170. assert response.status_code == 422
  171. @pytest.mark.asyncio
  172. @pytest.mark.integration
  173. async def test_rejects_unknown_category(self, async_client: AsyncClient):
  174. await _create_config(async_client)
  175. response = await async_client.post("/api/v1/github-backup/restore", json={"categories": ["cloud_profiles"]})
  176. assert response.status_code == 422
  177. @pytest.mark.asyncio
  178. @pytest.mark.integration
  179. async def test_rejects_malformed_ref(self, async_client: AsyncClient):
  180. await _create_config(async_client)
  181. response = await async_client.post(
  182. "/api/v1/github-backup/restore", json={"ref": "main", "categories": ["spools"]}
  183. )
  184. assert response.status_code == 422
  185. @pytest.mark.asyncio
  186. @pytest.mark.integration
  187. async def test_defaults_overwrite_to_false(self, async_client: AsyncClient):
  188. """The safe default: a restore only inserts what's missing."""
  189. await _create_config(async_client)
  190. mock = AsyncMock(return_value={"success": True, "message": "ok", "results": {}})
  191. with patch("backend.app.services.github_restore.github_restore_service.run_restore", new=mock):
  192. response = await async_client.post("/api/v1/github-backup/restore", json={"categories": ["spools"]})
  193. assert response.status_code == 200
  194. assert mock.await_args.kwargs["overwrite_existing"] is False
  195. @pytest.mark.asyncio
  196. @pytest.mark.integration
  197. async def test_service_failure_is_reported_in_body(self, async_client: AsyncClient):
  198. await _create_config(async_client)
  199. with patch(
  200. "backend.app.services.github_restore.github_restore_service.run_restore",
  201. new=AsyncMock(
  202. return_value={
  203. "success": False,
  204. "message": "A backup is currently running. Wait for it to finish before restoring.",
  205. "results": {},
  206. }
  207. ),
  208. ):
  209. response = await async_client.post("/api/v1/github-backup/restore", json={"categories": ["spools"]})
  210. assert response.status_code == 200
  211. assert response.json()["success"] is False
  212. assert "backup is currently running" in response.json()["message"]
  213. class TestStatusExposesRestoreState:
  214. @pytest.mark.asyncio
  215. @pytest.mark.integration
  216. async def test_restore_running_is_false_when_idle(self, async_client: AsyncClient):
  217. await _create_config(async_client)
  218. response = await async_client.get("/api/v1/github-backup/status")
  219. assert response.status_code == 200
  220. assert response.json()["restore_running"] is False
  221. @pytest.mark.asyncio
  222. @pytest.mark.integration
  223. async def test_restore_running_is_reported(self, async_client: AsyncClient):
  224. """The UI disables both action buttons off this flag."""
  225. await _create_config(async_client)
  226. from backend.app.services.github_restore import github_restore_service
  227. github_restore_service._running_restore = True
  228. github_restore_service._progress = "Restoring spool inventory..."
  229. try:
  230. response = await async_client.get("/api/v1/github-backup/status")
  231. finally:
  232. github_restore_service._running_restore = False
  233. github_restore_service._progress = None
  234. assert response.json()["restore_running"] is True
  235. assert response.json()["progress"] == "Restoring spool inventory..."
  236. @pytest.mark.asyncio
  237. @pytest.mark.integration
  238. async def test_unconfigured_status_still_has_the_field(self, async_client: AsyncClient):
  239. response = await async_client.get("/api/v1/github-backup/status")
  240. assert response.status_code == 200
  241. assert response.json()["restore_running"] is False
  242. class TestRestoredArchivesAreVisibleToTheirOwner(TestOwnershipPermissionsSetup):
  243. """The archive-ownership blocker, proved through the route that enforces it.
  244. ``_ensure_archive_visible`` fails closed on a NULL ``created_by_id`` — 404 for
  245. any caller without ``archives:read_all`` — so before the collector and the
  246. restore carried the column across, a multi-user instance got archives the
  247. tally called restored and their owner could not open.
  248. """
  249. @pytest.mark.asyncio
  250. @pytest.mark.integration
  251. async def test_the_owning_non_admin_can_open_a_restored_archive(
  252. self, async_client: AsyncClient, auth_setup, db_session
  253. ):
  254. from backend.app.models.archive import PrintArchive
  255. from backend.app.services.github_restore import _CategoryTally, github_restore_service
  256. owner_id = auth_setup["operator_user"]["id"]
  257. payload = {
  258. "archives": [
  259. {
  260. "id": 77,
  261. "filename": "benchy.3mf",
  262. "file_size": 2048,
  263. "content_hash": "abc123",
  264. "print_name": "Benchy",
  265. "started_at": "2026-03-01 10:00:00",
  266. "created_at": "2026-03-01 10:00:00",
  267. "created_by_id": owner_id,
  268. }
  269. ]
  270. }
  271. await github_restore_service._restore_archives(db_session, payload, False, _CategoryTally(), {})
  272. await db_session.commit()
  273. restored = (await db_session.execute(select(PrintArchive))).scalar_one()
  274. assert restored.id != 77, "the backup's primary key must not be reused"
  275. response = await async_client.get(
  276. f"/api/v1/archives/{restored.id}",
  277. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  278. )
  279. assert response.status_code == 200, "the owner cannot see their own restored archive"
  280. assert response.json()["print_name"] == "Benchy"
  281. @pytest.mark.asyncio
  282. @pytest.mark.integration
  283. async def test_a_different_operator_still_cannot(self, async_client: AsyncClient, auth_setup, db_session):
  284. """Control: carrying the owner across must not widen who can read it."""
  285. from backend.app.models.archive import PrintArchive
  286. from backend.app.services.github_restore import _CategoryTally, github_restore_service
  287. payload = {
  288. "archives": [
  289. {
  290. "id": 77,
  291. "filename": "benchy.3mf",
  292. "file_size": 2048,
  293. "content_hash": "abc123",
  294. "started_at": "2026-03-01 10:00:00",
  295. "created_by_id": auth_setup["operator_user"]["id"],
  296. }
  297. ]
  298. }
  299. await github_restore_service._restore_archives(db_session, payload, False, _CategoryTally(), {})
  300. await db_session.commit()
  301. restored = (await db_session.execute(select(PrintArchive))).scalar_one()
  302. response = await async_client.get(
  303. f"/api/v1/archives/{restored.id}",
  304. headers={"Authorization": f"Bearer {auth_setup['operator2_token']}"},
  305. )
  306. assert response.status_code == 404
  307. class TestRestoreDoesNotOpenTheMetricsEndpoint:
  308. """The companion-credential rule, proved against the endpoint it protects.
  309. ``/api/v1/metrics`` is on ``PUBLIC_API_ROUTES`` and its only gate is
  310. ``if token:``, so writing ``prometheus_enabled`` onto an instance with no
  311. ``prometheus_token`` row hands the entire metrics body to anyone who can
  312. reach the port. The restore refuses that token as credential-shaped, so
  313. before this change the pair came apart and the endpoint opened — with
  314. overwrite *off*, since the local row is missing rather than present.
  315. Driven through the real service and the real endpoint against one database:
  316. the unit tests can show the toggle is not written, only this can show what
  317. that means.
  318. """
  319. @pytest.mark.asyncio
  320. @pytest.mark.integration
  321. async def test_restoring_prometheus_enabled_leaves_the_endpoint_shut(self, async_client: AsyncClient, db_session):
  322. from backend.app.services.github_restore import _CategoryTally, github_restore_service
  323. # An instance that never enabled Prometheus: no toggle row, no token row.
  324. assert (await async_client.get("/api/v1/metrics")).status_code == 404
  325. tally = _CategoryTally()
  326. await github_restore_service._restore_settings(
  327. db_session,
  328. {"settings": {"prometheus_enabled": "true", "prometheus_token": "s3cret", "currency": "EUR"}},
  329. overwrite=False,
  330. tally=tally,
  331. )
  332. await db_session.commit()
  333. response = await async_client.get("/api/v1/metrics")
  334. assert response.status_code == 404, "a settings restore opened the metrics endpoint"
  335. assert "bambuddy_build_info" not in response.text
  336. assert any("switched off" in note for note in tally.notes)
  337. @pytest.mark.asyncio
  338. @pytest.mark.integration
  339. async def test_an_instance_with_its_own_token_still_gets_the_toggle_back(
  340. self, async_client: AsyncClient, db_session
  341. ):
  342. """Control. The rule must not break a legitimate Prometheus restore."""
  343. from backend.app.services.github_restore import _CategoryTally, github_restore_service
  344. await async_client.put(
  345. "/api/v1/settings/", json={"prometheus_enabled": False, "prometheus_token": "local-token"}
  346. )
  347. await github_restore_service._restore_settings(
  348. db_session,
  349. {"settings": {"prometheus_enabled": "true", "prometheus_token": "s3cret"}},
  350. overwrite=True,
  351. tally=_CategoryTally(),
  352. )
  353. await db_session.commit()
  354. assert (await async_client.get("/api/v1/metrics")).status_code == 401
  355. authorised = await async_client.get("/api/v1/metrics", headers={"Authorization": "Bearer local-token"})
  356. assert authorised.status_code == 200
  357. assert "bambuddy_build_info" in authorised.text