|
| 1 | +from cycode.cli.utils.url_utils import sanitize_repository_url |
| 2 | + |
| 3 | + |
| 4 | +def test_sanitize_repository_url_with_token() -> None: |
| 5 | + """Test that PAT tokens are removed from HTTPS URLs.""" |
| 6 | + url = 'https://token@github.com/user/repo.git' |
| 7 | + expected = 'https://github.com/user/repo.git' |
| 8 | + assert sanitize_repository_url(url) == expected |
| 9 | + |
| 10 | + |
| 11 | +def test_sanitize_repository_url_with_username_and_token() -> None: |
| 12 | + """Test that username and token are removed from HTTPS URLs.""" |
| 13 | + url = 'https://user:token@github.com/user/repo.git' |
| 14 | + expected = 'https://github.com/user/repo.git' |
| 15 | + assert sanitize_repository_url(url) == expected |
| 16 | + |
| 17 | + |
| 18 | +def test_sanitize_repository_url_with_port() -> None: |
| 19 | + """Test that URLs with ports are handled correctly.""" |
| 20 | + url = 'https://token@github.com:443/user/repo.git' |
| 21 | + expected = 'https://github.com:443/user/repo.git' |
| 22 | + assert sanitize_repository_url(url) == expected |
| 23 | + |
| 24 | + |
| 25 | +def test_sanitize_repository_url_ssh_format() -> None: |
| 26 | + """Test that SSH URLs are returned as-is (no credentials in URL format).""" |
| 27 | + url = 'git@github.com:user/repo.git' |
| 28 | + assert sanitize_repository_url(url) == url |
| 29 | + |
| 30 | + |
| 31 | +def test_sanitize_repository_url_ssh_protocol() -> None: |
| 32 | + """Test that ssh:// URLs are returned as-is.""" |
| 33 | + url = 'ssh://git@github.com/user/repo.git' |
| 34 | + assert sanitize_repository_url(url) == url |
| 35 | + |
| 36 | + |
| 37 | +def test_sanitize_repository_url_no_credentials() -> None: |
| 38 | + """Test that URLs without credentials are returned unchanged.""" |
| 39 | + url = 'https://github.com/user/repo.git' |
| 40 | + assert sanitize_repository_url(url) == url |
| 41 | + |
| 42 | + |
| 43 | +def test_sanitize_repository_url_none() -> None: |
| 44 | + """Test that None input returns None.""" |
| 45 | + assert sanitize_repository_url(None) is None |
| 46 | + |
| 47 | + |
| 48 | +def test_sanitize_repository_url_empty_string() -> None: |
| 49 | + """Test that empty string is returned as-is.""" |
| 50 | + assert sanitize_repository_url('') == '' |
| 51 | + |
| 52 | + |
| 53 | +def test_sanitize_repository_url_gitlab() -> None: |
| 54 | + """Test that GitLab URLs are sanitized correctly.""" |
| 55 | + url = 'https://oauth2:token@gitlab.com/user/repo.git' |
| 56 | + expected = 'https://gitlab.com/user/repo.git' |
| 57 | + assert sanitize_repository_url(url) == expected |
| 58 | + |
| 59 | + |
| 60 | +def test_sanitize_repository_url_bitbucket() -> None: |
| 61 | + """Test that Bitbucket URLs are sanitized correctly.""" |
| 62 | + url = 'https://x-token-auth:token@bitbucket.org/user/repo.git' |
| 63 | + expected = 'https://bitbucket.org/user/repo.git' |
| 64 | + assert sanitize_repository_url(url) == expected |
| 65 | + |
| 66 | + |
| 67 | +def test_sanitize_repository_url_with_path_and_query() -> None: |
| 68 | + """Test that URLs with paths, query params, and fragments are preserved.""" |
| 69 | + url = 'https://token@github.com/user/repo.git?ref=main#section' |
| 70 | + expected = 'https://github.com/user/repo.git?ref=main#section' |
| 71 | + assert sanitize_repository_url(url) == expected |
| 72 | + |
| 73 | + |
| 74 | +def test_sanitize_repository_url_invalid_url() -> None: |
| 75 | + """Test that invalid URLs are returned as-is (graceful degradation).""" |
| 76 | + # This should not raise an exception, but return the original |
| 77 | + url = 'not-a-valid-url' |
| 78 | + result = sanitize_repository_url(url) |
| 79 | + # Should return original since parsing fails |
| 80 | + assert result == url |
0 commit comments