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
26 from email.mime.text import MIMEText
27 from email.mime.multipart import MIMEMultipart
29 # Python 2.4 compatibility
30 from email.MIMEText import MIMEText
31 from email.MIMEMultipart import MIMEMultipart
33 test_mail_dir = 'patchwork/tests/mail'
34 test_patch_dir = 'patchwork/tests/patches'
36 class PatchTest(unittest.TestCase):
37 default_sender = 'Test Author <test@exmaple.com>'
38 default_subject = 'Test Subject'
39 project = Project(linkname = 'test-project')
41 def create_email(self, content, subject = None, sender = None,
44 subject = self.default_subject
46 sender = self.default_sender
50 body = MIMEText(content, _subtype = 'plain')
53 msg = MIMEText(content)
55 msg['Subject'] = subject
57 msg['List-Id'] = self.project.linkname
61 def read_patch(self, filename):
62 return file(os.path.join(test_patch_dir, filename)).read()
65 from patchwork.bin.parsemail import find_content, find_author
67 class InlinePatchTest(PatchTest):
68 patch_filename = '0001-add-line.patch'
69 test_comment = 'Test for attached patch'
72 self.orig_patch = self.read_patch(self.patch_filename)
73 email = self.create_email(self.test_comment + '\n' + self.orig_patch)
74 (self.patch, self.comment) = find_content(self.project, email)
76 def testPatchPresence(self):
77 self.assertTrue(self.patch is not None)
79 def testPatchContent(self):
80 self.assertEquals(self.patch.content, self.orig_patch)
82 def testCommentPresence(self):
83 self.assertTrue(self.comment is not None)
85 def testCommentContent(self):
86 self.assertEquals(self.comment.content, self.test_comment)
89 class AttachmentPatchTest(InlinePatchTest):
90 patch_filename = '0001-add-line.patch'
91 test_comment = 'Test for attached patch'
94 self.orig_patch = self.read_patch(self.patch_filename)
95 email = self.create_email(self.test_comment, multipart = True)
96 attachment = MIMEText(self.orig_patch, _subtype = 'x-patch')
97 email.attach(attachment)
98 (self.patch, self.comment) = find_content(self.project, email)
101 class SignatureCommentTest(InlinePatchTest):
102 patch_filename = '0001-add-line.patch'
103 test_comment = 'Test comment\nmore comment'
106 self.orig_patch = self.read_patch(self.patch_filename)
107 email = self.create_email( \
108 self.test_comment + '\n' + \
109 '-- \nsig\n' + self.orig_patch)
110 (self.patch, self.comment) = find_content(self.project, email)
113 class ListFooterTest(InlinePatchTest):
114 patch_filename = '0001-add-line.patch'
115 test_comment = 'Test comment\nmore comment'
118 self.orig_patch = self.read_patch(self.patch_filename)
119 email = self.create_email( \
120 self.test_comment + '\n' + \
121 '_______________________________________________\n' + \
122 'Linuxppc-dev mailing list\n' + \
124 (self.patch, self.comment) = find_content(self.project, email)
127 class UpdateCommentTest(InlinePatchTest):
128 """ Test for '---\nUpdate: v2' style comments to patches. """
129 patch_filename = '0001-add-line.patch'
130 test_comment = 'Test comment\nmore comment\n---\nUpdate: test update'
132 class UpdateSigCommentTest(SignatureCommentTest):
133 """ Test for '---\nUpdate: v2' style comments to patches, with a sig """
134 patch_filename = '0001-add-line.patch'
135 test_comment = 'Test comment\nmore comment\n---\nUpdate: test update'
137 class SenderEncodingTest(unittest.TestCase):
138 sender_name = u'example user'
139 sender_email = 'user@example.com'
140 from_header = 'example user <user@example.com>'
143 mail = 'From: %s\n' % self.from_header + \
144 'Subject: test\n\n' + \
146 self.email = message_from_string(mail)
147 (self.person, new) = find_author(self.email)
154 self.assertEquals(self.person.name, self.sender_name)
157 self.assertEquals(self.person.email, self.sender_email)
159 def testDBQueryName(self):
160 db_person = Person.objects.get(name = self.sender_name)
161 self.assertEquals(self.person, db_person)
163 def testDBQueryEmail(self):
164 db_person = Person.objects.get(email = self.sender_email)
165 self.assertEquals(self.person, db_person)
168 class SenderUTF8QPEncodingTest(SenderEncodingTest):
169 sender_name = u'\xe9xample user'
170 from_header = '=?utf-8?q?=C3=A9xample=20user?= <user@example.com>'
172 class SenderUTF8QPSplitEncodingTest(SenderEncodingTest):
173 sender_name = u'\xe9xample user'
174 from_header = '=?utf-8?q?=C3=A9xample=20?= user <user@example.com>'
176 class SenderUTF8B64EncodingTest(SenderUTF8QPEncodingTest):
177 from_header = '=?utf-8?B?w6l4YW1wbGUgdXNlcg==?= <user@example.com>'