test_credentials.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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.tests import TestCase
  22. from ..config import ConfigDict
  23. from ..credentials import match_partial_url, match_urls, urlmatch_credential_sections
  24. class TestCredentialHelpersUtils(TestCase):
  25. def test_match_urls(self):
  26. url = urlparse("https://github.com/jelmer/dulwich/")
  27. url_1 = urlparse("https://github.com/jelmer/dulwich")
  28. url_2 = urlparse("https://github.com/jelmer")
  29. url_3 = urlparse("https://github.com")
  30. self.assertTrue(match_urls(url, url_1))
  31. self.assertTrue(match_urls(url, url_2))
  32. self.assertTrue(match_urls(url, url_3))
  33. non_matching = urlparse("https://git.sr.ht/")
  34. self.assertFalse(match_urls(url, non_matching))
  35. def test_match_partial_url(self):
  36. url = urlparse("https://github.com/jelmer/dulwich/")
  37. self.assertTrue(match_partial_url(url, "github.com"))
  38. self.assertFalse(match_partial_url(url, "github.com/jelmer/"))
  39. self.assertTrue(match_partial_url(url, "github.com/jelmer/dulwich"))
  40. self.assertFalse(match_partial_url(url, "github.com/jel"))
  41. self.assertFalse(match_partial_url(url, "github.com/jel/"))
  42. def test_urlmatch_credential_sections(self):
  43. config = ConfigDict()
  44. config.set((b"credential", "https://github.com"), b"helper", "foo")
  45. config.set((b"credential", "git.sr.ht"), b"helper", "foo")
  46. config.set(b"credential", b"helper", "bar")
  47. self.assertEqual(
  48. list(urlmatch_credential_sections(config, "https://github.com")), [
  49. (b"credential", b"https://github.com"),
  50. (b"credential",),
  51. ])
  52. self.assertEqual(
  53. list(urlmatch_credential_sections(config, "https://git.sr.ht")), [
  54. (b"credential", b"git.sr.ht"),
  55. (b"credential",),
  56. ])
  57. self.assertEqual(
  58. list(urlmatch_credential_sections(config, "missing_url")), [
  59. (b"credential",)])