]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/tests/mboxviews.py
8c469a1ae76787c50b0a52be7dbed4449dff0056
[patchwork] / apps / patchwork / tests / 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 dateutil.parser, dateutil.tz
25 from django.test import TestCase
26 from django.test.client import Client
27 from patchwork.models import Patch, Comment, Person
28 from patchwork.tests.utils import defaults, create_user, find_in_context
29
30 class MboxPatchResponseTest(TestCase):
31     """ Test that the mbox view appends the Acked-by from a patch comment """
32     def setUp(self):
33         defaults.project.save()
34
35         self.person = defaults.patch_author_person
36         self.person.save()
37
38         self.patch = Patch(project = defaults.project,
39                            msgid = 'p1', name = 'testpatch',
40                            submitter = self.person, content = '')
41         self.patch.save()
42         comment = Comment(patch = self.patch, msgid = 'p1',
43                 submitter = self.person,
44                 content = 'comment 1 text\nAcked-by: 1\n')
45         comment.save()
46
47         comment = Comment(patch = self.patch, msgid = 'p2',
48                 submitter = self.person,
49                 content = 'comment 2 text\nAcked-by: 2\n')
50         comment.save()
51
52     def testPatchResponse(self):
53         response = self.client.get('/patch/%d/mbox/' % self.patch.id)
54         self.assertContains(response,
55                 'Acked-by: 1\nAcked-by: 2\n')
56
57 class MboxPatchSplitResponseTest(TestCase):
58     """ Test that the mbox view appends the Acked-by from a patch comment,
59         and places it before an '---' update line. """
60     def setUp(self):
61         defaults.project.save()
62
63         self.person = defaults.patch_author_person
64         self.person.save()
65
66         self.patch = Patch(project = defaults.project,
67                            msgid = 'p1', name = 'testpatch',
68                            submitter = self.person, content = '')
69         self.patch.save()
70         comment = Comment(patch = self.patch, msgid = 'p1',
71                 submitter = self.person,
72                 content = 'comment 1 text\nAcked-by: 1\n---\nupdate\n')
73         comment.save()
74
75         comment = Comment(patch = self.patch, msgid = 'p2',
76                 submitter = self.person,
77                 content = 'comment 2 text\nAcked-by: 2\n')
78         comment.save()
79
80     def testPatchResponse(self):
81         response = self.client.get('/patch/%d/mbox/' % self.patch.id)
82         self.assertContains(response,
83                 'Acked-by: 1\nAcked-by: 2\n')
84
85 class MboxPassThroughHeaderTest(TestCase):
86     """ Test that we see 'Cc' and 'To' headers passed through from original
87         message to mbox view """
88
89     def setUp(self):
90         defaults.project.save()
91         self.person = defaults.patch_author_person
92         self.person.save()
93
94         self.cc_header = 'Cc: CC Person <cc@example.com>'
95         self.to_header = 'To: To Person <to@example.com>'
96         self.date_header = 'Date: Fri, 7 Jun 2013 15:42:54 +1000'
97
98         self.patch = Patch(project = defaults.project,
99                            msgid = 'p1', name = 'testpatch',
100                            submitter = self.person, content = '')
101
102     def testCCHeader(self):
103         self.patch.headers = self.cc_header + '\n'
104         self.patch.save()
105
106         response = self.client.get('/patch/%d/mbox/' % self.patch.id)
107         self.assertContains(response, self.cc_header)
108
109     def testToHeader(self):
110         self.patch.headers = self.to_header + '\n'
111         self.patch.save()
112
113         response = self.client.get('/patch/%d/mbox/' % self.patch.id)
114         self.assertContains(response, self.to_header)
115
116     def testDateHeader(self):
117         self.patch.headers = self.date_header + '\n'
118         self.patch.save()
119
120         response = self.client.get('/patch/%d/mbox/' % self.patch.id)
121         self.assertContains(response, self.date_header)
122
123 class MboxBrokenFromHeaderTest(TestCase):
124     """ Test that a person with characters outside ASCII in his name do
125         produce correct From header. As RFC 2822 state we must retain the
126         <user@doamin.tld> format for the mail while the name part may be coded
127         in some ways. """
128
129     def setUp(self):
130         defaults.project.save()
131         self.person = defaults.patch_author_person
132         self.person.name = u'©ool guŷ'
133         self.person.save()
134
135         self.patch = Patch(project = defaults.project,
136                 msgid = 'p1', name = 'testpatch',
137                 submitter = self.person, content = '')
138
139     def testFromHeader(self):
140         self.patch.save()
141         from_email = '<' + self.person.email + '>'
142
143         response = self.client.get('/patch/%d/mbox/' % self.patch.id)
144         self.assertContains(response, from_email)
145
146 class MboxDateHeaderTest(TestCase):
147     """ Test that the date provided in the patch mail view is correct """
148
149     def setUp(self):
150         defaults.project.save()
151         self.person = defaults.patch_author_person
152         self.person.save()
153
154         self.patch = Patch(project = defaults.project,
155                            msgid = 'p1', name = 'testpatch',
156                            submitter = self.person, content = '')
157         self.patch.save()
158
159     def testDateHeader(self):
160         response = self.client.get('/patch/%d/mbox/' % self.patch.id)
161         mail = email.message_from_string(response.content)
162         mail_date = dateutil.parser.parse(mail['Date'])
163         # patch dates are all in UTC
164         patch_date = self.patch.date.replace(tzinfo=dateutil.tz.tzutc(),
165                                             microsecond=0)
166         self.assertEqual(mail_date, patch_date)