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 DiffWordInCommentTest(InlinePatchTest):
125 test_comment = 'Lines can start with words beginning in "diff"\n' + \
126 'difficult\nDifferent'
129 class UpdateCommentTest(InlinePatchTest):
130 """ Test for '---\nUpdate: v2' style comments to patches. """
131 patch_filename = '0001-add-line.patch'
132 test_comment = 'Test comment\nmore comment\n---\nUpdate: test update'
134 class UpdateSigCommentTest(SignatureCommentTest):
135 """ Test for '---\nUpdate: v2' style comments to patches, with a sig """
136 patch_filename = '0001-add-line.patch'
137 test_comment = 'Test comment\nmore comment\n---\nUpdate: test update'
139 class SenderEncodingTest(unittest.TestCase):
140 sender_name = u'example user'
141 sender_email = 'user@example.com'
142 from_header = 'example user <user@example.com>'
145 mail = 'From: %s\n' % self.from_header + \
146 'Subject: test\n\n' + \
148 self.email = message_from_string(mail)
149 (self.person, new) = find_author(self.email)
156 self.assertEquals(self.person.name, self.sender_name)
159 self.assertEquals(self.person.email, self.sender_email)
161 def testDBQueryName(self):
162 db_person = Person.objects.get(name = self.sender_name)
163 self.assertEquals(self.person, db_person)
165 def testDBQueryEmail(self):
166 db_person = Person.objects.get(email = self.sender_email)
167 self.assertEquals(self.person, db_person)
170 class SenderUTF8QPEncodingTest(SenderEncodingTest):
171 sender_name = u'\xe9xample user'
172 from_header = '=?utf-8?q?=C3=A9xample=20user?= <user@example.com>'
174 class SenderUTF8QPSplitEncodingTest(SenderEncodingTest):
175 sender_name = u'\xe9xample user'
176 from_header = '=?utf-8?q?=C3=A9xample?= user <user@example.com>'
178 class SenderUTF8B64EncodingTest(SenderUTF8QPEncodingTest):
179 from_header = '=?utf-8?B?w6l4YW1wbGUgdXNlcg==?= <user@example.com>'
181 class SubjectEncodingTest(PatchTest):
182 sender = 'example user <user@example.com>'
183 subject = 'test subject'
184 subject_header = 'test subject'
187 mail = 'From: %s\n' % self.sender + \
188 'Subject: %s\n\n' % self.subject_header + \
189 'test\n\n' + defaults.patch
190 self.projects = defaults.project
191 self.email = message_from_string(mail)
193 def testSubjectEncoding(self):
194 (patch, comment) = find_content(self.project, self.email)
195 self.assertEquals(patch.name, self.subject)
197 class SubjectUTF8QPEncodingTest(SubjectEncodingTest):
198 subject = u'test s\xfcbject'
199 subject_header = '=?utf-8?q?test=20s=c3=bcbject?='
201 class SubjectUTF8QPMultipleEncodingTest(SubjectEncodingTest):
202 subject = u'test s\xfcbject'
203 subject_header = 'test =?utf-8?q?s=c3=bcbject?='
205 class SenderCorrelationTest(unittest.TestCase):
206 existing_sender = 'Existing Sender <existing@example.com>'
207 non_existing_sender = 'Non-existing Sender <nonexisting@example.com>'
209 def mail(self, sender):
210 return message_from_string('From: %s\nSubject: Test\n\ntest\n' % sender)
213 self.existing_sender_mail = self.mail(self.existing_sender)
214 self.non_existing_sender_mail = self.mail(self.non_existing_sender)
215 (self.person, new) = find_author(self.existing_sender_mail)
218 def testExisingSender(self):
219 (person, new) = find_author(self.existing_sender_mail)
220 self.assertEqual(new, False)
221 self.assertEqual(person.id, self.person.id)
223 def testNonExisingSender(self):
224 (person, new) = find_author(self.non_existing_sender_mail)
225 self.assertEqual(new, True)
226 self.assertEqual(person.id, None)
228 def testExistingDifferentFormat(self):
229 mail = self.mail('existing@example.com')
230 (person, new) = find_author(mail)
231 self.assertEqual(new, False)
232 self.assertEqual(person.id, self.person.id)
234 def testExistingDifferentCase(self):
235 mail = self.mail(self.existing_sender.upper())
236 (person, new) = find_author(mail)
237 self.assertEqual(new, False)
238 self.assertEqual(person.id, self.person.id)
243 class MultipleProjectPatchTest(unittest.TestCase):
244 """ Test that patches sent to multiple patchwork projects are
245 handled correctly """
247 test_comment = 'Test Comment'
248 patch_filename = '0001-add-line.patch'
249 msgid = '<1@example.com>'
252 self.p1 = Project(linkname = 'test-project-1', name = 'Project 1',
253 listid = '1.example.com', listemail='1@example.com')
254 self.p2 = Project(linkname = 'test-project-2', name = 'Project 2',
255 listid = '2.example.com', listemail='2@example.com')
260 patch = read_patch(self.patch_filename)
261 email = create_email(self.test_comment + '\n' + patch)
262 email['Message-Id'] = self.msgid
265 email['List-ID'] = '<' + self.p1.listid + '>'
269 email['List-ID'] = '<' + self.p2.listid + '>'
272 def testParsedProjects(self):
273 self.assertEquals(Patch.objects.filter(project = self.p1).count(), 1)
274 self.assertEquals(Patch.objects.filter(project = self.p2).count(), 1)
281 class MultipleProjectPatchCommentTest(MultipleProjectPatchTest):
282 """ Test that followups to multiple-project patches end up on the
285 comment_msgid = '<2@example.com>'
286 comment_content = 'test comment'
289 super(MultipleProjectPatchCommentTest, self).setUp()
291 for project in [self.p1, self.p2]:
292 email = MIMEText(self.comment_content)
293 email['From'] = defaults.sender
294 email['Subject'] = defaults.subject
295 email['Message-Id'] = self.comment_msgid
296 email['List-ID'] = '<' + project.listid + '>'
297 email['In-Reply-To'] = self.msgid
300 def testParsedComment(self):
301 for project in [self.p1, self.p2]:
302 patch = Patch.objects.filter(project = project)[0]
303 # we should see two comments now - the original mail with the patch,
304 # and the one we parsed in setUp()
305 self.assertEquals(Comment.objects.filter(patch = patch).count(), 2)
307 class ListIdHeaderTest(unittest.TestCase):
308 """ Test that we parse List-Id headers from mails correctly """
310 self.project = Project(linkname = 'test-project-1', name = 'Project 1',
311 listid = '1.example.com', listemail='1@example.com')
314 def testNoListId(self):
316 project = find_project(email)
317 self.assertEquals(project, None)
319 def testBlankListId(self):
321 email['List-Id'] = ''
322 project = find_project(email)
323 self.assertEquals(project, None)
325 def testWhitespaceListId(self):
327 email['List-Id'] = ' '
328 project = find_project(email)
329 self.assertEquals(project, None)
331 def testSubstringListId(self):
333 email['List-Id'] = 'example.com'
334 project = find_project(email)
335 self.assertEquals(project, None)
337 def testShortListId(self):
338 """ Some mailing lists have List-Id headers in short formats, where it
339 is only the list ID itself (without enclosing angle-brackets). """
341 email['List-Id'] = self.project.listid
342 project = find_project(email)
343 self.assertEquals(project, self.project)
345 def testLongListId(self):
347 email['List-Id'] = 'Test text <%s>' % self.project.listid
348 project = find_project(email)
349 self.assertEquals(project, self.project)
352 self.project.delete()
354 class MBoxPatchTest(PatchTest):
356 self.mail = read_mail(self.mail_file, project = self.project)
358 class GitPullTest(MBoxPatchTest):
359 mail_file = '0001-git-pull-request.mbox'
361 def testGitPullRequest(self):
362 (patch, comment) = find_content(self.project, self.mail)
363 self.assertTrue(patch is not None)
364 self.assertTrue(patch.pull_url is not None)
365 self.assertTrue(patch.content is None)
366 self.assertTrue(comment is not None)
368 class GitPullWrappedTest(GitPullTest):
369 mail_file = '0002-git-pull-request-wrapped.mbox'
371 class GitPullWithDiffTest(MBoxPatchTest):
372 mail_file = '0003-git-pull-request-with-diff.mbox'
374 def testGitPullWithDiff(self):
375 (patch, comment) = find_content(self.project, self.mail)
376 self.assertTrue(patch is not None)
377 self.assertEqual('git://git.kernel.org/pub/scm/linux/kernel/git/tip/' +
378 'linux-2.6-tip.git x86-fixes-for-linus', patch.pull_url)
380 patch.content.startswith('diff --git a/arch/x86/include/asm/smp.h'),
382 self.assertTrue(comment is not None)
384 class GitPullGitSSHUrlTest(GitPullTest):
385 mail_file = '0004-git-pull-request-git+ssh.mbox'
387 class GitPullSSHUrlTest(GitPullTest):
388 mail_file = '0005-git-pull-request-ssh.mbox'
390 class GitPullHTTPUrlTest(GitPullTest):
391 mail_file = '0006-git-pull-request-http.mbox'
393 class CVSFormatPatchTest(MBoxPatchTest):
394 mail_file = '0007-cvs-format-diff.mbox'
397 (patch, comment) = find_content(self.project, self.mail)
398 self.assertTrue(patch is not None)
399 self.assertTrue(comment is not None)
400 self.assertTrue(patch.content.startswith('Index'))