test_pagure_hooks_pagure_hook.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2018 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. import unittest
  9. import sys
  10. import os
  11. import mock
  12. sys.path.insert(0, os.path.join(os.path.dirname(
  13. os.path.abspath(__file__)), '..'))
  14. import pagure.hooks.pagure_hook
  15. import tests
  16. class PagureHooksPagureHooktests(tests.SimplePagureTest):
  17. """ Tests for pagure.hooks.pagure_hook """
  18. def setUp(self):
  19. """ Set up the environnment, ran before every tests. """
  20. super(PagureHooksPagureHooktests, self).setUp()
  21. tests.create_projects(self.session)
  22. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  23. # Add one issue to each projects otherwise we won't be able to find
  24. project = pagure.lib.query.get_authorized_project(self.session, 'test')
  25. msg = pagure.lib.query.new_issue(
  26. session=self.session,
  27. repo=project,
  28. title='Test issue',
  29. content='We should work on this',
  30. user='pingou',
  31. )
  32. self.session.commit()
  33. self.assertEqual(msg.title, 'Test issue')
  34. project = pagure.lib.query.get_authorized_project(
  35. self.session, 'test2')
  36. msg = pagure.lib.query.new_issue(
  37. session=self.session,
  38. repo=project,
  39. title='Test issue on test2',
  40. content='We should work on this, really',
  41. user='foo',
  42. )
  43. self.session.commit()
  44. self.assertEqual(msg.title, 'Test issue on test2')
  45. # Create a fork of test for foo with its own ticket
  46. item = pagure.lib.model.Project(
  47. user_id=2, # foo
  48. name='test',
  49. is_fork=True,
  50. parent_id=1,
  51. description='test project #1',
  52. hook_token='aaabbbccc_foo',
  53. )
  54. item.close_status = [
  55. 'Invalid', 'Insufficient data', 'Fixed', 'Duplicate']
  56. self.session.add(item)
  57. self.session.commit()
  58. project = pagure.lib.query.get_authorized_project(
  59. self.session, 'test', user="foo")
  60. msg = pagure.lib.query.new_issue(
  61. session=self.session,
  62. repo=project,
  63. title='Test issue on fork/foo/test',
  64. content='We should work on this, really',
  65. user='foo',
  66. )
  67. self.session.commit()
  68. self.assertEqual(msg.title, 'Test issue on fork/foo/test')
  69. self.folder = os.path.join(self.path, 'repos', 'test.git')
  70. # Add a README to the git repo - First commit
  71. tests.add_readme_git_repo(self.folder)
  72. @mock.patch("pagure.hooks.pagure_hook.fixes_relation")
  73. def test_generate_revision_change_log_short_url(self, fixes_relation):
  74. """ Test generate_revision_change_log when the comment contains
  75. a short link to the same project.
  76. """
  77. # Add a commit with an url in the commit message
  78. tests.add_content_to_git(
  79. self.folder, branch='master', filename='sources', content='foo',
  80. message='Test commit message\n\nFixes #1'
  81. )
  82. project = pagure.lib.query.get_authorized_project(self.session, 'test')
  83. pagure.hooks.pagure_hook.generate_revision_change_log(
  84. session=self.session,
  85. project=project,
  86. username=None,
  87. repodir=self.folder,
  88. new_commits_list=['HEAD']
  89. )
  90. fixes_relation.assert_called_once_with(
  91. mock.ANY, None, mock.ANY, project.issues[0],
  92. 'http://localhost.localdomain/')
  93. @mock.patch("pagure.hooks.pagure_hook.fixes_relation")
  94. def test_generate_revision_change_log_full_url(self, fixes_relation):
  95. """ Test generate_revision_change_log when the comment contains
  96. a full link to another project.
  97. """
  98. # Add a commit with an url in the commit message
  99. tests.add_content_to_git(
  100. self.folder, branch='master', filename='sources', content='foo',
  101. message='Test commit message\n\n'
  102. 'Fixes http://localhost.localdomain/test2/issue/1'
  103. )
  104. project = pagure.lib.query.get_authorized_project(
  105. self.session, 'test')
  106. project2 = pagure.lib.query.get_authorized_project(
  107. self.session, 'test2')
  108. pagure.hooks.pagure_hook.generate_revision_change_log(
  109. session=self.session,
  110. project=project,
  111. username=None,
  112. repodir=self.folder,
  113. new_commits_list=['HEAD']
  114. )
  115. fixes_relation.assert_called_once_with(
  116. mock.ANY, None, mock.ANY, project2.issues[0],
  117. 'http://localhost.localdomain/')
  118. @mock.patch("pagure.hooks.pagure_hook.fixes_relation")
  119. def test_generate_revision_change_log_full_url_fork(self, fixes_relation):
  120. """ Test generate_revision_change_log when the comment contains
  121. a full link to a fork.
  122. """
  123. # Add a commit with an url in the commit message
  124. tests.add_content_to_git(
  125. self.folder, branch='master', filename='sources', content='foo',
  126. message='Test commit message\n\n'
  127. 'Fixes http://localhost.localdomain/fork/foo/test/issue/1'
  128. )
  129. project = pagure.lib.query.get_authorized_project(
  130. self.session, 'test')
  131. project_fork = pagure.lib.query.get_authorized_project(
  132. self.session, 'test', user="foo")
  133. pagure.hooks.pagure_hook.generate_revision_change_log(
  134. session=self.session,
  135. project=project,
  136. username=None,
  137. repodir=self.folder,
  138. new_commits_list=['HEAD']
  139. )
  140. fixes_relation.assert_called_once_with(
  141. mock.ANY, None, mock.ANY, project_fork.issues[0],
  142. 'http://localhost.localdomain/')
  143. if __name__ == '__main__':
  144. unittest.main(verbosity=2)