test_pagure_hooks_pagure_hook.py 6.1 KB

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