]> git.ozlabs.org Git - patchwork/blobdiff - apps/patchwork/tests/patchparser.py
tests/parser: Add subject encoding tests
[patchwork] / apps / patchwork / tests / patchparser.py
index 7b24bbcd62c43ce96ad7bbd6d75b46c8d6aabb6e..1135a0a57b9f9b795d2d882b76e5bdb89cf5cae6 100644 (file)
@@ -20,8 +20,8 @@
 import unittest
 import os
 from email import message_from_string
-from patchwork.models import Project, Person
-from patchwork.tests.utils import read_patch, create_email, defaults
+from patchwork.models import Project, Person, Patch, Comment
+from patchwork.tests.utils import read_patch, read_mail, create_email, defaults
 
 try:
     from email.mime.text import MIMEText
@@ -34,7 +34,8 @@ class PatchTest(unittest.TestCase):
     default_subject = defaults.subject
     project = defaults.project
 
-from patchwork.bin.parsemail import find_content, find_author
+from patchwork.bin.parsemail import find_content, find_author, find_project, \
+                                    parse_mail
 
 class InlinePatchTest(PatchTest):
     patch_filename = '0001-add-line.patch'
@@ -61,14 +62,18 @@ class InlinePatchTest(PatchTest):
 class AttachmentPatchTest(InlinePatchTest):
     patch_filename = '0001-add-line.patch'
     test_comment = 'Test for attached patch'
+    content_subtype = 'x-patch'
 
     def setUp(self):
         self.orig_patch = read_patch(self.patch_filename)
         email = create_email(self.test_comment, multipart = True)
-        attachment = MIMEText(self.orig_patch, _subtype = 'x-patch')
+        attachment = MIMEText(self.orig_patch, _subtype = self.content_subtype)
         email.attach(attachment)
         (self.patch, self.comment) = find_content(self.project, email)
 
+class AttachmentXDiffPatchTest(AttachmentPatchTest):
+    content_subtype = 'x-diff'
+
 class UTF8InlinePatchTest(InlinePatchTest):
     patch_filename = '0002-utf-8.patch'
     patch_encoding = 'utf-8'
@@ -79,6 +84,17 @@ class UTF8InlinePatchTest(InlinePatchTest):
                              content_encoding = self.patch_encoding)
         (self.patch, self.comment) = find_content(self.project, email)
 
+class NoCharsetInlinePatchTest(InlinePatchTest):
+    """ Test mails with no content-type or content-encoding header """
+    patch_filename = '0001-add-line.patch'
+
+    def setUp(self):
+        self.orig_patch = read_patch(self.patch_filename)
+        email = create_email(self.test_comment + '\n' + self.orig_patch)
+        del email['Content-Type']
+        del email['Content-Transfer-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'
@@ -157,6 +173,29 @@ class SenderUTF8QPSplitEncodingTest(SenderEncodingTest):
 class SenderUTF8B64EncodingTest(SenderUTF8QPEncodingTest):
     from_header = '=?utf-8?B?w6l4YW1wbGUgdXNlcg==?= <user@example.com>'
 
+class SubjectEncodingTest(PatchTest):
+    sender = 'example user <user@example.com>'
+    subject = 'test subject'
+    subject_header = 'test subject'
+
+    def setUp(self):
+        mail = 'From: %s\n' % self.sender + \
+               'Subject: %s\n\n' % self.subject_header + \
+               'test\n\n' + defaults.patch
+        self.projects = defaults.project
+        self.email = message_from_string(mail)
+
+    def testSubjectEncoding(self):
+        (patch, comment) = find_content(self.project, self.email)
+        self.assertEquals(patch.name, self.subject)
+
+class SubjectUTF8QPEncodingTest(SubjectEncodingTest):
+    subject = u'test s\xfcbject'
+    subject_header = '=?utf-8?q?test=20s=c3=bcbject?='
+
+class SubjectUTF8QPMultipleEncodingTest(SubjectEncodingTest):
+    subject = u'test s\xfcbject'
+    subject_header = 'test =?utf-8?q?s=c3=bcbject?='
 
 class SenderCorrelationTest(unittest.TestCase):
     existing_sender = 'Existing Sender <existing@example.com>'
@@ -171,8 +210,6 @@ class SenderCorrelationTest(unittest.TestCase):
         (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)
@@ -197,3 +234,125 @@ class SenderCorrelationTest(unittest.TestCase):
 
     def tearDown(self):
         self.person.delete()
+
+class MultipleProjectPatchTest(unittest.TestCase):
+    """ Test that patches sent to multiple patchwork projects are
+        handled correctly """
+
+    test_comment = 'Test Comment'
+    patch_filename = '0001-add-line.patch'
+    msgid = '<1@example.com>'
+
+    def setUp(self):
+        self.p1 = Project(linkname = 'test-project-1', name = 'Project 1',
+                listid = '1.example.com', listemail='1@example.com')
+        self.p2 = Project(linkname = 'test-project-2', name = 'Project 2',
+                listid = '2.example.com', listemail='2@example.com')
+
+        self.p1.save()
+        self.p2.save()
+
+        patch = read_patch(self.patch_filename)
+        email = create_email(self.test_comment + '\n' + patch)
+        email['Message-Id'] = self.msgid
+
+        del email['List-ID']
+        email['List-ID'] = '<' + self.p1.listid + '>'
+        parse_mail(email)
+
+        del email['List-ID']
+        email['List-ID'] = '<' + self.p2.listid + '>'
+        parse_mail(email)
+
+    def testParsedProjects(self):
+        self.assertEquals(Patch.objects.filter(project = self.p1).count(), 1)
+        self.assertEquals(Patch.objects.filter(project = self.p2).count(), 1)
+
+    def tearDown(self):
+        self.p1.delete()
+        self.p2.delete()
+
+
+class MultipleProjectPatchCommentTest(MultipleProjectPatchTest):
+    """ Test that followups to multiple-project patches end up on the
+        correct patch """
+
+    comment_msgid = '<2@example.com>'
+    comment_content = 'test comment'
+
+    def setUp(self):
+        super(MultipleProjectPatchCommentTest, self).setUp()
+
+        for project in [self.p1, self.p2]:
+            email = MIMEText(self.comment_content)
+            email['From'] = defaults.sender
+            email['Subject'] = defaults.subject
+            email['Message-Id'] = self.comment_msgid
+            email['List-ID'] = '<' + project.listid + '>'
+            email['In-Reply-To'] = self.msgid
+            parse_mail(email)
+
+    def testParsedComment(self):
+        for project in [self.p1, self.p2]:
+            patch = Patch.objects.filter(project = project)[0]
+            # we should see two comments now - the original mail with the patch,
+            # and the one we parsed in setUp()
+            self.assertEquals(Comment.objects.filter(patch = patch).count(), 2)
+
+class ListIdHeaderTest(unittest.TestCase):
+    """ Test that we parse List-Id headers from mails correctly """
+    def setUp(self):
+        self.project = Project(linkname = 'test-project-1', name = 'Project 1',
+                listid = '1.example.com', listemail='1@example.com')
+        self.project.save()
+
+    def testNoListId(self):
+        email = MIMEText('')
+        project = find_project(email)
+        self.assertEquals(project, None)
+
+    def testBlankListId(self):
+        email = MIMEText('')
+        email['List-Id'] = ''
+        project = find_project(email)
+        self.assertEquals(project, None)
+
+    def testWhitespaceListId(self):
+        email = MIMEText('')
+        email['List-Id'] = ' '
+        project = find_project(email)
+        self.assertEquals(project, None)
+
+    def testSubstringListId(self):
+        email = MIMEText('')
+        email['List-Id'] = 'example.com'
+        project = find_project(email)
+        self.assertEquals(project, None)
+
+    def testShortListId(self):
+        """ Some mailing lists have List-Id headers in short formats, where it
+            is only the list ID itself (without enclosing angle-brackets). """
+        email = MIMEText('')
+        email['List-Id'] = self.project.listid
+        project = find_project(email)
+        self.assertEquals(project, self.project)
+
+    def testLongListId(self):
+        email = MIMEText('')
+        email['List-Id'] = 'Test text <%s>' % self.project.listid
+        project = find_project(email)
+        self.assertEquals(project, self.project)
+
+    def tearDown(self):
+        self.project.delete()
+
+
+class GitPullTest(PatchTest):
+    def testGitPullRequest(self):
+        mail = read_mail('0001-git-pull-request.mbox',
+                        project = self.project)
+        (patch, comment) = find_content(self.project, mail)
+        self.assertTrue(patch is not None)
+        self.assertTrue(patch.pull_url is not None)
+        self.assertTrue(patch.content is None)
+        self.assertTrue(comment is not None)