]> git.ozlabs.org Git - patchwork/blob - patchwork/tests/test_mboxviews.py
Move to a more recent django project structure
[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     """ Test that the mbox view appends the Acked-by from a patch comment """
33     def setUp(self):
34         defaults.project.save()
35
36         self.person = defaults.patch_author_person
37         self.person.save()
38
39         self.patch = Patch(project = defaults.project,
40                            msgid = 'p1', name = 'testpatch',
41                            submitter = self.person, content = '')
42         self.patch.save()
43         comment = Comment(patch = self.patch, msgid = 'p1',
44                 submitter = self.person,
45                 content = 'comment 1 text\nAcked-by: 1\n')
46         comment.save()
47
48         comment = Comment(patch = self.patch, msgid = 'p2',
49                 submitter = self.person,
50                 content = 'comment 2 text\nAcked-by: 2\n')
51         comment.save()
52
53     def testPatchResponse(self):
54         response = self.client.get('/patch/%d/mbox/' % self.patch.id)
55         self.assertContains(response,
56                 'Acked-by: 1\nAcked-by: 2\n')
57
58 class MboxPatchSplitResponseTest(TestCase):
59     """ Test that the mbox view appends the Acked-by from a patch comment,
60         and places it before an '---' update line. """
61     def setUp(self):
62         defaults.project.save()
63
64         self.person = defaults.patch_author_person
65         self.person.save()
66
67         self.patch = Patch(project = defaults.project,
68                            msgid = 'p1', name = 'testpatch',
69                            submitter = self.person, content = '')
70         self.patch.save()
71         comment = Comment(patch = self.patch, msgid = 'p1',
72                 submitter = self.person,
73                 content = 'comment 1 text\nAcked-by: 1\n---\nupdate\n')
74         comment.save()
75
76         comment = Comment(patch = self.patch, msgid = 'p2',
77                 submitter = self.person,
78                 content = 'comment 2 text\nAcked-by: 2\n')
79         comment.save()
80
81     def testPatchResponse(self):
82         response = self.client.get('/patch/%d/mbox/' % self.patch.id)
83         self.assertContains(response,
84                 'Acked-by: 1\nAcked-by: 2\n')
85
86 class MboxPassThroughHeaderTest(TestCase):
87     """ Test that we see 'Cc' and 'To' headers passed through from original
88         message to mbox view """
89
90     def setUp(self):
91         defaults.project.save()
92         self.person = defaults.patch_author_person
93         self.person.save()
94
95         self.cc_header = 'Cc: CC Person <cc@example.com>'
96         self.to_header = 'To: To Person <to@example.com>'
97         self.date_header = 'Date: Fri, 7 Jun 2013 15:42:54 +1000'
98
99         self.patch = Patch(project = defaults.project,
100                            msgid = 'p1', name = 'testpatch',
101                            submitter = self.person, content = '')
102
103     def testCCHeader(self):
104         self.patch.headers = self.cc_header + '\n'
105         self.patch.save()
106
107         response = self.client.get('/patch/%d/mbox/' % self.patch.id)
108         self.assertContains(response, self.cc_header)
109
110     def testToHeader(self):
111         self.patch.headers = self.to_header + '\n'
112         self.patch.save()
113
114         response = self.client.get('/patch/%d/mbox/' % self.patch.id)
115         self.assertContains(response, self.to_header)
116
117     def testDateHeader(self):
118         self.patch.headers = self.date_header + '\n'
119         self.patch.save()
120
121         response = self.client.get('/patch/%d/mbox/' % self.patch.id)
122         self.assertContains(response, self.date_header)
123
124 class MboxBrokenFromHeaderTest(TestCase):
125     """ Test that a person with characters outside ASCII in his name do
126         produce correct From header. As RFC 2822 state we must retain the
127         <user@doamin.tld> format for the mail while the name part may be coded
128         in some ways. """
129
130     def setUp(self):
131         defaults.project.save()
132         self.person = defaults.patch_author_person
133         self.person.name = u'©ool guŷ'
134         self.person.save()
135
136         self.patch = Patch(project = defaults.project,
137                 msgid = 'p1', name = 'testpatch',
138                 submitter = self.person, content = '')
139
140     def testFromHeader(self):
141         self.patch.save()
142         from_email = '<' + self.person.email + '>'
143
144         response = self.client.get('/patch/%d/mbox/' % self.patch.id)
145         self.assertContains(response, from_email)
146
147 class MboxDateHeaderTest(TestCase):
148     """ Test that the date provided in the patch mail view is correct """
149
150     def setUp(self):
151         defaults.project.save()
152         self.person = defaults.patch_author_person
153         self.person.save()
154
155         self.patch = Patch(project = defaults.project,
156                            msgid = 'p1', name = 'testpatch',
157                            submitter = self.person, content = '')
158         self.patch.save()
159
160     def testDateHeader(self):
161         response = self.client.get('/patch/%d/mbox/' % self.patch.id)
162         mail = email.message_from_string(response.content)
163         mail_date = dateutil.parser.parse(mail['Date'])
164         # patch dates are all in UTC
165         patch_date = self.patch.date.replace(tzinfo=dateutil.tz.tzutc(),
166                                             microsecond=0)
167         self.assertEqual(mail_date, patch_date)
168
169     def testSuppliedDateHeader(self):
170         hour_offset = 3
171         tz = dateutil.tz.tzoffset(None, hour_offset * 60 * 60)
172         date = datetime.datetime.utcnow() - datetime.timedelta(days = 1)
173         date = date.replace(tzinfo=tz, microsecond=0)
174
175         self.patch.headers = 'Date: %s\n' % date.strftime("%a, %d %b %Y %T %z")
176         self.patch.save()
177
178         response = self.client.get('/patch/%d/mbox/' % self.patch.id)
179         mail = email.message_from_string(response.content)
180         mail_date = dateutil.parser.parse(mail['Date'])
181         self.assertEqual(mail_date, date)
182
183 class MboxCommentPostcriptUnchangedTest(TestCase):
184     """ Test that the mbox view doesn't change the postscript part of a mail.
185         There where always a missing blank right after the postscript
186         delimiter '---' and an additional newline right before. """
187     def setUp(self):
188         defaults.project.save()
189
190         self.person = defaults.patch_author_person
191         self.person.save()
192
193         self.patch = Patch(project = defaults.project,
194                            msgid = 'p1', name = 'testpatch',
195                            submitter = self.person, content = '')
196         self.patch.save()
197
198         self.txt = 'some comment\n---\n some/file | 1 +\n'
199
200         comment = Comment(patch = self.patch, msgid = 'p1',
201                 submitter = self.person,
202                 content = self.txt)
203         comment.save()
204
205     def testCommentUnchanged(self):
206         response = self.client.get('/patch/%d/mbox/' % self.patch.id)
207         self.assertContains(response, self.txt)
208         self.txt += "\n"
209         self.assertNotContains(response, self.txt)