incubator-allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tvansteenbu...@apache.org
Subject [2/4] git commit: [#6249] ticket:447 Use a stable Sender: header in email notifications
Date Tue, 29 Oct 2013 15:24:52 GMT
[#6249] ticket:447 Use a stable Sender: header in email notifications


Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/dc017bee
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/dc017bee
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/dc017bee

Branch: refs/heads/master
Commit: dc017bee964077adca3277eec41739ff8d68cd08
Parents: 91cfa35
Author: Igor Bondarenko <jetmind2@gmail.com>
Authored: Fri Oct 4 14:41:41 2013 +0000
Committer: Tim Van Steenburgh <tvansteenburgh@gmail.com>
Committed: Tue Oct 29 03:57:09 2013 +0000

----------------------------------------------------------------------
 Allura/allura/app.py                           |  9 +++++-
 Allura/allura/lib/mail_util.py                 |  4 ++-
 Allura/allura/model/notification.py            | 10 +++++++
 Allura/allura/tasks/mail_tasks.py              | 13 +++++----
 Allura/allura/tests/model/test_notification.py |  2 ++
 Allura/allura/tests/test_tasks.py              | 32 +++++++++++++++++++++
 6 files changed, 62 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/dc017bee/Allura/allura/app.py
----------------------------------------------------------------------
diff --git a/Allura/allura/app.py b/Allura/allura/app.py
index 0135969..bca70e0 100644
--- a/Allura/allura/app.py
+++ b/Allura/allura/app.py
@@ -36,10 +36,12 @@ from allura.lib.security import require, has_access, require_access
 from allura import model
 from allura.controllers import BaseController
 from allura.lib.decorators import require_post, event_handler
-from allura.lib.utils import permanent_redirect
+from allura.lib.utils import permanent_redirect, ConfigProxy
 
 log = logging.getLogger(__name__)
 
+config = ConfigProxy(common_suffix='forgemail.domain')
+
 
 class ConfigOption(object):
     """Definition of a configuration option for an :class:`Application`.
@@ -271,6 +273,11 @@ class Application(object):
         return self.config.url(project=self.project)
 
     @property
+    def email_address(self):
+        parts = list(reversed(self.url[1:-1].split('/')))
+        return '%s@%s%s' % (parts[0], '.'.join(parts[1:]), config.common_suffix)
+
+    @property
     def acl(self):
         """Return the :class:`Access Control List <allura.model.types.ACL>`
         for this Application.

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/dc017bee/Allura/allura/lib/mail_util.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/mail_util.py b/Allura/allura/lib/mail_util.py
index 3a67e10..ca156a9 100644
--- a/Allura/allura/lib/mail_util.py
+++ b/Allura/allura/lib/mail_util.py
@@ -182,7 +182,7 @@ class SMTPClient(object):
     def __init__(self):
         self._client = None
 
-    def sendmail(self, addrs, fromaddr, reply_to, subject, message_id, in_reply_to, message):
+    def sendmail(self, addrs, fromaddr, reply_to, subject, message_id, in_reply_to, message,
sender=None):
         if not addrs: return
         # We send one message with multiple envelope recipients, so use a generic To: addr
         # It might be nice to refactor to send one message per recipient, and use the actual
To: addr
@@ -191,6 +191,8 @@ class SMTPClient(object):
         message['Reply-To'] = Header(reply_to)
         message['Subject'] = Header(subject)
         message['Message-ID'] = Header('<' + message_id + u'>')
+        if sender:
+            message['Sender'] = AddrHeader(sender)
         if in_reply_to:
             if not isinstance(in_reply_to, basestring):
                 raise TypeError('Only strings are supported now, not lists')

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/dc017bee/Allura/allura/model/notification.py
----------------------------------------------------------------------
diff --git a/Allura/allura/model/notification.py b/Allura/allura/model/notification.py
index 8851362..1315eec 100644
--- a/Allura/allura/model/notification.py
+++ b/Allura/allura/model/notification.py
@@ -231,12 +231,19 @@ class Notification(MappedClass):
     def footer(self, toaddr=''):
         return self.ref.artifact.get_mail_footer(self, toaddr)
 
+    def _sender(self):
+        from allura.model import AppConfig
+        app_config = AppConfig.query.get(_id=self.app_config_id)
+        app = app_config.project.app_instance(app_config)
+        return app.email_address if app else None
+
     def send_simple(self, toaddr):
         allura.tasks.mail_tasks.sendsimplemail.post(
             toaddr=toaddr,
             fromaddr=self.from_address,
             reply_to=self.reply_to_address,
             subject=self.subject,
+            sender=self._sender(),
             message_id=self._id,
             in_reply_to=self.in_reply_to,
             text=(self.text or '') + self.footer(toaddr))
@@ -266,6 +273,7 @@ class Notification(MappedClass):
             subject=self.subject,
             message_id=self._id,
             in_reply_to=self.in_reply_to,
+            sender=self._sender(),
             text=(self.text or '') + self.footer())
 
     @classmethod
@@ -301,6 +309,7 @@ class Notification(MappedClass):
             fromaddr=from_address,
             reply_to=reply_to_address,
             subject=subject,
+            sender=self._sender(),
             message_id=h.gen_message_id(),
             text=text)
 
@@ -322,6 +331,7 @@ class Notification(MappedClass):
             fromaddr=from_address,
             reply_to=from_address,
             subject=subject,
+            sender=self._sender(),
             message_id=h.gen_message_id(),
             text=text)
 

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/dc017bee/Allura/allura/tasks/mail_tasks.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tasks/mail_tasks.py b/Allura/allura/tasks/mail_tasks.py
index 8e7eb97..0d7fc6d 100644
--- a/Allura/allura/tasks/mail_tasks.py
+++ b/Allura/allura/tasks/mail_tasks.py
@@ -76,7 +76,7 @@ def route_email(
 
 @task
 def sendmail(fromaddr, destinations, text, reply_to, subject,
-             message_id, in_reply_to=None):
+             message_id, in_reply_to=None, sender=None):
     from allura import model as M
     addrs_plain = []
     addrs_html = []
@@ -126,13 +126,13 @@ def sendmail(fromaddr, destinations, text, reply_to, subject,
     multi_msg = mail_util.make_multipart_message(plain_msg, html_msg)
     smtp_client.sendmail(
         addrs_multi, fromaddr, reply_to, subject, message_id,
-        in_reply_to, multi_msg)
+        in_reply_to, multi_msg, sender=sender)
     smtp_client.sendmail(
         addrs_plain, fromaddr, reply_to, subject, message_id,
-        in_reply_to, plain_msg)
+        in_reply_to, plain_msg, sender=sender)
     smtp_client.sendmail(
         addrs_html, fromaddr, reply_to, subject, message_id,
-        in_reply_to, html_msg)
+        in_reply_to, html_msg, sender=sender)
 
 @task
 def sendsimplemail(
@@ -142,7 +142,8 @@ def sendsimplemail(
     reply_to,
     subject,
     message_id,
-    in_reply_to=None):
+    in_reply_to=None,
+    sender=None):
     from allura import model as M
     if fromaddr is None:
         fromaddr = u'noreply@in.sf.net'
@@ -161,4 +162,4 @@ def sendsimplemail(
     multi_msg = mail_util.make_multipart_message(plain_msg, html_msg)
     smtp_client.sendmail(
         [toaddr], fromaddr, reply_to, subject, message_id,
-        in_reply_to, multi_msg)
+        in_reply_to, multi_msg, sender=sender)

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/dc017bee/Allura/allura/tests/model/test_notification.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/model/test_notification.py b/Allura/allura/tests/model/test_notification.py
index 1e1c273..57891b5 100644
--- a/Allura/allura/tests/model/test_notification.py
+++ b/Allura/allura/tests/model/test_notification.py
@@ -231,6 +231,8 @@ class TestPostNotifications(unittest.TestCase):
         assert_in(str(user2._id), first_destinations)
         assert_equal(email_tasks[0].kwargs['fromaddr'], '"Test Admin" <test-admin@users.localhost>')
         assert_equal(email_tasks[1].kwargs['fromaddr'], '"Test Admin" <test-admin@users.localhost>')
+        assert_equal(email_tasks[0].kwargs['sender'], 'wiki@test.p.in.sf.net')
+        assert_equal(email_tasks[1].kwargs['sender'], 'wiki@test.p.in.sf.net')
         assert email_tasks[0].kwargs['text'].startswith('Home modified by Test Admin')
         assert 'you indicated interest in ' in email_tasks[0].kwargs['text']
 

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/dc017bee/Allura/allura/tests/test_tasks.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/test_tasks.py b/Allura/allura/tests/test_tasks.py
index dcbde71..e5a2ecd 100644
--- a/Allura/allura/tests/test_tasks.py
+++ b/Allura/allura/tests/test_tasks.py
@@ -278,6 +278,38 @@ class TestMailTasks(unittest.TestCase):
             body = body.split('\n')
             assert_in('From: noreply@in.sf.net', body)
 
+    def test_email_sender_header(self):
+        c.user = M.User.by_username('test-admin')
+        with mock.patch.object(mail_tasks.smtp_client, '_client') as _client:
+            mail_tasks.sendsimplemail(
+                fromaddr=str(c.user._id),
+                toaddr='test@mail.com',
+                text=u'This is a test',
+                reply_to=u'noreply@sf.net',
+                subject=u'Test subject',
+                sender=u'tickets@test.p.sf.net',
+                message_id=h.gen_message_id())
+            assert_equal(_client.sendmail.call_count, 1)
+            return_path, rcpts, body = _client.sendmail.call_args[0]
+            body = body.split('\n')
+            assert_in('From: "Test Admin" <test-admin@users.localhost>', body)
+            assert_in('Sender: tickets@test.p.sf.net', body)
+
+            _client.reset_mock()
+            mail_tasks.sendmail(
+                fromaddr=str(c.user._id),
+                destinations=[ str(c.user._id) ],
+                text=u'This is a test',
+                reply_to=u'noreply@sf.net',
+                subject=u'Test subject',
+                sender=u'tickets@test.p.sf.net',
+                message_id=h.gen_message_id())
+            assert_equal(_client.sendmail.call_count, 1)
+            return_path, rcpts, body = _client.sendmail.call_args[0]
+            body = body.split('\n')
+            assert_in('From: "Test Admin" <test-admin@users.localhost>', body)
+            assert_in('Sender: tickets@test.p.sf.net', body)
+
     @td.with_wiki
     def test_receive_email_ok(self):
         c.user = M.User.by_username('test-admin')


Mime
View raw message