]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/tests/patchparser.py
f6909ce9e458608300f060b09a3c9d316267ebb3
[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, 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
177 class SenderCorrelationTest(unittest.TestCase):
178     existing_sender = 'Existing Sender <existing@example.com>'
179     non_existing_sender = 'Non-existing Sender <nonexisting@example.com>'
180
181     def mail(self, sender):
182         return message_from_string('From: %s\nSubject: Test\n\ntest\n' % sender)
183
184     def setUp(self):
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)
188         self.person.save()
189
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)
194
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)
199
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)
205
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)
211
212     def tearDown(self):
213         self.person.delete()
214
215 class MultipleProjectPatchTest(unittest.TestCase):
216     """ Test that patches sent to multiple patchwork projects are
217         handled correctly """
218
219     test_comment = 'Test Comment'
220     patch_filename = '0001-add-line.patch'
221     msgid = '<1@example.com>'
222
223     def setUp(self):
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')
228
229         self.p1.save()
230         self.p2.save()
231
232         patch = read_patch(self.patch_filename)
233         email = create_email(self.test_comment + '\n' + patch)
234         email['Message-Id'] = self.msgid
235
236         del email['List-ID']
237         email['List-ID'] = '<' + self.p1.listid + '>'
238         parse_mail(email)
239
240         del email['List-ID']
241         email['List-ID'] = '<' + self.p2.listid + '>'
242         parse_mail(email)
243
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)
247
248     def tearDown(self):
249         self.p1.delete()
250         self.p2.delete()
251
252
253 class MultipleProjectPatchCommentTest(MultipleProjectPatchTest):
254     """ Test that followups to multiple-project patches end up on the
255         correct patch """
256
257     comment_msgid = '<2@example.com>'
258     comment_content = 'test comment'
259
260     def setUp(self):
261         super(MultipleProjectPatchCommentTest, self).setUp()
262
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
270             parse_mail(email)
271
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)
278
279 class ListIdHeaderTest(unittest.TestCase):
280     """ Test that we parse List-Id headers from mails correctly """
281     def setUp(self):
282         self.project = Project(linkname = 'test-project-1', name = 'Project 1',
283                 listid = '1.example.com', listemail='1@example.com')
284         self.project.save()
285
286     def testNoListId(self):
287         email = MIMEText('')
288         project = find_project(email)
289         self.assertEquals(project, None)
290
291     def testBlankListId(self):
292         email = MIMEText('')
293         email['List-Id'] = ''
294         project = find_project(email)
295         self.assertEquals(project, None)
296
297     def testWhitespaceListId(self):
298         email = MIMEText('')
299         email['List-Id'] = ' '
300         project = find_project(email)
301         self.assertEquals(project, None)
302
303     def testSubstringListId(self):
304         email = MIMEText('')
305         email['List-Id'] = 'example.com'
306         project = find_project(email)
307         self.assertEquals(project, None)
308
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). """
312         email = MIMEText('')
313         email['List-Id'] = self.project.listid
314         project = find_project(email)
315         self.assertEquals(project, self.project)
316
317     def testLongListId(self):
318         email = MIMEText('')
319         email['List-Id'] = 'Test text <%s>' % self.project.listid
320         project = find_project(email)
321         self.assertEquals(project, self.project)
322
323     def tearDown(self):
324         self.project.delete()