summaryrefslogtreecommitdiffstats
path: root/apps/patchwork/tests/test_mboxviews.py
blob: 0e57f4260668846a050236517cc8a31e2d377218 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# vim: set fileencoding=utf-8 :
#
# Patchwork - automated patch tracking system
# Copyright (C) 2009 Jeremy Kerr <jk@ozlabs.org>
#
# This file is part of the Patchwork package.
#
# Patchwork is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Patchwork is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Patchwork; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import unittest
import email
import datetime
import dateutil.parser, dateutil.tz
from django.test import TestCase
from django.test.client import Client
from patchwork.models import Patch, Comment, Person
from patchwork.tests.utils import defaults, create_user, find_in_context

class MboxPatchResponseTest(TestCase):
    """ Test that the mbox view appends the Acked-by from a patch comment """
    def setUp(self):
        defaults.project.save()

        self.person = defaults.patch_author_person
        self.person.save()

        self.patch = Patch(project = defaults.project,
                           msgid = 'p1', name = 'testpatch',
                           submitter = self.person, content = '')
        self.patch.save()
        comment = Comment(patch = self.patch, msgid = 'p1',
                submitter = self.person,
                content = 'comment 1 text\nAcked-by: 1\n')
        comment.save()

        comment = Comment(patch = self.patch, msgid = 'p2',
                submitter = self.person,
                content = 'comment 2 text\nAcked-by: 2\n')
        comment.save()

    def testPatchResponse(self):
        response = self.client.get('/patch/%d/mbox/' % self.patch.id)
        self.assertContains(response,
                'Acked-by: 1\nAcked-by: 2\n')

class MboxPatchSplitResponseTest(TestCase):
    """ Test that the mbox view appends the Acked-by from a patch comment,
        and places it before an '---' update line. """
    def setUp(self):
        defaults.project.save()

        self.person = defaults.patch_author_person
        self.person.save()

        self.patch = Patch(project = defaults.project,
                           msgid = 'p1', name = 'testpatch',
                           submitter = self.person, content = '')
        self.patch.save()
        comment = Comment(patch = self.patch, msgid = 'p1',
                submitter = self.person,
                content = 'comment 1 text\nAcked-by: 1\n---\nupdate\n')
        comment.save()

        comment = Comment(patch = self.patch, msgid = 'p2',
                submitter = self.person,
                content = 'comment 2 text\nAcked-by: 2\n')
        comment.save()

    def testPatchResponse(self):
        response = self.client.get('/patch/%d/mbox/' % self.patch.id)
        self.assertContains(response,
                'Acked-by: 1\nAcked-by: 2\n')

class MboxPassThroughHeaderTest(TestCase):
    """ Test that we see 'Cc' and 'To' headers passed through from original
        message to mbox view """

    def setUp(self):
        defaults.project.save()
        self.person = defaults.patch_author_person
        self.person.save()

        self.cc_header = 'Cc: CC Person <cc@example.com>'
        self.to_header = 'To: To Person <to@example.com>'
        self.date_header = 'Date: Fri, 7 Jun 2013 15:42:54 +1000'

        self.patch = Patch(project = defaults.project,
                           msgid = 'p1', name = 'testpatch',
                           submitter = self.person, content = '')

    def testCCHeader(self):
        self.patch.headers = self.cc_header + '\n'
        self.patch.save()

        response = self.client.get('/patch/%d/mbox/' % self.patch.id)
        self.assertContains(response, self.cc_header)

    def testToHeader(self):
        self.patch.headers = self.to_header + '\n'
        self.patch.save()

        response = self.client.get('/patch/%d/mbox/' % self.patch.id)
        self.assertContains(response, self.to_header)

    def testDateHeader(self):
        self.patch.headers = self.date_header + '\n'
        self.patch.save()

        response = self.client.get('/patch/%d/mbox/' % self.patch.id)
        self.assertContains(response, self.date_header)

class MboxBrokenFromHeaderTest(TestCase):
    """ Test that a person with characters outside ASCII in his name do
        produce correct From header. As RFC 2822 state we must retain the
        <user@doamin.tld> format for the mail while the name part may be coded
        in some ways. """

    def setUp(self):
        defaults.project.save()
        self.person = defaults.patch_author_person
        self.person.name = u'©ool guŷ'
        self.person.save()

        self.patch = Patch(project = defaults.project,
                msgid = 'p1', name = 'testpatch',
                submitter = self.person, content = '')

    def testFromHeader(self):
        self.patch.save()
        from_email = '<' + self.person.email + '>'

        response = self.client.get('/patch/%d/mbox/' % self.patch.id)
        self.assertContains(response, from_email)

class MboxDateHeaderTest(TestCase):
    """ Test that the date provided in the patch mail view is correct """

    def setUp(self):
        defaults.project.save()
        self.person = defaults.patch_author_person
        self.person.save()

        self.patch = Patch(project = defaults.project,
                           msgid = 'p1', name = 'testpatch',
                           submitter = self.person, content = '')
        self.patch.save()

    def testDateHeader(self):
        response = self.client.get('/patch/%d/mbox/' % self.patch.id)
        mail = email.message_from_string(response.content)
        mail_date = dateutil.parser.parse(mail['Date'])
        # patch dates are all in UTC
        patch_date = self.patch.date.replace(tzinfo=dateutil.tz.tzutc(),
                                            microsecond=0)
        self.assertEqual(mail_date, patch_date)

    def testSuppliedDateHeader(self):
        hour_offset = 3
        tz = dateutil.tz.tzoffset(None, hour_offset * 60 * 60)
        date = datetime.datetime.utcnow() - datetime.timedelta(days = 1)
        date = date.replace(tzinfo=tz, microsecond=0)

        self.patch.headers = 'Date: %s\n' % date.strftime("%a, %d %b %Y %T %z")
        self.patch.save()

        response = self.client.get('/patch/%d/mbox/' % self.patch.id)
        mail = email.message_from_string(response.content)
        mail_date = dateutil.parser.parse(mail['Date'])
        self.assertEqual(mail_date, date)

class MboxCommentPostcriptUnchangedTest(TestCase):
    """ Test that the mbox view doesn't change the postscript part of a mail.
        There where always a missing blank right after the postscript
        delimiter '---' and an additional newline right before. """
    def setUp(self):
        defaults.project.save()

        self.person = defaults.patch_author_person
        self.person.save()

        self.patch = Patch(project = defaults.project,
                           msgid = 'p1', name = 'testpatch',
                           submitter = self.person, content = '')
        self.patch.save()

        self.txt = 'some comment\n---\n some/file | 1 +\n'

        comment = Comment(patch = self.patch, msgid = 'p1',
                submitter = self.person,
                content = self.txt)
        comment.save()

    def testCommentUnchanged(self):
        response = self.client.get('/patch/%d/mbox/' % self.patch.id)
        self.assertContains(response, self.txt)
        self.txt += "\n"
        self.assertNotContains(response, self.txt)