X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=apps%2Fpatchwork%2Ftests%2Fpatchparser.py;h=e508dc08a9beffcf376d5f3bb28baa84a76d44ad;hb=406d17e8df324c235172ed673ef65e998ca1f6a4;hp=6fe7968bfafa32aa12eef70c26412f72ceb21e70;hpb=918fab011f24f22f9915674c104258e20a5fcf26;p=patchwork diff --git a/apps/patchwork/tests/patchparser.py b/apps/patchwork/tests/patchparser.py index 6fe7968..e508dc0 100644 --- a/apps/patchwork/tests/patchparser.py +++ b/apps/patchwork/tests/patchparser.py @@ -19,42 +19,20 @@ 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 ' - 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): @@ -83,22 +61,35 @@ 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 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 +100,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 +156,46 @@ class SenderUTF8QPEncodingTest(SenderEncodingTest): class SenderUTF8QPSplitEncodingTest(SenderEncodingTest): sender_name = u'\xe9xample user' - from_header = '=?utf-8?q?=C3=A9xample=20?= 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()