git_auth_test.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #!/usr/bin/env vpython3
  2. # coding=utf-8
  3. # Copyright (c) 2012 The Chromium Authors. All rights reserved.
  4. # Use of this source code is governed by a BSD-style license that can be
  5. # found in the LICENSE file.
  6. """Unit tests for git_cl.py."""
  7. import logging
  8. import os
  9. import sys
  10. import unittest
  11. from typing import Iterable
  12. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  13. import git_auth
  14. import scm
  15. import scm_mock
  16. class TestConfigChanger(unittest.TestCase):
  17. def setUp(self):
  18. self._global_state_view: Iterable[tuple[str,
  19. list[str]]] = scm_mock.GIT(self)
  20. @property
  21. def global_state(self):
  22. return dict(self._global_state_view)
  23. def test_apply_new_auth(self):
  24. git_auth.ConfigChanger(
  25. mode=git_auth.ConfigMode.NEW_AUTH,
  26. remote_url=
  27. 'https://chromium.googlesource.com/chromium/tools/depot_tools.git',
  28. ).apply('/some/fake/dir')
  29. want = {
  30. '/some/fake/dir': {
  31. 'credential.https://chromium.googlesource.com/.helper':
  32. ['', 'luci'],
  33. 'http.cookiefile': [''],
  34. },
  35. }
  36. self.assertEqual(scm.GIT._dump_config_state(), want)
  37. def test_apply_new_auth_sso(self):
  38. git_auth.ConfigChanger(
  39. mode=git_auth.ConfigMode.NEW_AUTH_SSO,
  40. remote_url=
  41. 'https://chromium.googlesource.com/chromium/tools/depot_tools.git',
  42. ).apply('/some/fake/dir')
  43. want = {
  44. '/some/fake/dir': {
  45. 'protocol.sso.allow': ['always'],
  46. 'url.sso://chromium/.insteadof':
  47. ['https://chromium.googlesource.com/'],
  48. 'http.cookiefile': [''],
  49. },
  50. }
  51. self.assertEqual(scm.GIT._dump_config_state(), want)
  52. def test_apply_no_auth(self):
  53. git_auth.ConfigChanger(
  54. mode=git_auth.ConfigMode.NO_AUTH,
  55. remote_url=
  56. 'https://chromium.googlesource.com/chromium/tools/depot_tools.git',
  57. ).apply('/some/fake/dir')
  58. want = {
  59. '/some/fake/dir': {},
  60. }
  61. self.assertEqual(scm.GIT._dump_config_state(), want)
  62. def test_apply_chain_sso_new(self):
  63. git_auth.ConfigChanger(
  64. mode=git_auth.ConfigMode.NEW_AUTH_SSO,
  65. remote_url=
  66. 'https://chromium.googlesource.com/chromium/tools/depot_tools.git',
  67. ).apply('/some/fake/dir')
  68. git_auth.ConfigChanger(
  69. mode=git_auth.ConfigMode.NEW_AUTH,
  70. remote_url=
  71. 'https://chromium.googlesource.com/chromium/tools/depot_tools.git',
  72. ).apply('/some/fake/dir')
  73. want = {
  74. '/some/fake/dir': {
  75. 'credential.https://chromium.googlesource.com/.helper':
  76. ['', 'luci'],
  77. 'http.cookiefile': [''],
  78. },
  79. }
  80. self.assertEqual(scm.GIT._dump_config_state(), want)
  81. def test_apply_chain_new_sso(self):
  82. git_auth.ConfigChanger(
  83. mode=git_auth.ConfigMode.NEW_AUTH,
  84. remote_url=
  85. 'https://chromium.googlesource.com/chromium/tools/depot_tools.git',
  86. ).apply('/some/fake/dir')
  87. git_auth.ConfigChanger(
  88. mode=git_auth.ConfigMode.NEW_AUTH_SSO,
  89. remote_url=
  90. 'https://chromium.googlesource.com/chromium/tools/depot_tools.git',
  91. ).apply('/some/fake/dir')
  92. want = {
  93. '/some/fake/dir': {
  94. 'protocol.sso.allow': ['always'],
  95. 'url.sso://chromium/.insteadof':
  96. ['https://chromium.googlesource.com/'],
  97. 'http.cookiefile': [''],
  98. },
  99. }
  100. self.assertEqual(scm.GIT._dump_config_state(), want)
  101. def test_apply_chain_new_no(self):
  102. git_auth.ConfigChanger(
  103. mode=git_auth.ConfigMode.NEW_AUTH,
  104. remote_url=
  105. 'https://chromium.googlesource.com/chromium/tools/depot_tools.git',
  106. ).apply('/some/fake/dir')
  107. git_auth.ConfigChanger(
  108. mode=git_auth.ConfigMode.NO_AUTH,
  109. remote_url=
  110. 'https://chromium.googlesource.com/chromium/tools/depot_tools.git',
  111. ).apply('/some/fake/dir')
  112. want = {
  113. '/some/fake/dir': {},
  114. }
  115. self.assertEqual(scm.GIT._dump_config_state(), want)
  116. def test_apply_chain_sso_no(self):
  117. git_auth.ConfigChanger(
  118. mode=git_auth.ConfigMode.NEW_AUTH_SSO,
  119. remote_url=
  120. 'https://chromium.googlesource.com/chromium/tools/depot_tools.git',
  121. ).apply('/some/fake/dir')
  122. git_auth.ConfigChanger(
  123. mode=git_auth.ConfigMode.NO_AUTH,
  124. remote_url=
  125. 'https://chromium.googlesource.com/chromium/tools/depot_tools.git',
  126. ).apply('/some/fake/dir')
  127. want = {
  128. '/some/fake/dir': {},
  129. }
  130. self.assertEqual(scm.GIT._dump_config_state(), want)
  131. def test_apply_global_new_auth(self):
  132. git_auth.ConfigChanger(
  133. mode=git_auth.ConfigMode.NEW_AUTH,
  134. remote_url=
  135. 'https://chromium.googlesource.com/chromium/tools/depot_tools.git',
  136. ).apply_global('/some/fake/dir')
  137. want = {
  138. 'credential.https://chromium.googlesource.com/.helper':
  139. ['', 'luci'],
  140. }
  141. self.assertEqual(self.global_state, want)
  142. def test_apply_global_new_auth_sso(self):
  143. git_auth.ConfigChanger(
  144. mode=git_auth.ConfigMode.NEW_AUTH_SSO,
  145. remote_url=
  146. 'https://chromium.googlesource.com/chromium/tools/depot_tools.git',
  147. ).apply_global('/some/fake/dir')
  148. want = {
  149. 'protocol.sso.allow': ['always'],
  150. 'url.sso://chromium/.insteadof':
  151. ['https://chromium.googlesource.com/'],
  152. }
  153. self.assertEqual(self.global_state, want)
  154. if __name__ == '__main__':
  155. logging.basicConfig(
  156. level=logging.DEBUG if '-v' in sys.argv else logging.ERROR)
  157. unittest.main()