]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/tests/patchparser.py
[tests] Remove print from SenderCorrelationTest
[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 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
38
39 class InlinePatchTest(PatchTest):
40     patch_filename = '0001-add-line.patch'
41     test_comment = 'Test for attached patch'
42
43     def setUp(self):
44         self.orig_patch = read_patch(self.patch_filename)
45         email = create_email(self.test_comment + '\n' + self.orig_patch)
46         (self.patch, self.comment) = find_content(self.project, email)
47
48     def testPatchPresence(self):
49         self.assertTrue(self.patch is not None)
50
51     def testPatchContent(self):
52         self.assertEquals(self.patch.content, self.orig_patch)
53
54     def testCommentPresence(self):
55         self.assertTrue(self.comment is not None)
56
57     def testCommentContent(self):
58         self.assertEquals(self.comment.content, self.test_comment)
59
60
61 class AttachmentPatchTest(InlinePatchTest):
62     patch_filename = '0001-add-line.patch'
63     test_comment = 'Test for attached patch'
64
65     def setUp(self):
66         self.orig_patch = read_patch(self.patch_filename)
67         email = create_email(self.test_comment, multipart = True)
68         attachment = MIMEText(self.orig_patch, _subtype = 'x-patch')
69         email.attach(attachment)
70         (self.patch, self.comment) = find_content(self.project, email)
71
72 class UTF8InlinePatchTest(InlinePatchTest):
73     patch_filename = '0002-utf-8.patch'
74     patch_encoding = 'utf-8'
75
76     def setUp(self):
77         self.orig_patch = read_patch(self.patch_filename, self.patch_encoding)
78         email = create_email(self.test_comment + '\n' + self.orig_patch,
79                              content_encoding = self.patch_encoding)
80         (self.patch, self.comment) = find_content(self.project, email)
81
82 class SignatureCommentTest(InlinePatchTest):
83     patch_filename = '0001-add-line.patch'
84     test_comment = 'Test comment\nmore comment'
85
86     def setUp(self):
87         self.orig_patch = read_patch(self.patch_filename)
88         email = create_email( \
89                 self.test_comment + '\n' + \
90                 '-- \nsig\n' + self.orig_patch)
91         (self.patch, self.comment) = find_content(self.project, email)
92
93
94 class ListFooterTest(InlinePatchTest):
95     patch_filename = '0001-add-line.patch'
96     test_comment = 'Test comment\nmore comment'
97
98     def setUp(self):
99         self.orig_patch = read_patch(self.patch_filename)
100         email = create_email( \
101                 self.test_comment + '\n' + \
102                 '_______________________________________________\n' + \
103                 'Linuxppc-dev mailing list\n' + \
104                 self.orig_patch)
105         (self.patch, self.comment) = find_content(self.project, email)
106
107
108 class UpdateCommentTest(InlinePatchTest):
109     """ Test for '---\nUpdate: v2' style comments to patches. """
110     patch_filename = '0001-add-line.patch'
111     test_comment = 'Test comment\nmore comment\n---\nUpdate: test update'
112
113 class UpdateSigCommentTest(SignatureCommentTest):
114     """ Test for '---\nUpdate: v2' style comments to patches, with a sig """
115     patch_filename = '0001-add-line.patch'
116     test_comment = 'Test comment\nmore comment\n---\nUpdate: test update'
117
118 class SenderEncodingTest(unittest.TestCase):
119     sender_name = u'example user'
120     sender_email = 'user@example.com'
121     from_header = 'example user <user@example.com>'
122
123     def setUp(self):
124         mail = 'From: %s\n' % self.from_header + \
125                'Subject: test\n\n' + \
126                'test'
127         self.email = message_from_string(mail)
128         (self.person, new) = find_author(self.email)
129         self.person.save()
130
131     def tearDown(self):
132         self.person.delete()
133
134     def testName(self):
135         self.assertEquals(self.person.name, self.sender_name)
136
137     def testEmail(self):
138         self.assertEquals(self.person.email, self.sender_email)
139
140     def testDBQueryName(self):
141         db_person = Person.objects.get(name = self.sender_name)
142         self.assertEquals(self.person, db_person)
143
144     def testDBQueryEmail(self):
145         db_person = Person.objects.get(email = self.sender_email)
146         self.assertEquals(self.person, db_person)
147
148
149 class SenderUTF8QPEncodingTest(SenderEncodingTest):
150     sender_name = u'\xe9xample user'
151     from_header = '=?utf-8?q?=C3=A9xample=20user?= <user@example.com>'
152
153 class SenderUTF8QPSplitEncodingTest(SenderEncodingTest):
154     sender_name = u'\xe9xample user'
155     from_header = '=?utf-8?q?=C3=A9xample?= user <user@example.com>'
156
157 class SenderUTF8B64EncodingTest(SenderUTF8QPEncodingTest):
158     from_header = '=?utf-8?B?w6l4YW1wbGUgdXNlcg==?= <user@example.com>'
159
160
161 class SenderCorrelationTest(unittest.TestCase):
162     existing_sender = 'Existing Sender <existing@example.com>'
163     non_existing_sender = 'Non-existing Sender <nonexisting@example.com>'
164
165     def mail(self, sender):
166         return message_from_string('From: %s\nSubject: Test\n\ntest\n' % sender)
167
168     def setUp(self):
169         self.existing_sender_mail = self.mail(self.existing_sender)
170         self.non_existing_sender_mail = self.mail(self.non_existing_sender)
171         (self.person, new) = find_author(self.existing_sender_mail)
172         self.person.save()
173
174     def testExisingSender(self):
175         (person, new) = find_author(self.existing_sender_mail)
176         self.assertEqual(new, False)
177         self.assertEqual(person.id, self.person.id)
178
179     def testNonExisingSender(self):
180         (person, new) = find_author(self.non_existing_sender_mail)
181         self.assertEqual(new, True)
182         self.assertEqual(person.id, None)
183
184     def testExistingDifferentFormat(self):
185         mail = self.mail('existing@example.com')
186         (person, new) = find_author(mail)
187         self.assertEqual(new, False)
188         self.assertEqual(person.id, self.person.id)
189
190     def testExistingDifferentCase(self):
191         mail = self.mail(self.existing_sender.upper())
192         (person, new) = find_author(mail)
193         self.assertEqual(new, False)
194         self.assertEqual(person.id, self.person.id)
195
196     def tearDown(self):
197         self.person.delete()