test_pagure_lib_notify_email.py 8.7 KB

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