Blame tests/test_pagure_lib_notify.py

Pierre-Yves Chibon fd2f9e
# -*- coding: utf-8 -*-
Pierre-Yves Chibon fd2f9e
Pierre-Yves Chibon fd2f9e
"""
Pierre-Yves Chibon fd2f9e
 (c) 2016 - Copyright Red Hat Inc
Pierre-Yves Chibon fd2f9e
Pierre-Yves Chibon fd2f9e
 Authors:
Pierre-Yves Chibon fd2f9e
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon fd2f9e
Pierre-Yves Chibon fd2f9e
"""
Pierre-Yves Chibon fd2f9e
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard 626417
Pierre-Yves Chibon fd2f9e
import unittest
Pierre-Yves Chibon fd2f9e
import shutil
Pierre-Yves Chibon fd2f9e
import sys
Pierre-Yves Chibon fd2f9e
import os
Pierre-Yves Chibon fd2f9e
Pierre-Yves Chibon 33ff8d
from mock import patch, MagicMock
Pierre-Yves Chibon fd2f9e
Pierre-Yves Chibon fd2f9e
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon fd2f9e
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon fd2f9e
Pierre-Yves Chibon fd2f9e
import pagure.lib.model
Pierre-Yves Chibon fd2f9e
import pagure.lib.notify
Pierre-Yves Chibon 930073
import pagure.lib.query
Pierre-Yves Chibon fd2f9e
import tests
Pierre-Yves Chibon fd2f9e
Pierre-Yves Chibon fd2f9e
Pierre-Yves Chibon fd2f9e
class PagureLibNotifytests(tests.Modeltests):
Pierre-Yves Chibon fd2f9e
    """ Tests for pagure.lib.notify """
Pierre-Yves Chibon fd2f9e
Aurélien Bompard 626417
    maxDiff = None
Aurélien Bompard 626417
Pierre-Yves Chibon fd2f9e
    def test_get_emails_for_obj_issue(self):
Pierre-Yves Chibon fd2f9e
        """ Test the _get_emails_for_obj method from pagure.lib.notify. """
Pierre-Yves Chibon fd2f9e
Pierre-Yves Chibon fd2f9e
        # Create the project ns/test
Pierre-Yves Chibon fd2f9e
        item = pagure.lib.model.Project(
Pierre-Yves Chibon fd2f9e
            user_id=1,  # pingou
Pierre-Yves Chibon fd2f9e
            name='test3',
Pierre-Yves Chibon fd2f9e
            namespace='ns',
Pierre-Yves Chibon fd2f9e
            description='test project #1',
Pierre-Yves Chibon fd2f9e
            hook_token='aaabbbcccdd',
Pierre-Yves Chibon fd2f9e
        )
Pierre-Yves Chibon fd2f9e
        item.close_status = ['Invalid', 'Insufficient data', 'Fixed']
Pierre-Yves Chibon fd2f9e
        self.session.add(item)
Pierre-Yves Chibon fd2f9e
        self.session.commit()
Pierre-Yves Chibon fd2f9e
Pierre-Yves Chibon fd2f9e
        # Create the ticket
Pierre-Yves Chibon 930073
        iss = pagure.lib.query.new_issue(
Pierre-Yves Chibon fd2f9e
            issue_id=4,
Pierre-Yves Chibon fd2f9e
            session=self.session,
Pierre-Yves Chibon fd2f9e
            repo=item,
Pierre-Yves Chibon fd2f9e
            title='test issue',
Pierre-Yves Chibon fd2f9e
            content='content test issue',
Pierre-Yves Chibon fd2f9e
            user='pingou',
Pierre-Yves Chibon fd2f9e
        )
Pierre-Yves Chibon fd2f9e
        self.session.commit()
Pierre-Yves Chibon fd2f9e
        self.assertEqual(iss.id, 4)
Pierre-Yves Chibon fd2f9e
        self.assertEqual(iss.title, 'test issue')
Pierre-Yves Chibon fd2f9e
Pierre-Yves Chibon fd2f9e
        exp = set(['bar@pingou.com'])
Pierre-Yves Chibon fd2f9e
        out = pagure.lib.notify._get_emails_for_obj(iss)
Pierre-Yves Chibon fd2f9e
        self.assertEqual(out, exp)
Pierre-Yves Chibon fd2f9e
Pierre-Yves Chibon fd2f9e
        # Comment on the ticket
Pierre-Yves Chibon 930073
        out = pagure.lib.query.add_issue_comment(
Pierre-Yves Chibon fd2f9e
            self.session,
Pierre-Yves Chibon fd2f9e
            issue=iss,
Pierre-Yves Chibon fd2f9e
            comment='This is a comment',
Pierre-Yves Chibon fd2f9e
            user='foo',
Pierre-Yves Chibon fd2f9e
            notify=False)
Pierre-Yves Chibon fd2f9e
        self.assertEqual(out, 'Comment added')
Pierre-Yves Chibon fd2f9e
Pierre-Yves Chibon fd2f9e
        exp = set(['bar@pingou.com', 'foo@bar.com'])
Pierre-Yves Chibon fd2f9e
        out = pagure.lib.notify._get_emails_for_obj(iss)
Pierre-Yves Chibon fd2f9e
        self.assertEqual(out, exp)
Pierre-Yves Chibon fd2f9e
Pierre-Yves Chibon fd2f9e
        # Create user `bar`
Pierre-Yves Chibon fd2f9e
        item = pagure.lib.model.User(
Pierre-Yves Chibon fd2f9e
            user='bar',
Pierre-Yves Chibon fd2f9e
            fullname='bar name',
Pierre-Yves Chibon fd2f9e
            password='bar',
Pierre-Yves Chibon fd2f9e
            default_email='bar@bar.com',
Pierre-Yves Chibon fd2f9e
        )
Pierre-Yves Chibon fd2f9e
        self.session.add(item)
Pierre-Yves Chibon fd2f9e
        item = pagure.lib.model.UserEmail(
Pierre-Yves Chibon fd2f9e
            user_id=3,
Pierre-Yves Chibon fd2f9e
            email='bar@bar.com')
Pierre-Yves Chibon fd2f9e
        self.session.add(item)
Pierre-Yves Chibon fd2f9e
        self.session.commit()
Pierre-Yves Chibon fd2f9e
Pierre-Yves Chibon fd2f9e
        # Watch the ticket
Pierre-Yves Chibon 930073
        out = pagure.lib.query.set_watch_obj(self.session, 'bar', iss, True)
Pierre-Yves Chibon fd2f9e
        self.assertEqual(out, 'You are now watching this issue')
Pierre-Yves Chibon fd2f9e
Pierre-Yves Chibon fd2f9e
        exp = set(['bar@pingou.com', 'foo@bar.com', 'bar@bar.com'])
Pierre-Yves Chibon fd2f9e
        out = pagure.lib.notify._get_emails_for_obj(iss)
Pierre-Yves Chibon fd2f9e
        self.assertEqual(out, exp)
Pierre-Yves Chibon fd2f9e
Pierre-Yves Chibon c8242b
    def test_get_emails_for_obj_issue_watching_project(self):
Pierre-Yves Chibon c8242b
        """ Test the _get_emails_for_obj method from pagure.lib.notify. """
Pierre-Yves Chibon c8242b
Pierre-Yves Chibon c8242b
        # Create the project ns/test
Pierre-Yves Chibon c8242b
        item = pagure.lib.model.Project(
Pierre-Yves Chibon c8242b
            user_id=1,  # pingou
Pierre-Yves Chibon c8242b
            name='test3',
Pierre-Yves Chibon c8242b
            namespace='ns',
Pierre-Yves Chibon c8242b
            description='test project #1',
Pierre-Yves Chibon c8242b
            hook_token='aaabbbcccdd',
Pierre-Yves Chibon c8242b
        )
Pierre-Yves Chibon c8242b
        item.close_status = ['Invalid', 'Insufficient data', 'Fixed']
Pierre-Yves Chibon c8242b
        self.session.add(item)
Pierre-Yves Chibon c8242b
        self.session.commit()
Pierre-Yves Chibon c8242b
Pierre-Yves Chibon c8242b
        # Create the ticket
Pierre-Yves Chibon 930073
        iss = pagure.lib.query.new_issue(
Pierre-Yves Chibon c8242b
            issue_id=4,
Pierre-Yves Chibon c8242b
            session=self.session,
Pierre-Yves Chibon c8242b
            repo=item,
Pierre-Yves Chibon c8242b
            title='test issue',
Pierre-Yves Chibon c8242b
            content='content test issue',
Pierre-Yves Chibon c8242b
            user='pingou',
Pierre-Yves Chibon c8242b
        )
Pierre-Yves Chibon c8242b
        self.session.commit()
Pierre-Yves Chibon c8242b
        self.assertEqual(iss.id, 4)
Pierre-Yves Chibon c8242b
        self.assertEqual(iss.title, 'test issue')
Pierre-Yves Chibon c8242b
Pierre-Yves Chibon c8242b
        exp = set(['bar@pingou.com'])
Pierre-Yves Chibon c8242b
        out = pagure.lib.notify._get_emails_for_obj(iss)
Pierre-Yves Chibon c8242b
        self.assertEqual(out, exp)
Pierre-Yves Chibon c8242b
Pierre-Yves Chibon c8242b
        # Comment on the ticket
Pierre-Yves Chibon 930073
        out = pagure.lib.query.add_issue_comment(
Pierre-Yves Chibon c8242b
            self.session,
Pierre-Yves Chibon c8242b
            issue=iss,
Pierre-Yves Chibon c8242b
            comment='This is a comment',
Pierre-Yves Chibon c8242b
            user='foo',
Pierre-Yves Chibon c8242b
            notify=False)
Pierre-Yves Chibon c8242b
        self.assertEqual(out, 'Comment added')
Pierre-Yves Chibon c8242b
Pierre-Yves Chibon c8242b
        exp = set(['bar@pingou.com', 'foo@bar.com'])
Pierre-Yves Chibon c8242b
        out = pagure.lib.notify._get_emails_for_obj(iss)
Pierre-Yves Chibon c8242b
        self.assertEqual(out, exp)
Pierre-Yves Chibon c8242b
Pierre-Yves Chibon c8242b
        # Create user `bar`
Pierre-Yves Chibon c8242b
        item = pagure.lib.model.User(
Pierre-Yves Chibon c8242b
            user='bar',
Pierre-Yves Chibon c8242b
            fullname='bar name',
Pierre-Yves Chibon c8242b
            password='bar',
Pierre-Yves Chibon c8242b
            default_email='bar@bar.com',
Pierre-Yves Chibon c8242b
        )
Pierre-Yves Chibon c8242b
        self.session.add(item)
Pierre-Yves Chibon c8242b
        item = pagure.lib.model.UserEmail(
Pierre-Yves Chibon c8242b
            user_id=3,
Pierre-Yves Chibon c8242b
            email='bar@bar.com')
Pierre-Yves Chibon c8242b
        self.session.add(item)
Pierre-Yves Chibon c8242b
        self.session.commit()
Pierre-Yves Chibon c8242b
Pierre-Yves Chibon c8242b
        # Watch the project
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test3', namespace='ns')
Pierre-Yves Chibon 930073
        out = pagure.lib.query.update_watch_status(self.session, repo, 'bar', '1')
Matt Prahl b49f93
        self.assertEqual(
Pierre-Yves Chibon d7ac66
            out, 'You are now watching issues and PRs on this project')
Pierre-Yves Chibon c8242b
Pierre-Yves Chibon c8242b
        exp = set(['bar@pingou.com', 'foo@bar.com', 'bar@bar.com'])
Pierre-Yves Chibon c8242b
        out = pagure.lib.notify._get_emails_for_obj(iss)
Pierre-Yves Chibon c8242b
        self.assertEqual(out, exp)
Pierre-Yves Chibon c8242b
Pierre-Yves Chibon 33ff8d
    @patch('pagure.lib.notify.smtplib.SMTP')
Pierre-Yves Chibon 33ff8d
    def test_get_emails_for_obj_pr(self, mock_smtp):
Pierre-Yves Chibon fd2f9e
        """ Test the _get_emails_for_obj method from pagure.lib.notify. """
Pierre-Yves Chibon 33ff8d
        mock_smtp.return_value = MagicMock()
Pierre-Yves Chibon 33ff8d
Pierre-Yves Chibon fd2f9e
        tests.create_projects(self.session)
Pierre-Yves Chibon fd2f9e
Pierre-Yves Chibon fd2f9e
        # Create the project ns/test
Pierre-Yves Chibon fd2f9e
        item = pagure.lib.model.Project(
Pierre-Yves Chibon fd2f9e
            user_id=1,  # pingou
Pierre-Yves Chibon fd2f9e
            name='test3',
Pierre-Yves Chibon fd2f9e
            namespace='ns',
Pierre-Yves Chibon fd2f9e
            description='test project #1',
Pierre-Yves Chibon fd2f9e
            hook_token='aaabbbcccdd',
Pierre-Yves Chibon fd2f9e
        )
Pierre-Yves Chibon fd2f9e
        item.close_status = ['Invalid', 'Insufficient data', 'Fixed']
Pierre-Yves Chibon fd2f9e
        self.session.add(item)
Pierre-Yves Chibon fd2f9e
        self.session.commit()
Pierre-Yves Chibon fd2f9e
Pierre-Yves Chibon fd2f9e
        # Create the PR
Pierre-Yves Chibon 930073
        repo = pagure.lib.query._get_project(self.session, 'test')
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon fd2f9e
            session=self.session,
Pierre-Yves Chibon fd2f9e
            repo_from=repo,
Pierre-Yves Chibon fd2f9e
            branch_from='master',
Pierre-Yves Chibon fd2f9e
            repo_to=repo,
Pierre-Yves Chibon fd2f9e
            branch_to='master',
Pierre-Yves Chibon fd2f9e
            title='test pull-request',
Pierre-Yves Chibon fd2f9e
            user='pingou',
Pierre-Yves Chibon fd2f9e
        )
Pierre-Yves Chibon fd2f9e
        self.session.commit()
Pierre-Yves Chibon fd2f9e
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon fd2f9e
        self.assertEqual(req.title, 'test pull-request')
Pierre-Yves Chibon fd2f9e
        self.assertEqual(repo.open_requests, 1)
Pierre-Yves Chibon fd2f9e
Pierre-Yves Chibon fd2f9e
        exp = set(['bar@pingou.com'])
Pierre-Yves Chibon fd2f9e
        out = pagure.lib.notify._get_emails_for_obj(req)
Pierre-Yves Chibon fd2f9e
        self.assertEqual(out, exp)
Pierre-Yves Chibon fd2f9e
Pierre-Yves Chibon fd2f9e
        # Comment on the ticket
Pierre-Yves Chibon 930073
        out = pagure.lib.query.add_pull_request_comment(
Pierre-Yves Chibon fd2f9e
            self.session,
Pierre-Yves Chibon fd2f9e
            request=req,
Pierre-Yves Chibon fd2f9e
            commit=None,
Pierre-Yves Chibon fd2f9e
            tree_id=None,
Pierre-Yves Chibon fd2f9e
            filename=None,
Pierre-Yves Chibon fd2f9e
            row=None,
Pierre-Yves Chibon fd2f9e
            comment='This is a comment',
Pierre-Yves Chibon fd2f9e
            user='foo',
Pierre-Yves Chibon fd2f9e
            notify=False)
Pierre-Yves Chibon fd2f9e
        self.assertEqual(out, 'Comment added')
Pierre-Yves Chibon fd2f9e
Pierre-Yves Chibon fd2f9e
        exp = set(['bar@pingou.com', 'foo@bar.com'])
Pierre-Yves Chibon fd2f9e
        out = pagure.lib.notify._get_emails_for_obj(req)
Pierre-Yves Chibon fd2f9e
        self.assertEqual(out, exp)
Pierre-Yves Chibon fd2f9e
Pierre-Yves Chibon fd2f9e
        # Create user `bar`
Pierre-Yves Chibon fd2f9e
        item = pagure.lib.model.User(
Pierre-Yves Chibon fd2f9e
            user='bar',
Pierre-Yves Chibon fd2f9e
            fullname='bar name',
Pierre-Yves Chibon fd2f9e
            password='bar',
Pierre-Yves Chibon fd2f9e
            default_email='bar@bar.com',
Pierre-Yves Chibon fd2f9e
        )
Pierre-Yves Chibon fd2f9e
        self.session.add(item)
Pierre-Yves Chibon fd2f9e
        item = pagure.lib.model.UserEmail(
Pierre-Yves Chibon fd2f9e
            user_id=3,
Pierre-Yves Chibon fd2f9e
            email='bar@bar.com')
Pierre-Yves Chibon fd2f9e
        self.session.add(item)
Pierre-Yves Chibon fd2f9e
        self.session.commit()
Pierre-Yves Chibon fd2f9e
Pierre-Yves Chibon c8242b
        # Watch the pull-request
Pierre-Yves Chibon 930073
        out = pagure.lib.query.set_watch_obj(self.session, 'bar', req, True)
Pierre-Yves Chibon fd2f9e
        self.assertEqual(out, 'You are now watching this pull-request')
Pierre-Yves Chibon fd2f9e
Pierre-Yves Chibon fd2f9e
        exp = set(['bar@pingou.com', 'foo@bar.com', 'bar@bar.com'])
Pierre-Yves Chibon fd2f9e
        out = pagure.lib.notify._get_emails_for_obj(req)
Pierre-Yves Chibon fd2f9e
        self.assertEqual(out, exp)
Pierre-Yves Chibon c8242b
Pierre-Yves Chibon 33ff8d
    @patch('pagure.lib.notify.smtplib.SMTP')
Pierre-Yves Chibon 33ff8d
    def test_get_emails_for_obj_pr_watching_project(self, mock_smtp):
Pierre-Yves Chibon c8242b
        """ Test the _get_emails_for_obj method from pagure.lib.notify. """
Pierre-Yves Chibon 33ff8d
        mock_smtp.return_value = MagicMock()
Pierre-Yves Chibon 33ff8d
Pierre-Yves Chibon c8242b
        tests.create_projects(self.session)
Pierre-Yves Chibon c8242b
Pierre-Yves Chibon c8242b
        # Create the project ns/test
Pierre-Yves Chibon c8242b
        item = pagure.lib.model.Project(
Pierre-Yves Chibon c8242b
            user_id=1,  # pingou
Pierre-Yves Chibon c8242b
            name='test3',
Pierre-Yves Chibon c8242b
            namespace='ns',
Pierre-Yves Chibon c8242b
            description='test project #1',
Pierre-Yves Chibon c8242b
            hook_token='aaabbbcccdd',
Pierre-Yves Chibon c8242b
        )
Pierre-Yves Chibon c8242b
        item.close_status = ['Invalid', 'Insufficient data', 'Fixed']
Pierre-Yves Chibon c8242b
        self.session.add(item)
Pierre-Yves Chibon c8242b
        self.session.commit()
Pierre-Yves Chibon c8242b
Pierre-Yves Chibon c8242b
        # Create the PR
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon c8242b
            session=self.session,
Pierre-Yves Chibon c8242b
            repo_from=repo,
Pierre-Yves Chibon c8242b
            branch_from='master',
Pierre-Yves Chibon c8242b
            repo_to=repo,
Pierre-Yves Chibon c8242b
            branch_to='master',
Pierre-Yves Chibon c8242b
            title='test pull-request',
Pierre-Yves Chibon c8242b
            user='pingou',
Pierre-Yves Chibon c8242b
        )
Pierre-Yves Chibon c8242b
        self.session.commit()
Pierre-Yves Chibon c8242b
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon c8242b
        self.assertEqual(req.title, 'test pull-request')
Pierre-Yves Chibon c8242b
        self.assertEqual(repo.open_requests, 1)
Pierre-Yves Chibon c8242b
Pierre-Yves Chibon c8242b
        exp = set(['bar@pingou.com'])
Pierre-Yves Chibon c8242b
        out = pagure.lib.notify._get_emails_for_obj(req)
Pierre-Yves Chibon c8242b
        self.assertEqual(out, exp)
Pierre-Yves Chibon c8242b
Pierre-Yves Chibon c8242b
        # Comment on the ticket
Pierre-Yves Chibon 930073
        out = pagure.lib.query.add_pull_request_comment(
Pierre-Yves Chibon c8242b
            self.session,
Pierre-Yves Chibon c8242b
            request=req,
Pierre-Yves Chibon c8242b
            commit=None,
Pierre-Yves Chibon c8242b
            tree_id=None,
Pierre-Yves Chibon c8242b
            filename=None,
Pierre-Yves Chibon c8242b
            row=None,
Pierre-Yves Chibon c8242b
            comment='This is a comment',
Pierre-Yves Chibon c8242b
            user='foo',
Pierre-Yves Chibon c8242b
            notify=False)
Pierre-Yves Chibon c8242b
        self.assertEqual(out, 'Comment added')
Pierre-Yves Chibon c8242b
Pierre-Yves Chibon c8242b
        exp = set(['bar@pingou.com', 'foo@bar.com'])
Pierre-Yves Chibon c8242b
        out = pagure.lib.notify._get_emails_for_obj(req)
Pierre-Yves Chibon c8242b
        self.assertEqual(out, exp)
Pierre-Yves Chibon c8242b
Pierre-Yves Chibon c8242b
        # Create user `bar`
Pierre-Yves Chibon c8242b
        item = pagure.lib.model.User(
Pierre-Yves Chibon c8242b
            user='bar',
Pierre-Yves Chibon c8242b
            fullname='bar name',
Pierre-Yves Chibon c8242b
            password='bar',
Pierre-Yves Chibon c8242b
            default_email='bar@bar.com',
Pierre-Yves Chibon c8242b
        )
Pierre-Yves Chibon c8242b
        self.session.add(item)
Pierre-Yves Chibon c8242b
        item = pagure.lib.model.UserEmail(
Pierre-Yves Chibon c8242b
            user_id=3,
Pierre-Yves Chibon c8242b
            email='bar@bar.com')
Pierre-Yves Chibon c8242b
        self.session.add(item)
Pierre-Yves Chibon c8242b
        self.session.commit()
Pierre-Yves Chibon c8242b
Pierre-Yves Chibon c8242b
        # Watch the project
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        out = pagure.lib.query.update_watch_status(self.session, repo, 'bar', '1')
Matt Prahl b49f93
        self.assertEqual(
Pierre-Yves Chibon d7ac66
            out, 'You are now watching issues and PRs on this project')
Pierre-Yves Chibon c8242b
Pierre-Yves Chibon c8242b
        exp = set(['bar@pingou.com', 'foo@bar.com', 'bar@bar.com'])
Pierre-Yves Chibon c8242b
        out = pagure.lib.notify._get_emails_for_obj(req)
Pierre-Yves Chibon c8242b
        self.assertEqual(out, exp)
Pierre-Yves Chibon fd2f9e
Pierre-Yves Chibon b124e7
    def test_get_emails_for_obj_private_issue(self):
Pierre-Yves Chibon b124e7
        """ Test the _get_emails_for_obj method from pagure.lib.notify. """
Pierre-Yves Chibon b124e7
Pierre-Yves Chibon b124e7
        # Create the project ns/test
Pierre-Yves Chibon b124e7
        item = pagure.lib.model.Project(
Pierre-Yves Chibon b124e7
            user_id=1,  # pingou
Pierre-Yves Chibon b124e7
            name='test3',
Pierre-Yves Chibon b124e7
            namespace='ns',
Pierre-Yves Chibon b124e7
            description='test project #1',
Pierre-Yves Chibon b124e7
            hook_token='aaabbbcccdd',
Pierre-Yves Chibon b124e7
        )
Pierre-Yves Chibon b124e7
        item.close_status = ['Invalid', 'Insufficient data', 'Fixed']
Pierre-Yves Chibon b124e7
        self.session.add(item)
Pierre-Yves Chibon b124e7
        self.session.commit()
Pierre-Yves Chibon b124e7
Pierre-Yves Chibon b124e7
        # Create the private ticket
Pierre-Yves Chibon 930073
        iss = pagure.lib.query.new_issue(
Pierre-Yves Chibon b124e7
            issue_id=4,
Pierre-Yves Chibon b124e7
            session=self.session,
Pierre-Yves Chibon b124e7
            repo=item,
Pierre-Yves Chibon b124e7
            title='test issue',
Pierre-Yves Chibon b124e7
            content='content test issue',
Pierre-Yves Chibon b124e7
            user='pingou',
Pierre-Yves Chibon b124e7
            private=True,
Pierre-Yves Chibon b124e7
        )
Pierre-Yves Chibon b124e7
        self.session.commit()
Pierre-Yves Chibon b124e7
        self.assertEqual(iss.id, 4)
Pierre-Yves Chibon b124e7
        self.assertEqual(iss.title, 'test issue')
Pierre-Yves Chibon b124e7
Pierre-Yves Chibon b124e7
        exp = set(['bar@pingou.com'])
Pierre-Yves Chibon b124e7
        out = pagure.lib.notify._get_emails_for_obj(iss)
Pierre-Yves Chibon b124e7
        self.assertEqual(out, exp)
Pierre-Yves Chibon b124e7
Pierre-Yves Chibon b124e7
        # Comment on the ticket
Pierre-Yves Chibon 930073
        out = pagure.lib.query.add_issue_comment(
Pierre-Yves Chibon b124e7
            self.session,
Pierre-Yves Chibon b124e7
            issue=iss,
Pierre-Yves Chibon b124e7
            comment='This is a comment',
Pierre-Yves Chibon b124e7
            user='foo',
Pierre-Yves Chibon b124e7
            notify=False)
Pierre-Yves Chibon b124e7
        self.assertEqual(out, 'Comment added')
Pierre-Yves Chibon b124e7
Pierre-Yves Chibon b124e7
        exp = set(['bar@pingou.com', 'foo@bar.com'])
Pierre-Yves Chibon b124e7
        out = pagure.lib.notify._get_emails_for_obj(iss)
Pierre-Yves Chibon b124e7
        self.assertEqual(out, exp)
Pierre-Yves Chibon b124e7
Pierre-Yves Chibon b124e7
        # Create user `bar`
Pierre-Yves Chibon b124e7
        item = pagure.lib.model.User(
Pierre-Yves Chibon b124e7
            user='bar',
Pierre-Yves Chibon b124e7
            fullname='bar name',
Pierre-Yves Chibon b124e7
            password='bar',
Pierre-Yves Chibon b124e7
            default_email='bar@bar.com',
Pierre-Yves Chibon b124e7
        )
Pierre-Yves Chibon b124e7
        self.session.add(item)
Pierre-Yves Chibon b124e7
        item = pagure.lib.model.UserEmail(
Pierre-Yves Chibon b124e7
            user_id=3,
Pierre-Yves Chibon b124e7
            email='bar@bar.com')
Pierre-Yves Chibon b124e7
        self.session.add(item)
Pierre-Yves Chibon b124e7
        self.session.commit()
Pierre-Yves Chibon b124e7
Pierre-Yves Chibon b124e7
        # Add bar on the project with ticket acl
Pierre-Yves Chibon 930073
        project = pagure.lib.query._get_project(self.session, 'test3', namespace='ns')
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_user_to_project(
Pierre-Yves Chibon b124e7
            session=self.session,
Pierre-Yves Chibon b124e7
            project=project,
Pierre-Yves Chibon b124e7
            new_user='bar',
Pierre-Yves Chibon b124e7
            user='pingou',
Pierre-Yves Chibon b124e7
            access='ticket',
Pierre-Yves Chibon b124e7
        )
Pierre-Yves Chibon b124e7
        self.session.commit()
Pierre-Yves Chibon b124e7
        self.assertEqual(msg, 'User added')
Pierre-Yves Chibon b124e7
Pierre-Yves Chibon b124e7
        exp = set(['bar@pingou.com', 'foo@bar.com'])
Pierre-Yves Chibon b124e7
        out = pagure.lib.notify._get_emails_for_obj(iss)
Pierre-Yves Chibon b124e7
        self.assertEqual(out, exp)
Pierre-Yves Chibon b124e7
Pierre-Yves Chibon ec3e61
    @patch.dict(
Pierre-Yves Chibon ec3e61
        'pagure.config.config',
Pierre-Yves Chibon ec3e61
        {'EVENTSOURCE_SOURCE': 'localhost.localdomain'})
Pierre-Yves Chibon 33ff8d
    @patch('pagure.lib.notify.smtplib.SMTP')
Pierre-Yves Chibon 33ff8d
    def test_send_email(self, mock_smtp):
Pierre-Yves Chibon 81b2c9
        """ Test the send_email method from pagure.lib.notify. """
Pierre-Yves Chibon 33ff8d
        mock_smtp.return_value = MagicMock()
Pierre-Yves Chibon 33ff8d
Pierre-Yves Chibon 4c5244
        email = pagure.lib.notify.send_email(
Pierre-Yves Chibon 4c5244
            'Email content',
Pierre-Yves Chibon 773eb0
            'Email “Subject“',
Pierre-Yves Chibon 4c5244
            'foo@bar.com,zöé@foo.net',
Sachin Kamath f01117
            mail_id='test-pull-request-2edbf96ebe644f4bb31b94605e-1',
Pierre-Yves Chibon fc8c36
            in_reply_to='test-pull-request-2edbf96ebe644f4bb31b94605e',
Pierre-Yves Chibon 4c5244
            project_name='namespace/project',
Pierre-Yves Chibon 4c5244
            user_from='Zöé',
Pierre-Yves Chibon 4c5244
        )
Aurélien Bompard 626417
        # Due to differences in the way Python2 and Python3 encode non-ascii
Aurélien Bompard 626417
        # email headers, we compare the From and To headers separately from the
Aurélien Bompard 626417
        # rest of the message.
Pierre-Yves Chibon 02f40e
        self.assertEqual(
Pierre-Yves Chibon 02f40e
            email["From"],
Pierre-Yves Chibon 02f40e
            "=?utf-8?b?WsO2w6k=?= <pagure@localhost.localdomain>")</pagure@localhost.localdomain>
Aurélien Bompard 626417
        self.assertEqual(email["To"], "zöé@foo.net")
Aurélien Bompard 626417
        del email["From"]
Aurélien Bompard 626417
        del email["To"]
Pierre-Yves Chibon 4c5244
        exp = '''Content-Type: text/plain; charset="utf-8"
Pierre-Yves Chibon 4c5244
MIME-Version: 1.0
Pierre-Yves Chibon 4c5244
Content-Transfer-Encoding: base64
Pierre-Yves Chibon 773eb0
Subject: =?utf-8?b?W25hbWVzcGFjZS9wcm9qZWN0XSBFbWFpbCDigJxTdWJqZWN04oCc?=
Neal Gompa 9c34c4
mail-id: test-pull-request-2edbf96ebe644f4bb31b94605e-1@localhost.localdomain
Neal Gompa 9c34c4
Message-Id: <test-pull-request-2edbf96ebe644f4bb31b94605e-1@localhost.localdomain></test-pull-request-2edbf96ebe644f4bb31b94605e-1@localhost.localdomain>
Neal Gompa 9c34c4
In-Reply-To: <test-pull-request-2edbf96ebe644f4bb31b94605e@localhost.localdomain></test-pull-request-2edbf96ebe644f4bb31b94605e@localhost.localdomain>
Pierre-Yves Chibon 8d6e8b
X-Auto-Response-Suppress: All
Neal Gompa 9c34c4
X-pagure: http://localhost.localdomain/
Pierre-Yves Chibon 4c5244
X-pagure-project: namespace/project
Pierre-Yves Chibon 8d6e8b
List-ID: namespace/project
Neal Gompa 9c34c4
List-Archive: http://localhost.localdomain/namespace/project
Neal Gompa 9c34c4
Reply-To: reply+ddd73d6bcace71598118ece5808b9c1b8e68c73e1acc4302538257a0951bb920ea0765d3f262ddb3725e9369519b086a7873cb65fbfceb1a2a25897f8e2a54fa@localhost.localdomain
Neal Gompa 9c34c4
Mail-Followup-To: reply+ddd73d6bcace71598118ece5808b9c1b8e68c73e1acc4302538257a0951bb920ea0765d3f262ddb3725e9369519b086a7873cb65fbfceb1a2a25897f8e2a54fa@localhost.localdomain
Pierre-Yves Chibon 4c5244
Pierre-Yves Chibon 4c5244
RW1haWwgY29udGVudA==
Pierre-Yves Chibon 4c5244
'''
Pierre-Yves Chibon 4c5244
        self.assertEqual(email.as_string(), exp)
Pierre-Yves Chibon 4c5244
Pierre-Yves Chibon 186cd4
        email = pagure.lib.notify.send_email(
Pierre-Yves Chibon 186cd4
            'Email content',
Pierre-Yves Chibon 186cd4
            'Email “Subject“',
Aurélien Bompard 626417
            'foo@bar.com,zöé@foo.net',
Sachin Kamath f01117
            mail_id='test-pull-request-2edbf96ebe644f4bb31b94605e-1',
Pierre-Yves Chibon fc8c36
            in_reply_to='test-pull-request-2edbf96ebe644f4bb31b94605e',
Pierre-Yves Chibon 186cd4
            project_name='namespace/project',
Pierre-Yves Chibon 186cd4
            user_from='Zöé',
Pierre-Yves Chibon 186cd4
        )
Pierre-Yves Chibon 02f40e
        self.assertEqual(
Pierre-Yves Chibon 02f40e
            email["From"],
Pierre-Yves Chibon 02f40e
            "=?utf-8?b?WsO2w6k=?= <pagure@localhost.localdomain>")</pagure@localhost.localdomain>
Aurélien Bompard 626417
        self.assertEqual(email["To"], "zöé@foo.net")
Aurélien Bompard 626417
        del email["From"]
Aurélien Bompard 626417
        del email["To"]
Pierre-Yves Chibon 186cd4
        self.assertEqual(email.as_string(), exp)
Karsten Hopp 63175d
        exp = '''Content-Type: text/plain; charset="utf-8"
Karsten Hopp 63175d
MIME-Version: 1.0
Karsten Hopp 63175d
Content-Transfer-Encoding: base64
Karsten Hopp 63175d
Subject: =?utf-8?b?W25hbWVzcGFjZS9wcm9qZWN0XSBFbWFpbCDigJxTdWJqZWN04oCc?=
Karsten Hopp 63175d
In-Reply-To: <test-pull-request-2edbf96ebe644f4bb31b94605e@localhost.localdomain></test-pull-request-2edbf96ebe644f4bb31b94605e@localhost.localdomain>
Karsten Hopp 63175d
X-Auto-Response-Suppress: All
Karsten Hopp 63175d
X-pagure: http://localhost.localdomain/
Karsten Hopp 63175d
X-pagure-project: namespace/project
Karsten Hopp 63175d
List-ID: namespace/project
Karsten Hopp 63175d
List-Archive: http://localhost.localdomain/namespace/project
Karsten Hopp 63175d
Karsten Hopp 63175d
RW1haWwgY29udGVudA==
Karsten Hopp 63175d
'''
Karsten Hopp 63175d
        email = pagure.lib.notify.send_email(
Karsten Hopp 63175d
            'Email content',
Karsten Hopp 63175d
            'Email “Subject“',
Karsten Hopp 63175d
            'foo@bar.com,zöé@foo.net',
Karsten Hopp 63175d
            mail_id=None,
Karsten Hopp 63175d
            in_reply_to='test-pull-request-2edbf96ebe644f4bb31b94605e',
Karsten Hopp 63175d
            project_name='namespace/project',
Karsten Hopp 63175d
            user_from='Zöé',
Karsten Hopp 63175d
        )
Karsten Hopp 63175d
        del email["From"]
Karsten Hopp 63175d
        del email["To"]
Karsten Hopp 63175d
        self.assertEqual(email.as_string(), exp)
Pierre-Yves Chibon 186cd4
Pierre-Yves Chibon ec3e61
    @patch.dict('pagure.config.config', {'EVENTSOURCE_SOURCE': None})
Pierre-Yves Chibon ec3e61
    @patch('pagure.lib.notify.smtplib.SMTP')
Pierre-Yves Chibon ec3e61
    def test_send_email_no_reply_to(self, mock_smtp):
Pierre-Yves Chibon ec3e61
        """ Test the send_email method from pagure.lib.notify when there
Pierre-Yves Chibon ec3e61
        should not be a Reply-To header even if mail_id is defined. """
Pierre-Yves Chibon ec3e61
        mock_smtp.return_value = MagicMock()
Pierre-Yves Chibon ec3e61
Pierre-Yves Chibon ec3e61
        email = pagure.lib.notify.send_email(
Pierre-Yves Chibon ec3e61
            'Email content',
Pierre-Yves Chibon ec3e61
            'Email “Subject“',
Pierre-Yves Chibon ec3e61
            'foo@bar.com,zöé@foo.net',
Pierre-Yves Chibon ec3e61
            mail_id='test-pull-request-2edbf96ebe644f4bb31b94605e-1',
Pierre-Yves Chibon ec3e61
            in_reply_to='test-pull-request-2edbf96ebe644f4bb31b94605e',
Pierre-Yves Chibon ec3e61
            project_name='namespace/project',
Lenka Segura 9cb3f3
            reporter='reporter',
Lenka Segura 9cb3f3
            assignee='assignee',
Pierre-Yves Chibon ec3e61
            user_from='Zöé',
Pierre-Yves Chibon ec3e61
        )
Pierre-Yves Chibon ec3e61
        # Due to differences in the way Python2 and Python3 encode non-ascii
Pierre-Yves Chibon ec3e61
        # email headers, we compare the From and To headers separately from the
Pierre-Yves Chibon ec3e61
        # rest of the message.
Pierre-Yves Chibon 02f40e
        self.assertEqual(
Pierre-Yves Chibon 02f40e
            email["From"],
Pierre-Yves Chibon 02f40e
            "=?utf-8?b?WsO2w6k=?= <pagure@localhost.localdomain>")</pagure@localhost.localdomain>
Pierre-Yves Chibon ec3e61
        self.assertEqual(email["To"], "zöé@foo.net")
Pierre-Yves Chibon ec3e61
        del email["From"]
Pierre-Yves Chibon ec3e61
        del email["To"]
Pierre-Yves Chibon ec3e61
        exp = '''Content-Type: text/plain; charset="utf-8"
Pierre-Yves Chibon ec3e61
MIME-Version: 1.0
Pierre-Yves Chibon ec3e61
Content-Transfer-Encoding: base64
Pierre-Yves Chibon ec3e61
Subject: =?utf-8?b?W25hbWVzcGFjZS9wcm9qZWN0XSBFbWFpbCDigJxTdWJqZWN04oCc?=
Pierre-Yves Chibon ec3e61
mail-id: test-pull-request-2edbf96ebe644f4bb31b94605e-1@localhost.localdomain
Pierre-Yves Chibon ec3e61
Message-Id: <test-pull-request-2edbf96ebe644f4bb31b94605e-1@localhost.localdomain></test-pull-request-2edbf96ebe644f4bb31b94605e-1@localhost.localdomain>
Pierre-Yves Chibon ec3e61
In-Reply-To: <test-pull-request-2edbf96ebe644f4bb31b94605e@localhost.localdomain></test-pull-request-2edbf96ebe644f4bb31b94605e@localhost.localdomain>
Pierre-Yves Chibon ec3e61
X-Auto-Response-Suppress: All
Pierre-Yves Chibon ec3e61
X-pagure: http://localhost.localdomain/
Pierre-Yves Chibon ec3e61
X-pagure-project: namespace/project
Pierre-Yves Chibon ec3e61
List-ID: namespace/project
Pierre-Yves Chibon ec3e61
List-Archive: http://localhost.localdomain/namespace/project
Lenka Segura 9cb3f3
X-pagure-reporter: reporter
Lenka Segura 9cb3f3
X-pagure-assignee: assignee
Pierre-Yves Chibon ec3e61
Pierre-Yves Chibon ec3e61
RW1haWwgY29udGVudA==
Pierre-Yves Chibon ec3e61
'''
Pierre-Yves Chibon ec3e61
        self.assertEqual(email.as_string(), exp)
Pierre-Yves Chibon ec3e61
Pierre-Yves Chibon fd2f9e
Pierre-Yves Chibon fd2f9e
if __name__ == '__main__':
Pierre-Yves Chibon 393f31
    unittest.main(verbosity=2)