]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/tests/patchparser.py
tests/parser: Add subject encoding tests
[patchwork] / apps / patchwork / tests / patchparser.py
1 # Patchwork - automated patch tracking system
2 # Copyright (C) 2008 Jeremy Kerr <jk@ozlabs.org>
3 #
4 # This file is part of the Patchwork package.
5 #
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.
10 #
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.
15 #
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
19
20 import unittest
21 import os
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
25
26 try:
27     from email.mime.text import MIMEText
28 except ImportError:
29     # Python 2.4 compatibility
30     from email.MIMEText import MIMEText
31
32 class PatchTest(unittest.TestCase):
33     default_sender = defaults.sender
34     default_subject = defaults.subject
35     project = defaults.project
36
37 from patchwork.bin.parsemail import find_content, find_author, find_project, \
38                                     parse_mail
39
40 class InlinePatchTest(PatchTest):
41     patch_filename = '0001-add-line.patch'
42     test_comment = 'Test for attached patch'
43
44     def setUp(self):
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)
48
49     def testPatchPresence(self):
50         self.assertTrue(self.patch is not None)
51
52     def testPatchContent(self):
53         self.assertEquals(self.patch.content, self.orig_patch)
54
55     def testCommentPresence(self):
56         self.assertTrue(self.comment is not None)
57
58     def testCommentContent(self):
59         self.assertEquals(self.comment.content, self.test_comment)
60
61
62 class AttachmentPatchTest(InlinePatchTest):
63     patch_filename = '0001-add-line.patch'
64     test_comment = 'Test for attached patch'
65     content_subtype = 'x-patch'
66
67     def setUp(self):
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)
73
74 class AttachmentXDiffPatchTest(AttachmentPatchTest):
75     content_subtype = 'x-diff'
76
77 class UTF8InlinePatchTest(InlinePatchTest):
78     patch_filename = '0002-utf-8.patch'
79     patch_encoding = 'utf-8'
80
81     def setUp(self):
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)
86
87 class NoCharsetInlinePatchTest(InlinePatchTest):
88     """ Test mails with no content-type or content-encoding header """
89     patch_filename = '0001-add-line.patch'
90
91     def setUp(self):
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)
97
98 class SignatureCommentTest(InlinePatchTest):
99     patch_filename = '0001-add-line.patch'
100     test_comment = 'Test comment\nmore comment'
101
102     def setUp(self):
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)
108
109
110 class ListFooterTest(InlinePatchTest):
111     patch_filename = '0001-add-line.patch'
112     test_comment = 'Test comment\nmore comment'
113
114     def setUp(self):
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' + \
120                 self.orig_patch)
121         (self.patch, self.comment) = find_content(self.project, email)
122
123
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'
128
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'
133
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>'
138
139     def setUp(self):
140         mail = 'From: %s\n' % self.from_header + \
141                'Subject: test\n\n' + \
142                'test'
143         self.email = message_from_string(mail)
144         (self.person, new) = find_author(self.email)
145         self.person.save()
146
147     def tearDown(self):
148         self.person.delete()
149
150     def testName(self):
151         self.assertEquals(self.person.name, self.sender_name)
152
153     def testEmail(self):
154         self.assertEquals(self.person.email, self.sender_email)
155
156     def testDBQueryName(self):
157         db_person = Person.objects.get(name = self.sender_name)
158         self.assertEquals(self.person, db_person)
159
160     def testDBQueryEmail(self):
161         db_person = Person.objects.get(email = self.sender_email)
162         self.assertEquals(self.person, db_person)
163
164
165 class SenderUTF8QPEncodingTest(SenderEncodingTest):
166     sender_name = u'\xe9xample user'
167     from_header = '=?utf-8?q?=C3=A9xample=20user?= <user@example.com>'
168
169 class SenderUTF8QPSplitEncodingTest(SenderEncodingTest):
170     sender_name = u'\xe9xample user'
171     from_header = '=?utf-8?q?=C3=A9xample?= user <user@example.com>'
172
173 class SenderUTF8B64EncodingTest(SenderUTF8QPEncodingTest):
174     from_header = '=?utf-8?B?w6l4YW1wbGUgdXNlcg==?= <user@example.com>'
175
176 class SubjectEncodingTest(PatchTest):
177     sender = 'example user <user@example.com>'
178     subject = 'test subject'
179     subject_header = 'test subject'
180
181     def setUp(self):
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)
187
188     def testSubjectEncoding(self):
189         (patch, comment) = find_content(self.project, self.email)
190         self.assertEquals(patch.name, self.subject)
191
192 class SubjectUTF8QPEncodingTest(SubjectEncodingTest):
193     subject = u'test s\xfcbject'
194     subject_header = '=?utf-8?q?test=20s=c3=bcbject?='
195
196 class SubjectUTF8QPMultipleEncodingTest(SubjectEncodingTest):
197     subject = u'test s\xfcbject'
198     subject_header = 'test =?utf-8?q?s=c3=bcbject?='
199
200 class SenderCorrelationTest(unittest.TestCase):
201     existing_sender = 'Existing Sender <existing@example.com>'
202     non_existing_sender = 'Non-existing Sender <nonexisting@example.com>'
203
204     def mail(self, sender):
205         return message_from_string('From: %s\nSubject: Test\n\ntest\n' % sender)
206
207     def setUp(self):
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)
211         self.person.save()
212
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)
217
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)
222
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)
228
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)
234
235     def tearDown(self):
236         self.person.delete()
237
238 class MultipleProjectPatchTest(unittest.TestCase):
239     """ Test that patches sent to multiple patchwork projects are
240         handled correctly """
241
242     test_comment = 'Test Comment'
243     patch_filename = '0001-add-line.patch'
244     msgid = '<1@example.com>'
245
246     def setUp(self):
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')
251
252         self.p1.save()
253         self.p2.save()
254
255         patch = read_patch(self.patch_filename)
256         email = create_email(self.test_comment + '\n' + patch)
257         email['Message-Id'] = self.msgid
258
259         del email['List-ID']
260         email['List-ID'] = '<' + self.p1.listid + '>'
261         parse_mail(email)
262
263         del email['List-ID']
264         email['List-ID'] = '<' + self.p2.listid + '>'
265         parse_mail(email)
266
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)
270
271     def tearDown(self):
272         self.p1.delete()
273         self.p2.delete()
274
275
276 class MultipleProjectPatchCommentTest(MultipleProjectPatchTest):
277     """ Test that followups to multiple-project patches end up on the
278         correct patch """
279
280     comment_msgid = '<2@example.com>'
281     comment_content = 'test comment'
282
283     def setUp(self):
284         super(MultipleProjectPatchCommentTest, self).setUp()
285
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
293             parse_mail(email)
294
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)
301
302 class ListIdHeaderTest(unittest.TestCase):
303     """ Test that we parse List-Id headers from mails correctly """
304     def setUp(self):
305         self.project = Project(linkname = 'test-project-1', name = 'Project 1',
306                 listid = '1.example.com', listemail='1@example.com')
307         self.project.save()
308
309     def testNoListId(self):
310         email = MIMEText('')
311         project = find_project(email)
312         self.assertEquals(project, None)
313
314     def testBlankListId(self):
315         email = MIMEText('')
316         email['List-Id'] = ''
317         project = find_project(email)
318         self.assertEquals(project, None)
319
320     def testWhitespaceListId(self):
321         email = MIMEText('')
322         email['List-Id'] = ' '
323         project = find_project(email)
324         self.assertEquals(project, None)
325
326     def testSubstringListId(self):
327         email = MIMEText('')
328         email['List-Id'] = 'example.com'
329         project = find_project(email)
330         self.assertEquals(project, None)
331
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). """
335         email = MIMEText('')
336         email['List-Id'] = self.project.listid
337         project = find_project(email)
338         self.assertEquals(project, self.project)
339
340     def testLongListId(self):
341         email = MIMEText('')
342         email['List-Id'] = 'Test text <%s>' % self.project.listid
343         project = find_project(email)
344         self.assertEquals(project, self.project)
345
346     def tearDown(self):
347         self.project.delete()
348
349
350 class GitPullTest(PatchTest):
351     def testGitPullRequest(self):
352         mail = read_mail('0001-git-pull-request.mbox',
353                         project = self.project)
354         (patch, comment) = find_content(self.project, mail)
355         self.assertTrue(patch is not None)
356         self.assertTrue(patch.pull_url is not None)
357         self.assertTrue(patch.content is None)
358         self.assertTrue(comment is not None)