]> git.ozlabs.org Git - patchwork/blobdiff - apps/patchwork/tests/patchparser.py
[tests] Add tests for utf-8 patches
[patchwork] / apps / patchwork / tests / patchparser.py
index 6fe7968bfafa32aa12eef70c26412f72ceb21e70..7b24bbcd62c43ce96ad7bbd6d75b46c8d6aabb6e 100644 (file)
 
 import unittest
 import os
-from email.mime.text import MIMEText
-from email.mime.multipart import MIMEMultipart
 from email import message_from_string
 from patchwork.models import Project, Person
+from patchwork.tests.utils import read_patch, create_email, defaults
 
-test_mail_dir  = 'patchwork/tests/mail'
-test_patch_dir = 'patchwork/tests/patches'
+try:
+    from email.mime.text import MIMEText
+except ImportError:
+    # Python 2.4 compatibility
+    from email.MIMEText import MIMEText
 
 class PatchTest(unittest.TestCase):
-    default_sender = 'Test Author <test@exmaple.com>'
-    default_subject = 'Test Subject'
-    project = Project(linkname = 'test-project')
-
-    def create_email(self, content, subject = None, sender = None,
-            multipart = False):
-        if subject is None:
-            subject = self.default_subject
-        if sender is None:
-            sender = self.default_sender
-
-        if multipart:
-            msg = MIMEMultipart()
-            body = MIMEText(content, _subtype = 'plain')
-            msg.attach(body)
-        else:
-            msg = MIMEText(content)
-
-        msg['Subject'] = subject
-        msg['From'] = sender
-        msg['List-Id'] = self.project.linkname
-
-        return msg
-
-    def read_patch(self, filename):
-        return file(os.path.join(test_patch_dir, filename)).read()
-
+    default_sender = defaults.sender
+    default_subject = defaults.subject
+    project = defaults.project
 
 from patchwork.bin.parsemail import find_content, find_author
 
@@ -63,8 +41,8 @@ class InlinePatchTest(PatchTest):
     test_comment = 'Test for attached patch'
 
     def setUp(self):
-        self.orig_patch = self.read_patch(self.patch_filename)
-        email = self.create_email(self.test_comment + '\n' + self.orig_patch)
+        self.orig_patch = read_patch(self.patch_filename)
+        email = create_email(self.test_comment + '\n' + self.orig_patch)
         (self.patch, self.comment) = find_content(self.project, email)
 
     def testPatchPresence(self):
@@ -85,20 +63,29 @@ class AttachmentPatchTest(InlinePatchTest):
     test_comment = 'Test for attached patch'
 
     def setUp(self):
-        self.orig_patch = self.read_patch(self.patch_filename)
-        email = self.create_email(self.test_comment, multipart = True)
+        self.orig_patch = read_patch(self.patch_filename)
+        email = create_email(self.test_comment, multipart = True)
         attachment = MIMEText(self.orig_patch, _subtype = 'x-patch')
         email.attach(attachment)
         (self.patch, self.comment) = find_content(self.project, email)
 
+class UTF8InlinePatchTest(InlinePatchTest):
+    patch_filename = '0002-utf-8.patch'
+    patch_encoding = 'utf-8'
+
+    def setUp(self):
+        self.orig_patch = read_patch(self.patch_filename, self.patch_encoding)
+        email = create_email(self.test_comment + '\n' + self.orig_patch,
+                             content_encoding = self.patch_encoding)
+        (self.patch, self.comment) = find_content(self.project, email)
 
 class SignatureCommentTest(InlinePatchTest):
     patch_filename = '0001-add-line.patch'
     test_comment = 'Test comment\nmore comment'
 
     def setUp(self):
-        self.orig_patch = self.read_patch(self.patch_filename)
-        email = self.create_email( \
+        self.orig_patch = read_patch(self.patch_filename)
+        email = create_email( \
                 self.test_comment + '\n' + \
                 '-- \nsig\n' + self.orig_patch)
         (self.patch, self.comment) = find_content(self.project, email)
@@ -109,8 +96,8 @@ class ListFooterTest(InlinePatchTest):
     test_comment = 'Test comment\nmore comment'
 
     def setUp(self):
-        self.orig_patch = self.read_patch(self.patch_filename)
-        email = self.create_email( \
+        self.orig_patch = read_patch(self.patch_filename)
+        email = create_email( \
                 self.test_comment + '\n' + \
                 '_______________________________________________\n' + \
                 'Linuxppc-dev mailing list\n' + \
@@ -165,7 +152,48 @@ class SenderUTF8QPEncodingTest(SenderEncodingTest):
 
 class SenderUTF8QPSplitEncodingTest(SenderEncodingTest):
     sender_name = u'\xe9xample user'
-    from_header = '=?utf-8?q?=C3=A9xample=20?= user <user@example.com>'
+    from_header = '=?utf-8?q?=C3=A9xample?= user <user@example.com>'
 
 class SenderUTF8B64EncodingTest(SenderUTF8QPEncodingTest):
     from_header = '=?utf-8?B?w6l4YW1wbGUgdXNlcg==?= <user@example.com>'
+
+
+class SenderCorrelationTest(unittest.TestCase):
+    existing_sender = 'Existing Sender <existing@example.com>'
+    non_existing_sender = 'Non-existing Sender <nonexisting@example.com>'
+
+    def mail(self, sender):
+        return message_from_string('From: %s\nSubject: Test\n\ntest\n' % sender)
+
+    def setUp(self):
+        self.existing_sender_mail = self.mail(self.existing_sender)
+        self.non_existing_sender_mail = self.mail(self.non_existing_sender)
+        (self.person, new) = find_author(self.existing_sender_mail)
+        self.person.save()
+
+        print Person.objects.all()
+
+    def testExisingSender(self):
+        (person, new) = find_author(self.existing_sender_mail)
+        self.assertEqual(new, False)
+        self.assertEqual(person.id, self.person.id)
+
+    def testNonExisingSender(self):
+        (person, new) = find_author(self.non_existing_sender_mail)
+        self.assertEqual(new, True)
+        self.assertEqual(person.id, None)
+
+    def testExistingDifferentFormat(self):
+        mail = self.mail('existing@example.com')
+        (person, new) = find_author(mail)
+        self.assertEqual(new, False)
+        self.assertEqual(person.id, self.person.id)
+
+    def testExistingDifferentCase(self):
+        mail = self.mail(self.existing_sender.upper())
+        (person, new) = find_author(mail)
+        self.assertEqual(new, False)
+        self.assertEqual(person.id, self.person.id)
+
+    def tearDown(self):
+        self.person.delete()