11"""Tests for the ``git_utils`` module.
22
3- These tests validate the ``validate_github_token`` function, which ensures that
4- GitHub personal access tokens (PATs) are properly formatted.
3+ These tests validate various git utility functions for repository operations.
54"""
65
76from __future__ import annotations
87
98import base64
109from typing import TYPE_CHECKING
10+ from urllib .parse import urlparse
1111
1212import pytest
1313
14- from gitingest .utils .exceptions import InvalidGitHubTokenError
15- from gitingest .utils .git_utils import create_git_auth_header , create_git_command , is_github_host , validate_github_token
14+ from gitingest .utils .git_utils import create_git_auth_header , create_git_command , is_github_host
1615
1716if TYPE_CHECKING :
1817 from pathlib import Path
1918
2019 from pytest_mock import MockerFixture
2120
2221
23- @pytest .mark .parametrize (
24- "token" ,
25- [
26- # Valid tokens: correct prefixes and at least 36 allowed characters afterwards
27- "github_pat_" + "a" * 22 + "_" + "b" * 59 ,
28- "ghp_" + "A" * 36 ,
29- "ghu_" + "B" * 36 ,
30- "ghs_" + "C" * 36 ,
31- "ghr_" + "D" * 36 ,
32- "gho_" + "E" * 36 ,
33- ],
34- )
35- def test_validate_github_token_valid (token : str ) -> None :
36- """validate_github_token should accept properly-formatted tokens."""
37- # Should not raise any exception
38- validate_github_token (token )
39-
40-
41- @pytest .mark .parametrize (
42- "token" ,
43- [
44- "github_pat_short" , # Too short after prefix
45- "ghp_" + "b" * 35 , # one character short
46- "invalidprefix_" + "c" * 36 , # Wrong prefix
47- "github_pat_" + "!" * 36 , # Disallowed characters
48- "github_pat_" + "a" * 36 , # Too short after 'github_pat_' prefix
49- "" , # Empty string
50- ],
51- )
52- def test_validate_github_token_invalid (token : str ) -> None :
53- """Test that ``validate_github_token`` raises ``InvalidGitHubTokenError`` on malformed tokens."""
54- with pytest .raises (InvalidGitHubTokenError ):
55- validate_github_token (token )
5622
5723
5824@pytest .mark .parametrize (
@@ -72,15 +38,18 @@ def test_validate_github_token_invalid(token: str) -> None:
7238 "ghp_" + "d" * 36 ,
7339 [
7440 "-c" ,
75- create_git_auth_header ("ghp_" + "d" * 36 ),
76- ], # Auth header expected for GitHub URL + token
41+ create_git_auth_header ("ghp_" + "d" * 36 , "https://github.com/owner/repo.git" ),
42+ ], # Auth header expected when token is provided
7743 ),
7844 (
7945 ["git" , "clone" ],
8046 "/some/path" ,
8147 "https://gitlab.com/owner/repo.git" ,
8248 "ghp_" + "e" * 36 ,
83- [], # No auth header for non-GitHub URL even if token provided
49+ [
50+ "-c" ,
51+ create_git_auth_header ("ghp_" + "e" * 36 , "https://gitlab.com/owner/repo.git" ),
52+ ], # Auth header expected for any URL when token is provided
8453 ),
8554 ],
8655)
@@ -103,17 +72,19 @@ def test_create_git_command(
10372
10473
10574@pytest .mark .parametrize (
106- "token" ,
75+ ( "token" , "url" ) ,
10776 [
108- "ghp_abcdefghijklmnopqrstuvwxyz012345" , # typical ghp_ token
109- "github_pat_1234567890abcdef1234567890abcdef1234" ,
77+ ("ghp_abcdefghijklmnopqrstuvwxyz012345" , "https://github.com/owner/repo.git" ), # typical ghp_ token
78+ ("github_pat_1234567890abcdef1234567890abcdef1234" , "https://github.com/owner/repo.git" ),
79+ ("some_token" , "https://gitlab.com/owner/repo.git" ), # non-GitHub URL
11080 ],
11181)
112- def test_create_git_auth_header (token : str ) -> None :
82+ def test_create_git_auth_header (token : str , url : str ) -> None :
11383 """Test that ``create_git_auth_header`` produces correct base64-encoded header."""
114- header = create_git_auth_header (token )
84+ header = create_git_auth_header (token , url )
11585 expected_basic = base64 .b64encode (f"x-oauth-basic:{ token } " .encode ()).decode ()
116- expected = f"http.https://github.com/.extraheader=Authorization: Basic { expected_basic } "
86+ hostname = urlparse (url ).hostname
87+ expected = f"http.https://{ hostname } /.extraheader=Authorization: Basic { expected_basic } "
11788 assert header == expected
11889
11990
@@ -122,7 +93,7 @@ def test_create_git_auth_header(token: str) -> None:
12293 [
12394 ("https://github.com/foo/bar.git" , "ghp_" + "f" * 36 , True ),
12495 ("https://github.com/foo/bar.git" , None , False ),
125- ("https://gitlab.com/foo/bar.git" , "ghp_" + "g" * 36 , False ),
96+ ("https://gitlab.com/foo/bar.git" , "ghp_" + "g" * 36 , True ), # Now called for all URLs with token
12697 ],
12798)
12899def test_create_git_command_helper_calls (
0 commit comments