1 # Patchwork - automated patch tracking system
2 # Copyright (C) 2008 Jeremy Kerr <jk@ozlabs.org>
4 # This file is part of the Patchwork package.
6 # Patchwork is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # Patchwork is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Patchwork; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 from email import message_from_string
23 from patchwork.models import Project, Person
24 from patchwork.tests.utils import read_patch, create_email, defaults
27 from email.mime.text import MIMEText
29 # Python 2.4 compatibility
30 from email.MIMEText import MIMEText
32 class PatchTest(unittest.TestCase):
33 default_sender = defaults.sender
34 default_subject = defaults.subject
35 project = defaults.project
37 from patchwork.bin.parsemail import find_content, find_author
39 class InlinePatchTest(PatchTest):
40 patch_filename = '0001-add-line.patch'
41 test_comment = 'Test for attached patch'
44 self.orig_patch = read_patch(self.patch_filename)
45 email = create_email(self.test_comment + '\n' + self.orig_patch)
46 (self.patch, self.comment) = find_content(self.project, email)
48 def testPatchPresence(self):
49 self.assertTrue(self.patch is not None)
51 def testPatchContent(self):
52 self.assertEquals(self.patch.content, self.orig_patch)
54 def testCommentPresence(self):
55 self.assertTrue(self.comment is not None)
57 def testCommentContent(self):
58 self.assertEquals(self.comment.content, self.test_comment)
61 class AttachmentPatchTest(InlinePatchTest):
62 patch_filename = '0001-add-line.patch'
63 test_comment = 'Test for attached patch'
66 self.orig_patch = read_patch(self.patch_filename)
67 email = create_email(self.test_comment, multipart = True)
68 attachment = MIMEText(self.orig_patch, _subtype = 'x-patch')
69 email.attach(attachment)
70 (self.patch, self.comment) = find_content(self.project, email)
73 class SignatureCommentTest(InlinePatchTest):
74 patch_filename = '0001-add-line.patch'
75 test_comment = 'Test comment\nmore comment'
78 self.orig_patch = read_patch(self.patch_filename)
79 email = create_email( \
80 self.test_comment + '\n' + \
81 '-- \nsig\n' + self.orig_patch)
82 (self.patch, self.comment) = find_content(self.project, email)
85 class ListFooterTest(InlinePatchTest):
86 patch_filename = '0001-add-line.patch'
87 test_comment = 'Test comment\nmore comment'
90 self.orig_patch = read_patch(self.patch_filename)
91 email = create_email( \
92 self.test_comment + '\n' + \
93 '_______________________________________________\n' + \
94 'Linuxppc-dev mailing list\n' + \
96 (self.patch, self.comment) = find_content(self.project, email)
99 class UpdateCommentTest(InlinePatchTest):
100 """ Test for '---\nUpdate: v2' style comments to patches. """
101 patch_filename = '0001-add-line.patch'
102 test_comment = 'Test comment\nmore comment\n---\nUpdate: test update'
104 class UpdateSigCommentTest(SignatureCommentTest):
105 """ Test for '---\nUpdate: v2' style comments to patches, with a sig """
106 patch_filename = '0001-add-line.patch'
107 test_comment = 'Test comment\nmore comment\n---\nUpdate: test update'
109 class SenderEncodingTest(unittest.TestCase):
110 sender_name = u'example user'
111 sender_email = 'user@example.com'
112 from_header = 'example user <user@example.com>'
115 mail = 'From: %s\n' % self.from_header + \
116 'Subject: test\n\n' + \
118 self.email = message_from_string(mail)
119 (self.person, new) = find_author(self.email)
126 self.assertEquals(self.person.name, self.sender_name)
129 self.assertEquals(self.person.email, self.sender_email)
131 def testDBQueryName(self):
132 db_person = Person.objects.get(name = self.sender_name)
133 self.assertEquals(self.person, db_person)
135 def testDBQueryEmail(self):
136 db_person = Person.objects.get(email = self.sender_email)
137 self.assertEquals(self.person, db_person)
140 class SenderUTF8QPEncodingTest(SenderEncodingTest):
141 sender_name = u'\xe9xample user'
142 from_header = '=?utf-8?q?=C3=A9xample=20user?= <user@example.com>'
144 class SenderUTF8QPSplitEncodingTest(SenderEncodingTest):
145 sender_name = u'\xe9xample user'
146 from_header = '=?utf-8?q?=C3=A9xample?= user <user@example.com>'
148 class SenderUTF8B64EncodingTest(SenderUTF8QPEncodingTest):
149 from_header = '=?utf-8?B?w6l4YW1wbGUgdXNlcg==?= <user@example.com>'
152 class SenderCorrelationTest(unittest.TestCase):
153 existing_sender = 'Existing Sender <existing@example.com>'
154 non_existing_sender = 'Non-existing Sender <nonexisting@example.com>'
156 def mail(self, sender):
157 return message_from_string('From: %s\nSubject: Test\n\ntest\n' % sender)
160 self.existing_sender_mail = self.mail(self.existing_sender)
161 self.non_existing_sender_mail = self.mail(self.non_existing_sender)
162 (self.person, new) = find_author(self.existing_sender_mail)
165 print Person.objects.all()
167 def testExisingSender(self):
168 (person, new) = find_author(self.existing_sender_mail)
169 self.assertEqual(new, False)
170 self.assertEqual(person.id, self.person.id)
172 def testNonExisingSender(self):
173 (person, new) = find_author(self.non_existing_sender_mail)
174 self.assertEqual(new, True)
175 self.assertEqual(person.id, None)
177 def testExistingDifferentFormat(self):
178 mail = self.mail('existing@example.com')
179 (person, new) = find_author(mail)
180 self.assertEqual(new, False)
181 self.assertEqual(person.id, self.person.id)
183 def testExistingDifferentCase(self):
184 mail = self.mail(self.existing_sender.upper())
185 (person, new) = find_author(mail)
186 self.assertEqual(new, False)
187 self.assertEqual(person.id, self.person.id)