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
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
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
Pierre-Yves Chibon 73d120
sys.path.insert(
Pierre-Yves Chibon 73d120
    0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
Pierre-Yves Chibon 73d120
)
Adam Williamson a15f45
Pierre-Yves Chibon 73d120
import pagure.lib.query  # pylint: disable=wrong-import-position
Pierre-Yves Chibon 73d120
import pagure.lib.model  # pylint: disable=wrong-import-position
Pierre-Yves Chibon 73d120
import pagure.lib.notify  # pylint: disable=wrong-import-position
Pierre-Yves Chibon 73d120
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
Pierre-Yves Chibon 73d120
        patcher = mock.patch("pagure.lib.notify.send_email")
Adam Williamson a15f45
        patcher.start()
Adam Williamson a15f45
Pierre-Yves Chibon 73d120
        self.user1 = pagure.lib.query.get_user(self.session, "pingou")
Pierre-Yves Chibon 73d120
        self.user2 = pagure.lib.query.get_user(self.session, "foo")
Pierre-Yves Chibon 73d120
        self.project1 = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon 73d120
        self.project2 = pagure.lib.query._get_project(self.session, "test2")
Pierre-Yves Chibon 73d120
        self.project3 = pagure.lib.query._get_project(
Pierre-Yves Chibon 73d120
            self.session, "test3", namespace="somenamespace"
Pierre-Yves Chibon 73d120
        )
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
Pierre-Yves Chibon 73d120
            name="test",
Pierre-Yves Chibon 73d120
            description="test project #1",
Adam Williamson a15f45
            is_fork=True,
Adam Williamson a15f45
            parent_id=1,
Pierre-Yves Chibon 73d120
            hook_token="aaabbbyyy",
Adam Williamson a15f45
        )
Adam Williamson a15f45
        self.session.add(item)
Adam Williamson a15f45
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.forkedproject = pagure.lib.query._get_project(
Pierre-Yves Chibon 73d120
            self.session, "test", user="foo"
Pierre-Yves Chibon 73d120
        )
Adam Williamson a15f45
Adam Williamson a15f45
        # Report an issue on project #1
Pierre-Yves Chibon 930073
        self.issue1 = pagure.lib.query.new_issue(
Adam Williamson a15f45
            session=self.session,
Adam Williamson a15f45
            repo=self.project1,
Pierre-Yves Chibon 73d120
            title="issue",
Pierre-Yves Chibon 73d120
            content="a bug report",
Pierre-Yves Chibon 73d120
            user="pingou",
Adam Williamson a15f45
        )
Adam Williamson a15f45
Adam Williamson a15f45
        # Add a comment on the issue
Pierre-Yves Chibon 930073
        pagure.lib.query.add_issue_comment(
Pierre-Yves Chibon 73d120
            self.session, self.issue1, comment="Test comment", user="pingou"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        self.comment1 = pagure.lib.query.get_issue_comment(
Pierre-Yves Chibon 73d120
            self.session, self.issue1.uid, 1
Adam Williamson a15f45
        )
Adam Williamson a15f45
Adam Williamson a15f45
        # Report an issue on project #3 (namespaced)
Pierre-Yves Chibon 930073
        self.issue2 = pagure.lib.query.new_issue(
Adam Williamson a15f45
            session=self.session,
Adam Williamson a15f45
            repo=self.project3,
Pierre-Yves Chibon 73d120
            title="namespaced project issue",
Pierre-Yves Chibon 73d120
            content="a bug report on a namespaced project",
Pierre-Yves Chibon 73d120
            user="pingou",
Adam Williamson a15f45
        )
Adam Williamson a15f45
Adam Williamson a15f45
        # report an issue on foo's fork of project #1
Pierre-Yves Chibon 930073
        self.issue3 = pagure.lib.query.new_issue(
Adam Williamson a15f45
            session=self.session,
Adam Williamson a15f45
            repo=self.forkedproject,
Pierre-Yves Chibon 73d120
            title="forked project issue",
Pierre-Yves Chibon 73d120
            content="a bug report on a forked project",
Pierre-Yves Chibon 73d120
            user="pingou",
Adam Williamson a15f45
        )
Adam Williamson a15f45
Adam Williamson a15f45
        patcher.stop()
Adam Williamson a15f45
Pierre-Yves Chibon 73d120
    @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.
Pierre-Yves Chibon 73d120
        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...
Pierre-Yves Chibon 73d120
        self.assertEqual(kwargs["mail_id"], self.comment1.mail_id)
Adam Williamson a15f45
Adam Williamson a15f45
        # In reply to issue #1's mail ID.
Pierre-Yves Chibon 73d120
        self.assertEqual(kwargs["in_reply_to"], self.issue1.mail_id)
Adam Williamson a15f45
Adam Williamson a15f45
        # Project name should be...project (full) name.
Pierre-Yves Chibon 73d120
        self.assertEqual(kwargs["project_name"], self.project1.fullname)
Adam Williamson a15f45
Adam Williamson a15f45
        # Mail should be from user1 (who wrote the comment).
Pierre-Yves Chibon 73d120
        self.assertEqual(kwargs["user_from"], self.user1.fullname)
Adam Williamson a15f45
Pierre-Yves Chibon 73d120
    @mock.patch("pagure.lib.notify.send_email")
Pierre-Yves Chibon 73d120
    def test_notify_new_issue_namespaced(
Pierre-Yves Chibon 73d120
        self, fakemail
Pierre-Yves Chibon 73d120
    ):  # 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.
Pierre-Yves Chibon 73d120
        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.
Pierre-Yves Chibon 73d120
        self.assertEqual(kwargs["mail_id"], self.issue2.mail_id)
Adam Williamson a15f45
Adam Williamson a15f45
        # Project name should be...project (full) name.
Pierre-Yves Chibon 73d120
        self.assertEqual(kwargs["project_name"], self.project3.fullname)
Adam Williamson a15f45
Adam Williamson a15f45
        # Mail should be from user1 (who submitted the issue).
Pierre-Yves Chibon 73d120
        self.assertEqual(kwargs["user_from"], self.user1.fullname)
Adam Williamson a15f45
Pierre-Yves Chibon 73d120
    @mock.patch("pagure.lib.notify.send_email")
Pierre-Yves Chibon 73d120
    def test_notify_assigned_issue_forked(
Pierre-Yves Chibon 73d120
        self, fakemail
Pierre-Yves Chibon 73d120
    ):  # 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
"""
Pierre-Yves Chibon 73d120
        pagure.lib.notify.notify_assigned_issue(
Pierre-Yves Chibon 73d120
            self.issue3, self.user1, self.user2
Pierre-Yves Chibon 73d120
        )
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.
Pierre-Yves Chibon 73d120
        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/'
Pierre-Yves Chibon 73d120
        self.assertIn(
Pierre-Yves Chibon 73d120
            "{0}/assigned/".format(self.issue3.mail_id), kwargs["mail_id"]
Pierre-Yves Chibon 73d120
        )
Adam Williamson a15f45
Adam Williamson a15f45
        # Project name should be...project (full) name.
Pierre-Yves Chibon 73d120
        self.assertEqual(kwargs["project_name"], self.forkedproject.fullname)
Adam Williamson a15f45
Adam Williamson a15f45
        # Mail should be from user1 (who submitted the issue).
Pierre-Yves Chibon 73d120
        self.assertEqual(kwargs["user_from"], self.user2.fullname)
Adam Williamson a15f45
Pierre-Yves Chibon 73d120
    @mock.patch("pagure.lib.notify.send_email")
Adam Williamson a8fef8
    # for non-ASCII testing, we mock these return values
Pierre-Yves Chibon 73d120
    @mock.patch("pagure.lib.git.get_author", return_value="Cecil Cõmmîttër")
Pierre-Yves Chibon 73d120
    @mock.patch(
Pierre-Yves Chibon 73d120
        "pagure.lib.git.get_commit_subject", return_value="We love Motörhead"
Pierre-Yves Chibon 73d120
    )
Pierre-Yves Chibon 73d120
    def test_notify_new_commits(
Pierre-Yves Chibon 73d120
        self, _, __, fakemail
Pierre-Yves Chibon 73d120
    ):  # 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
Pierre-Yves Chibon 73d120
        pagure.lib.notify.notify_new_commits(
Pierre-Yves Chibon 73d120
            "/", self.project1, "master", ["abcdefg"]
Pierre-Yves Chibon 73d120
        )
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.
Pierre-Yves Chibon 73d120
        self.assertEqual(args[1], 'New Commits To "test" (master)')
Adam Williamson a8fef8
Adam Williamson a8fef8
        # Mail doesn't actually get sent to anyone by default
Pierre-Yves Chibon 73d120
        self.assertEqual(args[2], "")
Adam Williamson a8fef8
Adam Williamson a8fef8
        # Project name should be...project (full) name.
Pierre-Yves Chibon 73d120
        self.assertEqual(kwargs["project_name"], self.project1.fullname)
Pierre-Yves Chibon 73d120
Adam Williamson a8fef8
Adam Williamson a15f45
# Add more tests to verify that correct mails are sent to correct people here
Adam Williamson a15f45
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon 393f31
    unittest.main(verbosity=2)