From ec3e61872a6b983df94568a0cfe749473588002d Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jul 25 2018 09:44:46 +0000 Subject: Do not set the Reply-To and Mail-Followup-To headers when not needed Basically, when there is no milter configured, you can't reply to a ticket or PR by email. So in those cases, don't set these two headers at all, they are more confusing than anything... Fixes https://pagure.io/pagure/issue/3399 Signed-off-by: Pierre-Yves Chibon --- diff --git a/pagure/lib/notify.py b/pagure/lib/notify.py index bd98ba1..d6b0945 100644 --- a/pagure/lib/notify.py +++ b/pagure/lib/notify.py @@ -334,7 +334,8 @@ def send_email(text, subject, to_mail, if salt and not isinstance(salt, bytes): salt = salt.encode('utf-8') - if mail_id: + if mail_id and pagure_config['EVENTSOURCE_SOURCE']: + key = (b'<' + mail_id.encode("utf-8") + b'>' + salt + mailto.encode("utf-8")) if isinstance(key, six.text_type): diff --git a/tests/test_pagure_lib_notify.py b/tests/test_pagure_lib_notify.py index 2647912..79603c2 100644 --- a/tests/test_pagure_lib_notify.py +++ b/tests/test_pagure_lib_notify.py @@ -414,6 +414,9 @@ class PagureLibNotifytests(tests.Modeltests): out = pagure.lib.notify._get_emails_for_obj(iss) self.assertEqual(out, exp) + @patch.dict( + 'pagure.config.config', + {'EVENTSOURCE_SOURCE': 'localhost.localdomain'}) @patch('pagure.lib.notify.smtplib.SMTP') def test_send_email(self, mock_smtp): """ Test the notify_new_comment method from pagure.lib.notify. """ @@ -494,6 +497,46 @@ RW1haWwgY29udGVudA== del email["To"] self.assertEqual(email.as_string(), exp) + @patch.dict('pagure.config.config', {'EVENTSOURCE_SOURCE': None}) + @patch('pagure.lib.notify.smtplib.SMTP') + def test_send_email_no_reply_to(self, mock_smtp): + """ Test the send_email method from pagure.lib.notify when there + should not be a Reply-To header even if mail_id is defined. """ + mock_smtp.return_value = MagicMock() + + email = pagure.lib.notify.send_email( + 'Email content', + 'Email “Subject“', + 'foo@bar.com,zöé@foo.net', + mail_id='test-pull-request-2edbf96ebe644f4bb31b94605e-1', + in_reply_to='test-pull-request-2edbf96ebe644f4bb31b94605e', + project_name='namespace/project', + user_from='Zöé', + ) + # Due to differences in the way Python2 and Python3 encode non-ascii + # email headers, we compare the From and To headers separately from the + # rest of the message. + self.assertEqual(email["From"], "Zöé ") + self.assertEqual(email["To"], "zöé@foo.net") + del email["From"] + del email["To"] + exp = '''Content-Type: text/plain; charset="utf-8" +MIME-Version: 1.0 +Content-Transfer-Encoding: base64 +Subject: =?utf-8?b?W25hbWVzcGFjZS9wcm9qZWN0XSBFbWFpbCDigJxTdWJqZWN04oCc?= +mail-id: test-pull-request-2edbf96ebe644f4bb31b94605e-1@localhost.localdomain +Message-Id: +In-Reply-To: +X-Auto-Response-Suppress: All +X-pagure: http://localhost.localdomain/ +X-pagure-project: namespace/project +List-ID: namespace/project +List-Archive: http://localhost.localdomain/namespace/project + +RW1haWwgY29udGVudA== +''' + self.assertEqual(email.as_string(), exp) + if __name__ == '__main__': unittest.main(verbosity=2)