test_credentials.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # test_credentials.py -- tests for credentials.py
  2. # Copyright (C) 2022 Daniele Trifirò <daniele@iterative.ai>
  3. #
  4. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  5. # General Public License as public by the Free Software Foundation; version 2.0
  6. # or (at your option) any later version. You can redistribute it and/or
  7. # modify it under the terms of either of these two licenses.
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. # You should have received a copy of the licenses; if not, see
  16. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  17. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  18. # License, Version 2.0.
  19. #
  20. from urllib.parse import urlparse
  21. from dulwich.config import ConfigDict
  22. from dulwich.credentials import (
  23. match_partial_url,
  24. match_urls,
  25. urlmatch_credential_sections,
  26. )
  27. from . import TestCase
  28. class TestCredentialHelpersUtils(TestCase):
  29. def test_match_urls(self):
  30. url = urlparse("https://github.com/jelmer/dulwich/")
  31. url_1 = urlparse("https://github.com/jelmer/dulwich")
  32. url_2 = urlparse("https://github.com/jelmer")
  33. url_3 = urlparse("https://github.com")
  34. self.assertTrue(match_urls(url, url_1))
  35. self.assertTrue(match_urls(url, url_2))
  36. self.assertTrue(match_urls(url, url_3))
  37. non_matching = urlparse("https://git.sr.ht/")
  38. self.assertFalse(match_urls(url, non_matching))
  39. def test_match_partial_url(self):
  40. url = urlparse("https://github.com/jelmer/dulwich/")
  41. self.assertTrue(match_partial_url(url, "github.com"))
  42. self.assertFalse(match_partial_url(url, "github.com/jelmer/"))
  43. self.assertTrue(match_partial_url(url, "github.com/jelmer/dulwich"))
  44. self.assertFalse(match_partial_url(url, "github.com/jel"))
  45. self.assertFalse(match_partial_url(url, "github.com/jel/"))
  46. def test_urlmatch_credential_sections(self):
  47. config = ConfigDict()
  48. config.set((b"credential", "https://github.com"), b"helper", "foo")
  49. config.set((b"credential", "git.sr.ht"), b"helper", "foo")
  50. config.set(b"credential", b"helper", "bar")
  51. self.assertEqual(
  52. list(urlmatch_credential_sections(config, "https://github.com")),
  53. [
  54. (b"credential", b"https://github.com"),
  55. (b"credential",),
  56. ],
  57. )
  58. self.assertEqual(
  59. list(urlmatch_credential_sections(config, "https://git.sr.ht")),
  60. [
  61. (b"credential", b"git.sr.ht"),
  62. (b"credential",),
  63. ],
  64. )
  65. self.assertEqual(
  66. list(urlmatch_credential_sections(config, "missing_url")),
  67. [(b"credential",)],
  68. )