From f3ffebc2614102ff80b8cf791232e35b2e97201c Mon Sep 17 00:00:00 2001 From: Timothy Hobbs Date: Mon, 25 Mar 2019 17:13:10 +0100 Subject: [PATCH 1/5] Fix test regression from pr #731 https://github.com/django-helpdesk/django-helpdesk/pull/731/files#diff-dc9aca3bb03b3531dc114813db32f3c7R56 --- helpdesk/tests/test_get_email.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helpdesk/tests/test_get_email.py b/helpdesk/tests/test_get_email.py index 76184ec1..78bf1a6f 100644 --- a/helpdesk/tests/test_get_email.py +++ b/helpdesk/tests/test_get_email.py @@ -53,7 +53,7 @@ class GetEmailCommonTests(TestCase): with open(os.path.join(THIS_DIR, "test_files/blank-body-with-attachment.eml")) as fd: test_email = fd.read() ticket = helpdesk.email.object_from_message(test_email, self.queue_public, self.logger) - self.assertEqual(ticket.title, "FollowUpAttachment without body") + self.assertEqual(ticket.title, "Attachment without body") self.assertEqual(ticket.description, "") def test_email_with_quoted_printable_body(self): From 38dae04346fa35e05c8ef3d36ef5fbb3c9b864ef Mon Sep 17 00:00:00 2001 From: Timothy Hobbs Date: Mon, 25 Mar 2019 17:40:02 +0100 Subject: [PATCH 2/5] Allow the running of individually specified tests --- quicktest.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/quicktest.py b/quicktest.py index 85eb9c41..c427c49f 100644 --- a/quicktest.py +++ b/quicktest.py @@ -27,7 +27,8 @@ class QuickDjangoTest(object): 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.staticfiles', - 'bootstrap4form' + 'bootstrap4form', + 'helpdesk', ) MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', @@ -62,7 +63,7 @@ class QuickDjangoTest(object): def __init__(self, *args, **kwargs): - self.apps = args + self.tests = args self._tests() def _tests(self): @@ -79,7 +80,7 @@ class QuickDjangoTest(object): 'PORT': '', } }, - INSTALLED_APPS=self.INSTALLED_APPS + self.apps, + INSTALLED_APPS=self.INSTALLED_APPS, MIDDLEWARE=self.MIDDLEWARE, ROOT_URLCONF='helpdesk.tests.urls', STATIC_URL='/static/', @@ -92,7 +93,7 @@ class QuickDjangoTest(object): test_runner = DiscoverRunner(verbosity=1) django.setup() - failures = test_runner.run_tests(self.apps) + failures = test_runner.run_tests(self.tests) if failures: sys.exit(failures) @@ -102,13 +103,13 @@ if __name__ == '__main__': Example usage: - $ python quicktest.py app1 app2 + $ python quicktest.py test1 test2 """ parser = argparse.ArgumentParser( usage="[args]", - description="Run Django tests on the provided applications." + description="Run Django tests." ) - parser.add_argument('apps', nargs='+', type=str) + parser.add_argument('tests', nargs='+', type=str) args = parser.parse_args() - QuickDjangoTest(*args.apps) + QuickDjangoTest(*args.tests) From 669349621437beb22185f459af897e11d224593d Mon Sep 17 00:00:00 2001 From: Timothy Hobbs Date: Mon, 25 Mar 2019 17:45:42 +0100 Subject: [PATCH 3/5] Make specifying tests when calling quicktest optional --- quicktest.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/quicktest.py b/quicktest.py index c427c49f..c7912124 100644 --- a/quicktest.py +++ b/quicktest.py @@ -110,6 +110,8 @@ if __name__ == '__main__': usage="[args]", description="Run Django tests." ) - parser.add_argument('tests', nargs='+', type=str) + parser.add_argument('tests', nargs="*", type=str) args = parser.parse_args() + if not args.tests: + args.tests = ['helpdesk'] QuickDjangoTest(*args.tests) From 6886a7920eede6e27b8678ab9854601c948cf828 Mon Sep 17 00:00:00 2001 From: Timothy Hobbs Date: Tue, 26 Mar 2019 13:59:01 +0100 Subject: [PATCH 4/5] Specify encoding of html email bodies explicitly --- helpdesk/email.py | 10 ++++++++-- helpdesk/tests/test_get_email.py | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/helpdesk/email.py b/helpdesk/email.py index 2e076585..9bf442f2 100644 --- a/helpdesk/email.py +++ b/helpdesk/email.py @@ -481,9 +481,15 @@ def object_from_message(message, queue, logger): body.encode('utf-8') logger.debug("Discovered plain text MIME part") else: - payload = encoding.smart_bytes(part.get_payload(decode=True)) + payload = """ + + + + +%s +""" % encoding.smart_text(part.get_payload(decode=True)) files.append( - SimpleUploadedFile(_("email_html_body.html"), payload, 'text/html') + SimpleUploadedFile(_("email_html_body.html"), payload.encode("utf-8"), 'text/html') ) logger.debug("Discovered HTML MIME part") else: diff --git a/helpdesk/tests/test_get_email.py b/helpdesk/tests/test_get_email.py index 78bf1a6f..50a5f1e5 100644 --- a/helpdesk/tests/test_get_email.py +++ b/helpdesk/tests/test_get_email.py @@ -71,7 +71,7 @@ class GetEmailCommonTests(TestCase): attachments = FollowUpAttachment.objects.filter(followup=followup) self.assertEqual(len(attachments), 1) attachment = attachments[0] - self.assertEqual(attachment.file.read().decode("utf-8"), '
Tohle je test českých písmen odeslaných z gmailu.
\n') + self.assertIn('
Tohle je test českých písmen odeslaných z gmailu.
\n', attachment.file.read().decode("utf-8")) def test_email_with_8bit_encoding_and_utf_8(self): """ From 71d4662c7b9f829e927b259529f65371f27079d5 Mon Sep 17 00:00:00 2001 From: Timothy Hobbs Date: Tue, 26 Mar 2019 14:50:45 +0100 Subject: [PATCH 5/5] Fix #732 encoding of 8bit utf emails --- helpdesk/email.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/helpdesk/email.py b/helpdesk/email.py index 9bf442f2..483d96b1 100644 --- a/helpdesk/email.py +++ b/helpdesk/email.py @@ -471,9 +471,12 @@ def object_from_message(message, queue, logger): if part.get_content_maintype() == 'text' and name is None: if part.get_content_subtype() == 'plain': - body = EmailReplyParser.parse_reply( - decodeUnknown(part.get_content_charset(), part.get_payload(decode=True)) - ) + body = part.get_payload(decode=True) + # https://github.com/django-helpdesk/django-helpdesk/issues/732 + if part['Content-Transfer-Encoding'] == '8bit' and part.get_content_charset() == 'utf-8': + body = body.decode('unicode_escape') + body = decodeUnknown(part.get_content_charset(), body) + body = EmailReplyParser.parse_reply(body) # workaround to get unicode text out rather than escaped text try: body = body.encode('ascii').decode('unicode_escape')