]> git.ozlabs.org Git - patchwork/blob - patchwork/tests/test_mboxviews.py
tests/test_user: Add "profile unlink" tests
[patchwork] / patchwork / tests / test_mboxviews.py
1 # vim: set fileencoding=utf-8 :
2 #
3 # Patchwork - automated patch tracking system
4 # Copyright (C) 2009 Jeremy Kerr <jk@ozlabs.org>
5 #
6 # This file is part of the Patchwork package.
7 #
8 # Patchwork is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
12 #
13 # Patchwork is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Patchwork; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21
22 import unittest
23 import email
24 import datetime
25 import dateutil.parser, dateutil.tz
26 from django.test import TestCase
27 from django.test.client import Client
28 from patchwork.models import Patch, Comment, Person
29 from patchwork.tests.utils import defaults, create_user, find_in_context
30
31 class MboxPatchResponseTest(TestCase):
32     fixtures = ['default_states']
33
34     """ Test that the mbox view appends the Acked-by from a patch comment """
35     def setUp(self):
36         defaults.project.save()
37
38         self.person = defaults.patch_author_person
39         self.person.save()
40
41         self.patch = Patch(project = defaults.project,
42                            msgid = 'p1', name = 'testpatch',
43                            submitter = self.person, content = '')
44         self.patch.save()
45         comment = Comment(patch = self.patch, msgid = 'p1',
46                 submitter = self.person,
47                 content = 'comment 1 text\nAcked-by: 1\n')
48         comment.save()
49
50         comment = Comment(patch = self.patch, msgid = 'p2',
51                 submitter = self.person,
52                 content = 'comment 2 text\nAcked-by: 2\n')
53         comment.save()
54
55     def testPatchResponse(self):
56         response = self.client.get('/patch/%d/mbox/' % self.patch.id)
57         self.assertContains(response,
58                 'Acked-by: 1\nAcked-by: 2\n')
59
60 class MboxPatchSplitResponseTest(TestCase):
61     fixtures = ['default_states']
62
63     """ Test that the mbox view appends the Acked-by from a patch comment,
64         and places it before an '---' update line. """
65     def setUp(self):
66         defaults.project.save()
67
68         self.person = defaults.patch_author_person
69         self.person.save()
70
71         self.patch = Patch(project = defaults.project,
72                            msgid = 'p1', name = 'testpatch',
73                            submitter = self.person, content = '')
74         self.patch.save()
75         comment = Comment(patch = self.patch, msgid = 'p1',
76                 submitter = self.person,
77                 content = 'comment 1 text\nAcked-by: 1\n---\nupdate\n')
78         comment.save()
79
80         comment = Comment(patch = self.patch, msgid = 'p2',
81                 submitter = self.person,
82                 content = 'comment 2 text\nAcked-by: 2\n')
83         comment.save()
84
85     def testPatchResponse(self):
86         response = self.client.get('/patch/%d/mbox/' % self.patch.id)
87         self.assertContains(response,
88                 'Acked-by: 1\nAcked-by: 2\n')
89
90 class MboxPassThroughHeaderTest(TestCase):
91     fixtures = ['default_states']
92
93     """ Test that we see 'Cc' and 'To' headers passed through from original
94         message to mbox view """
95
96     def setUp(self):
97         defaults.project.save()
98         self.person = defaults.patch_author_person
99         self.person.save()
100
101         self.cc_header = 'Cc: CC Person <cc@example.com>'
102         self.to_header = 'To: To Person <to@example.com>'
103         self.date_header = 'Date: Fri, 7 Jun 2013 15:42:54 +1000'
104
105         self.patch = Patch(project = defaults.project,
106                            msgid = 'p1', name = 'testpatch',
107                            submitter = self.person, content = '')
108
109     def testCCHeader(self):
110         self.patch.headers = self.cc_header + '\n'
111         self.patch.save()
112
113         response = self.client.get('/patch/%d/mbox/' % self.patch.id)
114         self.assertContains(response, self.cc_header)
115
116     def testToHeader(self):
117         self.patch.headers = self.to_header + '\n'
118         self.patch.save()
119
120         response = self.client.get('/patch/%d/mbox/' % self.patch.id)
121         self.assertContains(response, self.to_header)
122
123     def testDateHeader(self):
124         self.patch.headers = self.date_header + '\n'
125         self.patch.save()
126
127         response = self.client.get('/patch/%d/mbox/' % self.patch.id)
128         self.assertContains(response, self.date_header)
129
130 class MboxBrokenFromHeaderTest(TestCase):
131     fixtures = ['default_states']
132
133     """ Test that a person with characters outside ASCII in his name do
134         produce correct From header. As RFC 2822 state we must retain the
135         <user@doamin.tld> format for the mail while the name part may be coded
136         in some ways. """
137
138     def setUp(self):
139         defaults.project.save()
140         self.person = defaults.patch_author_person
141         self.person.name = u'©ool guŷ'
142         self.person.save()
143
144         self.patch = Patch(project = defaults.project,
145                 msgid = 'p1', name = 'testpatch',
146                 submitter = self.person, content = '')
147
148     def testFromHeader(self):
149         self.patch.save()
150         from_email = '<' + self.person.email + '>'
151
152         response = self.client.get('/patch/%d/mbox/' % self.patch.id)
153         self.assertContains(response, from_email)
154
155 class MboxDateHeaderTest(TestCase):
156     fixtures = ['default_states']
157
158     """ Test that the date provided in the patch mail view is correct """
159
160     def setUp(self):
161         defaults.project.save()
162         self.person = defaults.patch_author_person
163         self.person.save()
164
165         self.patch = Patch(project = defaults.project,
166                            msgid = 'p1', name = 'testpatch',
167                            submitter = self.person, content = '')
168         self.patch.save()
169
170     def testDateHeader(self):
171         response = self.client.get('/patch/%d/mbox/' % self.patch.id)
172         mail = email.message_from_string(response.content)
173         mail_date = dateutil.parser.parse(mail['Date'])
174         # patch dates are all in UTC
175         patch_date = self.patch.date.replace(tzinfo=dateutil.tz.tzutc(),
176                                             microsecond=0)
177         self.assertEqual(mail_date, patch_date)
178
179     def testSuppliedDateHeader(self):
180         hour_offset = 3
181         tz = dateutil.tz.tzoffset(None, hour_offset * 60 * 60)
182         date = datetime.datetime.utcnow() - datetime.timedelta(days = 1)
183         date = date.replace(tzinfo=tz, microsecond=0)
184
185         self.patch.headers = 'Date: %s\n' % date.strftime("%a, %d %b %Y %T %z")
186         self.patch.save()
187
188         response = self.client.get('/patch/%d/mbox/' % self.patch.id)
189         mail = email.message_from_string(response.content)
190         mail_date = dateutil.parser.parse(mail['Date'])
191         self.assertEqual(mail_date, date)
192
193 class MboxCommentPostcriptUnchangedTest(TestCase):
194     fixtures = ['default_states']
195
196     """ Test that the mbox view doesn't change the postscript part of a mail.
197         There where always a missing blank right after the postscript
198         delimiter '---' and an additional newline right before. """
199     def setUp(self):
200         defaults.project.save()
201
202         self.person = defaults.patch_author_person
203         self.person.save()
204
205         self.patch = Patch(project = defaults.project,
206                            msgid = 'p1', name = 'testpatch',
207                            submitter = self.person, content = '')
208         self.patch.save()
209
210         self.txt = 'some comment\n---\n some/file | 1 +\n'
211
212         comment = Comment(patch = self.patch, msgid = 'p1',
213                 submitter = self.person,
214                 content = self.txt)
215         comment.save()
216
217     def testCommentUnchanged(self):
218         response = self.client.get('/patch/%d/mbox/' % self.patch.id)
219         self.assertContains(response, self.txt)
220         self.txt += "\n"
221         self.assertNotContains(response, self.txt)