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
+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')
+ 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
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):
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 <user@example.com>'
+
+ 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?= <user@example.com>'
+
+class SenderUTF8QPSplitEncodingTest(SenderEncodingTest):
+ sender_name = u'\xe9xample user'
+ 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()
+
+ 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()