test_credentials.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 public 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_urlmatch_credential_sections(self) -> None:
  48. config = ConfigDict()
  49. config.set((b"credential", "https://github.com"), b"helper", "foo")
  50. config.set((b"credential", "git.sr.ht"), b"helper", "foo")
  51. config.set(b"credential", b"helper", "bar")
  52. self.assertEqual(
  53. list(urlmatch_credential_sections(config, "https://github.com")),
  54. [
  55. (b"credential", b"https://github.com"),
  56. (b"credential",),
  57. ],
  58. )
  59. self.assertEqual(
  60. list(urlmatch_credential_sections(config, "https://git.sr.ht")),
  61. [
  62. (b"credential", b"git.sr.ht"),
  63. (b"credential",),
  64. ],
  65. )
  66. self.assertEqual(
  67. list(urlmatch_credential_sections(config, "missing_url")),
  68. [(b"credential",)],
  69. )