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, Patch, Comment
24 from patchwork.tests.utils import read_patch, read_mail, 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, find_project, \
40 class InlinePatchTest(PatchTest):
41 patch_filename = '0001-add-line.patch'
42 test_comment = 'Test for attached patch'
45 self.orig_patch = read_patch(self.patch_filename)
46 email = create_email(self.test_comment + '\n' + self.orig_patch)
47 (self.patch, self.comment) = find_content(self.project, email)
49 def testPatchPresence(self):
50 self.assertTrue(self.patch is not None)
52 def testPatchContent(self):
53 self.assertEquals(self.patch.content, self.orig_patch)
55 def testCommentPresence(self):
56 self.assertTrue(self.comment is not None)
58 def testCommentContent(self):
59 self.assertEquals(self.comment.content, self.test_comment)
62 class AttachmentPatchTest(InlinePatchTest):
63 patch_filename = '0001-add-line.patch'
64 test_comment = 'Test for attached patch'
65 content_subtype = 'x-patch'
68 self.orig_patch = read_patch(self.patch_filename)
69 email = create_email(self.test_comment, multipart = True)
70 attachment = MIMEText(self.orig_patch, _subtype = self.content_subtype)
71 email.attach(attachment)
72 (self.patch, self.comment) = find_content(self.project, email)
74 class AttachmentXDiffPatchTest(AttachmentPatchTest):
75 content_subtype = 'x-diff'
77 class UTF8InlinePatchTest(InlinePatchTest):
78 patch_filename = '0002-utf-8.patch'
79 patch_encoding = 'utf-8'
82 self.orig_patch = read_patch(self.patch_filename, self.patch_encoding)
83 email = create_email(self.test_comment + '\n' + self.orig_patch,
84 content_encoding = self.patch_encoding)
85 (self.patch, self.comment) = find_content(self.project, email)
87 class NoCharsetInlinePatchTest(InlinePatchTest):
88 """ Test mails with no content-type or content-encoding header """
89 patch_filename = '0001-add-line.patch'
92 self.orig_patch = read_patch(self.patch_filename)
93 email = create_email(self.test_comment + '\n' + self.orig_patch)
94 del email['Content-Type']
95 del email['Content-Transfer-Encoding']
96 (self.patch, self.comment) = find_content(self.project, email)
98 class SignatureCommentTest(InlinePatchTest):
99 patch_filename = '0001-add-line.patch'
100 test_comment = 'Test comment\nmore comment'
103 self.orig_patch = read_patch(self.patch_filename)
104 email = create_email( \
105 self.test_comment + '\n' + \
106 '-- \nsig\n' + self.orig_patch)
107 (self.patch, self.comment) = find_content(self.project, email)
110 class ListFooterTest(InlinePatchTest):
111 patch_filename = '0001-add-line.patch'
112 test_comment = 'Test comment\nmore comment'
115 self.orig_patch = read_patch(self.patch_filename)
116 email = create_email( \
117 self.test_comment + '\n' + \
118 '_______________________________________________\n' + \
119 'Linuxppc-dev mailing list\n' + \
121 (self.patch, self.comment) = find_content(self.project, email)
124 class UpdateCommentTest(InlinePatchTest):
125 """ Test for '---\nUpdate: v2' style comments to patches. """
126 patch_filename = '0001-add-line.patch'
127 test_comment = 'Test comment\nmore comment\n---\nUpdate: test update'
129 class UpdateSigCommentTest(SignatureCommentTest):
130 """ Test for '---\nUpdate: v2' style comments to patches, with a sig """
131 patch_filename = '0001-add-line.patch'
132 test_comment = 'Test comment\nmore comment\n---\nUpdate: test update'
134 class SenderEncodingTest(unittest.TestCase):
135 sender_name = u'example user'
136 sender_email = 'user@example.com'
137 from_header = 'example user <user@example.com>'
140 mail = 'From: %s\n' % self.from_header + \
141 'Subject: test\n\n' + \
143 self.email = message_from_string(mail)
144 (self.person, new) = find_author(self.email)
151 self.assertEquals(self.person.name, self.sender_name)
154 self.assertEquals(self.person.email, self.sender_email)
156 def testDBQueryName(self):
157 db_person = Person.objects.get(name = self.sender_name)
158 self.assertEquals(self.person, db_person)
160 def testDBQueryEmail(self):
161 db_person = Person.objects.get(email = self.sender_email)
162 self.assertEquals(self.person, db_person)
165 class SenderUTF8QPEncodingTest(SenderEncodingTest):
166 sender_name = u'\xe9xample user'
167 from_header = '=?utf-8?q?=C3=A9xample=20user?= <user@example.com>'
169 class SenderUTF8QPSplitEncodingTest(SenderEncodingTest):
170 sender_name = u'\xe9xample user'
171 from_header = '=?utf-8?q?=C3=A9xample?= user <user@example.com>'
173 class SenderUTF8B64EncodingTest(SenderUTF8QPEncodingTest):
174 from_header = '=?utf-8?B?w6l4YW1wbGUgdXNlcg==?= <user@example.com>'
176 class SubjectEncodingTest(PatchTest):
177 sender = 'example user <user@example.com>'
178 subject = 'test subject'
179 subject_header = 'test subject'
182 mail = 'From: %s\n' % self.sender + \
183 'Subject: %s\n\n' % self.subject_header + \
184 'test\n\n' + defaults.patch
185 self.projects = defaults.project
186 self.email = message_from_string(mail)
188 def testSubjectEncoding(self):
189 (patch, comment) = find_content(self.project, self.email)
190 self.assertEquals(patch.name, self.subject)
192 class SubjectUTF8QPEncodingTest(SubjectEncodingTest):
193 subject = u'test s\xfcbject'
194 subject_header = '=?utf-8?q?test=20s=c3=bcbject?='
196 class SubjectUTF8QPMultipleEncodingTest(SubjectEncodingTest):
197 subject = u'test s\xfcbject'
198 subject_header = 'test =?utf-8?q?s=c3=bcbject?='
200 class SenderCorrelationTest(unittest.TestCase):
201 existing_sender = 'Existing Sender <existing@example.com>'
202 non_existing_sender = 'Non-existing Sender <nonexisting@example.com>'
204 def mail(self, sender):
205 return message_from_string('From: %s\nSubject: Test\n\ntest\n' % sender)
208 self.existing_sender_mail = self.mail(self.existing_sender)
209 self.non_existing_sender_mail = self.mail(self.non_existing_sender)
210 (self.person, new) = find_author(self.existing_sender_mail)
213 def testExisingSender(self):
214 (person, new) = find_author(self.existing_sender_mail)
215 self.assertEqual(new, False)
216 self.assertEqual(person.id, self.person.id)
218 def testNonExisingSender(self):
219 (person, new) = find_author(self.non_existing_sender_mail)
220 self.assertEqual(new, True)
221 self.assertEqual(person.id, None)
223 def testExistingDifferentFormat(self):
224 mail = self.mail('existing@example.com')
225 (person, new) = find_author(mail)
226 self.assertEqual(new, False)
227 self.assertEqual(person.id, self.person.id)
229 def testExistingDifferentCase(self):
230 mail = self.mail(self.existing_sender.upper())
231 (person, new) = find_author(mail)
232 self.assertEqual(new, False)
233 self.assertEqual(person.id, self.person.id)
238 class MultipleProjectPatchTest(unittest.TestCase):
239 """ Test that patches sent to multiple patchwork projects are
240 handled correctly """
242 test_comment = 'Test Comment'
243 patch_filename = '0001-add-line.patch'
244 msgid = '<1@example.com>'
247 self.p1 = Project(linkname = 'test-project-1', name = 'Project 1',
248 listid = '1.example.com', listemail='1@example.com')
249 self.p2 = Project(linkname = 'test-project-2', name = 'Project 2',
250 listid = '2.example.com', listemail='2@example.com')
255 patch = read_patch(self.patch_filename)
256 email = create_email(self.test_comment + '\n' + patch)
257 email['Message-Id'] = self.msgid
260 email['List-ID'] = '<' + self.p1.listid + '>'
264 email['List-ID'] = '<' + self.p2.listid + '>'
267 def testParsedProjects(self):
268 self.assertEquals(Patch.objects.filter(project = self.p1).count(), 1)
269 self.assertEquals(Patch.objects.filter(project = self.p2).count(), 1)
276 class MultipleProjectPatchCommentTest(MultipleProjectPatchTest):
277 """ Test that followups to multiple-project patches end up on the
280 comment_msgid = '<2@example.com>'
281 comment_content = 'test comment'
284 super(MultipleProjectPatchCommentTest, self).setUp()
286 for project in [self.p1, self.p2]:
287 email = MIMEText(self.comment_content)
288 email['From'] = defaults.sender
289 email['Subject'] = defaults.subject
290 email['Message-Id'] = self.comment_msgid
291 email['List-ID'] = '<' + project.listid + '>'
292 email['In-Reply-To'] = self.msgid
295 def testParsedComment(self):
296 for project in [self.p1, self.p2]:
297 patch = Patch.objects.filter(project = project)[0]
298 # we should see two comments now - the original mail with the patch,
299 # and the one we parsed in setUp()
300 self.assertEquals(Comment.objects.filter(patch = patch).count(), 2)
302 class ListIdHeaderTest(unittest.TestCase):
303 """ Test that we parse List-Id headers from mails correctly """
305 self.project = Project(linkname = 'test-project-1', name = 'Project 1',
306 listid = '1.example.com', listemail='1@example.com')
309 def testNoListId(self):
311 project = find_project(email)
312 self.assertEquals(project, None)
314 def testBlankListId(self):
316 email['List-Id'] = ''
317 project = find_project(email)
318 self.assertEquals(project, None)
320 def testWhitespaceListId(self):
322 email['List-Id'] = ' '
323 project = find_project(email)
324 self.assertEquals(project, None)
326 def testSubstringListId(self):
328 email['List-Id'] = 'example.com'
329 project = find_project(email)
330 self.assertEquals(project, None)
332 def testShortListId(self):
333 """ Some mailing lists have List-Id headers in short formats, where it
334 is only the list ID itself (without enclosing angle-brackets). """
336 email['List-Id'] = self.project.listid
337 project = find_project(email)
338 self.assertEquals(project, self.project)
340 def testLongListId(self):
342 email['List-Id'] = 'Test text <%s>' % self.project.listid
343 project = find_project(email)
344 self.assertEquals(project, self.project)
347 self.project.delete()
350 class GitPullTest(PatchTest):
351 mail_file = '0001-git-pull-request.mbox'
353 def testGitPullRequest(self):
354 mail = read_mail(self.mail_file, project = self.project)
355 (patch, comment) = find_content(self.project, mail)
356 self.assertTrue(patch is not None)
357 self.assertTrue(patch.pull_url is not None)
358 self.assertTrue(patch.content is None)
359 self.assertTrue(comment is not None)
361 class GitPullWrappedTest(GitPullTest):
362 mail_file = '0002-git-pull-request-wrapped.mbox'
364 class GitPullWithDiffTest(PatchTest):
365 def testGitPullWithDiff(self):
366 mail = read_mail('0003-git-pull-request-with-diff.mbox',
367 project = self.project)
368 (patch, comment) = find_content(self.project, mail)
369 self.assertTrue(patch is not None)
370 self.assertEqual('git://git.kernel.org/pub/scm/linux/kernel/git/tip/' +
371 'linux-2.6-tip.git x86-fixes-for-linus', patch.pull_url)
373 patch.content.startswith('diff --git a/arch/x86/include/asm/smp.h'),
375 self.assertTrue(comment is not None)