You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by br...@apache.org on 2014/09/16 00:13:34 UTC
[2/2] git commit: [#7677] update mail tasks to handle actual ObjectId
in addr params
[#7677] update mail tasks to handle actual ObjectId in addr params
Project: http://git-wip-us.apache.org/repos/asf/allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/allura/commit/01e009f3
Tree: http://git-wip-us.apache.org/repos/asf/allura/tree/01e009f3
Diff: http://git-wip-us.apache.org/repos/asf/allura/diff/01e009f3
Branch: refs/heads/db/7677
Commit: 01e009f33421ac9c6c92c1284bda2f0242267def
Parents: 505174a
Author: Dave Brondsema <db...@slashdotmedia.com>
Authored: Mon Sep 15 18:41:44 2014 +0000
Committer: Dave Brondsema <db...@slashdotmedia.com>
Committed: Mon Sep 15 22:13:20 2014 +0000
----------------------------------------------------------------------
Allura/allura/tasks/mail_tasks.py | 14 ++++++++++----
Allura/allura/tests/test_tasks.py | 14 ++++++++++++++
2 files changed, 24 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/allura/blob/01e009f3/Allura/allura/tasks/mail_tasks.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tasks/mail_tasks.py b/Allura/allura/tasks/mail_tasks.py
index 4fce0d7..663c690 100644
--- a/Allura/allura/tasks/mail_tasks.py
+++ b/Allura/allura/tasks/mail_tasks.py
@@ -88,6 +88,9 @@ def sendmail(fromaddr, destinations, text, reply_to, subject,
'''
Send an email to the specified list of destinations with respect to the preferred email format specified by user.
It is best for broadcast messages.
+
+ :param fromaddr: ObjectId or str(ObjectId) of user, or email address str
+
'''
from allura import model as M
addrs_plain = []
@@ -95,7 +98,7 @@ def sendmail(fromaddr, destinations, text, reply_to, subject,
addrs_multi = []
if fromaddr is None:
fromaddr = g.noreply
- elif '@' not in fromaddr:
+ elif not isinstance(fromaddr, basestring) or '@' not in fromaddr:
log.warning('Looking up user with fromaddr: %s', fromaddr)
user = M.User.query.get(_id=ObjectId(fromaddr), disabled=False)
if not user:
@@ -164,11 +167,15 @@ def sendsimplemail(
'''
Send a single mail to the specified address.
It is best for single user notifications.
+
+ :param fromaddr: ObjectId or str(ObjectId) of user, or email address str
+ :param toaddr: ObjectId or str(ObjectId) of user, or email address str
+
'''
from allura import model as M
if fromaddr is None:
fromaddr = g.noreply
- elif '@' not in fromaddr:
+ elif not isinstance(fromaddr, basestring) or '@' not in fromaddr:
log.warning('Looking up user with fromaddr: %s', fromaddr)
user = M.User.query.get(_id=ObjectId(fromaddr), disabled=False)
if not user:
@@ -177,8 +184,7 @@ def sendsimplemail(
else:
fromaddr = user.email_address_header()
-
- if '@' not in toaddr:
+ if not isinstance(toaddr, basestring) or '@' not in toaddr:
log.warning('Looking up user with toaddr: %s', toaddr)
user = M.User.query.get(_id=ObjectId(toaddr), disabled=False)
if not user:
http://git-wip-us.apache.org/repos/asf/allura/blob/01e009f3/Allura/allura/tests/test_tasks.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/test_tasks.py b/Allura/allura/tests/test_tasks.py
index 5dd70b6..13d5c17 100644
--- a/Allura/allura/tests/test_tasks.py
+++ b/Allura/allura/tests/test_tasks.py
@@ -388,6 +388,20 @@ class TestMailTasks(unittest.TestCase):
assert_in('CC: someone@example.com', body)
assert_in('someone@example.com', rcpts)
+ def test_fromaddr_objectid_not_str(self):
+ c.user = M.User.by_username('test-admin')
+ with mock.patch.object(mail_tasks.smtp_client, '_client') as _client:
+ mail_tasks.sendsimplemail(
+ fromaddr=c.user._id,
+ toaddr='test@mail.com',
+ text=u'This is a test',
+ reply_to=g.noreply,
+ subject=u'Test subject',
+ message_id=h.gen_message_id())
+ assert_equal(_client.sendmail.call_count, 1)
+ return_path, rcpts, body = _client.sendmail.call_args[0]
+ assert_in('From: "Test Admin" <te...@users.localhost>', body)
+
@td.with_wiki
def test_receive_email_ok(self):
c.user = M.User.by_username('test-admin')