|
| 1 | +"""Tests for hooks backup permissions (GitHub issue #532). |
| 2 | +
|
| 3 | +Reading ``/repos/{owner}/{repo}/hooks`` needs admin access to the repository. |
| 4 | +Without it GitHub responds with HTTP 404 rather than 403, which used to log an |
| 5 | +API error for every repository the user doesn't administer. |
| 6 | +""" |
| 7 | + |
| 8 | +import logging |
| 9 | +from urllib.error import HTTPError |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | +from github_backup import github_backup |
| 14 | + |
| 15 | +REPOS_TEMPLATE = "https://api.github.com/repos" |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def hook_args(create_args): |
| 20 | + return create_args(include_hooks=True, token_classic="ghp_token") |
| 21 | + |
| 22 | + |
| 23 | +def _repository(**fields): |
| 24 | + return {"full_name": "owner/repo", **fields} |
| 25 | + |
| 26 | + |
| 27 | +def test_hooks_skipped_without_request_when_not_admin( |
| 28 | + hook_args, tmp_path, monkeypatch, caplog |
| 29 | +): |
| 30 | + def fail_retrieve_data(*args, **kwargs): |
| 31 | + raise AssertionError("hooks should not be requested without admin access") |
| 32 | + |
| 33 | + monkeypatch.setattr(github_backup, "retrieve_data", fail_retrieve_data) |
| 34 | + |
| 35 | + with caplog.at_level(logging.INFO, logger=github_backup.logger.name): |
| 36 | + github_backup.backup_hooks( |
| 37 | + hook_args, |
| 38 | + tmp_path, |
| 39 | + _repository(permissions={"admin": False, "push": True, "pull": True}), |
| 40 | + REPOS_TEMPLATE, |
| 41 | + ) |
| 42 | + |
| 43 | + assert "Skipping hooks for owner/repo since admin access is required" in caplog.text |
| 44 | + assert not (tmp_path / "hooks").exists() |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.parametrize( |
| 48 | + "repository", |
| 49 | + [ |
| 50 | + _repository(permissions={"admin": True, "push": True, "pull": True}), |
| 51 | + _repository(), |
| 52 | + ], |
| 53 | + ids=["admin", "no-permissions-field"], |
| 54 | +) |
| 55 | +def test_hooks_requested_when_admin_or_permissions_unknown( |
| 56 | + hook_args, tmp_path, monkeypatch, repository |
| 57 | +): |
| 58 | + requested = [] |
| 59 | + |
| 60 | + def fake_retrieve_data(passed_args, template, *args, **kwargs): |
| 61 | + requested.append(template) |
| 62 | + return [{"id": 1, "name": "web"}] |
| 63 | + |
| 64 | + monkeypatch.setattr(github_backup, "retrieve_data", fake_retrieve_data) |
| 65 | + |
| 66 | + github_backup.backup_hooks(hook_args, tmp_path, repository, REPOS_TEMPLATE) |
| 67 | + |
| 68 | + assert requested == ["https://api.github.com/repos/owner/repo/hooks"] |
| 69 | + assert (tmp_path / "hooks" / "hooks.json").exists() |
| 70 | + |
| 71 | + |
| 72 | +def test_hooks_404_is_skipped_with_reason(hook_args, tmp_path, monkeypatch, caplog): |
| 73 | + def fake_retrieve_data(passed_args, template, *args, **kwargs): |
| 74 | + raise HTTPError(template, 404, "Not Found", hdrs=None, fp=None) |
| 75 | + |
| 76 | + monkeypatch.setattr(github_backup, "retrieve_data", fake_retrieve_data) |
| 77 | + |
| 78 | + with caplog.at_level(logging.INFO, logger=github_backup.logger.name): |
| 79 | + github_backup.backup_hooks( |
| 80 | + hook_args, |
| 81 | + tmp_path, |
| 82 | + _repository(permissions={"admin": True}), |
| 83 | + REPOS_TEMPLATE, |
| 84 | + ) |
| 85 | + |
| 86 | + assert "Unable to read hooks for owner/repo, skipping" in caplog.text |
| 87 | + |
| 88 | + |
| 89 | +def test_hooks_non_404_error_propagates(hook_args, tmp_path, monkeypatch): |
| 90 | + def fake_retrieve_data(passed_args, template, *args, **kwargs): |
| 91 | + raise HTTPError(template, 500, "Server Error", hdrs=None, fp=None) |
| 92 | + |
| 93 | + monkeypatch.setattr(github_backup, "retrieve_data", fake_retrieve_data) |
| 94 | + |
| 95 | + with pytest.raises(HTTPError): |
| 96 | + github_backup.backup_hooks(hook_args, tmp_path, _repository(), REPOS_TEMPLATE) |
0 commit comments