test_github_empty_repo_backup_3565.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. """A first Git backup to a GitHub repository with no commits (#3565).
  2. GitHub answers every Git Data call -- the branch lookup, but also blobs and
  3. trees -- with 409 "Git Repository is empty" until the repository has a commit.
  4. Bambuddy read the 409 on the branch lookup as a failure, and its blob/tree
  5. initial-commit path could not have started the repository either. The first
  6. commit now goes through the Contents API, and the rest follows as usual.
  7. """
  8. import base64
  9. import json
  10. from unittest.mock import AsyncMock
  11. import pytest
  12. from backend.app.services.git_providers.github import GitHubBackend
  13. from backend.tests.unit.test_git_providers import _blob_sha, _make_mock_response
  14. pytestmark = pytest.mark.unit
  15. REPO_URL = "https://github.com/owner/repo"
  16. EMPTY = {"message": "Git Repository is empty.", "status": "409"}
  17. FILES = {"settings.json": {"a": 1}, "printers.json": [{"name": "X1C"}]}
  18. def _empty_409():
  19. return _make_mock_response(409, EMPTY)
  20. def _client(*, get, put=(), post=(), patch=None):
  21. client = AsyncMock()
  22. client.get = AsyncMock(side_effect=list(get))
  23. client.put = AsyncMock(side_effect=list(put))
  24. client.post = AsyncMock(side_effect=list(post))
  25. client.patch = AsyncMock(return_value=patch or _make_mock_response(200, {}))
  26. return client
  27. def _branch_with(*paths):
  28. """The three reads push_files makes on a branch holding these backup files."""
  29. tree = [{"type": "blob", "path": p, "sha": _blob_sha(FILES[p])} for p in paths]
  30. return [
  31. _make_mock_response(200, {"object": {"sha": "c0"}}),
  32. _make_mock_response(200, {"tree": {"sha": "t0"}}),
  33. _make_mock_response(200, {"tree": tree}),
  34. ]
  35. def _seeded(sha="c0"):
  36. return _make_mock_response(201, {"commit": {"sha": sha}, "content": {}})
  37. async def _push(client, files=FILES, branch="main"):
  38. return await GitHubBackend().push_files(REPO_URL, "ghp_token", branch, files, client)
  39. class TestTheFirstBackup:
  40. @pytest.mark.asyncio
  41. async def test_the_reported_case_creates_the_first_commit_and_pushes_the_rest(self):
  42. client = _client(
  43. get=[_empty_409(), *_branch_with("settings.json")],
  44. put=[_seeded()],
  45. post=[
  46. _make_mock_response(201, {"sha": "blob-printers"}),
  47. _make_mock_response(201, {"sha": "t1"}),
  48. _make_mock_response(201, {"sha": "c1"}),
  49. ],
  50. )
  51. result = await _push(client)
  52. assert result["status"] == "success"
  53. assert result["files_changed"] == 2
  54. assert result["commit_sha"] == "c1"
  55. # The first commit through the Contents API, on the configured branch,
  56. # with the file exactly as push_files writes it, so it is not re-sent.
  57. url = client.put.await_args.args[0]
  58. body = client.put.await_args.kwargs["json"]
  59. assert url == "https://api.github.com/repos/owner/repo/contents/settings.json"
  60. assert body["branch"] == "main"
  61. assert base64.b64decode(body["content"]) == json.dumps(FILES["settings.json"], indent=2).encode()
  62. # The second file is the only blob: the seeded one was recognised.
  63. blob_posts = [c for c in client.post.await_args_list if c.args[0].endswith("/git/blobs")]
  64. assert len(blob_posts) == 1
  65. @pytest.mark.asyncio
  66. async def test_a_one_file_backup_is_done_by_the_first_commit(self):
  67. client = _client(get=[_empty_409()], put=[_seeded()])
  68. result = await _push(client, files={"settings.json": FILES["settings.json"]})
  69. assert result == {
  70. "status": "success",
  71. "message": "Initial backup created - 1 files",
  72. "commit_sha": "c0",
  73. "files_changed": 1,
  74. }
  75. client.post.assert_not_awaited()
  76. @pytest.mark.asyncio
  77. async def test_a_backup_branch_github_will_not_create_branches_off_the_default_one(self):
  78. files = {"settings.json": FILES["settings.json"]}
  79. client = _client(
  80. get=[
  81. _empty_409(),
  82. # After the commit on the default branch: backup branch missing,
  83. # so it is branched off the default one and pushed to again.
  84. _make_mock_response(404, {}),
  85. _make_mock_response(200, {"default_branch": "main"}),
  86. _make_mock_response(200, {"object": {"sha": "c0"}}),
  87. *_branch_with("settings.json"),
  88. ],
  89. put=[_make_mock_response(404, {"message": "Branch bambuddy-backup not found"}), _seeded()],
  90. post=[_make_mock_response(201, {"ref": "refs/heads/bambuddy-backup"})],
  91. )
  92. result = await _push(client, files=files, branch="bambuddy-backup")
  93. assert result["status"] == "success"
  94. first, second = client.put.await_args_list
  95. assert first.kwargs["json"]["branch"] == "bambuddy-backup"
  96. assert "branch" not in second.kwargs["json"]
  97. assert client.post.await_args.kwargs["json"] == {"ref": "refs/heads/bambuddy-backup", "sha": "c0"}
  98. @pytest.mark.asyncio
  99. async def test_an_empty_default_branch_behind_a_missing_backup_branch_is_seeded_too(self):
  100. client = _client(
  101. get=[
  102. _make_mock_response(404, {}),
  103. _make_mock_response(200, {"default_branch": "main"}),
  104. _empty_409(),
  105. ],
  106. put=[_seeded()],
  107. )
  108. result = await _push(client, files={"settings.json": FILES["settings.json"]}, branch="bambuddy-backup")
  109. assert result["status"] == "success"
  110. client.post.assert_not_awaited()
  111. class TestWhatStaysAFailure:
  112. @pytest.mark.asyncio
  113. async def test_another_409_is_reported_not_seeded(self):
  114. client = _client(get=[_make_mock_response(409, {"message": "Conflict"}, text="Conflict")])
  115. result = await _push(client)
  116. assert result["status"] == "failed"
  117. assert "Failed to get branch ref (HTTP 409)" in result["message"]
  118. client.put.assert_not_awaited()
  119. @pytest.mark.asyncio
  120. async def test_a_refused_first_commit_says_why(self):
  121. client = _client(
  122. get=[_empty_409()],
  123. put=[_make_mock_response(403, {}, text="Resource not accessible by personal access token")],
  124. )
  125. result = await _push(client)
  126. assert result["status"] == "failed"
  127. assert "HTTP 403" in result["message"]
  128. assert "personal access token" in result["message"]
  129. @pytest.mark.asyncio
  130. async def test_a_repository_still_empty_after_the_first_commit_is_not_seeded_twice(self):
  131. """GitHub lagging behind its own write: the next backup retries."""
  132. client = _client(get=[_empty_409(), _empty_409()], put=[_seeded()])
  133. result = await _push(client)
  134. assert result["status"] == "failed"
  135. assert "replication lag" in result["message"]
  136. assert client.put.await_count == 1