]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/tests/patchparser.py
[test] 2.4-ise test cases
[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
24
25 try:
26     from email.mime.text import MIMEText
27     from email.mime.multipart import MIMEMultipart
28 except ImportError:
29     # Python 2.4 compatibility
30     from email.MIMEText import MIMEText
31     from email.MIMEMultipart import MIMEMultipart
32
33 test_mail_dir  = 'patchwork/tests/mail'
34 test_patch_dir = 'patchwork/tests/patches'
35
36 class PatchTest(unittest.TestCase):
37     default_sender = 'Test Author <test@exmaple.com>'
38     default_subject = 'Test Subject'
39     project = Project(linkname = 'test-project')
40
41     def create_email(self, content, subject = None, sender = None,
42             multipart = False):
43         if subject is None:
44             subject = self.default_subject
45         if sender is None:
46             sender = self.default_sender
47
48         if multipart:
49             msg = MIMEMultipart()
50             body = MIMEText(content, _subtype = 'plain')
51             msg.attach(body)
52         else:
53             msg = MIMEText(content)
54
55         msg['Subject'] = subject
56         msg['From'] = sender
57         msg['List-Id'] = self.project.linkname
58
59         return msg
60
61     def read_patch(self, filename):
62         return file(os.path.join(test_patch_dir, filename)).read()
63
64
65 from patchwork.bin.parsemail import find_content, find_author
66
67 class InlinePatchTest(PatchTest):
68     patch_filename = '0001-add-line.patch'
69     test_comment = 'Test for attached patch'
70
71     def setUp(self):
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)
75
76     def testPatchPresence(self):
77         self.assertTrue(self.patch is not None)
78
79     def testPatchContent(self):
80         self.assertEquals(self.patch.content, self.orig_patch)
81
82     def testCommentPresence(self):
83         self.assertTrue(self.comment is not None)
84
85     def testCommentContent(self):
86         self.assertEquals(self.comment.content, self.test_comment)
87
88
89 class AttachmentPatchTest(InlinePatchTest):
90     patch_filename = '0001-add-line.patch'
91     test_comment = 'Test for attached patch'
92
93     def setUp(self):
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)
99
100
101 class SignatureCommentTest(InlinePatchTest):
102     patch_filename = '0001-add-line.patch'
103     test_comment = 'Test comment\nmore comment'
104
105     def setUp(self):
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)
111
112
113 class ListFooterTest(InlinePatchTest):
114     patch_filename = '0001-add-line.patch'
115     test_comment = 'Test comment\nmore comment'
116
117     def setUp(self):
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' + \
123                 self.orig_patch)
124         (self.patch, self.comment) = find_content(self.project, email)
125
126
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'
131
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'
136
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>'
141
142     def setUp(self):
143         mail = 'From: %s\n' % self.from_header + \
144                'Subject: test\n\n' + \
145                'test'
146         self.email = message_from_string(mail)
147         (self.person, new) = find_author(self.email)
148         self.person.save()
149
150     def tearDown(self):
151         self.person.delete()
152
153     def testName(self):
154         self.assertEquals(self.person.name, self.sender_name)
155
156     def testEmail(self):
157         self.assertEquals(self.person.email, self.sender_email)
158
159     def testDBQueryName(self):
160         db_person = Person.objects.get(name = self.sender_name)
161         self.assertEquals(self.person, db_person)
162
163     def testDBQueryEmail(self):
164         db_person = Person.objects.get(email = self.sender_email)
165         self.assertEquals(self.person, db_person)
166
167
168 class SenderUTF8QPEncodingTest(SenderEncodingTest):
169     sender_name = u'\xe9xample user'
170     from_header = '=?utf-8?q?=C3=A9xample=20user?= <user@example.com>'
171
172 class SenderUTF8QPSplitEncodingTest(SenderEncodingTest):
173     sender_name = u'\xe9xample user'
174     from_header = '=?utf-8?q?=C3=A9xample=20?= user <user@example.com>'
175
176 class SenderUTF8B64EncodingTest(SenderUTF8QPEncodingTest):
177     from_header = '=?utf-8?B?w6l4YW1wbGUgdXNlcg==?= <user@example.com>'