test_pagure_lib_notify_email.py 8.7 KB

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