]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/tests/mboxviews.py
views: Move mbox handling from models to views
[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
97         self.patch = Patch(project = defaults.project,
98                            msgid = 'p1', name = 'testpatch',
99                            submitter = self.person, content = '')
100
101     def testCCHeader(self):
102         self.patch.headers = self.cc_header + '\n'
103         self.patch.save()
104
105         response = self.client.get('/patch/%d/mbox/' % self.patch.id)
106         self.assertContains(response, self.cc_header)
107
108     def testToHeader(self):
109         self.patch.headers = self.to_header + '\n'
110         self.patch.save()
111
112         response = self.client.get('/patch/%d/mbox/' % self.patch.id)
113         self.assertContains(response, self.to_header)
114
115 class MboxBrokenFromHeaderTest(TestCase):
116     """ Test that a person with characters outside ASCII in his name do
117         produce correct From header. As RFC 2822 state we must retain the
118         <user@doamin.tld> format for the mail while the name part may be coded
119         in some ways. """
120
121     def setUp(self):
122         defaults.project.save()
123         self.person = defaults.patch_author_person
124         self.person.name = u'©ool guŷ'
125         self.person.save()
126
127         self.patch = Patch(project = defaults.project,
128                 msgid = 'p1', name = 'testpatch',
129                 submitter = self.person, content = '')
130
131     def testFromHeader(self):
132         self.patch.save()
133         from_email = '<' + self.person.email + '>'
134
135         response = self.client.get('/patch/%d/mbox/' % self.patch.id)
136         self.assertContains(response, from_email)
137
138 class MboxDateHeaderTest(TestCase):
139     """ Test that the date provided in the patch mail view is correct """
140
141     def setUp(self):
142         defaults.project.save()
143         self.person = defaults.patch_author_person
144         self.person.save()
145
146         self.patch = Patch(project = defaults.project,
147                            msgid = 'p1', name = 'testpatch',
148                            submitter = self.person, content = '')
149         self.patch.save()
150
151     def testDateHeader(self):
152         response = self.client.get('/patch/%d/mbox/' % self.patch.id)
153         mail = email.message_from_string(response.content)
154         mail_date = dateutil.parser.parse(mail['Date'])
155         # patch dates are all in UTC
156         patch_date = self.patch.date.replace(tzinfo=dateutil.tz.tzutc(),
157                                             microsecond=0)
158         self.assertEqual(mail_date, patch_date)