test_credentials.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # test_credentials.py -- tests for credentials.py
  2. # Copyright (C) 2022 Daniele Trifirò <daniele@iterative.ai>
  3. #
  4. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  5. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  6. # General Public License as published by the Free Software Foundation; version 2.0
  7. # or (at your option) any later version. You can redistribute it and/or
  8. # modify it under the terms of either of these two licenses.
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # You should have received a copy of the licenses; if not, see
  17. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  18. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  19. # License, Version 2.0.
  20. #
  21. from urllib.parse import urlparse
  22. from dulwich.config import ConfigDict
  23. from dulwich.credentials import (
  24. match_partial_url,
  25. match_urls,
  26. urlmatch_credential_sections,
  27. )
  28. from . import TestCase
  29. class TestCredentialHelpersUtils(TestCase):
  30. def test_match_urls(self) -> None:
  31. url = urlparse("https://github.com/jelmer/dulwich/")
  32. url_1 = urlparse("https://github.com/jelmer/dulwich")
  33. url_2 = urlparse("https://github.com/jelmer")
  34. url_3 = urlparse("https://github.com")
  35. self.assertTrue(match_urls(url, url_1))
  36. self.assertTrue(match_urls(url, url_2))
  37. self.assertTrue(match_urls(url, url_3))
  38. non_matching = urlparse("https://git.sr.ht/")
  39. self.assertFalse(match_urls(url, non_matching))
  40. def test_match_partial_url(self) -> None:
  41. url = urlparse("https://github.com/jelmer/dulwich/")
  42. self.assertTrue(match_partial_url(url, "github.com"))
  43. self.assertFalse(match_partial_url(url, "github.com/jelmer/"))
  44. self.assertTrue(match_partial_url(url, "github.com/jelmer/dulwich"))
  45. self.assertFalse(match_partial_url(url, "github.com/jel"))
  46. self.assertFalse(match_partial_url(url, "github.com/jel/"))
  47. def test_match_partial_url_with_scheme(self) -> None:
  48. """Test match_partial_url with a URL that includes a scheme."""
  49. url = urlparse("https://github.com/jelmer/dulwich/")
  50. # Match with same scheme
  51. self.assertTrue(match_partial_url(url, "https://github.com"))
  52. self.assertTrue(match_partial_url(url, "https://github.com/jelmer/dulwich"))
  53. # No match with different scheme
  54. self.assertFalse(match_partial_url(url, "http://github.com"))
  55. self.assertFalse(match_partial_url(url, "ssh://github.com"))
  56. def test_urlmatch_credential_sections(self) -> None:
  57. config = ConfigDict()
  58. config.set((b"credential", "https://github.com"), b"helper", "foo")
  59. config.set((b"credential", "git.sr.ht"), b"helper", "foo")
  60. config.set(b"credential", b"helper", "bar")
  61. self.assertEqual(
  62. list(urlmatch_credential_sections(config, "https://github.com")),
  63. [
  64. (b"credential", b"https://github.com"),
  65. (b"credential",),
  66. ],
  67. )
  68. self.assertEqual(
  69. list(urlmatch_credential_sections(config, "https://git.sr.ht")),
  70. [
  71. (b"credential", b"git.sr.ht"),
  72. (b"credential",),
  73. ],
  74. )
  75. self.assertEqual(
  76. list(urlmatch_credential_sections(config, "missing_url")),
  77. [(b"credential",)],
  78. )
  79. def test_urlmatch_credential_sections_with_other_sections(self) -> None:
  80. """Test that non-credential sections are skipped."""
  81. config = ConfigDict()
  82. config.set((b"credential", "https://github.com"), b"helper", "foo")
  83. config.set(b"credential", b"helper", "bar")
  84. # Add some non-credential sections
  85. config.set(b"user", b"name", "Test User")
  86. config.set(b"core", b"editor", "vim")
  87. # Should only return credential sections
  88. result = list(urlmatch_credential_sections(config, "https://github.com"))
  89. self.assertEqual(
  90. result,
  91. [
  92. (b"credential", b"https://github.com"),
  93. (b"credential",),
  94. ],
  95. )