incubator-allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tvansteenbu...@apache.org
Subject git commit: [#6910] Make emails with missing From identify as anon
Date Fri, 22 Nov 2013 19:07:10 GMT
Updated Branches:
  refs/heads/master b53e49a33 -> b9cb65b82


[#6910] Make emails with missing From identify as anon

Signed-off-by: Cory Johns <cjohns@slashdotmedia.com>


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

Branch: refs/heads/master
Commit: b9cb65b82689d9367ee20e454c4f5cdd27b9ca80
Parents: b53e49a
Author: Cory Johns <cjohns@slashdotmedia.com>
Authored: Fri Nov 22 18:07:40 2013 +0000
Committer: Cory Johns <cjohns@slashdotmedia.com>
Committed: Fri Nov 22 18:07:40 2013 +0000

----------------------------------------------------------------------
 Allura/allura/lib/mail_util.py        |  5 +++-
 Allura/allura/tests/test_mail_util.py | 42 +++++++++++++++++++++++++++++-
 2 files changed, 45 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/b9cb65b8/Allura/allura/lib/mail_util.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/mail_util.py b/Allura/allura/lib/mail_util.py
index 78fcc78..92ca47a 100644
--- a/Allura/allura/lib/mail_util.py
+++ b/Allura/allura/lib/mail_util.py
@@ -155,7 +155,10 @@ def identify_sender(peer, email_address, headers, msg):
     addr = M.EmailAddress.query.get(_id=M.EmailAddress.canonical(email_address))
     if addr and addr.claimed_by_user_id:
         return addr.claimed_by_user()
-    addr = M.EmailAddress.query.get(_id=M.EmailAddress.canonical(headers.get('From', '')))
+    from_address = headers.get('From', '').strip()
+    if not from_address:
+        return M.User.anonymous()
+    addr = M.EmailAddress.query.get(_id=M.EmailAddress.canonical(from_address))
     if addr and addr.claimed_by_user_id:
         return addr.claimed_by_user()
     return M.User.anonymous()

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/b9cb65b8/Allura/allura/tests/test_mail_util.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/test_mail_util.py b/Allura/allura/tests/test_mail_util.py
index d604a72..8cd044f 100644
--- a/Allura/allura/tests/test_mail_util.py
+++ b/Allura/allura/tests/test_mail_util.py
@@ -22,13 +22,20 @@ from email.MIMEMultipart import MIMEMultipart
 from email.MIMEText import MIMEText
 from email import header
 
+import mock
 from nose.tools import raises, assert_equal, assert_false, assert_true
 from ming.orm import ThreadLocalORMSession
 
 from alluratest.controller import setup_basic_test, setup_global_objects
 from allura.lib.utils import ConfigProxy
 
-from allura.lib.mail_util import parse_address, parse_message, Header, is_autoreply
+from allura.lib.mail_util import (
+        parse_address,
+        parse_message,
+        Header,
+        is_autoreply,
+        identify_sender,
+    )
 from allura.lib.exceptions import AddressException
 from allura.tests import decorators as td
 
@@ -172,3 +179,36 @@ class TestIsAutoreply(object):
     def test_return_path(self):
         self.msg['headers']['Return-Path'] = '<>'
         assert_true(is_autoreply(self.msg))
+
+class TestIdentifySender(object):
+    @mock.patch('allura.model.EmailAddress')
+    def test_arg(self, EA):
+        EA.canonical = lambda e:e
+        EA.query.get.side_effect = [mock.Mock(claimed_by_user_id=True, claimed_by_user=lambda:'user')]
+        assert_equal(identify_sender(None, 'arg', None, None), 'user')
+        EA.query.get.assert_called_once_with(_id='arg')
+
+    @mock.patch('allura.model.EmailAddress')
+    def test_header(self, EA):
+        EA.canonical = lambda e:e
+        EA.query.get.side_effect = [None, mock.Mock(claimed_by_user_id=True, claimed_by_user=lambda:'user')]
+        assert_equal(identify_sender(None, 'arg', {'From': 'from'}, None), 'user')
+        assert_equal(EA.query.get.call_args_list, [mock.call(_id='arg'), mock.call(_id='from')])
+
+    @mock.patch('allura.model.User')
+    @mock.patch('allura.model.EmailAddress')
+    def test_no_header(self, EA, User):
+        anon = User.anonymous()
+        EA.canonical = lambda e:e
+        EA.query.get.side_effect = [None, mock.Mock(claimed_by_user_id=True, claimed_by_user=lambda:'user')]
+        assert_equal(identify_sender(None, 'arg', {}, None), anon)
+        assert_equal(EA.query.get.call_args_list, [mock.call(_id='arg')])
+
+    @mock.patch('allura.model.User')
+    @mock.patch('allura.model.EmailAddress')
+    def test_no_match(self, EA, User):
+        anon = User.anonymous()
+        EA.canonical = lambda e:e
+        EA.query.get.side_effect = [None, None]
+        assert_equal(identify_sender(None, 'arg', {'From': 'from'}, None), anon)
+        assert_equal(EA.query.get.call_args_list, [mock.call(_id='arg'), mock.call(_id='from')])


Mime
View raw message