test_tasks.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. num_commits, authors, num_authors, last_time = tasks.commits_author_stats(
  46. self.repopath
  47. )
  48. self.assertEqual(num_commits, 1)
  49. self.assertEqual(num_authors, 1)
  50. self.assertEqual(last_time, "2018-01-01 00:00")
  51. self.assertIn(
  52. authors,
  53. [
  54. [
  55. (
  56. 1,
  57. [
  58. (
  59. "Alice",
  60. "alice@example.com",
  61. "https://seccdn.libravatar.org/avatar/"
  62. "ff8d9819fc0e12bf0d24892e45987e249a28dce836a85cad60e28eaaa8c6d976"
  63. "?s=32&d=retro",
  64. )
  65. ],
  66. )
  67. ],
  68. [
  69. (
  70. 1,
  71. [
  72. (
  73. "Alice",
  74. "alice@example.com",
  75. "https://seccdn.libravatar.org/avatar/"
  76. "ff8d9819fc0e12bf0d24892e45987e249a28dce836a85cad60e28eaaa8c6d976"
  77. "?d=retro&s=32",
  78. )
  79. ],
  80. )
  81. ],
  82. ],
  83. )
  84. def test_rename_user_and_merge(self):
  85. self.commits = [
  86. MockCommit("Alice", "alice@example.com"),
  87. MockCommit("Bad name", "alice@example.com", "2018-01-01 00:00"),
  88. ]
  89. self.authors = {
  90. "alice@example.com": MockUser("Alice", "alice@example.com")
  91. }
  92. num_commits, authors, num_authors, last_time = tasks.commits_author_stats(
  93. self.repopath
  94. )
  95. self.assertEqual(num_commits, 2)
  96. self.assertEqual(num_authors, 1)
  97. self.assertEqual(last_time, "2018-01-01 00:00")
  98. self.assertIn(
  99. authors,
  100. [
  101. [
  102. (
  103. 2,
  104. [
  105. (
  106. "Alice",
  107. "alice@example.com",
  108. "https://seccdn.libravatar.org/avatar/"
  109. "ff8d9819fc0e12bf0d24892e45987e249a28dce836a85cad60e28eaaa8c6d976"
  110. "?s=32&d=retro",
  111. )
  112. ],
  113. )
  114. ],
  115. [
  116. (
  117. 2,
  118. [
  119. (
  120. "Alice",
  121. "alice@example.com",
  122. "https://seccdn.libravatar.org/avatar/"
  123. "ff8d9819fc0e12bf0d24892e45987e249a28dce836a85cad60e28eaaa8c6d976"
  124. "?d=retro&s=32",
  125. )
  126. ],
  127. )
  128. ],
  129. ],
  130. )
  131. def test_preserve_unknown_author(self):
  132. self.commits = [
  133. MockCommit("Alice", "alice@example.com", "2018-01-01 00:00")
  134. ]
  135. self.authors = {}
  136. num_commits, authors, num_authors, last_time = tasks.commits_author_stats(
  137. self.repopath
  138. )
  139. self.assertEqual(num_commits, 1)
  140. self.assertEqual(num_authors, 1)
  141. self.assertEqual(last_time, "2018-01-01 00:00")
  142. self.assertIn(
  143. authors,
  144. [
  145. [
  146. (
  147. 1,
  148. [
  149. (
  150. "Alice",
  151. "alice@example.com",
  152. "https://seccdn.libravatar.org/avatar/"
  153. "ff8d9819fc0e12bf0d24892e45987e249a28dce836a85cad60e28eaaa8c6d976"
  154. "?s=32&d=retro",
  155. )
  156. ],
  157. )
  158. ],
  159. [
  160. (
  161. 1,
  162. [
  163. (
  164. "Alice",
  165. "alice@example.com",
  166. "https://seccdn.libravatar.org/avatar/"
  167. "ff8d9819fc0e12bf0d24892e45987e249a28dce836a85cad60e28eaaa8c6d976"
  168. "?d=retro&s=32",
  169. )
  170. ],
  171. )
  172. ],
  173. ],
  174. )
  175. def test_handle_empty_email(self):
  176. self.commits = [
  177. # Two commits for Alice to ensure order of the result.
  178. MockCommit("Alice", None),
  179. MockCommit("Alice", None),
  180. MockCommit("Bob", "", "2018-01-01 00:00"),
  181. ]
  182. self.authors = {}
  183. num_commits, authors, num_authors, last_time = tasks.commits_author_stats(
  184. self.repopath
  185. )
  186. self.assertEqual(num_commits, 3)
  187. self.assertEqual(num_authors, 2)
  188. self.assertEqual(last_time, "2018-01-01 00:00")
  189. self.assertEqual(
  190. authors, [(2, [("Alice", None, None)]), (1, [("Bob", "", None)])]
  191. )
  192. class TestGitolitePostCompileOnly(object):
  193. @patch("pagure.lib.git_auth.get_git_auth_helper")
  194. def test_backend_has_post_compile_only(self, get_helper):
  195. helper = MagicMock()
  196. get_helper.return_value = helper
  197. helper.post_compile_only = MagicMock()
  198. tasks.gitolite_post_compile_only()
  199. helper.post_compile_only.assert_called_once()
  200. @patch("pagure.lib.git_auth.get_git_auth_helper")
  201. def test_backend_doesnt_have_post_compile_only(self, get_helper):
  202. helper = MagicMock()
  203. get_helper.return_value = helper
  204. helper.generate_acls = MagicMock()
  205. del helper.post_compile_only
  206. tasks.gitolite_post_compile_only()
  207. helper.generate_acls.assert_called_once_with(project=None)