X-Git-Url: http://git.ozlabs.org/?p=patchwork;a=blobdiff_plain;f=apps%2Fpatchwork%2Ftests%2Fpatchparser.py;h=f6909ce9e458608300f060b09a3c9d316267ebb3;hp=3b10ef5166808ae7006224fbfb0330e59d06c201;hb=7d9334e879857f8a380bc9509b6cbf9972cecc25;hpb=26c532d2cfeec0bd4339b5c1db4bb2b3d4a3d3f0 diff --git a/apps/patchwork/tests/patchparser.py b/apps/patchwork/tests/patchparser.py index 3b10ef5..f6909ce 100644 --- a/apps/patchwork/tests/patchparser.py +++ b/apps/patchwork/tests/patchparser.py @@ -19,51 +19,31 @@ import unittest import os -from email.mime.text import MIMEText -from email.mime.multipart import MIMEMultipart -from patchwork.models import Project +from email import message_from_string +from patchwork.models import Project, Person, Patch, Comment +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 ' - default_subject = 'Test Subject' - project = Project(linkname = 'test-project') + default_sender = defaults.sender + default_subject = defaults.subject + project = defaults.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() - - -from patchwork.bin.parsemail import find_content +from patchwork.bin.parsemail import find_content, find_author, find_project, \ + parse_mail class InlinePatchTest(PatchTest): patch_filename = '0001-add-line.patch' 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): @@ -82,23 +62,263 @@ 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 = self.read_patch(self.patch_filename) - email = self.create_email(self.test_comment, multipart = True) - attachment = MIMEText(self.orig_patch, _subtype = 'x-patch') + self.orig_patch = read_patch(self.patch_filename) + email = create_email(self.test_comment, multipart = True) + 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' + + 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 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' 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) + +class ListFooterTest(InlinePatchTest): + patch_filename = '0001-add-line.patch' + test_comment = 'Test comment\nmore comment' + + def setUp(self): + self.orig_patch = read_patch(self.patch_filename) + email = create_email( \ + self.test_comment + '\n' + \ + '_______________________________________________\n' + \ + 'Linuxppc-dev mailing list\n' + \ + self.orig_patch) + (self.patch, self.comment) = find_content(self.project, email) + + +class UpdateCommentTest(InlinePatchTest): + """ Test for '---\nUpdate: v2' style comments to patches. """ + patch_filename = '0001-add-line.patch' + test_comment = 'Test comment\nmore comment\n---\nUpdate: test update' + +class UpdateSigCommentTest(SignatureCommentTest): + """ Test for '---\nUpdate: v2' style comments to patches, with a sig """ + patch_filename = '0001-add-line.patch' + test_comment = 'Test comment\nmore comment\n---\nUpdate: test update' + +class SenderEncodingTest(unittest.TestCase): + sender_name = u'example user' + sender_email = 'user@example.com' + from_header = 'example user ' + + def setUp(self): + mail = 'From: %s\n' % self.from_header + \ + 'Subject: test\n\n' + \ + 'test' + self.email = message_from_string(mail) + (self.person, new) = find_author(self.email) + self.person.save() + + def tearDown(self): + self.person.delete() + + def testName(self): + self.assertEquals(self.person.name, self.sender_name) + + def testEmail(self): + self.assertEquals(self.person.email, self.sender_email) + + def testDBQueryName(self): + db_person = Person.objects.get(name = self.sender_name) + self.assertEquals(self.person, db_person) + + def testDBQueryEmail(self): + db_person = Person.objects.get(email = self.sender_email) + self.assertEquals(self.person, db_person) + + +class SenderUTF8QPEncodingTest(SenderEncodingTest): + sender_name = u'\xe9xample user' + from_header = '=?utf-8?q?=C3=A9xample=20user?= ' + +class SenderUTF8QPSplitEncodingTest(SenderEncodingTest): + sender_name = u'\xe9xample user' + from_header = '=?utf-8?q?=C3=A9xample?= user ' + +class SenderUTF8B64EncodingTest(SenderUTF8QPEncodingTest): + from_header = '=?utf-8?B?w6l4YW1wbGUgdXNlcg==?= ' + + +class SenderCorrelationTest(unittest.TestCase): + existing_sender = 'Existing Sender ' + non_existing_sender = 'Non-existing Sender ' + + 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() + + 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() + +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()