Return-Path: X-Original-To: apmail-allura-commits-archive@www.apache.org Delivered-To: apmail-allura-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 063FE11E11 for ; Thu, 18 Sep 2014 07:29:36 +0000 (UTC) Received: (qmail 43920 invoked by uid 500); 18 Sep 2014 07:29:36 -0000 Delivered-To: apmail-allura-commits-archive@allura.apache.org Received: (qmail 43884 invoked by uid 500); 18 Sep 2014 07:29:36 -0000 Mailing-List: contact commits-help@allura.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@allura.apache.org Delivered-To: mailing list commits@allura.apache.org Received: (qmail 43595 invoked by uid 99); 18 Sep 2014 07:29:36 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 18 Sep 2014 07:29:36 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 38678A1A78D; Thu, 18 Sep 2014 07:29:36 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: jetmind@apache.org To: commits@allura.apache.org Date: Thu, 18 Sep 2014 07:29:45 -0000 Message-Id: <88c11dbbab9d4dbcaacc363e22a4e2e5@git.apache.org> In-Reply-To: <461714665cc44612a9541f626544ec25@git.apache.org> References: <461714665cc44612a9541f626544ec25@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [10/43] 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/je/42cc_7656 Commit: 01e009f33421ac9c6c92c1284bda2f0242267def Parents: 505174a Author: Dave Brondsema Authored: Mon Sep 15 18:41:44 2014 +0000 Committer: Dave Brondsema 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" ', body) + @td.with_wiki def test_receive_email_ok(self): c.user = M.User.by_username('test-admin')