forked from coderamp-labs/gitingest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_git_utils.py
More file actions
306 lines (269 loc) · 10.3 KB
/
test_git_utils.py
File metadata and controls
306 lines (269 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
"""Tests for the ``git_utils`` module.
These tests validate the ``validate_github_token`` function, which ensures that
GitHub personal access tokens (PATs) are properly formatted.
"""
from __future__ import annotations
import base64
from typing import TYPE_CHECKING
import pytest
from gitingest.utils.exceptions import InvalidGitHubTokenError
from gitingest.utils.git_utils import create_git_auth_header, create_git_command, is_github_host, validate_github_token
if TYPE_CHECKING:
from pathlib import Path
from pytest_mock import MockerFixture
@pytest.mark.parametrize(
"token",
[
# Valid tokens: correct prefixes and at least 36 allowed characters afterwards
"github_pat_" + "a" * 22 + "_" + "b" * 59,
"ghp_" + "A" * 36,
"ghu_" + "B" * 36,
"ghs_" + "C" * 36,
"ghr_" + "D" * 36,
"gho_" + "E" * 36,
],
)
def test_validate_github_token_valid(token: str) -> None:
"""validate_github_token should accept properly-formatted tokens."""
# Should not raise any exception
validate_github_token(token)
@pytest.mark.parametrize(
"token",
[
"github_pat_short", # Too short after prefix
"ghp_" + "b" * 35, # one character short
"invalidprefix_" + "c" * 36, # Wrong prefix
"github_pat_" + "!" * 36, # Disallowed characters
"github_pat_" + "a" * 36, # Too short after 'github_pat_' prefix
"", # Empty string
],
)
def test_validate_github_token_invalid(token: str) -> None:
"""Test that ``validate_github_token`` raises ``InvalidGitHubTokenError`` on malformed tokens."""
with pytest.raises(InvalidGitHubTokenError):
validate_github_token(token)
@pytest.mark.parametrize(
("base_cmd", "local_path", "url", "token", "expected_suffix"),
[
(
["git", "clone"],
"/some/path",
"https://github.com/owner/repo.git",
None,
[], # No auth header expected when token is None
),
(
["git", "clone"],
"/some/path",
"https://github.com/owner/repo.git",
"ghp_" + "d" * 36,
[
"-c",
create_git_auth_header("ghp_" + "d" * 36),
], # Auth header expected for GitHub URL + token
),
(
["git", "clone"],
"/some/path",
"https://gitlab.com/owner/repo.git",
"ghp_" + "e" * 36,
[], # No auth header for non-GitHub URL even if token provided
),
],
)
def test_create_git_command(
base_cmd: list[str],
local_path: str,
url: str,
token: str | None,
expected_suffix: list[str],
) -> None:
"""Test that ``create_git_command`` builds the correct command list based on inputs."""
cmd = create_git_command(base_cmd, local_path, url, token)
# The command should start with base_cmd and the -C option
expected_prefix = [*base_cmd, "-C", local_path]
assert cmd[: len(expected_prefix)] == expected_prefix
# The suffix (anything after prefix) should match expected
assert cmd[len(expected_prefix) :] == expected_suffix
@pytest.mark.parametrize(
"token",
[
"ghp_abcdefghijklmnopqrstuvwxyz012345", # typical ghp_ token
"github_pat_1234567890abcdef1234567890abcdef1234",
],
)
def test_create_git_auth_header(token: str) -> None:
"""Test that ``create_git_auth_header`` produces correct base64-encoded header."""
header = create_git_auth_header(token)
expected_basic = base64.b64encode(f"x-oauth-basic:{token}".encode()).decode()
expected = f"http.https://github.com/.extraheader=Authorization: Basic {expected_basic}"
assert header == expected
@pytest.mark.parametrize(
("url", "token", "should_call"),
[
("https://github.com/foo/bar.git", "ghp_" + "f" * 36, True),
("https://github.com/foo/bar.git", None, False),
("https://gitlab.com/foo/bar.git", "ghp_" + "g" * 36, False),
],
)
def test_create_git_command_helper_calls(
mocker: MockerFixture,
tmp_path: Path,
*,
url: str,
token: str | None,
should_call: bool,
) -> None:
"""Test that ``create_git_auth_header`` is invoked only when appropriate."""
work_dir = tmp_path / "repo"
header_mock = mocker.patch("gitingest.utils.git_utils.create_git_auth_header", return_value="HEADER")
cmd = create_git_command(["git", "clone"], str(work_dir), url, token)
if should_call:
header_mock.assert_called_once_with(token, url=url)
assert "HEADER" in cmd
else:
header_mock.assert_not_called()
assert "HEADER" not in cmd
@pytest.mark.parametrize(
("url", "expected"),
[
# GitHub.com URLs
("https://github.com/owner/repo.git", True),
("http://github.com/owner/repo.git", True),
("https://github.com/owner/repo", True),
# GitHub Enterprise URLs
("https://github.company.com/owner/repo.git", True),
("https://github.enterprise.org/owner/repo.git", True),
("http://github.internal/owner/repo.git", True),
("https://github.example.co.uk/owner/repo.git", True),
# Non-GitHub URLs
("https://gitlab.com/owner/repo.git", False),
("https://bitbucket.org/owner/repo.git", False),
("https://git.example.com/owner/repo.git", False),
("https://mygithub.com/owner/repo.git", False), # doesn't start with "github."
("https://subgithub.com/owner/repo.git", False),
("https://example.com/github/repo.git", False),
# Edge cases
("", False),
("not-a-url", False),
("ftp://github.com/owner/repo.git", True), # Different protocol but still github.com
],
)
def test_is_github_host(url: str, *, expected: bool) -> None:
"""Test that ``is_github_host`` correctly identifies GitHub and GitHub Enterprise URLs."""
assert is_github_host(url) == expected
@pytest.mark.parametrize(
("token", "url", "expected_hostname"),
[
# GitHub.com URLs (default)
("ghp_" + "a" * 36, "https://github.com", "github.com"),
("ghp_" + "a" * 36, "https://github.com/owner/repo.git", "github.com"),
# GitHub Enterprise URLs
("ghp_" + "b" * 36, "https://github.company.com", "github.company.com"),
("ghp_" + "c" * 36, "https://github.enterprise.org/owner/repo.git", "github.enterprise.org"),
("ghp_" + "d" * 36, "http://github.internal", "github.internal"),
],
)
def test_create_git_auth_header_with_ghe_url(token: str, url: str, expected_hostname: str) -> None:
"""Test that ``create_git_auth_header`` handles GitHub Enterprise URLs correctly."""
header = create_git_auth_header(token, url=url)
expected_basic = base64.b64encode(f"x-oauth-basic:{token}".encode()).decode()
expected = f"http.https://{expected_hostname}/.extraheader=Authorization: Basic {expected_basic}"
assert header == expected
@pytest.mark.parametrize(
("base_cmd", "local_path", "url", "token", "expected_auth_hostname"),
[
# GitHub.com URLs - should use default hostname
(
["git", "clone"],
"/some/path",
"https://github.com/owner/repo.git",
"ghp_" + "a" * 36,
"github.com",
),
# GitHub Enterprise URLs - should use custom hostname
(
["git", "clone"],
"/some/path",
"https://github.company.com/owner/repo.git",
"ghp_" + "b" * 36,
"github.company.com",
),
(
["git", "clone"],
"/some/path",
"https://github.enterprise.org/owner/repo.git",
"ghp_" + "c" * 36,
"github.enterprise.org",
),
(
["git", "clone"],
"/some/path",
"http://github.internal/owner/repo.git",
"ghp_" + "d" * 36,
"github.internal",
),
],
)
def test_create_git_command_with_ghe_urls(
base_cmd: list[str],
local_path: str,
url: str,
token: str,
expected_auth_hostname: str,
) -> None:
"""Test that ``create_git_command`` handles GitHub Enterprise URLs correctly."""
cmd = create_git_command(base_cmd, local_path, url, token)
# Should have base command and -C option
expected_prefix = [*base_cmd, "-C", local_path]
assert cmd[: len(expected_prefix)] == expected_prefix
# Should have -c and auth header
assert "-c" in cmd
auth_header_index = cmd.index("-c") + 1
auth_header = cmd[auth_header_index]
# Verify the auth header contains the expected hostname
assert f"http.https://{expected_auth_hostname}/" in auth_header
assert "Authorization: Basic" in auth_header
@pytest.mark.parametrize(
("base_cmd", "local_path", "url", "token"),
[
# Should NOT add auth headers for non-GitHub URLs
(["git", "clone"], "/some/path", "https://gitlab.com/owner/repo.git", "ghp_" + "a" * 36),
(["git", "clone"], "/some/path", "https://bitbucket.org/owner/repo.git", "ghp_" + "b" * 36),
(["git", "clone"], "/some/path", "https://git.example.com/owner/repo.git", "ghp_" + "c" * 36),
],
)
def test_create_git_command_ignores_non_github_urls(
base_cmd: list[str],
local_path: str,
url: str,
token: str,
) -> None:
"""Test that ``create_git_command`` does not add auth headers for non-GitHub URLs."""
cmd = create_git_command(base_cmd, local_path, url, token)
# Should only have base command and -C option, no auth headers
expected = [*base_cmd, "-C", local_path]
assert cmd == expected
@pytest.mark.parametrize(
"url",
[
"",
"not-a-url",
"ftp://github.com/owner/repo.git",
"github.com/owner/repo.git",
"https://",
],
)
def test_is_github_host_edge_cases(url: str) -> None:
"""Test is_github_host with malformed or edge-case URLs."""
try:
result = is_github_host(url)
assert isinstance(result, bool)
except (ValueError, TypeError) as exc:
pytest.fail(f"is_github_host raised {exc.__class__.__name__} for url: {url}")
def test_token_not_in_command_plaintext() -> None:
"""Ensure the token is not present in the command as plain text."""
token = "ghp_" + "x" * 36
cmd = create_git_command(["git", "clone"], "/tmp", "https://github.com/owner/repo.git", token)
for part in cmd:
assert token not in part or "Basic" in part