Blame tests/test_pagure_lib_notify_email.py

Adam Williamson a15f45
# -*- coding: utf-8 -*-
Adam Williamson a15f45
Adam Williamson a15f45
"""
Adam Williamson a15f45
 (c) 2016 - Copyright Red Hat Inc
Adam Williamson a15f45
Adam Williamson a15f45
 Authors:
Adam Williamson a15f45
   Adam Williamson <awilliam@redhat.com></awilliam@redhat.com>
Adam Williamson a15f45
Adam Williamson a15f45
"""
Adam Williamson a15f45
Adam Williamson a15f45
import unittest
Adam Williamson a15f45
import sys
Adam Williamson a15f45
import os
Adam Williamson a15f45
Adam Williamson a15f45
import mock
Adam Williamson a15f45
Adam Williamson a15f45
sys.path.insert(0, os.path.join(os.path.dirname(
Adam Williamson a15f45
    os.path.abspath(__file__)), '..'))
Adam Williamson a15f45
Adam Williamson a15f45
import pagure.lib           # pylint: disable=wrong-import-position
Adam Williamson a15f45
import pagure.lib.model     # pylint: disable=wrong-import-position
Adam Williamson a15f45
import pagure.lib.notify    # pylint: disable=wrong-import-position
Adam Williamson a15f45
import tests                # pylint: disable=wrong-import-position
Adam Williamson a15f45
Adam Williamson a15f45
Adam Williamson a15f45
class PagureLibNotifyEmailtests(tests.Modeltests):
Adam Williamson a15f45
    """ Some tests for the various email construction functions. In
Adam Williamson a15f45
    their own class so they can have some shared fixtures.
Adam Williamson a15f45
    """
Adam Williamson a15f45
Adam Williamson a15f45
    def setUp(self):
Adam Williamson a15f45
        """ Override setUp to add more fixtures used for many tests. """
Adam Williamson a15f45
        super(PagureLibNotifyEmailtests, self).setUp()
Pierre-Yves Chibon b130e5
Adam Williamson a15f45
        tests.create_projects(self.session)
Adam Williamson a15f45
Adam Williamson a15f45
        # we don't want to send any mails while setting up
Adam Williamson a15f45
        patcher = mock.patch('pagure.lib.notify.send_email')
Adam Williamson a15f45
        patcher.start()
Adam Williamson a15f45
Adam Williamson a15f45
        self.user1 = pagure.lib.get_user(self.session, 'pingou')
Adam Williamson a15f45
        self.user2 = pagure.lib.get_user(self.session, 'foo')
Farhaan Bukhsh 940880
        self.project1 = pagure.lib._get_project(self.session, 'test')
Farhaan Bukhsh 940880
        self.project2 = pagure.lib._get_project(self.session, 'test2')
Farhaan Bukhsh 940880
        self.project3 = pagure.lib._get_project(self.session, 'test3', namespace='somenamespace')
Adam Williamson a15f45
Adam Williamson a15f45
        # Create a forked repo, should be project #4
Adam Williamson a15f45
        # Not using fork_project as it tries to do a git clone
Adam Williamson a15f45
        item = pagure.lib.model.Project(
Adam Williamson a15f45
            user_id=2,  # foo
Adam Williamson a15f45
            name='test',
Adam Williamson a15f45
            description='test project #1',
Adam Williamson a15f45
            is_fork=True,
Adam Williamson a15f45
            parent_id=1,
Adam Williamson a15f45
            hook_token='aaabbbyyy',
Adam Williamson a15f45
        )
Adam Williamson a15f45
        self.session.add(item)
Adam Williamson a15f45
        self.session.commit()
Farhaan Bukhsh 940880
        self.forkedproject = pagure.lib._get_project(self.session, 'test', user='foo')
Adam Williamson a15f45
Adam Williamson a15f45
        # Report an issue on project #1
Adam Williamson a15f45
        self.issue1 = pagure.lib.new_issue(
Adam Williamson a15f45
            session=self.session,
Adam Williamson a15f45
            repo=self.project1,
Adam Williamson a15f45
            title='issue',
Adam Williamson a15f45
            content='a bug report',
Adam Williamson a15f45
            user='pingou',
Adam Williamson a15f45
            ticketfolder=None,
Adam Williamson a15f45
        )
Adam Williamson a15f45
Adam Williamson a15f45
        # Add a comment on the issue
Adam Williamson a15f45
        pagure.lib.add_issue_comment(
Adam Williamson a15f45
            self.session,
Adam Williamson a15f45
            self.issue1,
Adam Williamson a15f45
            comment='Test comment',
Adam Williamson a15f45
            user='pingou',
Adam Williamson a15f45
            ticketfolder=None,
Adam Williamson a15f45
        )
Adam Williamson a15f45
        self.comment1 = pagure.lib.get_issue_comment(self.session, self.issue1.uid, 1)
Adam Williamson a15f45
Adam Williamson a15f45
        # Report an issue on project #3 (namespaced)
Adam Williamson a15f45
        self.issue2 = pagure.lib.new_issue(
Adam Williamson a15f45
            session=self.session,
Adam Williamson a15f45
            repo=self.project3,
Adam Williamson a15f45
            title='namespaced project issue',
Adam Williamson a15f45
            content='a bug report on a namespaced project',
Adam Williamson a15f45
            user='pingou',
Adam Williamson a15f45
            ticketfolder=None,
Adam Williamson a15f45
        )
Adam Williamson a15f45
Adam Williamson a15f45
        # report an issue on foo's fork of project #1
Adam Williamson a15f45
        self.issue3 = pagure.lib.new_issue(
Adam Williamson a15f45
            session=self.session,
Adam Williamson a15f45
            repo=self.forkedproject,
Adam Williamson a15f45
            title='forked project issue',
Adam Williamson a15f45
            content='a bug report on a forked project',
Adam Williamson a15f45
            user='pingou',
Adam Williamson a15f45
            ticketfolder=None,
Adam Williamson a15f45
        )
Adam Williamson a15f45
Adam Williamson a15f45
        patcher.stop()
Adam Williamson a15f45
Adam Williamson a15f45
    @mock.patch('pagure.lib.notify.send_email')
Adam Williamson a15f45
    def test_notify_new_comment(self, fakemail):
Adam Williamson a15f45
        """Simple test for notification about new comment."""
Adam Williamson a15f45
        exptext = u"""
Adam Williamson a15f45
pingou added a new comment to an issue you are following:
Adam Williamson a15f45
``
Adam Williamson a15f45
Test comment
Adam Williamson a15f45
``
Adam Williamson a15f45
Adam Williamson a15f45
To reply, visit the link below
Adam Williamson a15f45
https://pagure.org/test/issue/1
Adam Williamson a15f45
"""
Adam Williamson a15f45
        pagure.lib.notify.notify_new_comment(self.comment1)
Adam Williamson a15f45
        (_, args, kwargs) = fakemail.mock_calls[0]
Adam Williamson a15f45
Adam Williamson a15f45
        # Mail text should be as expected.
Adam Williamson a15f45
        self.assertEqual(args[0], exptext)
Adam Williamson a15f45
Adam Williamson a15f45
        # Mail subject should be as expected.
Martin Basti 099e80
        self.assertEqual(args[1], u'Issue #1: issue')
Adam Williamson a15f45
Adam Williamson a15f45
        # Mail should be sent to user #1.
Adam Williamson a15f45
        self.assertEqual(args[2], self.user1.default_email)
Adam Williamson a15f45
Adam Williamson a15f45
        # Mail ID should be comment #1's mail ID...
Adam Williamson a15f45
        self.assertEqual(kwargs['mail_id'], self.comment1.mail_id)
Adam Williamson a15f45
Adam Williamson a15f45
        # In reply to issue #1's mail ID.
Adam Williamson a15f45
        self.assertEqual(kwargs['in_reply_to'], self.issue1.mail_id)
Adam Williamson a15f45
Adam Williamson a15f45
        # Project name should be...project (full) name.
Adam Williamson a15f45
        self.assertEqual(kwargs['project_name'], self.project1.fullname)
Adam Williamson a15f45
Adam Williamson a15f45
        # Mail should be from user1 (who wrote the comment).
Adam Williamson a15f45
        self.assertEqual(kwargs['user_from'], self.user1.fullname)
Adam Williamson a15f45
Adam Williamson a15f45
    @mock.patch('pagure.lib.notify.send_email')
Adam Williamson a15f45
    def test_notify_new_issue_namespaced(self, fakemail):   # pylint: disable=invalid-name
Adam Williamson a15f45
        """Test for notifying of a new issue, namespaced project."""
Adam Williamson a15f45
        exptext = u"""
Adam Williamson a15f45
pingou reported a new issue against the project: `test3` that you are following:
Adam Williamson a15f45
``
Adam Williamson a15f45
a bug report on a namespaced project
Adam Williamson a15f45
``
Adam Williamson a15f45
Adam Williamson a15f45
To reply, visit the link below
Adam Williamson a15f45
https://pagure.org/somenamespace/test3/issue/1
Adam Williamson a15f45
"""
Adam Williamson a15f45
        pagure.lib.notify.notify_new_issue(self.issue2)
Adam Williamson a15f45
        (_, args, kwargs) = fakemail.mock_calls[0]
Adam Williamson a15f45
Adam Williamson a15f45
        # Mail text should be as expected.
Adam Williamson a15f45
        self.assertEqual(args[0], exptext)
Adam Williamson a15f45
Adam Williamson a15f45
        # Mail subject should be as expected.
Martin Basti 099e80
        self.assertEqual(args[1], u'Issue #1: namespaced project issue')
Adam Williamson a15f45
Adam Williamson a15f45
        # Mail should be sent to user #1.
Adam Williamson a15f45
        self.assertEqual(args[2], self.user1.default_email)
Adam Williamson a15f45
Adam Williamson a15f45
        # Mail ID should be issue's mail ID.
Adam Williamson a15f45
        self.assertEqual(kwargs['mail_id'], self.issue2.mail_id)
Adam Williamson a15f45
Adam Williamson a15f45
        # Project name should be...project (full) name.
Adam Williamson a15f45
        self.assertEqual(kwargs['project_name'], self.project3.fullname)
Adam Williamson a15f45
Adam Williamson a15f45
        # Mail should be from user1 (who submitted the issue).
Adam Williamson a15f45
        self.assertEqual(kwargs['user_from'], self.user1.fullname)
Adam Williamson a15f45
Adam Williamson a15f45
    @mock.patch('pagure.lib.notify.send_email')
Adam Williamson a15f45
    def test_notify_assigned_issue_forked(self, fakemail):  # pylint: disable=invalid-name
Adam Williamson a15f45
        """Test for notifying re-assignment of issue on forked project.
Adam Williamson a15f45
        'foo' reassigns issue on his fork of 'test' to 'pingou'.
Adam Williamson a15f45
        """
Adam Williamson a15f45
        exptext = u"""
Adam Williamson a15f45
The issue: `forked project issue` of project: `test` has been assigned to `pingou` by foo.
Adam Williamson a15f45
Adam Williamson a15f45
https://pagure.org/fork/foo/test/issue/1
Adam Williamson a15f45
"""
Adam Williamson a15f45
        pagure.lib.notify.notify_assigned_issue(self.issue3, self.user1, self.user2)
Adam Williamson a15f45
        (_, args, kwargs) = fakemail.mock_calls[0]
Adam Williamson a15f45
Adam Williamson a15f45
        # Mail text should be as expected.
Adam Williamson a15f45
        self.assertEqual(args[0], exptext)
Adam Williamson a15f45
Adam Williamson a15f45
        # Mail subject should be as expected.
Martin Basti 099e80
        self.assertEqual(args[1], u'Issue #1: forked project issue')
Adam Williamson a15f45
Adam Williamson a15f45
        # Mail should be sent to user #1.
Adam Williamson a15f45
        # NOTE: Not sent to user #2...
Adam Williamson a15f45
        self.assertEqual(args[2], self.user1.default_email)
Adam Williamson a15f45
Adam Williamson a15f45
        # Mail ID should contain issue's mail ID and '/assigned/'
Adam Williamson a15f45
        self.assertIn("{0}/assigned/".format(self.issue3.mail_id), kwargs['mail_id'])
Adam Williamson a15f45
Adam Williamson a15f45
        # Project name should be...project (full) name.
Adam Williamson a15f45
        self.assertEqual(kwargs['project_name'], self.forkedproject.fullname)
Adam Williamson a15f45
Adam Williamson a15f45
        # Mail should be from user1 (who submitted the issue).
Adam Williamson a15f45
        self.assertEqual(kwargs['user_from'], self.user2.fullname)
Adam Williamson a15f45
Adam Williamson a15f45
# Add more tests to verify that correct mails are sent to correct people here
Adam Williamson a15f45
Adam Williamson a15f45
if __name__ == '__main__':
Pierre-Yves Chibon 393f31
    unittest.main(verbosity=2)