test_pagure_lib_notify_email.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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.query # 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.query.get_user(self.session, 'pingou')
  31. self.user2 = pagure.lib.query.get_user(self.session, 'foo')
  32. self.project1 = pagure.lib.query._get_project(self.session, 'test')
  33. self.project2 = pagure.lib.query._get_project(self.session, 'test2')
  34. self.project3 = pagure.lib.query._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.query._get_project(self.session, 'test', user='foo')
  48. # Report an issue on project #1
  49. self.issue1 = pagure.lib.query.new_issue(
  50. session=self.session,
  51. repo=self.project1,
  52. title='issue',
  53. content='a bug report',
  54. user='pingou',
  55. )
  56. # Add a comment on the issue
  57. pagure.lib.query.add_issue_comment(
  58. self.session,
  59. self.issue1,
  60. comment='Test comment',
  61. user='pingou',
  62. )
  63. self.comment1 = pagure.lib.query.get_issue_comment(self.session, self.issue1.uid, 1)
  64. # Report an issue on project #3 (namespaced)
  65. self.issue2 = pagure.lib.query.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. )
  72. # report an issue on foo's fork of project #1
  73. self.issue3 = pagure.lib.query.new_issue(
  74. session=self.session,
  75. repo=self.forkedproject,
  76. title='forked project issue',
  77. content='a bug report on a forked project',
  78. user='pingou',
  79. )
  80. patcher.stop()
  81. @mock.patch('pagure.lib.notify.send_email')
  82. def test_notify_new_comment(self, fakemail):
  83. """Simple test for notification about new comment."""
  84. exptext = """
  85. pingou added a new comment to an issue you are following:
  86. ``
  87. Test comment
  88. ``
  89. To reply, visit the link below
  90. http://localhost.localdomain/test/issue/1
  91. """
  92. pagure.lib.notify.notify_new_comment(self.comment1)
  93. (_, args, kwargs) = fakemail.mock_calls[0]
  94. # Mail text should be as expected.
  95. self.assertEqual(args[0], exptext)
  96. self.assertTrue(isinstance(args[0], six.text_type))
  97. # Mail subject should be as expected.
  98. self.assertEqual(args[1], 'Issue #1: issue')
  99. # Mail should be sent to user #1.
  100. self.assertEqual(args[2], self.user1.default_email)
  101. # Mail ID should be comment #1's mail ID...
  102. self.assertEqual(kwargs['mail_id'], self.comment1.mail_id)
  103. # In reply to issue #1's mail ID.
  104. self.assertEqual(kwargs['in_reply_to'], self.issue1.mail_id)
  105. # Project name should be...project (full) name.
  106. self.assertEqual(kwargs['project_name'], self.project1.fullname)
  107. # Mail should be from user1 (who wrote the comment).
  108. self.assertEqual(kwargs['user_from'], self.user1.fullname)
  109. @mock.patch('pagure.lib.notify.send_email')
  110. def test_notify_new_issue_namespaced(self, fakemail): # pylint: disable=invalid-name
  111. """Test for notifying of a new issue, namespaced project."""
  112. exptext = """
  113. pingou reported a new issue against the project: `test3` that you are following:
  114. ``
  115. a bug report on a namespaced project
  116. ``
  117. To reply, visit the link below
  118. http://localhost.localdomain/somenamespace/test3/issue/1
  119. """
  120. pagure.lib.notify.notify_new_issue(self.issue2)
  121. (_, args, kwargs) = fakemail.mock_calls[0]
  122. # Mail text should be as expected.
  123. self.assertEqual(args[0], exptext)
  124. self.assertTrue(isinstance(args[0], six.text_type))
  125. # Mail subject should be as expected.
  126. self.assertEqual(args[1], 'Issue #1: namespaced project issue')
  127. # Mail should be sent to user #1.
  128. self.assertEqual(args[2], self.user1.default_email)
  129. # Mail ID should be issue's mail ID.
  130. self.assertEqual(kwargs['mail_id'], self.issue2.mail_id)
  131. # Project name should be...project (full) name.
  132. self.assertEqual(kwargs['project_name'], self.project3.fullname)
  133. # Mail should be from user1 (who submitted the issue).
  134. self.assertEqual(kwargs['user_from'], self.user1.fullname)
  135. @mock.patch('pagure.lib.notify.send_email')
  136. def test_notify_assigned_issue_forked(self, fakemail): # pylint: disable=invalid-name
  137. """Test for notifying re-assignment of issue on forked project.
  138. 'foo' reassigns issue on his fork of 'test' to 'pingou'.
  139. """
  140. exptext = """
  141. The issue: `forked project issue` of project: `test` has been assigned to `pingou` by foo.
  142. http://localhost.localdomain/fork/foo/test/issue/1
  143. """
  144. pagure.lib.notify.notify_assigned_issue(self.issue3, self.user1, self.user2)
  145. (_, args, kwargs) = fakemail.mock_calls[0]
  146. # Mail text should be as expected.
  147. self.assertEqual(args[0], exptext)
  148. self.assertTrue(isinstance(args[0], six.text_type))
  149. # Mail subject should be as expected.
  150. self.assertEqual(args[1], 'Issue #1: forked project issue')
  151. # Mail should be sent to user #1.
  152. # NOTE: Not sent to user #2...
  153. self.assertEqual(args[2], self.user1.default_email)
  154. # Mail ID should contain issue's mail ID and '/assigned/'
  155. self.assertIn("{0}/assigned/".format(self.issue3.mail_id), kwargs['mail_id'])
  156. # Project name should be...project (full) name.
  157. self.assertEqual(kwargs['project_name'], self.forkedproject.fullname)
  158. # Mail should be from user1 (who submitted the issue).
  159. self.assertEqual(kwargs['user_from'], self.user2.fullname)
  160. @mock.patch('pagure.lib.notify.send_email')
  161. # for non-ASCII testing, we mock these return values
  162. @mock.patch('pagure.lib.git.get_author', return_value="Cecil Cõmmîttër")
  163. @mock.patch('pagure.lib.git.get_commit_subject', return_value="We love Motörhead")
  164. def test_notify_new_commits(self, _, __, fakemail): # pylint: disable=invalid-name
  165. """Test for notification on new commits, especially when
  166. non-ASCII text is involved.
  167. """
  168. exptext = """
  169. The following commits were pushed to the repo test on branch
  170. master, which you are following:
  171. abcdefg Cecil Cõmmîttër We love Motörhead
  172. To view more about the commits, visit:
  173. http://localhost.localdomain/test/commits/master
  174. """
  175. # first arg (abspath) doesn't matter and we can use a commit
  176. # ID that doesn't actually exist, as we are mocking
  177. # the get_author and get_commit_subject calls anyway
  178. pagure.lib.notify.notify_new_commits('/', self.project1, 'master', ['abcdefg'])
  179. (_, args, kwargs) = fakemail.mock_calls[0]
  180. # Mail text should be as expected.
  181. self.assertEqual(args[0], exptext)
  182. self.assertTrue(isinstance(args[0], six.text_type))
  183. # Mail subject should be as expected.
  184. self.assertEqual(args[1], u'New Commits To "test" (master)')
  185. # Mail doesn't actually get sent to anyone by default
  186. self.assertEqual(args[2], '')
  187. # Project name should be...project (full) name.
  188. self.assertEqual(kwargs['project_name'], self.project1.fullname)
  189. # Add more tests to verify that correct mails are sent to correct people here
  190. if __name__ == '__main__':
  191. unittest.main(verbosity=2)