|
| 1 | +from unittest.mock import MagicMock, patch |
| 2 | + |
| 3 | +import pytest |
| 4 | +import responses |
| 5 | +from responses import matchers |
| 6 | + |
| 7 | +from gitea_github_sync.config import Config |
| 8 | +from gitea_github_sync.gitea import Gitea, get_gitea |
| 9 | +from gitea_github_sync.repository import Repository, Visibility |
| 10 | + |
| 11 | +GITEA_BASE_API_URL = "https://gitea.yourinstance.com/api/v1" |
| 12 | +GITEA_TOKEN = "your-gitea-token" |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture |
| 16 | +def conf_fixture() -> Config: |
| 17 | + return Config( |
| 18 | + github_token="some-token", gitea_api_url=GITEA_BASE_API_URL, gitea_token=GITEA_TOKEN |
| 19 | + ) |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture |
| 23 | +def gitea_fixture(conf_fixture: Config) -> Gitea: |
| 24 | + return Gitea(api_url=conf_fixture.gitea_api_url, api_token=conf_fixture.gitea_token) |
| 25 | + |
| 26 | + |
| 27 | +@responses.activate |
| 28 | +def test_get_repos(gitea_fixture: Gitea) -> None: |
| 29 | + |
| 30 | + json = [ |
| 31 | + {"full_name": "some-team/a-repo", "private": True}, |
| 32 | + {"full_name": "some-team/b-repo", "private": False}, |
| 33 | + ] |
| 34 | + |
| 35 | + expected_repos = [ |
| 36 | + Repository(full_repo_name="some-team/a-repo", visibility=Visibility.PRIVATE), |
| 37 | + Repository(full_repo_name="some-team/b-repo", visibility=Visibility.PUBLIC), |
| 38 | + ] |
| 39 | + responses.get( |
| 40 | + f"{GITEA_BASE_API_URL}/user/repos", |
| 41 | + match=[matchers.header_matcher({"Authorization": f"token {GITEA_TOKEN}"})], |
| 42 | + json=json, |
| 43 | + ) |
| 44 | + |
| 45 | + result = gitea_fixture.get_repos() |
| 46 | + assert expected_repos == result |
| 47 | + |
| 48 | + |
| 49 | +@responses.activate |
| 50 | +def test_get_repos_multiple_pages(gitea_fixture: Gitea) -> None: |
| 51 | + |
| 52 | + json_1 = [ |
| 53 | + {"full_name": "some-team/a-repo", "private": True}, |
| 54 | + {"full_name": "some-team/b-repo", "private": False}, |
| 55 | + ] |
| 56 | + json_2 = [ |
| 57 | + {"full_name": "some-team/c-repo", "private": True}, |
| 58 | + {"full_name": "some-team/d-repo", "private": False}, |
| 59 | + ] |
| 60 | + json_3 = [ |
| 61 | + {"full_name": "some-team/e-repo", "private": True}, |
| 62 | + {"full_name": "some-team/f-repo", "private": False}, |
| 63 | + ] |
| 64 | + |
| 65 | + expected_repos = [ |
| 66 | + Repository(full_repo_name="some-team/a-repo", visibility=Visibility.PRIVATE), |
| 67 | + Repository(full_repo_name="some-team/b-repo", visibility=Visibility.PUBLIC), |
| 68 | + Repository(full_repo_name="some-team/c-repo", visibility=Visibility.PRIVATE), |
| 69 | + Repository(full_repo_name="some-team/d-repo", visibility=Visibility.PUBLIC), |
| 70 | + Repository(full_repo_name="some-team/e-repo", visibility=Visibility.PRIVATE), |
| 71 | + Repository(full_repo_name="some-team/f-repo", visibility=Visibility.PUBLIC), |
| 72 | + ] |
| 73 | + responses.get( |
| 74 | + f"{GITEA_BASE_API_URL}/user/repos", |
| 75 | + match=[matchers.header_matcher({"Authorization": f"token {GITEA_TOKEN}"})], |
| 76 | + json=json_1, |
| 77 | + headers={ |
| 78 | + "link": ( |
| 79 | + f'<{GITEA_BASE_API_URL}/user/repos?page=2>; rel="next",' |
| 80 | + + f'<{GITEA_BASE_API_URL}/user/repos?page=3>; rel="last"' |
| 81 | + ) |
| 82 | + }, |
| 83 | + ) |
| 84 | + responses.get( |
| 85 | + f"{GITEA_BASE_API_URL}/user/repos?page=2", |
| 86 | + match=[matchers.header_matcher({"Authorization": f"token {GITEA_TOKEN}"})], |
| 87 | + json=json_2, |
| 88 | + headers={ |
| 89 | + "link": ( |
| 90 | + f'<{GITEA_BASE_API_URL}/user/repos?page=3>; rel="next",' |
| 91 | + + f'<{GITEA_BASE_API_URL}/user/repos?page=3>; rel="last",' |
| 92 | + + f'<{GITEA_BASE_API_URL}/user/repos?page=1>; rel="first",' |
| 93 | + + f'<{GITEA_BASE_API_URL}/user/repos?page=1>; rel="prev"' |
| 94 | + ) |
| 95 | + }, |
| 96 | + ) |
| 97 | + responses.get( |
| 98 | + f"{GITEA_BASE_API_URL}/user/repos?page=3", |
| 99 | + match=[matchers.header_matcher({"Authorization": f"token {GITEA_TOKEN}"})], |
| 100 | + json=json_3, |
| 101 | + headers={ |
| 102 | + "link": ( |
| 103 | + f'<{GITEA_BASE_API_URL}/user/repos?page=1>; rel="first",' |
| 104 | + + f'<{GITEA_BASE_API_URL}/user/repos?page=1>; rel="prev"' |
| 105 | + ) |
| 106 | + }, |
| 107 | + ) |
| 108 | + |
| 109 | + result = gitea_fixture.get_repos() |
| 110 | + assert expected_repos == result |
| 111 | + |
| 112 | + |
| 113 | +def test_gitea(gitea_fixture: Gitea, conf_fixture: Config) -> None: |
| 114 | + gt = get_gitea(conf_fixture) |
| 115 | + |
| 116 | + assert gt == gitea_fixture |
| 117 | + |
| 118 | + |
| 119 | +@patch("gitea_github_sync.gitea.config.load_config", autospec=True) |
| 120 | +def test_gitea_default_value( |
| 121 | + mock_load_config: MagicMock, gitea_fixture: Gitea, conf_fixture: Config |
| 122 | +) -> None: |
| 123 | + mock_load_config.return_value = conf_fixture |
| 124 | + gt = get_gitea() |
| 125 | + |
| 126 | + assert gt == gitea_fixture |
| 127 | + mock_load_config.assert_called_once() |
0 commit comments