]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/tests/patchparser.py
[parser] Decode From: headers
[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.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
26
27 test_mail_dir  = 'patchwork/tests/mail'
28 test_patch_dir = 'patchwork/tests/patches'
29
30 class PatchTest(unittest.TestCase):
31     default_sender = 'Test Author <test@exmaple.com>'
32     default_subject = 'Test Subject'
33     project = Project(linkname = 'test-project')
34
35     def create_email(self, content, subject = None, sender = None,
36             multipart = False):
37         if subject is None:
38             subject = self.default_subject
39         if sender is None:
40             sender = self.default_sender
41
42         if multipart:
43             msg = MIMEMultipart()
44             body = MIMEText(content, _subtype = 'plain')
45             msg.attach(body)
46         else:
47             msg = MIMEText(content)
48
49         msg['Subject'] = subject
50         msg['From'] = sender
51         msg['List-Id'] = self.project.linkname
52
53         return msg
54
55     def read_patch(self, filename):
56         return file(os.path.join(test_patch_dir, filename)).read()
57
58
59 from patchwork.bin.parsemail import find_content, find_author
60
61 class InlinePatchTest(PatchTest):
62     patch_filename = '0001-add-line.patch'
63     test_comment = 'Test for attached patch'
64
65     def setUp(self):
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)
69
70     def testPatchPresence(self):
71         self.assertTrue(self.patch is not None)
72
73     def testPatchContent(self):
74         self.assertEquals(self.patch.content, self.orig_patch)
75
76     def testCommentPresence(self):
77         self.assertTrue(self.comment is not None)
78
79     def testCommentContent(self):
80         self.assertEquals(self.comment.content, self.test_comment)
81
82
83 class AttachmentPatchTest(InlinePatchTest):
84     patch_filename = '0001-add-line.patch'
85     test_comment = 'Test for attached patch'
86
87     def setUp(self):
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)
93
94
95 class SignatureCommentTest(InlinePatchTest):
96     patch_filename = '0001-add-line.patch'
97     test_comment = 'Test comment\nmore comment'
98
99     def setUp(self):
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)
105
106
107 class ListFooterTest(InlinePatchTest):
108     patch_filename = '0001-add-line.patch'
109     test_comment = 'Test comment\nmore comment'
110
111     def setUp(self):
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' + \
117                 self.orig_patch)
118         (self.patch, self.comment) = find_content(self.project, email)
119
120
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'
125
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'
130
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>'
135
136     def setUp(self):
137         mail = 'From: %s\n' % self.from_header + \
138                'Subject: test\n\n' + \
139                'test'
140         self.email = message_from_string(mail)
141         (self.person, new) = find_author(self.email)
142         self.person.save()
143
144     def tearDown(self):
145         self.person.delete()
146
147     def testName(self):
148         self.assertEquals(self.person.name, self.sender_name)
149
150     def testEmail(self):
151         self.assertEquals(self.person.email, self.sender_email)
152
153     def testDBQueryName(self):
154         db_person = Person.objects.get(name = self.sender_name)
155         self.assertEquals(self.person, db_person)
156
157     def testDBQueryEmail(self):
158         db_person = Person.objects.get(email = self.sender_email)
159         self.assertEquals(self.person, db_person)
160
161
162 class SenderUTF8QPEncodingTest(SenderEncodingTest):
163     sender_name = u'\xe9xample user'
164     from_header = '=?utf-8?q?=C3=A9xample=20user?= <user@example.com>'
165
166 class SenderUTF8QPSplitEncodingTest(SenderEncodingTest):
167     sender_name = u'\xe9xample user'
168     from_header = '=?utf-8?q?=C3=A9xample=20?= user <user@example.com>'
169
170 class SenderUTF8B64EncodingTest(SenderUTF8QPEncodingTest):
171     from_header = '=?utf-8?B?w6l4YW1wbGUgdXNlcg==?= <user@example.com>'