test_tasks.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. from mock import patch, MagicMock, Mock
  2. from collections import namedtuple
  3. import os
  4. import unittest
  5. from pagure.lib import tasks
  6. MockUser = namedtuple('MockUser', ['fullname', 'default_email'])
  7. class MockCommit(object):
  8. def __init__(self, name, email, time='1970-01-01 00:00'):
  9. self.author = Mock(email=email)
  10. self.author.name = name
  11. self.commit_time = time
  12. @patch('pagure.lib.query.create_session', new=Mock())
  13. class TestCommitsAuthorStats(unittest.TestCase):
  14. def setUp(self):
  15. self.search_user_patcher = patch('pagure.lib.query.search_user')
  16. mock_search_user = self.search_user_patcher.start()
  17. mock_search_user.side_effect = lambda _, email: self.authors.get(email)
  18. self.pygit_patcher = patch('pygit2.Repository')
  19. mock_repo = self.pygit_patcher.start().return_value
  20. def mock_walk_impl(*args, **kwargs):
  21. for commit in self.commits:
  22. yield commit
  23. mock_repo.walk.side_effect = mock_walk_impl
  24. self.repopath = Mock()
  25. exists = os.path.exists
  26. def mock_exists_impl(path):
  27. if path == self.repopath:
  28. return True
  29. return exists(path)
  30. self.exists_patcher = patch('os.path.exists')
  31. mock_exists = self.exists_patcher.start()
  32. mock_exists.side_effect = mock_exists_impl
  33. def tearDown(self):
  34. self.search_user_patcher.stop()
  35. self.pygit_patcher.stop()
  36. self.exists_patcher.stop()
  37. def test_no_change(self):
  38. self.commits = [
  39. MockCommit('Alice', 'alice@example.com', '2018-01-01 00:00'),
  40. ]
  41. self.authors = {
  42. 'alice@example.com': MockUser('Alice', 'alice@example.com'),
  43. }
  44. num_commits, authors, num_authors, last_time = \
  45. tasks.commits_author_stats(self.repopath)
  46. self.assertEqual(num_commits, 1)
  47. self.assertEqual(num_authors, 1)
  48. self.assertEqual(last_time, '2018-01-01 00:00')
  49. self.assertIn(
  50. authors,
  51. [
  52. [(1, [(
  53. 'Alice', 'alice@example.com',
  54. 'https://seccdn.libravatar.org/avatar/'
  55. 'ff8d9819fc0e12bf0d24892e45987e249a28dce836a85cad60e28eaaa8c6d976'
  56. '?s=32&d=retro'
  57. )])],
  58. [(1, [(
  59. 'Alice', 'alice@example.com',
  60. 'https://seccdn.libravatar.org/avatar/'
  61. 'ff8d9819fc0e12bf0d24892e45987e249a28dce836a85cad60e28eaaa8c6d976'
  62. '?d=retro&s=32'
  63. )])]
  64. ]
  65. )
  66. def test_rename_user_and_merge(self):
  67. self.commits = [
  68. MockCommit('Alice', 'alice@example.com'),
  69. MockCommit('Bad name', 'alice@example.com', '2018-01-01 00:00'),
  70. ]
  71. self.authors = {
  72. 'alice@example.com': MockUser('Alice', 'alice@example.com'),
  73. }
  74. num_commits, authors, num_authors, last_time = \
  75. tasks.commits_author_stats(self.repopath)
  76. self.assertEqual(num_commits, 2)
  77. self.assertEqual(num_authors, 1)
  78. self.assertEqual(last_time, '2018-01-01 00:00')
  79. self.assertIn(
  80. authors,
  81. [
  82. [(2, [(
  83. 'Alice', 'alice@example.com',
  84. 'https://seccdn.libravatar.org/avatar/'
  85. 'ff8d9819fc0e12bf0d24892e45987e249a28dce836a85cad60e28eaaa8c6d976'
  86. '?s=32&d=retro'
  87. )])],
  88. [(2, [(
  89. 'Alice', 'alice@example.com',
  90. 'https://seccdn.libravatar.org/avatar/'
  91. 'ff8d9819fc0e12bf0d24892e45987e249a28dce836a85cad60e28eaaa8c6d976'
  92. '?d=retro&s=32'
  93. )])]
  94. ]
  95. )
  96. def test_preserve_unknown_author(self):
  97. self.commits = [
  98. MockCommit('Alice', 'alice@example.com', '2018-01-01 00:00'),
  99. ]
  100. self.authors = {}
  101. num_commits, authors, num_authors, last_time = \
  102. tasks.commits_author_stats(self.repopath)
  103. self.assertEqual(num_commits, 1)
  104. self.assertEqual(num_authors, 1)
  105. self.assertEqual(last_time, '2018-01-01 00:00')
  106. self.assertIn(
  107. authors,
  108. [
  109. [(1, [(
  110. 'Alice', 'alice@example.com',
  111. 'https://seccdn.libravatar.org/avatar/'
  112. 'ff8d9819fc0e12bf0d24892e45987e249a28dce836a85cad60e28eaaa8c6d976'
  113. '?s=32&d=retro'
  114. )])],
  115. [(1, [(
  116. 'Alice', 'alice@example.com',
  117. 'https://seccdn.libravatar.org/avatar/'
  118. 'ff8d9819fc0e12bf0d24892e45987e249a28dce836a85cad60e28eaaa8c6d976'
  119. '?d=retro&s=32'
  120. )])]
  121. ]
  122. )
  123. def test_handle_empty_email(self):
  124. self.commits = [
  125. # Two commits for Alice to ensure order of the result.
  126. MockCommit('Alice', None),
  127. MockCommit('Alice', None),
  128. MockCommit('Bob', '', '2018-01-01 00:00'),
  129. ]
  130. self.authors = {}
  131. num_commits, authors, num_authors, last_time = \
  132. tasks.commits_author_stats(self.repopath)
  133. self.assertEqual(num_commits, 3)
  134. self.assertEqual(num_authors, 2)
  135. self.assertEqual(last_time, '2018-01-01 00:00')
  136. self.assertEqual(authors, [(2, [('Alice', None, None)]),
  137. (1, [('Bob', '', None)])])
  138. class TestGitolitePostCompileOnly(object):
  139. @patch('pagure.lib.git_auth.get_git_auth_helper')
  140. def test_backend_has_post_compile_only(self, get_helper):
  141. helper = MagicMock()
  142. get_helper.return_value = helper
  143. helper.post_compile_only = MagicMock()
  144. tasks.gitolite_post_compile_only()
  145. helper.post_compile_only.assert_called_once()
  146. @patch('pagure.lib.git_auth.get_git_auth_helper')
  147. def test_backend_doesnt_have_post_compile_only(self, get_helper):
  148. helper = MagicMock()
  149. get_helper.return_value = helper
  150. helper.generate_acls = MagicMock()
  151. del helper.post_compile_only
  152. tasks.gitolite_post_compile_only()
  153. helper.generate_acls.assert_called_once_with(project=None)