test_github_restore_api.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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": {
  149. "restored": 1,
  150. "skipped": 2,
  151. "failed": 0,
  152. "notes": [
  153. {
  154. "code": "settingsCredentialsSkipped",
  155. "params": {"count": 1},
  156. "message": "1 credential-like key(s) skipped",
  157. }
  158. ],
  159. },
  160. },
  161. }
  162. with patch(
  163. "backend.app.services.github_restore.github_restore_service.run_restore",
  164. new=AsyncMock(return_value=outcome),
  165. ) as mock:
  166. response = await async_client.post(
  167. "/api/v1/github-backup/restore",
  168. json={"ref": "aaa1111", "categories": ["spools", "settings"], "overwrite_existing": True},
  169. )
  170. assert response.status_code == 200
  171. body = response.json()
  172. assert body["results"]["spools"]["restored"] == 4
  173. # Notes cross the wire as code + params + English fallback, so a
  174. # non-English client can translate them (#2656).
  175. assert body["results"]["settings"]["notes"] == [
  176. {
  177. "code": "settingsCredentialsSkipped",
  178. "params": {"count": 1},
  179. "message": "1 credential-like key(s) skipped",
  180. }
  181. ]
  182. assert mock.await_args.kwargs["overwrite_existing"] is True
  183. assert mock.await_args.kwargs["ref"] == "aaa1111"
  184. @pytest.mark.asyncio
  185. @pytest.mark.integration
  186. async def test_rejects_empty_category_list(self, async_client: AsyncClient):
  187. await _create_config(async_client)
  188. response = await async_client.post("/api/v1/github-backup/restore", json={"categories": []})
  189. assert response.status_code == 422
  190. @pytest.mark.asyncio
  191. @pytest.mark.integration
  192. async def test_rejects_unknown_category(self, async_client: AsyncClient):
  193. await _create_config(async_client)
  194. response = await async_client.post("/api/v1/github-backup/restore", json={"categories": ["cloud_profiles"]})
  195. assert response.status_code == 422
  196. @pytest.mark.asyncio
  197. @pytest.mark.integration
  198. async def test_rejects_malformed_ref(self, async_client: AsyncClient):
  199. await _create_config(async_client)
  200. response = await async_client.post(
  201. "/api/v1/github-backup/restore", json={"ref": "main", "categories": ["spools"]}
  202. )
  203. assert response.status_code == 422
  204. @pytest.mark.asyncio
  205. @pytest.mark.integration
  206. async def test_defaults_overwrite_to_false(self, async_client: AsyncClient):
  207. """The safe default: a restore only inserts what's missing."""
  208. await _create_config(async_client)
  209. mock = AsyncMock(return_value={"success": True, "message": "ok", "results": {}})
  210. with patch("backend.app.services.github_restore.github_restore_service.run_restore", new=mock):
  211. response = await async_client.post("/api/v1/github-backup/restore", json={"categories": ["spools"]})
  212. assert response.status_code == 200
  213. assert mock.await_args.kwargs["overwrite_existing"] is False
  214. @pytest.mark.asyncio
  215. @pytest.mark.integration
  216. async def test_service_failure_is_reported_in_body(self, async_client: AsyncClient):
  217. await _create_config(async_client)
  218. with patch(
  219. "backend.app.services.github_restore.github_restore_service.run_restore",
  220. new=AsyncMock(
  221. return_value={
  222. "success": False,
  223. "message": "A backup is currently running. Wait for it to finish before restoring.",
  224. "results": {},
  225. }
  226. ),
  227. ):
  228. response = await async_client.post("/api/v1/github-backup/restore", json={"categories": ["spools"]})
  229. assert response.status_code == 200
  230. assert response.json()["success"] is False
  231. assert "backup is currently running" in response.json()["message"]
  232. class TestStatusExposesRestoreState:
  233. @pytest.mark.asyncio
  234. @pytest.mark.integration
  235. async def test_restore_running_is_false_when_idle(self, async_client: AsyncClient):
  236. await _create_config(async_client)
  237. response = await async_client.get("/api/v1/github-backup/status")
  238. assert response.status_code == 200
  239. assert response.json()["restore_running"] is False
  240. @pytest.mark.asyncio
  241. @pytest.mark.integration
  242. async def test_restore_running_is_reported(self, async_client: AsyncClient):
  243. """The UI disables both action buttons off this flag."""
  244. await _create_config(async_client)
  245. from backend.app.services.github_restore import github_restore_service
  246. github_restore_service._running_restore = True
  247. github_restore_service._progress = "Restoring spool inventory..."
  248. try:
  249. response = await async_client.get("/api/v1/github-backup/status")
  250. finally:
  251. github_restore_service._running_restore = False
  252. github_restore_service._progress = None
  253. assert response.json()["restore_running"] is True
  254. assert response.json()["progress"] == "Restoring spool inventory..."
  255. @pytest.mark.asyncio
  256. @pytest.mark.integration
  257. async def test_unconfigured_status_still_has_the_field(self, async_client: AsyncClient):
  258. response = await async_client.get("/api/v1/github-backup/status")
  259. assert response.status_code == 200
  260. assert response.json()["restore_running"] is False
  261. class TestRestoredArchivesAreVisibleToTheirOwner(TestOwnershipPermissionsSetup):
  262. """The archive-ownership blocker, proved through the route that enforces it.
  263. ``_ensure_archive_visible`` fails closed on a NULL ``created_by_id`` — 404 for
  264. any caller without ``archives:read_all`` — so before the collector and the
  265. restore carried the column across, a multi-user instance got archives the
  266. tally called restored and their owner could not open.
  267. """
  268. @pytest.mark.asyncio
  269. @pytest.mark.integration
  270. async def test_the_owning_non_admin_can_open_a_restored_archive(
  271. self, async_client: AsyncClient, auth_setup, db_session
  272. ):
  273. from backend.app.models.archive import PrintArchive
  274. from backend.app.services.github_restore import _CategoryTally, github_restore_service
  275. owner_id = auth_setup["operator_user"]["id"]
  276. payload = {
  277. "archives": [
  278. {
  279. "id": 77,
  280. "filename": "benchy.3mf",
  281. "file_size": 2048,
  282. "content_hash": "abc123",
  283. "print_name": "Benchy",
  284. "started_at": "2026-03-01 10:00:00",
  285. "created_at": "2026-03-01 10:00:00",
  286. "created_by_id": owner_id,
  287. }
  288. ]
  289. }
  290. await github_restore_service._restore_archives(db_session, payload, False, _CategoryTally(), {})
  291. await db_session.commit()
  292. restored = (await db_session.execute(select(PrintArchive))).scalar_one()
  293. assert restored.id != 77, "the backup's primary key must not be reused"
  294. response = await async_client.get(
  295. f"/api/v1/archives/{restored.id}",
  296. headers={"Authorization": f"Bearer {auth_setup['operator_token']}"},
  297. )
  298. assert response.status_code == 200, "the owner cannot see their own restored archive"
  299. assert response.json()["print_name"] == "Benchy"
  300. @pytest.mark.asyncio
  301. @pytest.mark.integration
  302. async def test_a_different_operator_still_cannot(self, async_client: AsyncClient, auth_setup, db_session):
  303. """Control: carrying the owner across must not widen who can read it."""
  304. from backend.app.models.archive import PrintArchive
  305. from backend.app.services.github_restore import _CategoryTally, github_restore_service
  306. payload = {
  307. "archives": [
  308. {
  309. "id": 77,
  310. "filename": "benchy.3mf",
  311. "file_size": 2048,
  312. "content_hash": "abc123",
  313. "started_at": "2026-03-01 10:00:00",
  314. "created_by_id": auth_setup["operator_user"]["id"],
  315. }
  316. ]
  317. }
  318. await github_restore_service._restore_archives(db_session, payload, False, _CategoryTally(), {})
  319. await db_session.commit()
  320. restored = (await db_session.execute(select(PrintArchive))).scalar_one()
  321. response = await async_client.get(
  322. f"/api/v1/archives/{restored.id}",
  323. headers={"Authorization": f"Bearer {auth_setup['operator2_token']}"},
  324. )
  325. assert response.status_code == 404
  326. class TestRestoreDoesNotOpenTheMetricsEndpoint:
  327. """The companion-credential rule, proved against the endpoint it protects.
  328. ``/api/v1/metrics`` is on ``PUBLIC_API_ROUTES`` and its only gate is
  329. ``if token:``, so writing ``prometheus_enabled`` onto an instance with no
  330. ``prometheus_token`` row hands the entire metrics body to anyone who can
  331. reach the port. The restore refuses that token as credential-shaped, so
  332. before this change the pair came apart and the endpoint opened — with
  333. overwrite *off*, since the local row is missing rather than present.
  334. Driven through the real service and the real endpoint against one database:
  335. the unit tests can show the toggle is not written, only this can show what
  336. that means.
  337. """
  338. @pytest.mark.asyncio
  339. @pytest.mark.integration
  340. async def test_restoring_prometheus_enabled_leaves_the_endpoint_shut(self, async_client: AsyncClient, db_session):
  341. from backend.app.services.github_restore import _CategoryTally, github_restore_service
  342. # An instance that never enabled Prometheus: no toggle row, no token row.
  343. assert (await async_client.get("/api/v1/metrics")).status_code == 404
  344. tally = _CategoryTally()
  345. await github_restore_service._restore_settings(
  346. db_session,
  347. {"settings": {"prometheus_enabled": "true", "prometheus_token": "s3cret", "currency": "EUR"}},
  348. overwrite=False,
  349. tally=tally,
  350. )
  351. await db_session.commit()
  352. response = await async_client.get("/api/v1/metrics")
  353. assert response.status_code == 404, "a settings restore opened the metrics endpoint"
  354. assert "bambuddy_build_info" not in response.text
  355. assert any(note["code"] == "settingsCompanionSkipped" for note in tally.notes)
  356. @pytest.mark.asyncio
  357. @pytest.mark.integration
  358. async def test_an_instance_with_its_own_token_still_gets_the_toggle_back(
  359. self, async_client: AsyncClient, db_session
  360. ):
  361. """Control. The rule must not break a legitimate Prometheus restore."""
  362. from backend.app.services.github_restore import _CategoryTally, github_restore_service
  363. await async_client.put(
  364. "/api/v1/settings/", json={"prometheus_enabled": False, "prometheus_token": "local-token"}
  365. )
  366. await github_restore_service._restore_settings(
  367. db_session,
  368. {"settings": {"prometheus_enabled": "true", "prometheus_token": "s3cret"}},
  369. overwrite=True,
  370. tally=_CategoryTally(),
  371. )
  372. await db_session.commit()
  373. assert (await async_client.get("/api/v1/metrics")).status_code == 401
  374. authorised = await async_client.get("/api/v1/metrics", headers={"Authorization": "Bearer local-token"})
  375. assert authorised.status_code == 200
  376. assert "bambuddy_build_info" in authorised.text