From 715aea125ab7a9c0686929395f36d54f6434a899 Mon Sep 17 00:00:00 2001 From: Adolfo Fitoria Date: Tue, 18 Sep 2012 09:03:01 -0600 Subject: buggy commit, error parsing signature from html emails --- askbot/mail/__init__.py | 11 ++- askbot/tests/reply_by_email_tests.py | 153 ++++++++++++++++++++++++++++------- 2 files changed, 133 insertions(+), 31 deletions(-) diff --git a/askbot/mail/__init__.py b/askbot/mail/__init__.py index 739b147e..2629aa47 100644 --- a/askbot/mail/__init__.py +++ b/askbot/mail/__init__.py @@ -158,8 +158,8 @@ def mail_moderators( try: msg = mail.EmailMessage( - subject_line, - body_text, + subject_line, + body_text, from_email, recipient_list, headers = headers or {} @@ -287,10 +287,13 @@ def process_attachment(attachment): def extract_user_signature(text, reply_code): """extracts email signature as text trailing the reply code""" - if reply_code in text: + #FIXME: buggy in html code + striped_text = clean_html_email(text.replace('
', '\n')) + #striped_text = strip_tags(text.replace('
', '\n')) + if reply_code in striped_text: #extract the signature tail = list() - for line in reversed(text.splitlines()): + for line in reversed(striped_text.splitlines()): #scan backwards from the end until the magic line if reply_code in line: break diff --git a/askbot/tests/reply_by_email_tests.py b/askbot/tests/reply_by_email_tests.py index 9248fc50..3336a3af 100644 --- a/askbot/tests/reply_by_email_tests.py +++ b/askbot/tests/reply_by_email_tests.py @@ -1,6 +1,7 @@ from django.utils.translation import ugettext as _ from askbot.models import ReplyAddress from askbot.mail.lamson_handlers import PROCESS, VALIDATE_EMAIL, get_parts +from askbot.mail import extract_user_signature from askbot import const @@ -146,7 +147,6 @@ class EmailSignatureDetectionTests(AskbotTestCase): def setUp(self): self.u1 = self.create_user('user1', status = 'a') self.u2 = self.create_user('user2', status = 'a') - self.u3 = self.create_user('user3', status = 'a') def test_detect_signature_in_response(self): question = self.post_question(user = self.u1) @@ -194,31 +194,130 @@ class EmailSignatureDetectionTests(AskbotTestCase): signature = self.reload_object(self.u2).email_signature self.assertEqual(signature, 'Yours Truly') - def test_detect_signature_in_html_welcome_response(self): - reply_token = ReplyAddress.objects.create_new( - user = self.u3, - reply_action = 'validate_email' - ) - self.u3.email_signature = '' - self.u3.save() - signature = 'Yours Truly' - - msg = MockMessage( - 'some text', - self.u3.email, - signature = signature, - response_code = reply_token.address - ) - - html_message = 'some text' + signature - - msg.attach_alternative(html_message, 'text/html') - VALIDATE_EMAIL( - msg, - address = reply_token.address - ) - - signature = self.reload_object(self.u3).email_signature - self.assertEqual(signature, 'Yours Truly') +class SignatureDetectionTests(AskbotTestCase): + '''Simple test to detect signature from text''' + def setUp(self): + self.u1 = self.create_user('user1', status = 'a') + self.u1.email_signature = '''--\nFoo Bar''' + self.u1.save() + self.plain_text = '''welcome! + +On Mon, Sep 17, 2012 at 9:01 AM, wrote: + +> ** +> Welcome to Askbot! +> +> Important: *Please reply* to this message, without editing it. We need +> this to determine your email signature and that the email address is valid +> and was typed correctly. +> +> Until we receive the response from you, you will not be able ask or answer +> questions on askbot by email. +> ------------------------------ +> +> Sincerely, +> askbot Administrator +> +> ofqssnfqkvlw +> + + + +-- +Foo Bar +''' + self.html = '''welcome!

On Mon, Sep 17, 2012 at 9:01 AM,= + <foo@bar.com> wrote:
+=20 + + + =20 + =20 + =20 + =20 + + =20 + =20 + + =20 + + =20 + +
+ + + + +
+ + + + + + + + + + +
=20 +
=20 + + + + +
=20 + + + + + + + + + + +
=20 +

Welcome to KnowledgePoint!

+
=20 + =20 + +

+ Important: Please reply to this message, without editing it. W= +e need this to determine your email signature and that the email address is= + valid and was typed correctly. +

+

+ Until we receive the response from you, you will not be able ask or ans= +wer questions on KnowledgePoint by email. +

+ +
=20 +
+ =20 +

Sincerely,
KnowledgePoint Administrator

+

ofqssnfqkvlw

+ +
+
+
=20 +
+
=20 +
+



--
Foo Bar +
''' + + def test_plain_text_parse(self): + signature = extract_user_signature(self.plain_text, 'ofqssnfqkvlw') + self.assertEquals(signature, self.u1.email_signature) + + def test_html_parse(self): + signature = extract_user_signature(self.html, 'ofqssnfqkvlw') + self.assertEquals(signature, self.u1.email_signature) -- cgit v1.2.3-1-g7c22