test_updates_api.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. """Integration tests for Updates API endpoints."""
  2. from pathlib import Path
  3. from unittest.mock import AsyncMock, MagicMock, patch
  4. import pytest
  5. from httpx import AsyncClient
  6. class TestUpdatesAPI:
  7. @pytest.mark.asyncio
  8. async def test_get_version(self, async_client: AsyncClient):
  9. response = await async_client.get("/api/v1/updates/version")
  10. assert response.status_code == 200
  11. @pytest.mark.asyncio
  12. async def test_apply_update_docker_rejection(self, async_client: AsyncClient):
  13. with patch("backend.app.api.routes.updates._is_docker_environment", return_value=True):
  14. response = await async_client.post("/api/v1/updates/apply")
  15. result = response.json()
  16. assert result["success"] is False
  17. assert result["is_docker"] is True
  18. @pytest.mark.asyncio
  19. async def test_apply_update_non_docker(self, async_client: AsyncClient):
  20. """Test non-Docker path - mock _perform_update to prevent side effects."""
  21. with (
  22. patch("backend.app.api.routes.updates._is_docker_environment", return_value=False),
  23. patch("backend.app.api.routes.updates._perform_update", new_callable=AsyncMock),
  24. ):
  25. response = await async_client.post("/api/v1/updates/apply")
  26. assert response.json()["success"] is True
  27. def test_is_docker_with_dockerenv(self):
  28. from backend.app.api.routes.updates import _is_docker_environment
  29. with patch("os.path.exists", return_value=True):
  30. assert _is_docker_environment() is True
  31. def test_parse_version(self):
  32. from backend.app.api.routes.updates import parse_version
  33. assert parse_version("0.1.5")[:3] == (0, 1, 5)
  34. def test_is_newer_version(self):
  35. from backend.app.api.routes.updates import is_newer_version
  36. assert is_newer_version("0.1.5", "0.1.5b7") is True
  37. def test_parse_github_remote_recognises_ssh_https_and_dotgit(self):
  38. """`_parse_github_remote` must accept the four canonical forms `git
  39. remote -v` prints; anything else returns None so callers can treat
  40. it as 'reset to expected URL'."""
  41. from backend.app.api.routes.updates import _parse_github_remote
  42. assert _parse_github_remote("git@github.com:maziggy/bambuddy.git") == (
  43. "maziggy",
  44. "bambuddy",
  45. )
  46. assert _parse_github_remote("git@github.com:maziggy/bambuddy") == (
  47. "maziggy",
  48. "bambuddy",
  49. )
  50. assert _parse_github_remote("https://github.com/maziggy/bambuddy.git") == (
  51. "maziggy",
  52. "bambuddy",
  53. )
  54. assert _parse_github_remote("https://github.com/maziggy/bambuddy") == (
  55. "maziggy",
  56. "bambuddy",
  57. )
  58. # Non-GitHub host → None (we don't claim ownership over arbitrary
  59. # forge URLs).
  60. assert _parse_github_remote("git@gitlab.com:maziggy/bambuddy.git") is None
  61. # Empty / malformed → None.
  62. assert _parse_github_remote("") is None
  63. assert _parse_github_remote("not-a-url") is None
  64. assert _parse_github_remote("https://github.com/maziggy") is None # no /repo
  65. @pytest.mark.asyncio
  66. async def test_perform_update_preserves_ssh_origin_when_pointing_at_correct_repo(self, tmp_path):
  67. """Regression for the developer-checkout footgun: if origin already
  68. points at github.com/maziggy/bambuddy via SSH, the updater must
  69. leave it alone instead of clobbering it with HTTPS. Pre-fix, every
  70. Apply Update click rewrote `git@github.com:...` to `https://...`,
  71. breaking subsequent `git push` for any developer testing the
  72. upgrade flow against their own checkout."""
  73. from backend.app.api.routes import updates as updates_module
  74. app_dir = tmp_path / "app"
  75. data_dir = tmp_path / "app" / "data"
  76. app_dir.mkdir()
  77. data_dir.mkdir()
  78. (app_dir / "requirements.txt").write_text("fastapi\n")
  79. calls: list[dict] = []
  80. async def fake_create_subprocess_exec(*args, **kwargs):
  81. calls.append({"args": args, "cwd": kwargs.get("cwd")})
  82. proc = MagicMock()
  83. # When the updater asks `git remote get-url origin`, return the
  84. # SSH URL. Every other subprocess returns successfully with no
  85. # output.
  86. if "get-url" in args and "origin" in args:
  87. proc.communicate = AsyncMock(return_value=(b"git@github.com:maziggy/bambuddy.git\n", b""))
  88. else:
  89. proc.communicate = AsyncMock(return_value=(b"", b""))
  90. proc.returncode = 0
  91. return proc
  92. with (
  93. patch.object(updates_module.settings, "base_dir", data_dir),
  94. patch.object(updates_module.settings, "app_dir", app_dir),
  95. patch.object(updates_module, "_find_executable", return_value="/usr/bin/git"),
  96. patch.object(
  97. updates_module.asyncio,
  98. "create_subprocess_exec",
  99. side_effect=fake_create_subprocess_exec,
  100. ),
  101. ):
  102. await updates_module._perform_update()
  103. # The updater MUST NOT have run `git remote set-url origin <https>`
  104. # because origin already pointed at the right repo over SSH.
  105. set_url_calls = [c for c in calls if "set-url" in c["args"] and "origin" in c["args"]]
  106. assert not set_url_calls, (
  107. "Updater clobbered an SSH origin pointing at the correct repo. "
  108. "Captured set-url calls: " + repr([c["args"] for c in set_url_calls])
  109. )
  110. @pytest.mark.asyncio
  111. async def test_perform_update_resets_origin_when_pointing_elsewhere(self, tmp_path):
  112. """Defensive: if origin points at a fork or unrelated repo (or is
  113. missing), the updater should still rewrite it to the canonical
  114. HTTPS URL so subsequent fetch / reset works against the right
  115. repo. This is the original behaviour that the SSH-preservation
  116. fix above must NOT regress."""
  117. from backend.app.api.routes import updates as updates_module
  118. from backend.app.core.config import GITHUB_REPO
  119. app_dir = tmp_path / "app"
  120. data_dir = tmp_path / "app" / "data"
  121. app_dir.mkdir()
  122. data_dir.mkdir()
  123. (app_dir / "requirements.txt").write_text("fastapi\n")
  124. calls: list[dict] = []
  125. async def fake_create_subprocess_exec(*args, **kwargs):
  126. calls.append({"args": args, "cwd": kwargs.get("cwd")})
  127. proc = MagicMock()
  128. # origin is set to a fork — must be rewritten.
  129. if "get-url" in args and "origin" in args:
  130. proc.communicate = AsyncMock(return_value=(b"git@github.com:somefork/bambuddy.git\n", b""))
  131. else:
  132. proc.communicate = AsyncMock(return_value=(b"", b""))
  133. proc.returncode = 0
  134. return proc
  135. with (
  136. patch.object(updates_module.settings, "base_dir", data_dir),
  137. patch.object(updates_module.settings, "app_dir", app_dir),
  138. patch.object(updates_module, "_find_executable", return_value="/usr/bin/git"),
  139. patch.object(
  140. updates_module.asyncio,
  141. "create_subprocess_exec",
  142. side_effect=fake_create_subprocess_exec,
  143. ),
  144. ):
  145. await updates_module._perform_update()
  146. set_url_calls = [c for c in calls if "set-url" in c["args"] and "origin" in c["args"]]
  147. assert set_url_calls, "Updater must rewrite origin when it points at a fork."
  148. rewritten_to = set_url_calls[0]["args"][-1]
  149. assert rewritten_to == f"https://github.com/{GITHUB_REPO}.git", (
  150. f"Expected origin to be reset to canonical HTTPS URL; got: {rewritten_to}"
  151. )
  152. @pytest.mark.asyncio
  153. async def test_perform_update_runs_pip_in_app_dir_not_data_dir(self, tmp_path):
  154. """Native install: `requirements.txt` lives at INSTALL_PATH (the source-
  155. code dir), NOT at DATA_DIR (where systemd sets DATA_DIR=INSTALL_PATH/data).
  156. Pre-fix, the updater ran `pip install -r requirements.txt` with
  157. `cwd=settings.base_dir`, which on a native install resolves to the data
  158. dir — `requirements.txt` isn't there and pip fails with `Could not open
  159. requirements file`. The fix: pip's cwd is `settings.app_dir` (the source
  160. tree) so it can actually find the file.
  161. This test mocks every subprocess so it can capture the cwd of each call
  162. and assert that the pip step runs in app_dir while git steps continue
  163. to run in base_dir (their existing behaviour — git walks up to find
  164. `.git` so that path keeps working)."""
  165. from backend.app.api.routes import updates as updates_module
  166. # Set up fake install layout: app_dir has requirements.txt, data_dir is
  167. # a sibling (mirroring `INSTALL_PATH=/opt/bambuddy`, `DATA_DIR=/opt/bambuddy/data`).
  168. app_dir = tmp_path / "app"
  169. data_dir = tmp_path / "app" / "data"
  170. app_dir.mkdir()
  171. data_dir.mkdir()
  172. (app_dir / "requirements.txt").write_text("fastapi\n")
  173. # Capture every subprocess call's cwd + the executable token.
  174. calls: list[dict] = []
  175. async def fake_create_subprocess_exec(*args, **kwargs):
  176. calls.append({"args": args, "cwd": kwargs.get("cwd")})
  177. proc = MagicMock()
  178. proc.communicate = AsyncMock(return_value=(b"", b""))
  179. proc.returncode = 0
  180. return proc
  181. with (
  182. patch.object(updates_module.settings, "base_dir", data_dir),
  183. patch.object(updates_module.settings, "app_dir", app_dir),
  184. patch.object(updates_module, "_find_executable", return_value="/usr/bin/git"),
  185. patch.object(
  186. updates_module.asyncio,
  187. "create_subprocess_exec",
  188. side_effect=fake_create_subprocess_exec,
  189. ),
  190. ):
  191. await updates_module._perform_update()
  192. # Find the pip invocation (sys.executable + "-m" + "pip" + "install").
  193. pip_calls = [c for c in calls if "pip" in c["args"] and "install" in c["args"]]
  194. assert pip_calls, "pip install was never invoked. Captured: " + repr([c["args"] for c in calls])
  195. pip_cwd = pip_calls[0]["cwd"]
  196. assert pip_cwd == str(app_dir), (
  197. f"pip install must run in app_dir ({app_dir}) so it finds "
  198. f"requirements.txt; got cwd={pip_cwd}. Regression to base_dir "
  199. f"breaks every native-install upgrade."
  200. )
  201. # Sanity check: the requirements.txt that pip would read actually exists
  202. # at the captured cwd. If this fails the cwd is wrong even if it isn't
  203. # base_dir — useful diagnostic if someone refactors path handling.
  204. assert (Path(pip_cwd) / "requirements.txt").exists()