1 # vim: set fileencoding=utf-8 :
3 # Patchwork - automated patch tracking system
4 # Copyright (C) 2009 Jeremy Kerr <jk@ozlabs.org>
6 # This file is part of the Patchwork package.
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.
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.
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
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
31 class MboxPatchResponseTest(TestCase):
32 """ Test that the mbox view appends the Acked-by from a patch comment """
34 defaults.project.save()
36 self.person = defaults.patch_author_person
39 self.patch = Patch(project = defaults.project,
40 msgid = 'p1', name = 'testpatch',
41 submitter = self.person, content = '')
43 comment = Comment(patch = self.patch, msgid = 'p1',
44 submitter = self.person,
45 content = 'comment 1 text\nAcked-by: 1\n')
48 comment = Comment(patch = self.patch, msgid = 'p2',
49 submitter = self.person,
50 content = 'comment 2 text\nAcked-by: 2\n')
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')
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. """
62 defaults.project.save()
64 self.person = defaults.patch_author_person
67 self.patch = Patch(project = defaults.project,
68 msgid = 'p1', name = 'testpatch',
69 submitter = self.person, content = '')
71 comment = Comment(patch = self.patch, msgid = 'p1',
72 submitter = self.person,
73 content = 'comment 1 text\nAcked-by: 1\n---\nupdate\n')
76 comment = Comment(patch = self.patch, msgid = 'p2',
77 submitter = self.person,
78 content = 'comment 2 text\nAcked-by: 2\n')
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')
86 class MboxPassThroughHeaderTest(TestCase):
87 """ Test that we see 'Cc' and 'To' headers passed through from original
88 message to mbox view """
91 defaults.project.save()
92 self.person = defaults.patch_author_person
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'
99 self.patch = Patch(project = defaults.project,
100 msgid = 'p1', name = 'testpatch',
101 submitter = self.person, content = '')
103 def testCCHeader(self):
104 self.patch.headers = self.cc_header + '\n'
107 response = self.client.get('/patch/%d/mbox/' % self.patch.id)
108 self.assertContains(response, self.cc_header)
110 def testToHeader(self):
111 self.patch.headers = self.to_header + '\n'
114 response = self.client.get('/patch/%d/mbox/' % self.patch.id)
115 self.assertContains(response, self.to_header)
117 def testDateHeader(self):
118 self.patch.headers = self.date_header + '\n'
121 response = self.client.get('/patch/%d/mbox/' % self.patch.id)
122 self.assertContains(response, self.date_header)
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
131 defaults.project.save()
132 self.person = defaults.patch_author_person
133 self.person.name = u'©ool guŷ'
136 self.patch = Patch(project = defaults.project,
137 msgid = 'p1', name = 'testpatch',
138 submitter = self.person, content = '')
140 def testFromHeader(self):
142 from_email = '<' + self.person.email + '>'
144 response = self.client.get('/patch/%d/mbox/' % self.patch.id)
145 self.assertContains(response, from_email)
147 class MboxDateHeaderTest(TestCase):
148 """ Test that the date provided in the patch mail view is correct """
151 defaults.project.save()
152 self.person = defaults.patch_author_person
155 self.patch = Patch(project = defaults.project,
156 msgid = 'p1', name = 'testpatch',
157 submitter = self.person, content = '')
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(),
167 self.assertEqual(mail_date, patch_date)
169 def testSuppliedDateHeader(self):
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)
175 self.patch.headers = 'Date: %s\n' % date.strftime("%a, %d %b %Y %T %z")
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)