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
Aurélien Bompard 626417
from __future__ import unicode_literals
Aurélien Bompard 626417
Adam Williamson a15f45
import unittest
Adam Williamson a15f45
import sys
Adam Williamson a15f45
import os
Adam Williamson a15f45
Adam Williamson a15f45
import mock
Aurélien Bompard 626417
import six
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."""
Aurélien Bompard 626417
        exptext = """
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
Neal Gompa 9c34c4
http://localhost.localdomain/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)
Aurélien Bompard 626417
        self.assertTrue(isinstance(args[0], six.text_type))
Adam Williamson a15f45
Adam Williamson a15f45
        # Mail subject should be as expected.
Aurélien Bompard 626417
        self.assertEqual(args[1], '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."""
Aurélien Bompard 626417
        exptext = """
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
Neal Gompa 9c34c4
http://localhost.localdomain/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)
Aurélien Bompard 626417
        self.assertTrue(isinstance(args[0], six.text_type))
Adam Williamson a15f45
Adam Williamson a15f45
        # Mail subject should be as expected.
Aurélien Bompard 626417
        self.assertEqual(args[1], '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
        """
Aurélien Bompard 626417
        exptext = """
Adam Williamson a15f45
The issue: `forked project issue` of project: `test` has been assigned to `pingou` by foo.
Adam Williamson a15f45
Neal Gompa 9c34c4
http://localhost.localdomain/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)
Aurélien Bompard 626417
        self.assertTrue(isinstance(args[0], six.text_type))
Adam Williamson a15f45
Adam Williamson a15f45
        # Mail subject should be as expected.
Aurélien Bompard 626417
        self.assertEqual(args[1], '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 a8fef8
    @mock.patch('pagure.lib.notify.send_email')
Adam Williamson a8fef8
    # for non-ASCII testing, we mock these return values
Adam Williamson a8fef8
    @mock.patch('pagure.lib.git.get_author', return_value="Cecil Cõmmîttër")
Adam Williamson a8fef8
    @mock.patch('pagure.lib.git.get_commit_subject', return_value="We love Motörhead")
Adam Williamson a8fef8
    def test_notify_new_commits(self, _, __, fakemail):  # pylint: disable=invalid-name
Adam Williamson a8fef8
        """Test for notification on new commits, especially when
Adam Williamson a8fef8
        non-ASCII text is involved.
Adam Williamson a8fef8
        """
Aurélien Bompard 626417
        exptext = """
Adam Williamson a8fef8
The following commits were pushed to the repo test on branch
Adam Williamson a8fef8
master, which you are following:
Adam Williamson a8fef8
abcdefg    Cecil Cõmmîttër    We love Motörhead
Adam Williamson a8fef8
Adam Williamson a8fef8
Adam Williamson a8fef8
Adam Williamson a8fef8
To view more about the commits, visit:
Neal Gompa 9c34c4
http://localhost.localdomain/test/commits/master
Adam Williamson a8fef8
"""
Adam Williamson a8fef8
        # first arg (abspath) doesn't matter and we can use a commit
Adam Williamson a8fef8
        # ID that doesn't actually exist, as we are mocking
Adam Williamson a8fef8
        # the get_author and get_commit_subject calls anyway
Adam Williamson a8fef8
        pagure.lib.notify.notify_new_commits('/', self.project1, 'master', ['abcdefg'])
Adam Williamson a8fef8
        (_, args, kwargs) = fakemail.mock_calls[0]
Adam Williamson a8fef8
Adam Williamson a8fef8
        # Mail text should be as expected.
Adam Williamson a8fef8
        self.assertEqual(args[0], exptext)
Aurélien Bompard 626417
        self.assertTrue(isinstance(args[0], six.text_type))
Adam Williamson a8fef8
Adam Williamson a8fef8
        # Mail subject should be as expected.
Adam Williamson a8fef8
        self.assertEqual(args[1], u'New Commits To "test" (master)')
Adam Williamson a8fef8
Adam Williamson a8fef8
        # Mail doesn't actually get sent to anyone by default
Adam Williamson a8fef8
        self.assertEqual(args[2], '')
Adam Williamson a8fef8
Adam Williamson a8fef8
        # Project name should be...project (full) name.
Adam Williamson a8fef8
        self.assertEqual(kwargs['project_name'], self.project1.fullname)
Adam Williamson a8fef8
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)