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.mime.text import MIMEText
23 from email.mime.multipart import MIMEMultipart
24 from email import message_from_string
25 from patchwork.models import Project, Person
27 test_mail_dir = 'patchwork/tests/mail'
28 test_patch_dir = 'patchwork/tests/patches'
30 class PatchTest(unittest.TestCase):
31 default_sender = 'Test Author <test@exmaple.com>'
32 default_subject = 'Test Subject'
33 project = Project(linkname = 'test-project')
35 def create_email(self, content, subject = None, sender = None,
38 subject = self.default_subject
40 sender = self.default_sender
44 body = MIMEText(content, _subtype = 'plain')
47 msg = MIMEText(content)
49 msg['Subject'] = subject
51 msg['List-Id'] = self.project.linkname
55 def read_patch(self, filename):
56 return file(os.path.join(test_patch_dir, filename)).read()
59 from patchwork.bin.parsemail import find_content, find_author
61 class InlinePatchTest(PatchTest):
62 patch_filename = '0001-add-line.patch'
63 test_comment = 'Test for attached patch'
66 self.orig_patch = self.read_patch(self.patch_filename)
67 email = self.create_email(self.test_comment + '\n' + self.orig_patch)
68 (self.patch, self.comment) = find_content(self.project, email)
70 def testPatchPresence(self):
71 self.assertTrue(self.patch is not None)
73 def testPatchContent(self):
74 self.assertEquals(self.patch.content, self.orig_patch)
76 def testCommentPresence(self):
77 self.assertTrue(self.comment is not None)
79 def testCommentContent(self):
80 self.assertEquals(self.comment.content, self.test_comment)
83 class AttachmentPatchTest(InlinePatchTest):
84 patch_filename = '0001-add-line.patch'
85 test_comment = 'Test for attached patch'
88 self.orig_patch = self.read_patch(self.patch_filename)
89 email = self.create_email(self.test_comment, multipart = True)
90 attachment = MIMEText(self.orig_patch, _subtype = 'x-patch')
91 email.attach(attachment)
92 (self.patch, self.comment) = find_content(self.project, email)
95 class SignatureCommentTest(InlinePatchTest):
96 patch_filename = '0001-add-line.patch'
97 test_comment = 'Test comment\nmore comment'
100 self.orig_patch = self.read_patch(self.patch_filename)
101 email = self.create_email( \
102 self.test_comment + '\n' + \
103 '-- \nsig\n' + self.orig_patch)
104 (self.patch, self.comment) = find_content(self.project, email)
107 class ListFooterTest(InlinePatchTest):
108 patch_filename = '0001-add-line.patch'
109 test_comment = 'Test comment\nmore comment'
112 self.orig_patch = self.read_patch(self.patch_filename)
113 email = self.create_email( \
114 self.test_comment + '\n' + \
115 '_______________________________________________\n' + \
116 'Linuxppc-dev mailing list\n' + \
118 (self.patch, self.comment) = find_content(self.project, email)
121 class UpdateCommentTest(InlinePatchTest):
122 """ Test for '---\nUpdate: v2' style comments to patches. """
123 patch_filename = '0001-add-line.patch'
124 test_comment = 'Test comment\nmore comment\n---\nUpdate: test update'
126 class UpdateSigCommentTest(SignatureCommentTest):
127 """ Test for '---\nUpdate: v2' style comments to patches, with a sig """
128 patch_filename = '0001-add-line.patch'
129 test_comment = 'Test comment\nmore comment\n---\nUpdate: test update'
131 class SenderEncodingTest(unittest.TestCase):
132 sender_name = u'example user'
133 sender_email = 'user@example.com'
134 from_header = 'example user <user@example.com>'
137 mail = 'From: %s\n' % self.from_header + \
138 'Subject: test\n\n' + \
140 self.email = message_from_string(mail)
141 (self.person, new) = find_author(self.email)
148 self.assertEquals(self.person.name, self.sender_name)
151 self.assertEquals(self.person.email, self.sender_email)
153 def testDBQueryName(self):
154 db_person = Person.objects.get(name = self.sender_name)
155 self.assertEquals(self.person, db_person)
157 def testDBQueryEmail(self):
158 db_person = Person.objects.get(email = self.sender_email)
159 self.assertEquals(self.person, db_person)
162 class SenderUTF8QPEncodingTest(SenderEncodingTest):
163 sender_name = u'\xe9xample user'
164 from_header = '=?utf-8?q?=C3=A9xample=20user?= <user@example.com>'
166 class SenderUTF8QPSplitEncodingTest(SenderEncodingTest):
167 sender_name = u'\xe9xample user'
168 from_header = '=?utf-8?q?=C3=A9xample=20?= user <user@example.com>'
170 class SenderUTF8B64EncodingTest(SenderUTF8QPEncodingTest):
171 from_header = '=?utf-8?B?w6l4YW1wbGUgdXNlcg==?= <user@example.com>'