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, State, \
24 get_default_initial_patch_state
25 from patchwork.tests.utils import read_patch, read_mail, create_email, \
29 from email.mime.text import MIMEText
31 # Python 2.4 compatibility
32 from email.MIMEText import MIMEText
34 class PatchTest(unittest.TestCase):
35 default_sender = defaults.sender
36 default_subject = defaults.subject
37 project = defaults.project
39 from patchwork.bin.parsemail import find_content, find_author, find_project, \
42 class InlinePatchTest(PatchTest):
43 patch_filename = '0001-add-line.patch'
44 test_comment = 'Test for attached patch'
47 self.orig_patch = read_patch(self.patch_filename)
48 email = create_email(self.test_comment + '\n' + self.orig_patch)
49 (self.patch, self.comment) = find_content(self.project, email)
51 def testPatchPresence(self):
52 self.assertTrue(self.patch is not None)
54 def testPatchContent(self):
55 self.assertEquals(self.patch.content, self.orig_patch)
57 def testCommentPresence(self):
58 self.assertTrue(self.comment is not None)
60 def testCommentContent(self):
61 self.assertEquals(self.comment.content, self.test_comment)
64 class AttachmentPatchTest(InlinePatchTest):
65 patch_filename = '0001-add-line.patch'
66 test_comment = 'Test for attached patch'
67 content_subtype = 'x-patch'
70 self.orig_patch = read_patch(self.patch_filename)
71 email = create_email(self.test_comment, multipart = True)
72 attachment = MIMEText(self.orig_patch, _subtype = self.content_subtype)
73 email.attach(attachment)
74 (self.patch, self.comment) = find_content(self.project, email)
76 class AttachmentXDiffPatchTest(AttachmentPatchTest):
77 content_subtype = 'x-diff'
79 class UTF8InlinePatchTest(InlinePatchTest):
80 patch_filename = '0002-utf-8.patch'
81 patch_encoding = 'utf-8'
84 self.orig_patch = read_patch(self.patch_filename, self.patch_encoding)
85 email = create_email(self.test_comment + '\n' + self.orig_patch,
86 content_encoding = self.patch_encoding)
87 (self.patch, self.comment) = find_content(self.project, email)
89 class NoCharsetInlinePatchTest(InlinePatchTest):
90 """ Test mails with no content-type or content-encoding header """
91 patch_filename = '0001-add-line.patch'
94 self.orig_patch = read_patch(self.patch_filename)
95 email = create_email(self.test_comment + '\n' + self.orig_patch)
96 del email['Content-Type']
97 del email['Content-Transfer-Encoding']
98 (self.patch, self.comment) = find_content(self.project, email)
100 class SignatureCommentTest(InlinePatchTest):
101 patch_filename = '0001-add-line.patch'
102 test_comment = 'Test comment\nmore comment'
105 self.orig_patch = read_patch(self.patch_filename)
106 email = create_email( \
107 self.test_comment + '\n' + \
108 '-- \nsig\n' + self.orig_patch)
109 (self.patch, self.comment) = find_content(self.project, email)
112 class ListFooterTest(InlinePatchTest):
113 patch_filename = '0001-add-line.patch'
114 test_comment = 'Test comment\nmore comment'
117 self.orig_patch = read_patch(self.patch_filename)
118 email = create_email( \
119 self.test_comment + '\n' + \
120 '_______________________________________________\n' + \
121 'Linuxppc-dev mailing list\n' + \
123 (self.patch, self.comment) = find_content(self.project, email)
126 class DiffWordInCommentTest(InlinePatchTest):
127 test_comment = 'Lines can start with words beginning in "diff"\n' + \
128 'difficult\nDifferent'
131 class UpdateCommentTest(InlinePatchTest):
132 """ Test for '---\nUpdate: v2' style comments to patches. """
133 patch_filename = '0001-add-line.patch'
134 test_comment = 'Test comment\nmore comment\n---\nUpdate: test update'
136 class UpdateSigCommentTest(SignatureCommentTest):
137 """ Test for '---\nUpdate: v2' style comments to patches, with a sig """
138 patch_filename = '0001-add-line.patch'
139 test_comment = 'Test comment\nmore comment\n---\nUpdate: test update'
141 class SenderEncodingTest(unittest.TestCase):
142 sender_name = u'example user'
143 sender_email = 'user@example.com'
144 from_header = 'example user <user@example.com>'
147 mail = 'From: %s\n' % self.from_header + \
148 'Subject: test\n\n' + \
150 self.email = message_from_string(mail)
151 (self.person, new) = find_author(self.email)
158 self.assertEquals(self.person.name, self.sender_name)
161 self.assertEquals(self.person.email, self.sender_email)
163 def testDBQueryName(self):
164 db_person = Person.objects.get(name = self.sender_name)
165 self.assertEquals(self.person, db_person)
167 def testDBQueryEmail(self):
168 db_person = Person.objects.get(email = self.sender_email)
169 self.assertEquals(self.person, db_person)
172 class SenderUTF8QPEncodingTest(SenderEncodingTest):
173 sender_name = u'\xe9xample user'
174 from_header = '=?utf-8?q?=C3=A9xample=20user?= <user@example.com>'
176 class SenderUTF8QPSplitEncodingTest(SenderEncodingTest):
177 sender_name = u'\xe9xample user'
178 from_header = '=?utf-8?q?=C3=A9xample?= user <user@example.com>'
180 class SenderUTF8B64EncodingTest(SenderUTF8QPEncodingTest):
181 from_header = '=?utf-8?B?w6l4YW1wbGUgdXNlcg==?= <user@example.com>'
183 class SubjectEncodingTest(PatchTest):
184 sender = 'example user <user@example.com>'
185 subject = 'test subject'
186 subject_header = 'test subject'
189 mail = 'From: %s\n' % self.sender + \
190 'Subject: %s\n\n' % self.subject_header + \
191 'test\n\n' + defaults.patch
192 self.projects = defaults.project
193 self.email = message_from_string(mail)
195 def testSubjectEncoding(self):
196 (patch, comment) = find_content(self.project, self.email)
197 self.assertEquals(patch.name, self.subject)
199 class SubjectUTF8QPEncodingTest(SubjectEncodingTest):
200 subject = u'test s\xfcbject'
201 subject_header = '=?utf-8?q?test=20s=c3=bcbject?='
203 class SubjectUTF8QPMultipleEncodingTest(SubjectEncodingTest):
204 subject = u'test s\xfcbject'
205 subject_header = 'test =?utf-8?q?s=c3=bcbject?='
207 class SenderCorrelationTest(unittest.TestCase):
208 existing_sender = 'Existing Sender <existing@example.com>'
209 non_existing_sender = 'Non-existing Sender <nonexisting@example.com>'
211 def mail(self, sender):
212 return message_from_string('From: %s\nSubject: Test\n\ntest\n' % sender)
215 self.existing_sender_mail = self.mail(self.existing_sender)
216 self.non_existing_sender_mail = self.mail(self.non_existing_sender)
217 (self.person, new) = find_author(self.existing_sender_mail)
220 def testExisingSender(self):
221 (person, new) = find_author(self.existing_sender_mail)
222 self.assertEqual(new, False)
223 self.assertEqual(person.id, self.person.id)
225 def testNonExisingSender(self):
226 (person, new) = find_author(self.non_existing_sender_mail)
227 self.assertEqual(new, True)
228 self.assertEqual(person.id, None)
230 def testExistingDifferentFormat(self):
231 mail = self.mail('existing@example.com')
232 (person, new) = find_author(mail)
233 self.assertEqual(new, False)
234 self.assertEqual(person.id, self.person.id)
236 def testExistingDifferentCase(self):
237 mail = self.mail(self.existing_sender.upper())
238 (person, new) = find_author(mail)
239 self.assertEqual(new, False)
240 self.assertEqual(person.id, self.person.id)
245 class MultipleProjectPatchTest(unittest.TestCase):
246 """ Test that patches sent to multiple patchwork projects are
247 handled correctly """
249 test_comment = 'Test Comment'
250 patch_filename = '0001-add-line.patch'
251 msgid = '<1@example.com>'
254 self.p1 = Project(linkname = 'test-project-1', name = 'Project 1',
255 listid = '1.example.com', listemail='1@example.com')
256 self.p2 = Project(linkname = 'test-project-2', name = 'Project 2',
257 listid = '2.example.com', listemail='2@example.com')
262 patch = read_patch(self.patch_filename)
263 email = create_email(self.test_comment + '\n' + patch)
264 email['Message-Id'] = self.msgid
267 email['List-ID'] = '<' + self.p1.listid + '>'
271 email['List-ID'] = '<' + self.p2.listid + '>'
274 def testParsedProjects(self):
275 self.assertEquals(Patch.objects.filter(project = self.p1).count(), 1)
276 self.assertEquals(Patch.objects.filter(project = self.p2).count(), 1)
283 class MultipleProjectPatchCommentTest(MultipleProjectPatchTest):
284 """ Test that followups to multiple-project patches end up on the
287 comment_msgid = '<2@example.com>'
288 comment_content = 'test comment'
291 super(MultipleProjectPatchCommentTest, self).setUp()
293 for project in [self.p1, self.p2]:
294 email = MIMEText(self.comment_content)
295 email['From'] = defaults.sender
296 email['Subject'] = defaults.subject
297 email['Message-Id'] = self.comment_msgid
298 email['List-ID'] = '<' + project.listid + '>'
299 email['In-Reply-To'] = self.msgid
302 def testParsedComment(self):
303 for project in [self.p1, self.p2]:
304 patch = Patch.objects.filter(project = project)[0]
305 # we should see two comments now - the original mail with the patch,
306 # and the one we parsed in setUp()
307 self.assertEquals(Comment.objects.filter(patch = patch).count(), 2)
309 class ListIdHeaderTest(unittest.TestCase):
310 """ Test that we parse List-Id headers from mails correctly """
312 self.project = Project(linkname = 'test-project-1', name = 'Project 1',
313 listid = '1.example.com', listemail='1@example.com')
316 def testNoListId(self):
318 project = find_project(email)
319 self.assertEquals(project, None)
321 def testBlankListId(self):
323 email['List-Id'] = ''
324 project = find_project(email)
325 self.assertEquals(project, None)
327 def testWhitespaceListId(self):
329 email['List-Id'] = ' '
330 project = find_project(email)
331 self.assertEquals(project, None)
333 def testSubstringListId(self):
335 email['List-Id'] = 'example.com'
336 project = find_project(email)
337 self.assertEquals(project, None)
339 def testShortListId(self):
340 """ Some mailing lists have List-Id headers in short formats, where it
341 is only the list ID itself (without enclosing angle-brackets). """
343 email['List-Id'] = self.project.listid
344 project = find_project(email)
345 self.assertEquals(project, self.project)
347 def testLongListId(self):
349 email['List-Id'] = 'Test text <%s>' % self.project.listid
350 project = find_project(email)
351 self.assertEquals(project, self.project)
354 self.project.delete()
356 class MBoxPatchTest(PatchTest):
358 self.mail = read_mail(self.mail_file, project = self.project)
360 class GitPullTest(MBoxPatchTest):
361 mail_file = '0001-git-pull-request.mbox'
363 def testGitPullRequest(self):
364 (patch, comment) = find_content(self.project, self.mail)
365 self.assertTrue(patch is not None)
366 self.assertTrue(patch.pull_url is not None)
367 self.assertTrue(patch.content is None)
368 self.assertTrue(comment is not None)
370 class GitPullWrappedTest(GitPullTest):
371 mail_file = '0002-git-pull-request-wrapped.mbox'
373 class GitPullWithDiffTest(MBoxPatchTest):
374 mail_file = '0003-git-pull-request-with-diff.mbox'
376 def testGitPullWithDiff(self):
377 (patch, comment) = find_content(self.project, self.mail)
378 self.assertTrue(patch is not None)
379 self.assertEqual('git://git.kernel.org/pub/scm/linux/kernel/git/tip/' +
380 'linux-2.6-tip.git x86-fixes-for-linus', patch.pull_url)
382 patch.content.startswith('diff --git a/arch/x86/include/asm/smp.h'),
384 self.assertTrue(comment is not None)
386 class GitPullGitSSHUrlTest(GitPullTest):
387 mail_file = '0004-git-pull-request-git+ssh.mbox'
389 class GitPullSSHUrlTest(GitPullTest):
390 mail_file = '0005-git-pull-request-ssh.mbox'
392 class GitPullHTTPUrlTest(GitPullTest):
393 mail_file = '0006-git-pull-request-http.mbox'
395 class CVSFormatPatchTest(MBoxPatchTest):
396 mail_file = '0007-cvs-format-diff.mbox'
399 (patch, comment) = find_content(self.project, self.mail)
400 self.assertTrue(patch is not None)
401 self.assertTrue(comment is not None)
402 self.assertTrue(patch.content.startswith('Index'))
404 class DelegateRequestTest(unittest.TestCase):
405 patch_filename = '0001-add-line.patch'
406 msgid = '<1@example.com>'
407 invalid_delegate_email = "nobody"
410 self.patch = read_patch(self.patch_filename)
411 self.user = create_user()
412 self.p1 = Project(linkname = 'test-project-1', name = 'Project 1',
413 listid = '1.example.com', listemail='1@example.com')
417 email = create_email(self.patch)
419 email['List-ID'] = '<' + self.p1.listid + '>'
420 email['Message-Id'] = self.msgid
423 def testDelegate(self):
424 email = self.get_email()
425 email['X-Patchwork-Delegate'] = self.user.email
427 self.assertEquals(Patch.objects.filter(project=self.p1).count(), 1)
428 self.assertEquals(Patch.objects.get(pk=1).delegate, self.user)
430 def testNoDelegate(self):
431 email = self.get_email()
433 self.assertEquals(Patch.objects.filter(project=self.p1).count(), 1)
434 self.assertEquals(Patch.objects.get(pk=1).delegate, None)
436 def testInvalidDelegateFallsBackToNoDelegate(self):
437 email = self.get_email()
438 email['X-Patchwork-Delegate'] = self.invalid_delegate_email
440 self.assertEquals(Patch.objects.filter(project=self.p1).count(), 1)
441 self.assertEquals(Patch.objects.get(pk=1).delegate, None)
447 class InitialPatchStateTest(unittest.TestCase):
448 patch_filename = '0001-add-line.patch'
449 msgid = '<1@example.com>'
450 invalid_state_name = "Nonexistent Test State"
453 self.patch = read_patch(self.patch_filename)
454 self.user = create_user()
455 self.p1 = Project(linkname = 'test-project-1', name = 'Project 1',
456 listid = '1.example.com', listemail='1@example.com')
458 self.default_state = get_default_initial_patch_state()
459 self.nondefault_state = State.objects.get(name="Accepted")
462 email = create_email(self.patch)
464 email['List-ID'] = '<' + self.p1.listid + '>'
465 email['Message-Id'] = self.msgid
468 def testNonDefaultStateIsActuallyNotTheDefaultState(self):
469 self.assertNotEqual(self.default_state, self.nondefault_state)
471 def testExplicitNonDefaultStateRequest(self):
472 email = self.get_email()
473 email['X-Patchwork-State'] = self.nondefault_state.name
475 self.assertEquals(Patch.objects.filter(project=self.p1).count(), 1)
476 self.assertEquals(Patch.objects.get(pk=1).state, self.nondefault_state)
478 def testExplicitDefaultStateRequest(self):
479 email = self.get_email()
480 email['X-Patchwork-State'] = self.default_state.name
482 self.assertEquals(Patch.objects.filter(project=self.p1).count(), 1)
483 self.assertEquals(Patch.objects.get(pk=1).state, self.default_state)
485 def testImplicitDefaultStateRequest(self):
486 email = self.get_email()
488 self.assertEquals(Patch.objects.filter(project=self.p1).count(), 1)
489 self.assertEquals(Patch.objects.get(pk=1).state, self.default_state)
491 def testInvalidTestStateDoesNotExist(self):
492 with self.assertRaises(State.DoesNotExist):
493 State.objects.get(name=self.invalid_state_name)
495 def testInvalidStateRequestFallsBackToDefaultState(self):
496 email = self.get_email()
497 email['X-Patchwork-State'] = self.invalid_state_name
499 self.assertEquals(Patch.objects.filter(project=self.p1).count(), 1)
500 self.assertEquals(Patch.objects.get(pk=1).state, self.default_state)