test_data_backends.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. from unittest import skipIf
  2. from unittest.mock import patch
  3. from django.test import TestCase
  4. from core.data_backends import url_has_embedded_credentials
  5. try:
  6. import dulwich # noqa: F401
  7. DULWICH_AVAILABLE = True
  8. except ImportError:
  9. DULWICH_AVAILABLE = False
  10. class URLEmbeddedCredentialsTestCase(TestCase):
  11. def test_url_with_embedded_username(self):
  12. self.assertTrue(url_has_embedded_credentials('https://myuser@bitbucket.org/workspace/repo.git'))
  13. def test_url_without_embedded_username(self):
  14. self.assertFalse(url_has_embedded_credentials('https://bitbucket.org/workspace/repo.git'))
  15. def test_url_with_username_and_password(self):
  16. self.assertTrue(url_has_embedded_credentials('https://user:pass@bitbucket.org/workspace/repo.git'))
  17. def test_various_providers_with_embedded_username(self):
  18. urls = [
  19. 'https://user@bitbucket.org/workspace/repo.git',
  20. 'https://user@github.com/owner/repo.git',
  21. 'https://deploy-key@gitlab.com/group/project.git',
  22. 'http://user@internal-git.example.com/repo.git',
  23. ]
  24. for url in urls:
  25. with self.subTest(url=url):
  26. self.assertTrue(url_has_embedded_credentials(url))
  27. def test_various_providers_without_embedded_username(self):
  28. """Various Git providers without embedded usernames."""
  29. urls = [
  30. 'https://bitbucket.org/workspace/repo.git',
  31. 'https://github.com/owner/repo.git',
  32. 'https://gitlab.com/group/project.git',
  33. 'http://internal-git.example.com/repo.git',
  34. ]
  35. for url in urls:
  36. with self.subTest(url=url):
  37. self.assertFalse(url_has_embedded_credentials(url))
  38. def test_ssh_url(self):
  39. # git@host:path format doesn't parse as having a username in the traditional sense
  40. self.assertFalse(url_has_embedded_credentials('git@github.com:owner/repo.git'))
  41. def test_file_url(self):
  42. self.assertFalse(url_has_embedded_credentials('file:///path/to/repo'))
  43. @skipIf(not DULWICH_AVAILABLE, "dulwich is not installed")
  44. class GitBackendCredentialIntegrationTestCase(TestCase):
  45. """
  46. Integration tests that verify GitBackend correctly applies credential logic.
  47. These tests require dulwich to be installed and verify the full integration
  48. of the credential handling in GitBackend.fetch().
  49. """
  50. def _get_clone_kwargs(self, url, **params):
  51. from core.data_backends import GitBackend
  52. backend = GitBackend(url=url, **params)
  53. with patch('dulwich.porcelain.clone') as mock_clone, \
  54. patch('dulwich.porcelain.NoneStream'):
  55. try:
  56. with backend.fetch():
  57. pass
  58. except Exception:
  59. pass
  60. if mock_clone.called:
  61. return mock_clone.call_args.kwargs
  62. return {}
  63. def test_url_with_embedded_username_skips_explicit_credentials(self):
  64. kwargs = self._get_clone_kwargs(
  65. url='https://myuser@bitbucket.org/workspace/repo.git',
  66. username='myuser',
  67. password='my-api-key'
  68. )
  69. self.assertEqual(kwargs.get('username'), None)
  70. self.assertEqual(kwargs.get('password'), None)
  71. def test_url_without_embedded_username_passes_explicit_credentials(self):
  72. kwargs = self._get_clone_kwargs(
  73. url='https://bitbucket.org/workspace/repo.git',
  74. username='myuser',
  75. password='my-api-key'
  76. )
  77. self.assertEqual(kwargs.get('username'), 'myuser')
  78. self.assertEqual(kwargs.get('password'), 'my-api-key')
  79. def test_url_with_embedded_username_no_explicit_credentials(self):
  80. kwargs = self._get_clone_kwargs(
  81. url='https://myuser@bitbucket.org/workspace/repo.git'
  82. )
  83. self.assertEqual(kwargs.get('username'), None)
  84. self.assertEqual(kwargs.get('password'), None)
  85. def test_public_repo_no_credentials(self):
  86. kwargs = self._get_clone_kwargs(
  87. url='https://github.com/public/repo.git'
  88. )
  89. self.assertEqual(kwargs.get('username'), None)
  90. self.assertEqual(kwargs.get('password'), None)