| |
| |
| """ |
| (c) 2016 - Copyright Red Hat Inc |
| |
| Authors: |
| Adam Williamson <awilliam@redhat.com> |
| |
| """ |
| |
| import unittest |
| import sys |
| import os |
| |
| import mock |
| |
| sys.path.insert(0, os.path.join(os.path.dirname( |
| os.path.abspath(__file__)), '..')) |
| |
| import pagure.lib |
| import pagure.lib.model |
| import pagure.lib.notify |
| import tests |
| |
| |
| class PagureLibNotifyEmailtests(tests.Modeltests): |
| """ Some tests for the various email construction functions. In |
| their own class so they can have some shared fixtures. |
| """ |
| |
| def setUp(self): |
| """ Override setUp to add more fixtures used for many tests. """ |
| super(PagureLibNotifyEmailtests, self).setUp() |
| pagure.SESSION = self.session |
| tests.create_projects(self.session) |
| |
| |
| patcher = mock.patch('pagure.lib.notify.send_email') |
| patcher.start() |
| |
| self.user1 = pagure.lib.get_user(self.session, 'pingou') |
| self.user2 = pagure.lib.get_user(self.session, 'foo') |
| self.project1 = pagure.lib.get_project(self.session, 'test') |
| self.project2 = pagure.lib.get_project(self.session, 'test2') |
| self.project3 = pagure.lib.get_project(self.session, 'test3', namespace='somenamespace') |
| |
| |
| |
| item = pagure.lib.model.Project( |
| user_id=2, |
| name='test', |
| description='test project #1', |
| is_fork=True, |
| parent_id=1, |
| hook_token='aaabbbyyy', |
| ) |
| self.session.add(item) |
| self.session.commit() |
| self.forkedproject = pagure.lib.get_project(self.session, 'test', user='foo') |
| |
| |
| self.issue1 = pagure.lib.new_issue( |
| session=self.session, |
| repo=self.project1, |
| title='issue', |
| content='a bug report', |
| user='pingou', |
| ticketfolder=None, |
| ) |
| |
| |
| pagure.lib.add_issue_comment( |
| self.session, |
| self.issue1, |
| comment='Test comment', |
| user='pingou', |
| ticketfolder=None, |
| ) |
| self.comment1 = pagure.lib.get_issue_comment(self.session, self.issue1.uid, 1) |
| |
| |
| self.issue2 = pagure.lib.new_issue( |
| session=self.session, |
| repo=self.project3, |
| title='namespaced project issue', |
| content='a bug report on a namespaced project', |
| user='pingou', |
| ticketfolder=None, |
| ) |
| |
| |
| self.issue3 = pagure.lib.new_issue( |
| session=self.session, |
| repo=self.forkedproject, |
| title='forked project issue', |
| content='a bug report on a forked project', |
| user='pingou', |
| ticketfolder=None, |
| ) |
| |
| patcher.stop() |
| |
| @mock.patch('pagure.lib.notify.send_email') |
| def test_notify_new_comment(self, fakemail): |
| """Simple test for notification about new comment.""" |
| exptext = u""" |
| pingou added a new comment to an issue you are following: |
| `` |
| Test comment |
| `` |
| |
| To reply, visit the link below |
| https://pagure.org/test/issue/1 |
| """ |
| pagure.lib.notify.notify_new_comment(self.comment1) |
| (_, args, kwargs) = fakemail.mock_calls[0] |
| |
| |
| self.assertEqual(args[0], exptext) |
| |
| |
| self.assertEqual(args[1], u'Issue #1 `issue`') |
| |
| |
| self.assertEqual(args[2], self.user1.default_email) |
| |
| |
| self.assertEqual(kwargs['mail_id'], self.comment1.mail_id) |
| |
| |
| self.assertEqual(kwargs['in_reply_to'], self.issue1.mail_id) |
| |
| |
| self.assertEqual(kwargs['project_name'], self.project1.fullname) |
| |
| |
| self.assertEqual(kwargs['user_from'], self.user1.fullname) |
| |
| @mock.patch('pagure.lib.notify.send_email') |
| def test_notify_new_issue_namespaced(self, fakemail): |
| """Test for notifying of a new issue, namespaced project.""" |
| exptext = u""" |
| pingou reported a new issue against the project: `test3` that you are following: |
| `` |
| a bug report on a namespaced project |
| `` |
| |
| To reply, visit the link below |
| https://pagure.org/somenamespace/test3/issue/1 |
| """ |
| pagure.lib.notify.notify_new_issue(self.issue2) |
| (_, args, kwargs) = fakemail.mock_calls[0] |
| |
| |
| self.assertEqual(args[0], exptext) |
| |
| |
| self.assertEqual(args[1], u'Issue #1 `namespaced project issue`') |
| |
| |
| self.assertEqual(args[2], self.user1.default_email) |
| |
| |
| self.assertEqual(kwargs['mail_id'], self.issue2.mail_id) |
| |
| |
| self.assertEqual(kwargs['project_name'], self.project3.fullname) |
| |
| |
| self.assertEqual(kwargs['user_from'], self.user1.fullname) |
| |
| @mock.patch('pagure.lib.notify.send_email') |
| def test_notify_assigned_issue_forked(self, fakemail): |
| """Test for notifying re-assignment of issue on forked project. |
| 'foo' reassigns issue on his fork of 'test' to 'pingou'. |
| """ |
| exptext = u""" |
| The issue: `forked project issue` of project: `test` has been assigned to `pingou` by foo. |
| |
| https://pagure.org/fork/foo/test/issue/1 |
| """ |
| pagure.lib.notify.notify_assigned_issue(self.issue3, self.user1, self.user2) |
| (_, args, kwargs) = fakemail.mock_calls[0] |
| |
| |
| self.assertEqual(args[0], exptext) |
| |
| |
| self.assertEqual(args[1], u'Issue #1 `forked project issue`') |
| |
| |
| |
| self.assertEqual(args[2], self.user1.default_email) |
| |
| |
| self.assertIn("{0}/assigned/".format(self.issue3.mail_id), kwargs['mail_id']) |
| |
| |
| self.assertEqual(kwargs['project_name'], self.forkedproject.fullname) |
| |
| |
| self.assertEqual(kwargs['user_from'], self.user2.fullname) |
| |
| |
| |
| if __name__ == '__main__': |
| SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureLibNotifyEmailtests) |
| unittest.TextTestRunner(verbosity=2).run(SUITE) |