|
| 1 | +"""Tests for case-insensitive username/organization filtering.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +from unittest.mock import Mock |
| 5 | + |
| 6 | +from github_backup import github_backup |
| 7 | + |
| 8 | + |
| 9 | +class TestCaseSensitivity: |
| 10 | + """Test suite for case-insensitive username matching in filter_repositories.""" |
| 11 | + |
| 12 | + def test_filter_repositories_case_insensitive_user(self): |
| 13 | + """Should filter repositories case-insensitively for usernames. |
| 14 | +
|
| 15 | + Reproduces issue #198 where typing 'iamrodos' fails to match |
| 16 | + repositories with owner.login='Iamrodos' (the canonical case from GitHub API). |
| 17 | + """ |
| 18 | + # Simulate user typing lowercase username |
| 19 | + args = Mock() |
| 20 | + args.user = "iamrodos" # lowercase (what user typed) |
| 21 | + args.repository = None |
| 22 | + args.name_regex = None |
| 23 | + args.languages = None |
| 24 | + args.exclude = None |
| 25 | + args.fork = False |
| 26 | + args.private = False |
| 27 | + args.public = False |
| 28 | + args.all = True |
| 29 | + |
| 30 | + # Simulate GitHub API returning canonical case |
| 31 | + repos = [ |
| 32 | + { |
| 33 | + "name": "repo1", |
| 34 | + "owner": {"login": "Iamrodos"}, # Capital I (canonical from API) |
| 35 | + "private": False, |
| 36 | + "fork": False, |
| 37 | + }, |
| 38 | + { |
| 39 | + "name": "repo2", |
| 40 | + "owner": {"login": "Iamrodos"}, |
| 41 | + "private": False, |
| 42 | + "fork": False, |
| 43 | + }, |
| 44 | + ] |
| 45 | + |
| 46 | + filtered = github_backup.filter_repositories(args, repos) |
| 47 | + |
| 48 | + # Should match despite case difference |
| 49 | + assert len(filtered) == 2 |
| 50 | + assert filtered[0]["name"] == "repo1" |
| 51 | + assert filtered[1]["name"] == "repo2" |
| 52 | + |
| 53 | + def test_filter_repositories_case_insensitive_org(self): |
| 54 | + """Should filter repositories case-insensitively for organizations. |
| 55 | +
|
| 56 | + Tests the example from issue #198 where 'prai-org' doesn't match 'PRAI-Org'. |
| 57 | + """ |
| 58 | + args = Mock() |
| 59 | + args.user = "prai-org" # lowercase (what user typed) |
| 60 | + args.repository = None |
| 61 | + args.name_regex = None |
| 62 | + args.languages = None |
| 63 | + args.exclude = None |
| 64 | + args.fork = False |
| 65 | + args.private = False |
| 66 | + args.public = False |
| 67 | + args.all = True |
| 68 | + |
| 69 | + repos = [ |
| 70 | + { |
| 71 | + "name": "repo1", |
| 72 | + "owner": {"login": "PRAI-Org"}, # Different case (canonical from API) |
| 73 | + "private": False, |
| 74 | + "fork": False, |
| 75 | + }, |
| 76 | + ] |
| 77 | + |
| 78 | + filtered = github_backup.filter_repositories(args, repos) |
| 79 | + |
| 80 | + # Should match despite case difference |
| 81 | + assert len(filtered) == 1 |
| 82 | + assert filtered[0]["name"] == "repo1" |
| 83 | + |
| 84 | + def test_filter_repositories_case_variations(self): |
| 85 | + """Should handle various case combinations correctly.""" |
| 86 | + args = Mock() |
| 87 | + args.user = "TeSt-UsEr" # Mixed case |
| 88 | + args.repository = None |
| 89 | + args.name_regex = None |
| 90 | + args.languages = None |
| 91 | + args.exclude = None |
| 92 | + args.fork = False |
| 93 | + args.private = False |
| 94 | + args.public = False |
| 95 | + args.all = True |
| 96 | + |
| 97 | + repos = [ |
| 98 | + {"name": "repo1", "owner": {"login": "test-user"}, "private": False, "fork": False}, |
| 99 | + {"name": "repo2", "owner": {"login": "TEST-USER"}, "private": False, "fork": False}, |
| 100 | + {"name": "repo3", "owner": {"login": "TeSt-UsEr"}, "private": False, "fork": False}, |
| 101 | + {"name": "repo4", "owner": {"login": "other-user"}, "private": False, "fork": False}, |
| 102 | + ] |
| 103 | + |
| 104 | + filtered = github_backup.filter_repositories(args, repos) |
| 105 | + |
| 106 | + # Should match first 3 (all case variations of same user) |
| 107 | + assert len(filtered) == 3 |
| 108 | + assert set(r["name"] for r in filtered) == {"repo1", "repo2", "repo3"} |
| 109 | + |
| 110 | + |
| 111 | +if __name__ == "__main__": |
| 112 | + pytest.main([__file__, "-v"]) |
0 commit comments