test_tasks.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. from __future__ import unicode_literals, absolute_import
  2. from mock import patch, MagicMock, Mock
  3. from collections import namedtuple
  4. import os
  5. import unittest
  6. from pagure.lib import tasks
  7. MockUser = namedtuple("MockUser", ["fullname", "default_email"])
  8. class MockCommit(object):
  9. def __init__(self, name, email, time="1970-01-01 00:00"):
  10. self.author = Mock(email=email)
  11. self.author.name = name
  12. self.commit_time = time
  13. @patch("pagure.lib.query.create_session", new=Mock())
  14. class TestCommitsAuthorStats(unittest.TestCase):
  15. def setUp(self):
  16. self.search_user_patcher = patch("pagure.lib.query.search_user")
  17. mock_search_user = self.search_user_patcher.start()
  18. mock_search_user.side_effect = lambda _, email: self.authors.get(email)
  19. self.pygit_patcher = patch("pygit2.Repository")
  20. mock_repo = self.pygit_patcher.start().return_value
  21. def mock_walk_impl(*args, **kwargs):
  22. for commit in self.commits:
  23. yield commit
  24. mock_repo.walk.side_effect = mock_walk_impl
  25. self.repopath = Mock()
  26. exists = os.path.exists
  27. def mock_exists_impl(path):
  28. if path == self.repopath:
  29. return True
  30. return exists(path)
  31. self.exists_patcher = patch("os.path.exists")
  32. mock_exists = self.exists_patcher.start()
  33. mock_exists.side_effect = mock_exists_impl
  34. def tearDown(self):
  35. self.search_user_patcher.stop()
  36. self.pygit_patcher.stop()
  37. self.exists_patcher.stop()
  38. def test_no_change(self):
  39. self.commits = [
  40. MockCommit("Alice", "alice@example.com", "2018-01-01 00:00")
  41. ]
  42. self.authors = {
  43. "alice@example.com": MockUser("Alice", "alice@example.com")
  44. }
  45. (
  46. num_commits,
  47. authors,
  48. num_authors,
  49. last_time,
  50. ) = tasks.commits_author_stats(self.repopath)
  51. self.assertEqual(num_commits, 1)
  52. self.assertEqual(num_authors, 1)
  53. self.assertEqual(last_time, "2018-01-01 00:00")
  54. self.assertIn(
  55. authors,
  56. [
  57. [
  58. (
  59. 1,
  60. [
  61. (
  62. "Alice",
  63. "alice@example.com",
  64. "https://seccdn.libravatar.org/avatar/"
  65. "ff8d9819fc0e12bf0d24892e45987e249a28dce836a85cad60e28eaaa8c6d976"
  66. "?s=32&d=retro",
  67. )
  68. ],
  69. )
  70. ],
  71. [
  72. (
  73. 1,
  74. [
  75. (
  76. "Alice",
  77. "alice@example.com",
  78. "https://seccdn.libravatar.org/avatar/"
  79. "ff8d9819fc0e12bf0d24892e45987e249a28dce836a85cad60e28eaaa8c6d976"
  80. "?d=retro&s=32",
  81. )
  82. ],
  83. )
  84. ],
  85. ],
  86. )
  87. def test_rename_user_and_merge(self):
  88. self.commits = [
  89. MockCommit("Alice", "alice@example.com"),
  90. MockCommit("Bad name", "alice@example.com", "2018-01-01 00:00"),
  91. ]
  92. self.authors = {
  93. "alice@example.com": MockUser("Alice", "alice@example.com")
  94. }
  95. (
  96. num_commits,
  97. authors,
  98. num_authors,
  99. last_time,
  100. ) = tasks.commits_author_stats(self.repopath)
  101. self.assertEqual(num_commits, 2)
  102. self.assertEqual(num_authors, 1)
  103. self.assertEqual(last_time, "2018-01-01 00:00")
  104. self.assertIn(
  105. authors,
  106. [
  107. [
  108. (
  109. 2,
  110. [
  111. (
  112. "Alice",
  113. "alice@example.com",
  114. "https://seccdn.libravatar.org/avatar/"
  115. "ff8d9819fc0e12bf0d24892e45987e249a28dce836a85cad60e28eaaa8c6d976"
  116. "?s=32&d=retro",
  117. )
  118. ],
  119. )
  120. ],
  121. [
  122. (
  123. 2,
  124. [
  125. (
  126. "Alice",
  127. "alice@example.com",
  128. "https://seccdn.libravatar.org/avatar/"
  129. "ff8d9819fc0e12bf0d24892e45987e249a28dce836a85cad60e28eaaa8c6d976"
  130. "?d=retro&s=32",
  131. )
  132. ],
  133. )
  134. ],
  135. ],
  136. )
  137. def test_preserve_unknown_author(self):
  138. self.commits = [
  139. MockCommit("Alice", "alice@example.com", "2018-01-01 00:00")
  140. ]
  141. self.authors = {}
  142. (
  143. num_commits,
  144. authors,
  145. num_authors,
  146. last_time,
  147. ) = tasks.commits_author_stats(self.repopath)
  148. self.assertEqual(num_commits, 1)
  149. self.assertEqual(num_authors, 1)
  150. self.assertEqual(last_time, "2018-01-01 00:00")
  151. self.assertIn(
  152. authors,
  153. [
  154. [
  155. (
  156. 1,
  157. [
  158. (
  159. "Alice",
  160. "alice@example.com",
  161. "https://seccdn.libravatar.org/avatar/"
  162. "ff8d9819fc0e12bf0d24892e45987e249a28dce836a85cad60e28eaaa8c6d976"
  163. "?s=32&d=retro",
  164. )
  165. ],
  166. )
  167. ],
  168. [
  169. (
  170. 1,
  171. [
  172. (
  173. "Alice",
  174. "alice@example.com",
  175. "https://seccdn.libravatar.org/avatar/"
  176. "ff8d9819fc0e12bf0d24892e45987e249a28dce836a85cad60e28eaaa8c6d976"
  177. "?d=retro&s=32",
  178. )
  179. ],
  180. )
  181. ],
  182. ],
  183. )
  184. def test_handle_empty_email(self):
  185. self.commits = [
  186. # Two commits for Alice to ensure order of the result.
  187. MockCommit("Alice", None),
  188. MockCommit("Alice", None),
  189. MockCommit("Bob", "", "2018-01-01 00:00"),
  190. ]
  191. self.authors = {}
  192. (
  193. num_commits,
  194. authors,
  195. num_authors,
  196. last_time,
  197. ) = tasks.commits_author_stats(self.repopath)
  198. self.assertEqual(num_commits, 3)
  199. self.assertEqual(num_authors, 2)
  200. self.assertEqual(last_time, "2018-01-01 00:00")
  201. self.assertEqual(
  202. authors, [(2, [("Alice", None, None)]), (1, [("Bob", "", None)])]
  203. )
  204. class TestGitolitePostCompileOnly(object):
  205. @patch("pagure.lib.git_auth.get_git_auth_helper")
  206. def test_backend_has_post_compile_only(self, get_helper):
  207. helper = MagicMock()
  208. get_helper.return_value = helper
  209. helper.post_compile_only = MagicMock()
  210. tasks.gitolite_post_compile_only()
  211. helper.post_compile_only.assert_called_once()
  212. @patch("pagure.lib.git_auth.get_git_auth_helper")
  213. def test_backend_doesnt_have_post_compile_only(self, get_helper):
  214. helper = MagicMock()
  215. get_helper.return_value = helper
  216. helper.generate_acls = MagicMock()
  217. del helper.post_compile_only
  218. tasks.gitolite_post_compile_only()
  219. helper.generate_acls.assert_called_once_with(project=None)