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, 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>'
177 class SenderCorrelationTest(unittest.TestCase):
178 existing_sender = 'Existing Sender <existing@example.com>'
179 non_existing_sender = 'Non-existing Sender <nonexisting@example.com>'
181 def mail(self, sender):
182 return message_from_string('From: %s\nSubject: Test\n\ntest\n' % sender)
185 self.existing_sender_mail = self.mail(self.existing_sender)
186 self.non_existing_sender_mail = self.mail(self.non_existing_sender)
187 (self.person, new) = find_author(self.existing_sender_mail)
190 def testExisingSender(self):
191 (person, new) = find_author(self.existing_sender_mail)
192 self.assertEqual(new, False)
193 self.assertEqual(person.id, self.person.id)
195 def testNonExisingSender(self):
196 (person, new) = find_author(self.non_existing_sender_mail)
197 self.assertEqual(new, True)
198 self.assertEqual(person.id, None)
200 def testExistingDifferentFormat(self):
201 mail = self.mail('existing@example.com')
202 (person, new) = find_author(mail)
203 self.assertEqual(new, False)
204 self.assertEqual(person.id, self.person.id)
206 def testExistingDifferentCase(self):
207 mail = self.mail(self.existing_sender.upper())
208 (person, new) = find_author(mail)
209 self.assertEqual(new, False)
210 self.assertEqual(person.id, self.person.id)
215 class MultipleProjectPatchTest(unittest.TestCase):
216 """ Test that patches sent to multiple patchwork projects are
217 handled correctly """
219 test_comment = 'Test Comment'
220 patch_filename = '0001-add-line.patch'
221 msgid = '<1@example.com>'
224 self.p1 = Project(linkname = 'test-project-1', name = 'Project 1',
225 listid = '1.example.com', listemail='1@example.com')
226 self.p2 = Project(linkname = 'test-project-2', name = 'Project 2',
227 listid = '2.example.com', listemail='2@example.com')
232 patch = read_patch(self.patch_filename)
233 email = create_email(self.test_comment + '\n' + patch)
234 email['Message-Id'] = self.msgid
237 email['List-ID'] = '<' + self.p1.listid + '>'
241 email['List-ID'] = '<' + self.p2.listid + '>'
244 def testParsedProjects(self):
245 self.assertEquals(Patch.objects.filter(project = self.p1).count(), 1)
246 self.assertEquals(Patch.objects.filter(project = self.p2).count(), 1)
253 class MultipleProjectPatchCommentTest(MultipleProjectPatchTest):
254 """ Test that followups to multiple-project patches end up on the
257 comment_msgid = '<2@example.com>'
258 comment_content = 'test comment'
261 super(MultipleProjectPatchCommentTest, self).setUp()
263 for project in [self.p1, self.p2]:
264 email = MIMEText(self.comment_content)
265 email['From'] = defaults.sender
266 email['Subject'] = defaults.subject
267 email['Message-Id'] = self.comment_msgid
268 email['List-ID'] = '<' + project.listid + '>'
269 email['In-Reply-To'] = self.msgid
272 def testParsedComment(self):
273 for project in [self.p1, self.p2]:
274 patch = Patch.objects.filter(project = project)[0]
275 # we should see two comments now - the original mail with the patch,
276 # and the one we parsed in setUp()
277 self.assertEquals(Comment.objects.filter(patch = patch).count(), 2)
279 class ListIdHeaderTest(unittest.TestCase):
280 """ Test that we parse List-Id headers from mails correctly """
282 self.project = Project(linkname = 'test-project-1', name = 'Project 1',
283 listid = '1.example.com', listemail='1@example.com')
286 def testNoListId(self):
288 project = find_project(email)
289 self.assertEquals(project, None)
291 def testBlankListId(self):
293 email['List-Id'] = ''
294 project = find_project(email)
295 self.assertEquals(project, None)
297 def testWhitespaceListId(self):
299 email['List-Id'] = ' '
300 project = find_project(email)
301 self.assertEquals(project, None)
303 def testSubstringListId(self):
305 email['List-Id'] = 'example.com'
306 project = find_project(email)
307 self.assertEquals(project, None)
309 def testShortListId(self):
310 """ Some mailing lists have List-Id headers in short formats, where it
311 is only the list ID itself (without enclosing angle-brackets). """
313 email['List-Id'] = self.project.listid
314 project = find_project(email)
315 self.assertEquals(project, self.project)
317 def testLongListId(self):
319 email['List-Id'] = 'Test text <%s>' % self.project.listid
320 project = find_project(email)
321 self.assertEquals(project, self.project)
324 self.project.delete()