test_pagure_lib_notify_email.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2016 - Copyright Red Hat Inc
  4. Authors:
  5. Adam Williamson <awilliam@redhat.com>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. import unittest
  9. import sys
  10. import os
  11. import mock
  12. import six
  13. sys.path.insert(
  14. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  15. )
  16. import pagure.lib.query # pylint: disable=wrong-import-position
  17. import pagure.lib.model # pylint: disable=wrong-import-position
  18. import pagure.lib.notify # pylint: disable=wrong-import-position
  19. import tests # pylint: disable=wrong-import-position
  20. class PagureLibNotifyEmailtests(tests.Modeltests):
  21. """ Some tests for the various email construction functions. In
  22. their own class so they can have some shared fixtures.
  23. """
  24. def setUp(self):
  25. """ Override setUp to add more fixtures used for many tests. """
  26. super(PagureLibNotifyEmailtests, self).setUp()
  27. tests.create_projects(self.session)
  28. # we don't want to send any mails while setting up
  29. patcher = mock.patch("pagure.lib.notify.send_email")
  30. patcher.start()
  31. self.user1 = pagure.lib.query.get_user(self.session, "pingou")
  32. self.user2 = pagure.lib.query.get_user(self.session, "foo")
  33. self.project1 = pagure.lib.query._get_project(self.session, "test")
  34. self.project2 = pagure.lib.query._get_project(self.session, "test2")
  35. self.project3 = pagure.lib.query._get_project(
  36. self.session, "test3", namespace="somenamespace"
  37. )
  38. # Create a forked repo, should be project #4
  39. # Not using fork_project as it tries to do a git clone
  40. item = pagure.lib.model.Project(
  41. user_id=2, # foo
  42. name="test",
  43. description="test project #1",
  44. is_fork=True,
  45. parent_id=1,
  46. hook_token="aaabbbyyy",
  47. )
  48. self.session.add(item)
  49. self.session.commit()
  50. self.forkedproject = pagure.lib.query._get_project(
  51. self.session, "test", user="foo"
  52. )
  53. # Report an issue on project #1
  54. self.issue1 = pagure.lib.query.new_issue(
  55. session=self.session,
  56. repo=self.project1,
  57. title="issue",
  58. content="a bug report",
  59. user="pingou",
  60. )
  61. # Add a comment on the issue
  62. pagure.lib.query.add_issue_comment(
  63. self.session, self.issue1, comment="Test comment", user="pingou"
  64. )
  65. self.comment1 = pagure.lib.query.get_issue_comment(
  66. self.session, self.issue1.uid, 1
  67. )
  68. # Report an issue on project #3 (namespaced)
  69. self.issue2 = pagure.lib.query.new_issue(
  70. session=self.session,
  71. repo=self.project3,
  72. title="namespaced project issue",
  73. content="a bug report on a namespaced project",
  74. user="pingou",
  75. )
  76. # report an issue on foo's fork of project #1
  77. self.issue3 = pagure.lib.query.new_issue(
  78. session=self.session,
  79. repo=self.forkedproject,
  80. title="forked project issue",
  81. content="a bug report on a forked project",
  82. user="pingou",
  83. )
  84. patcher.stop()
  85. @mock.patch("pagure.lib.notify.send_email")
  86. def test_notify_new_comment(self, fakemail):
  87. """Simple test for notification about new comment."""
  88. exptext = """
  89. pingou added a new comment to an issue you are following:
  90. ``
  91. Test comment
  92. ``
  93. To reply, visit the link below
  94. http://localhost.localdomain/test/issue/1
  95. """
  96. pagure.lib.notify.notify_new_comment(self.comment1)
  97. (_, args, kwargs) = fakemail.mock_calls[0]
  98. # Mail text should be as expected.
  99. self.assertEqual(args[0], exptext)
  100. self.assertTrue(isinstance(args[0], six.text_type))
  101. # Mail subject should be as expected.
  102. self.assertEqual(args[1], "Issue #1: issue")
  103. # Mail should be sent to user #1.
  104. self.assertEqual(args[2], self.user1.default_email)
  105. # Mail ID should be comment #1's mail ID...
  106. self.assertEqual(kwargs["mail_id"], self.comment1.mail_id)
  107. # In reply to issue #1's mail ID.
  108. self.assertEqual(kwargs["in_reply_to"], self.issue1.mail_id)
  109. # Project name should be...project (full) name.
  110. self.assertEqual(kwargs["project_name"], self.project1.fullname)
  111. # Mail should be from user1 (who wrote the comment).
  112. self.assertEqual(kwargs["user_from"], self.user1.fullname)
  113. @mock.patch("pagure.lib.notify.send_email")
  114. def test_notify_new_issue_namespaced(
  115. self, fakemail
  116. ): # pylint: disable=invalid-name
  117. """Test for notifying of a new issue, namespaced project."""
  118. exptext = """
  119. pingou reported a new issue against the project: `test3` that you are following:
  120. ``
  121. a bug report on a namespaced project
  122. ``
  123. To reply, visit the link below
  124. http://localhost.localdomain/somenamespace/test3/issue/1
  125. """
  126. pagure.lib.notify.notify_new_issue(self.issue2)
  127. (_, args, kwargs) = fakemail.mock_calls[0]
  128. # Mail text should be as expected.
  129. self.assertEqual(args[0], exptext)
  130. self.assertTrue(isinstance(args[0], six.text_type))
  131. # Mail subject should be as expected.
  132. self.assertEqual(args[1], "Issue #1: namespaced project issue")
  133. # Mail should be sent to user #1.
  134. self.assertEqual(args[2], self.user1.default_email)
  135. # Mail ID should be issue's mail ID.
  136. self.assertEqual(kwargs["mail_id"], self.issue2.mail_id)
  137. # Project name should be...project (full) name.
  138. self.assertEqual(kwargs["project_name"], self.project3.fullname)
  139. # Mail should be from user1 (who submitted the issue).
  140. self.assertEqual(kwargs["user_from"], self.user1.fullname)
  141. @mock.patch("pagure.lib.notify.send_email")
  142. def test_notify_assigned_issue_forked(
  143. self, fakemail
  144. ): # pylint: disable=invalid-name
  145. """Test for notifying re-assignment of issue on forked project.
  146. 'foo' reassigns issue on his fork of 'test' to 'pingou'.
  147. """
  148. exptext = """
  149. The issue: `forked project issue` of project: `test` has been assigned to `pingou` by foo.
  150. http://localhost.localdomain/fork/foo/test/issue/1
  151. """
  152. pagure.lib.notify.notify_assigned_issue(
  153. self.issue3, self.user1, self.user2
  154. )
  155. (_, args, kwargs) = fakemail.mock_calls[0]
  156. # Mail text should be as expected.
  157. self.assertEqual(args[0], exptext)
  158. self.assertTrue(isinstance(args[0], six.text_type))
  159. # Mail subject should be as expected.
  160. self.assertEqual(args[1], "Issue #1: forked project issue")
  161. # Mail should be sent to user #1.
  162. # NOTE: Not sent to user #2...
  163. self.assertEqual(args[2], self.user1.default_email)
  164. # Mail ID should contain issue's mail ID and '/assigned/'
  165. self.assertIn(
  166. "{0}/assigned/".format(self.issue3.mail_id), kwargs["mail_id"]
  167. )
  168. # Project name should be...project (full) name.
  169. self.assertEqual(kwargs["project_name"], self.forkedproject.fullname)
  170. # Mail should be from user1 (who submitted the issue).
  171. self.assertEqual(kwargs["user_from"], self.user2.fullname)
  172. @mock.patch("pagure.lib.notify.send_email")
  173. # for non-ASCII testing, we mock these return values
  174. @mock.patch("pagure.lib.git.get_author", return_value="Cecil Cõmmîttër")
  175. @mock.patch(
  176. "pagure.lib.git.get_commit_subject", return_value="We love Motörhead"
  177. )
  178. def test_notify_new_commits(
  179. self, _, __, fakemail
  180. ): # pylint: disable=invalid-name
  181. """Test for notification on new commits, especially when
  182. non-ASCII text is involved.
  183. """
  184. exptext = """
  185. The following commits were pushed to the repo test on branch
  186. master, which you are following:
  187. abcdefg Cecil Cõmmîttër We love Motörhead
  188. To view more about the commits, visit:
  189. http://localhost.localdomain/test/commits/master
  190. """
  191. # first arg (abspath) doesn't matter and we can use a commit
  192. # ID that doesn't actually exist, as we are mocking
  193. # the get_author and get_commit_subject calls anyway
  194. pagure.lib.notify.notify_new_commits(
  195. "/", self.project1, "master", ["abcdefg"]
  196. )
  197. (_, args, kwargs) = fakemail.mock_calls[0]
  198. # Mail text should be as expected.
  199. self.assertEqual(args[0], exptext)
  200. self.assertTrue(isinstance(args[0], six.text_type))
  201. # Mail subject should be as expected.
  202. self.assertEqual(args[1], 'New Commits To "test" (master)')
  203. # Mail doesn't actually get sent to anyone by default
  204. self.assertEqual(args[2], "")
  205. # Project name should be...project (full) name.
  206. self.assertEqual(kwargs["project_name"], self.project1.fullname)
  207. # Add more tests to verify that correct mails are sent to correct people here
  208. if __name__ == "__main__":
  209. unittest.main(verbosity=2)