1
0

test_tasks.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 = \
  46. tasks.commits_author_stats(self.repopath)
  47. self.assertEqual(num_commits, 1)
  48. self.assertEqual(num_authors, 1)
  49. self.assertEqual(last_time, '2018-01-01 00:00')
  50. self.assertIn(
  51. authors,
  52. [
  53. [(1, [(
  54. 'Alice', 'alice@example.com',
  55. 'https://seccdn.libravatar.org/avatar/'
  56. 'ff8d9819fc0e12bf0d24892e45987e249a28dce836a85cad60e28eaaa8c6d976'
  57. '?s=32&d=retro'
  58. )])],
  59. [(1, [(
  60. 'Alice', 'alice@example.com',
  61. 'https://seccdn.libravatar.org/avatar/'
  62. 'ff8d9819fc0e12bf0d24892e45987e249a28dce836a85cad60e28eaaa8c6d976'
  63. '?d=retro&s=32'
  64. )])]
  65. ]
  66. )
  67. def test_rename_user_and_merge(self):
  68. self.commits = [
  69. MockCommit('Alice', 'alice@example.com'),
  70. MockCommit('Bad name', 'alice@example.com', '2018-01-01 00:00'),
  71. ]
  72. self.authors = {
  73. 'alice@example.com': MockUser('Alice', 'alice@example.com'),
  74. }
  75. num_commits, authors, num_authors, last_time = \
  76. tasks.commits_author_stats(self.repopath)
  77. self.assertEqual(num_commits, 2)
  78. self.assertEqual(num_authors, 1)
  79. self.assertEqual(last_time, '2018-01-01 00:00')
  80. self.assertIn(
  81. authors,
  82. [
  83. [(2, [(
  84. 'Alice', 'alice@example.com',
  85. 'https://seccdn.libravatar.org/avatar/'
  86. 'ff8d9819fc0e12bf0d24892e45987e249a28dce836a85cad60e28eaaa8c6d976'
  87. '?s=32&d=retro'
  88. )])],
  89. [(2, [(
  90. 'Alice', 'alice@example.com',
  91. 'https://seccdn.libravatar.org/avatar/'
  92. 'ff8d9819fc0e12bf0d24892e45987e249a28dce836a85cad60e28eaaa8c6d976'
  93. '?d=retro&s=32'
  94. )])]
  95. ]
  96. )
  97. def test_preserve_unknown_author(self):
  98. self.commits = [
  99. MockCommit('Alice', 'alice@example.com', '2018-01-01 00:00'),
  100. ]
  101. self.authors = {}
  102. num_commits, authors, num_authors, last_time = \
  103. tasks.commits_author_stats(self.repopath)
  104. self.assertEqual(num_commits, 1)
  105. self.assertEqual(num_authors, 1)
  106. self.assertEqual(last_time, '2018-01-01 00:00')
  107. self.assertIn(
  108. authors,
  109. [
  110. [(1, [(
  111. 'Alice', 'alice@example.com',
  112. 'https://seccdn.libravatar.org/avatar/'
  113. 'ff8d9819fc0e12bf0d24892e45987e249a28dce836a85cad60e28eaaa8c6d976'
  114. '?s=32&d=retro'
  115. )])],
  116. [(1, [(
  117. 'Alice', 'alice@example.com',
  118. 'https://seccdn.libravatar.org/avatar/'
  119. 'ff8d9819fc0e12bf0d24892e45987e249a28dce836a85cad60e28eaaa8c6d976'
  120. '?d=retro&s=32'
  121. )])]
  122. ]
  123. )
  124. def test_handle_empty_email(self):
  125. self.commits = [
  126. # Two commits for Alice to ensure order of the result.
  127. MockCommit('Alice', None),
  128. MockCommit('Alice', None),
  129. MockCommit('Bob', '', '2018-01-01 00:00'),
  130. ]
  131. self.authors = {}
  132. num_commits, authors, num_authors, last_time = \
  133. tasks.commits_author_stats(self.repopath)
  134. self.assertEqual(num_commits, 3)
  135. self.assertEqual(num_authors, 2)
  136. self.assertEqual(last_time, '2018-01-01 00:00')
  137. self.assertEqual(authors, [(2, [('Alice', None, None)]),
  138. (1, [('Bob', '', None)])])
  139. class TestGitolitePostCompileOnly(object):
  140. @patch('pagure.lib.git_auth.get_git_auth_helper')
  141. def test_backend_has_post_compile_only(self, get_helper):
  142. helper = MagicMock()
  143. get_helper.return_value = helper
  144. helper.post_compile_only = MagicMock()
  145. tasks.gitolite_post_compile_only()
  146. helper.post_compile_only.assert_called_once()
  147. @patch('pagure.lib.git_auth.get_git_auth_helper')
  148. def test_backend_doesnt_have_post_compile_only(self, get_helper):
  149. helper = MagicMock()
  150. get_helper.return_value = helper
  151. helper.generate_acls = MagicMock()
  152. del helper.post_compile_only
  153. tasks.gitolite_post_compile_only()
  154. helper.generate_acls.assert_called_once_with(project=None)