|
| 1 | +"""Tests for listing a user's repositories with a GitHub App installation token.""" |
| 2 | + |
| 3 | +from unittest.mock import patch |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from github_backup import github_backup |
| 8 | + |
| 9 | +NO_USER = {"login": None} |
| 10 | + |
| 11 | + |
| 12 | +def page(*names): |
| 13 | + return { |
| 14 | + "total_count": 3, |
| 15 | + "repositories": [{"name": name, "private": True} for name in names], |
| 16 | + } |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def mock_retrieve_data(): |
| 21 | + with patch("github_backup.github_backup.retrieve_data") as mock: |
| 22 | + yield mock |
| 23 | + |
| 24 | + |
| 25 | +def test_app_lists_user_repositories_from_installation(create_args, mock_retrieve_data): |
| 26 | + args = create_args(as_app=True, include_repository=True) |
| 27 | + mock_retrieve_data.return_value = [page("a", "b"), page("c")] |
| 28 | + |
| 29 | + repos = github_backup.retrieve_repositories(args, NO_USER) |
| 30 | + |
| 31 | + assert mock_retrieve_data.call_args.args[1] == ( |
| 32 | + "https://api.github.com/installation/repositories" |
| 33 | + ) |
| 34 | + assert [r["name"] for r in repos] == ["a", "b", "c"] |
| 35 | + |
| 36 | + |
| 37 | +def test_app_installation_listing_uses_ghes_host(create_args, mock_retrieve_data): |
| 38 | + args = create_args( |
| 39 | + as_app=True, include_repository=True, github_host="ghe.example.com" |
| 40 | + ) |
| 41 | + mock_retrieve_data.return_value = [page("a")] |
| 42 | + |
| 43 | + github_backup.retrieve_repositories(args, NO_USER) |
| 44 | + |
| 45 | + assert mock_retrieve_data.call_args.args[1] == ( |
| 46 | + "https://ghe.example.com/api/v3/installation/repositories" |
| 47 | + ) |
| 48 | + |
| 49 | + |
| 50 | +def test_app_organization_still_lists_org_repositories(create_args, mock_retrieve_data): |
| 51 | + args = create_args(as_app=True, include_repository=True, organization=True) |
| 52 | + mock_retrieve_data.return_value = [{"name": "a"}] |
| 53 | + |
| 54 | + repos = github_backup.retrieve_repositories(args, NO_USER) |
| 55 | + |
| 56 | + assert mock_retrieve_data.call_args.args[1] == ( |
| 57 | + "https://api.github.com/orgs/testuser/repos" |
| 58 | + ) |
| 59 | + assert repos == [{"name": "a"}] |
| 60 | + |
| 61 | + |
| 62 | +def test_app_single_repository_is_fetched_directly(create_args, mock_retrieve_data): |
| 63 | + args = create_args(as_app=True, repository="dotfiles") |
| 64 | + mock_retrieve_data.return_value = [{"name": "dotfiles"}] |
| 65 | + |
| 66 | + repos = github_backup.retrieve_repositories(args, NO_USER) |
| 67 | + |
| 68 | + assert mock_retrieve_data.call_args.args[1] == ( |
| 69 | + "https://api.github.com/repos/testuser/dotfiles" |
| 70 | + ) |
| 71 | + assert repos == [{"name": "dotfiles"}] |
0 commit comments