~launchpad-pqm/launchpad/devel

8687.15.17 by Karl Fogel
Add the copyright header block to the rest of the files under lib/lp/.
1
# Copyright 2009 Canonical Ltd.  This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
6334.3.21 by Aaron Bentley
Cleanups
3
6334.6.36 by Aaron Bentley
Rename code review messages to code review comments
4
"""Test CodeReviewComment emailing functionality."""
6334.3.6 by Aaron Bentley
Add more docs
5
6334.3.21 by Aaron Bentley
Cleanups
6
7675.632.1 by Gary Poster
fix failing test by adding a transaction.commit()
7
import transaction
7407.1.12 by Tim Penhey
Test that the mailer gets the correct attachments.
8
from zope.component import getUtility
8899.1.2 by Aaron Bentley
Use BMP.address for code review comments if person hides their email.
9
from zope.security.proxy import removeSecurityProxy
7407.1.12 by Tim Penhey
Test that the mailer gets the correct attachments.
10
12929.9.2 by j.c.sackett
Moved messages from canonical to lp.services
11
from lp.services.messages.interfaces.message import IMessageSet
6334.3.26 by Aaron Bentley
Update from review
12
from canonical.launchpad.mail import format_address
8555.2.9 by Tim Penhey
Move CodeReviewVote enum.
13
from canonical.launchpad.webapp import canonical_url
11666.3.5 by Curtis Hovey
Import layers from canonical.testing.layers.
14
from canonical.testing.layers import LaunchpadFunctionalLayer
8555.2.9 by Tim Penhey
Move CodeReviewVote enum.
15
from lp.code.enums import (
11403.1.4 by Henning Eggers
Reformatted imports using format-imports script r32.
16
    BranchSubscriptionNotificationLevel,
17
    CodeReviewNotificationLevel,
18
    CodeReviewVote,
19
    )
20
from lp.code.mail.codereviewcomment import CodeReviewCommentMailer
21
from lp.testing import (
22
    login,
23
    login_person,
24
    TestCaseWithFactory,
25
    )
6334.3.4 by Aaron Bentley
Test CodeReviewMessage.forCreation
26
27
6334.6.36 by Aaron Bentley
Rename code review messages to code review comments
28
class TestCodeReviewComment(TestCaseWithFactory):
29
    """Test that comments are generated as expected."""
6334.3.4 by Aaron Bentley
Test CodeReviewMessage.forCreation
30
31
    layer = LaunchpadFunctionalLayer
32
33
    def setUp(self):
6334.3.6 by Aaron Bentley
Add more docs
34
        """Prepare test fixtures."""
6334.3.31 by Aaron Bentley
Fix tests to cope with default-anonymity
35
        TestCaseWithFactory.setUp(self, user='test@canonical.com')
6334.3.4 by Aaron Bentley
Test CodeReviewMessage.forCreation
36
6334.6.36 by Aaron Bentley
Rename code review messages to code review comments
37
    def makeCommentAndSubscriber(self, notification_level=None,
6334.6.32 by Aaron Bentley
Handle messages with existing footers
38
                                 body=None, as_reply=False, vote=None,
6475.2.22 by Barry Warsaw
mergeRF
39
                                 vote_tag=None, subject=None):
6334.6.36 by Aaron Bentley
Rename code review messages to code review comments
40
        """Return a comment and a subscriber."""
6334.3.4 by Aaron Bentley
Test CodeReviewMessage.forCreation
41
        sender = self.factory.makePerson(
6438.2.4 by Tim Penhey
Fix tests due to new branch subscribers with valid email addresses.
42
            displayname='Sender', email='sender@example.com')
6334.6.36 by Aaron Bentley
Rename code review messages to code review comments
43
        comment = self.factory.makeCodeReviewComment(
6475.2.22 by Barry Warsaw
mergeRF
44
            sender, body=body, vote=vote, vote_tag=vote_tag, subject=subject)
6334.3.10 by Aaron Bentley
Add In-Reply-To to possible headers
45
        if as_reply:
6334.6.36 by Aaron Bentley
Rename code review messages to code review comments
46
            comment = self.factory.makeCodeReviewComment(
6475.2.22 by Barry Warsaw
mergeRF
47
                sender, body=body, parent=comment, subject=subject)
6334.3.4 by Aaron Bentley
Test CodeReviewMessage.forCreation
48
        subscriber = self.factory.makePerson(
6438.2.4 by Tim Penhey
Fix tests due to new branch subscribers with valid email addresses.
49
            displayname='Subscriber', email='subscriber@example.com')
6334.3.8 by Aaron Bentley
Ensure mail doesn't go to people who don't want it.
50
        if notification_level is None:
51
            notification_level = CodeReviewNotificationLevel.FULL
6334.6.36 by Aaron Bentley
Rename code review messages to code review comments
52
        comment.branch_merge_proposal.source_branch.subscribe(
6334.3.4 by Aaron Bentley
Test CodeReviewMessage.forCreation
53
            subscriber, BranchSubscriptionNotificationLevel.NOEMAIL, None,
7675.708.3 by Tim Penhey
Add subscribed_by and unsubscribed_by to the subscribe and unsubscribe branch methods.
54
            notification_level, subscriber)
10482.2.9 by Tim Penhey
Make the last few tests pass.
55
        # Email is not sent on construction, so fake a root message id on the
56
        # merge proposal.
57
        login_person(comment.branch_merge_proposal.registrant)
58
        comment.branch_merge_proposal.root_message_id = 'fake-id'
59
        # Log our test user back in.
60
        login('test@canonical.com')
6334.6.36 by Aaron Bentley
Rename code review messages to code review comments
61
        return comment, subscriber
6334.3.4 by Aaron Bentley
Test CodeReviewMessage.forCreation
62
6334.7.4 by Aaron Bentley
Update from review
63
    def makeMailer(self, body=None, as_reply=False, vote=None, vote_tag=None):
6334.6.36 by Aaron Bentley
Rename code review messages to code review comments
64
        """Return a CodeReviewCommentMailer and the sole subscriber."""
65
        comment, subscriber = self.makeCommentAndSubscriber(
6334.6.32 by Aaron Bentley
Handle messages with existing footers
66
            body=body, as_reply=as_reply, vote=vote, vote_tag=vote_tag)
6334.6.36 by Aaron Bentley
Rename code review messages to code review comments
67
        return CodeReviewCommentMailer.forCreation(comment), subscriber
6334.3.5 by Aaron Bentley
Test email generation
68
6677.1.2 by Jonathan Lange
Refactor the bit of the tests that inspects getRecipientPersons
69
    def assertRecipientsMatches(self, recipients, mailer):
70
        """Assert that `mailer` will send to the people in `recipients`."""
6677.1.3 by Jonathan Lange
Only access IEmailAddress.email once.
71
        persons = zip(*(mailer._recipients.getRecipientPersons()))[1]
72
        self.assertEqual(set(recipients), set(persons))
6677.1.2 by Jonathan Lange
Refactor the bit of the tests that inspects getRecipientPersons
73
6334.3.5 by Aaron Bentley
Test email generation
74
    def test_forCreation(self):
6334.3.6 by Aaron Bentley
Add more docs
75
        """Ensure that forCreation produces a mailer with expected values."""
6334.6.36 by Aaron Bentley
Rename code review messages to code review comments
76
        comment, subscriber = self.makeCommentAndSubscriber()
77
        mailer = CodeReviewCommentMailer.forCreation(comment)
78
        self.assertEqual(comment.message.subject,
6334.3.4 by Aaron Bentley
Test CodeReviewMessage.forCreation
79
                         mailer._subject_template)
6438.2.4 by Tim Penhey
Fix tests due to new branch subscribers with valid email addresses.
80
        bmp = comment.branch_merge_proposal
81
        # The branch owners are implicitly subscribed to their branches
82
        # when the branches are created.
6677.1.2 by Jonathan Lange
Refactor the bit of the tests that inspects getRecipientPersons
83
        self.assertRecipientsMatches(
84
            [subscriber, bmp.source_branch.owner, bmp.target_branch.owner],
85
            mailer)
6334.3.4 by Aaron Bentley
Test CodeReviewMessage.forCreation
86
        self.assertEqual(
6334.6.36 by Aaron Bentley
Rename code review messages to code review comments
87
            comment.branch_merge_proposal, mailer.merge_proposal)
88
        sender = comment.message.owner
6334.3.26 by Aaron Bentley
Update from review
89
        sender_address = format_address(sender.displayname,
90
            sender.preferredemail.email)
91
        self.assertEqual(sender_address, mailer.from_address)
6334.6.36 by Aaron Bentley
Rename code review messages to code review comments
92
        self.assertEqual(comment, mailer.code_review_comment)
6334.3.4 by Aaron Bentley
Test CodeReviewMessage.forCreation
93
6334.3.8 by Aaron Bentley
Ensure mail doesn't go to people who don't want it.
94
    def test_forCreationStatusSubscriber(self):
95
        """Ensure that subscriptions with STATUS aren't used."""
6334.6.36 by Aaron Bentley
Rename code review messages to code review comments
96
        comment, subscriber = self.makeCommentAndSubscriber(
6334.3.8 by Aaron Bentley
Ensure mail doesn't go to people who don't want it.
97
            CodeReviewNotificationLevel.STATUS)
6334.6.36 by Aaron Bentley
Rename code review messages to code review comments
98
        mailer = CodeReviewCommentMailer.forCreation(comment)
6438.2.4 by Tim Penhey
Fix tests due to new branch subscribers with valid email addresses.
99
        bmp = comment.branch_merge_proposal
100
        # The branch owners are implicitly subscribed to their branches
101
        # when the branches are created.
6677.1.2 by Jonathan Lange
Refactor the bit of the tests that inspects getRecipientPersons
102
        self.assertRecipientsMatches(
103
            [bmp.source_branch.owner, bmp.target_branch.owner], mailer)
6334.3.8 by Aaron Bentley
Ensure mail doesn't go to people who don't want it.
104
105
    def test_forCreationStatusNoEmail(self):
106
        """Ensure that subscriptions with NOEMAIL aren't used."""
6334.6.36 by Aaron Bentley
Rename code review messages to code review comments
107
        comment, subscriber = self.makeCommentAndSubscriber(
6334.3.8 by Aaron Bentley
Ensure mail doesn't go to people who don't want it.
108
            CodeReviewNotificationLevel.NOEMAIL)
6334.6.36 by Aaron Bentley
Rename code review messages to code review comments
109
        mailer = CodeReviewCommentMailer.forCreation(comment)
6438.2.4 by Tim Penhey
Fix tests due to new branch subscribers with valid email addresses.
110
        bmp = comment.branch_merge_proposal
111
        # The branch owners are implicitly subscribed to their branches
112
        # when the branches are created.
6677.1.2 by Jonathan Lange
Refactor the bit of the tests that inspects getRecipientPersons
113
        self.assertRecipientsMatches(
114
            [bmp.source_branch.owner, bmp.target_branch.owner], mailer)
6334.3.8 by Aaron Bentley
Ensure mail doesn't go to people who don't want it.
115
6475.2.22 by Barry Warsaw
mergeRF
116
    def test_subjectWithStringExpansions(self):
117
        # The mailer should not attempt to expand templates in the subject.
118
        comment, subscriber = self.makeCommentAndSubscriber(
119
            subject='A %(carefully)s constructed subject')
120
        mailer = CodeReviewCommentMailer.forCreation(comment)
121
        self.assertEqual(
122
            'A %(carefully)s constructed subject',
12505.5.15 by Ian Booth
Complete tests
123
            mailer._getSubject(email=None, recipient=subscriber))
6475.2.22 by Barry Warsaw
mergeRF
124
6334.3.9 by Aaron Bentley
Add Message-Id and Reply-To headers
125
    def test_getReplyAddress(self):
6334.3.21 by Aaron Bentley
Cleanups
126
        """Ensure that the reply-to address is reasonable."""
6334.3.9 by Aaron Bentley
Add Message-Id and Reply-To headers
127
        mailer, subscriber = self.makeMailer()
6334.6.36 by Aaron Bentley
Rename code review messages to code review comments
128
        merge_proposal = mailer.code_review_comment.branch_merge_proposal
6334.3.9 by Aaron Bentley
Add Message-Id and Reply-To headers
129
        expected = 'mp+%d@code.launchpad.dev' % merge_proposal.id
130
        self.assertEqual(expected, mailer._getReplyToAddress())
131
6334.3.5 by Aaron Bentley
Test email generation
132
    def test_generateEmail(self):
6334.3.23 by Aaron Bentley
More cleanups
133
        """Ensure mailer's generateEmail method produces expected values."""
6334.3.10 by Aaron Bentley
Add In-Reply-To to possible headers
134
        mailer, subscriber = self.makeMailer(as_reply=True)
7325.5.9 by Aaron Bentley
Update tests for MailController API
135
        ctrl = mailer.generateEmail(
136
            subscriber.preferredemail.email, subscriber)
6334.6.36 by Aaron Bentley
Rename code review messages to code review comments
137
        message = mailer.code_review_comment.message
7325.5.8 by Aaron Bentley
Convert to new MailController object
138
        self.assertEqual(ctrl.subject, message.subject)
139
        self.assertEqual(ctrl.body.splitlines()[:-3],
6334.3.5 by Aaron Bentley
Test email generation
140
                         message.text_contents.splitlines())
141
        source_branch = mailer.merge_proposal.source_branch
7027.4.6 by Paul Hummer
Fixed the tests in code review comment
142
        branch_name = source_branch.bzr_identity
7325.5.9 by Aaron Bentley
Update tests for MailController API
143
        self.assertEqual(
144
            ctrl.body.splitlines()[-3:], ['-- ',
145
            canonical_url(mailer.merge_proposal),
146
            'You are subscribed to branch %s.' % branch_name])
6334.3.26 by Aaron Bentley
Update from review
147
        rationale = mailer._recipients.getReason('subscriber@example.com')[1]
6334.3.5 by Aaron Bentley
Test email generation
148
        expected = {'X-Launchpad-Branch': source_branch.unique_name,
6334.3.9 by Aaron Bentley
Add Message-Id and Reply-To headers
149
                    'X-Launchpad-Message-Rationale': rationale,
8615.2.2 by Tim Penhey
Fix the test for code review comment emails.
150
                    'X-Launchpad-Notification-Type': 'code-review',
6714.2.2 by Tim Penhey
Fix the mailout tests for the X-Launchpad-Project header.
151
                    'X-Launchpad-Project': source_branch.product.name,
6334.3.9 by Aaron Bentley
Add Message-Id and Reply-To headers
152
                    'Message-Id': message.rfc822msgid,
6334.3.10 by Aaron Bentley
Add In-Reply-To to possible headers
153
                    'Reply-To': mailer._getReplyToAddress(),
154
                    'In-Reply-To': message.parent.rfc822msgid}
6334.3.9 by Aaron Bentley
Add Message-Id and Reply-To headers
155
        for header, value in expected.items():
6805.13.53 by Aaron Bentley
Merge branch-mail into diff-orm
156
            self.assertEqual(value, ctrl.headers[header], header)
7325.5.9 by Aaron Bentley
Update tests for MailController API
157
        self.assertEqual(expected, ctrl.headers)
6334.3.5 by Aaron Bentley
Test email generation
158
6821.4.4 by Aaron Bentley
Use root_message_id, not root_comment, as in-reply-to id
159
    def test_useRootMessageId(self):
160
        """Ensure mailer's generateEmail method produces expected values."""
161
        mailer, subscriber = self.makeMailer(as_reply=False)
7325.5.9 by Aaron Bentley
Update tests for MailController API
162
        ctrl = mailer.generateEmail(
163
            subscriber.preferredemail.email, subscriber)
6821.4.4 by Aaron Bentley
Use root_message_id, not root_comment, as in-reply-to id
164
        self.assertEqual(mailer.merge_proposal.root_message_id,
7325.5.8 by Aaron Bentley
Convert to new MailController object
165
                         ctrl.headers['In-Reply-To'])
6821.4.4 by Aaron Bentley
Use root_message_id, not root_comment, as in-reply-to id
166
167
    def test_nonReplyCommentUsesRootMessageId(self):
168
        """Ensure mailer's generateEmail method produces expected values."""
169
        comment, subscriber = self.makeCommentAndSubscriber()
170
        second_comment = self.factory.makeCodeReviewComment(
171
            merge_proposal=comment.branch_merge_proposal)
172
        mailer = CodeReviewCommentMailer.forCreation(second_comment)
7325.5.9 by Aaron Bentley
Update tests for MailController API
173
        ctrl = mailer.generateEmail(
174
            subscriber.preferredemail.email, subscriber)
6821.4.4 by Aaron Bentley
Use root_message_id, not root_comment, as in-reply-to id
175
        self.assertEqual(comment.branch_merge_proposal.root_message_id,
7325.5.8 by Aaron Bentley
Convert to new MailController object
176
                         ctrl.headers['In-Reply-To'])
6821.4.4 by Aaron Bentley
Use root_message_id, not root_comment, as in-reply-to id
177
6334.6.32 by Aaron Bentley
Handle messages with existing footers
178
    def test_appendToFooter(self):
179
        """If there is an existing footer, we append to it."""
180
        mailer, subscriber = self.makeMailer(
6334.7.7 by Aaron Bentley
Updates from review
181
            body='Hi!\n'
182
            '-- \n'
183
            'I am a wacky guy.\n')
7027.4.6 by Paul Hummer
Fixed the tests in code review comment
184
        branch_name = mailer.merge_proposal.source_branch.bzr_identity
12505.5.20 by Ian Booth
Fix test
185
        body = mailer._getBody(subscriber.preferredemail.email, subscriber)
6334.6.32 by Aaron Bentley
Handle messages with existing footers
186
        self.assertEqual(body.splitlines()[1:],
6334.7.7 by Aaron Bentley
Updates from review
187
            ['-- ', 'I am a wacky guy.', '',
6475.2.22 by Barry Warsaw
mergeRF
188
             canonical_url(mailer.merge_proposal),
6334.6.32 by Aaron Bentley
Handle messages with existing footers
189
             'You are subscribed to branch %s.' % branch_name])
190
6334.3.18 by Aaron Bentley
Add display of votes to CodeReviewMessage emails
191
    def test_generateEmailWithVote(self):
6334.3.21 by Aaron Bentley
Cleanups
192
        """Ensure that votes are displayed."""
6334.3.18 by Aaron Bentley
Add display of votes to CodeReviewMessage emails
193
        mailer, subscriber = self.makeMailer(
194
            vote=CodeReviewVote.APPROVE)
7325.5.9 by Aaron Bentley
Update tests for MailController API
195
        ctrl = mailer.generateEmail(
196
            subscriber.preferredemail.email, subscriber)
7402.1.3 by Tim Penhey
Fix tests.
197
        self.assertEqual('Review: Approve', ctrl.body.splitlines()[0])
7325.5.8 by Aaron Bentley
Convert to new MailController object
198
        self.assertEqual(ctrl.body.splitlines()[1:-3],
6334.3.18 by Aaron Bentley
Add display of votes to CodeReviewMessage emails
199
                         mailer.message.text_contents.splitlines())
200
201
    def test_generateEmailWithVoteAndTag(self):
6334.3.26 by Aaron Bentley
Update from review
202
        """Ensure that vote tags are displayed."""
6334.3.18 by Aaron Bentley
Add display of votes to CodeReviewMessage emails
203
        mailer, subscriber = self.makeMailer(
204
            vote=CodeReviewVote.APPROVE, vote_tag='DBTAG')
7325.5.9 by Aaron Bentley
Update tests for MailController API
205
        ctrl = mailer.generateEmail(
206
            subscriber.preferredemail.email, subscriber)
7402.1.3 by Tim Penhey
Fix tests.
207
        self.assertEqual('Review: Approve dbtag', ctrl.body.splitlines()[0])
7325.5.8 by Aaron Bentley
Convert to new MailController object
208
        self.assertEqual(ctrl.body.splitlines()[1:-3],
6334.3.18 by Aaron Bentley
Add display of votes to CodeReviewMessage emails
209
                         mailer.message.text_contents.splitlines())
210
10831.1.1 by Aaron Bentley
Fix code review comments from mail with encoded attachments
211
    def makeComment(self, email_message):
212
        message = getUtility(IMessageSet).fromEmail(email_message.as_string())
213
        bmp = self.factory.makeBranchMergeProposal()
214
        comment = bmp.createCommentFromMessage(
215
            message, None, None, email_message)
216
        # We need to make sure the Librarian is up-to-date, so we commit.
217
        transaction.commit()
218
        return comment
219
7407.1.12 by Tim Penhey
Test that the mailer gets the correct attachments.
220
    def test_mailer_attachments(self):
7407.1.13 by Tim Penhey
Update following review.
221
        # Ensure that the attachments are attached.
222
        # Only attachments that we would show in the web ui are attached,
223
        # so the diff should be attached, and the jpeg image not.
7407.1.12 by Tim Penhey
Test that the mailer gets the correct attachments.
224
        msg = self.factory.makeEmailMessage(
225
            body='This is the body of the email.',
226
            attachments=[
227
                ('inc.diff', 'text/x-diff', 'This is a diff.'),
228
                ('pic.jpg', 'image/jpeg', 'Binary data')])
10831.1.1 by Aaron Bentley
Fix code review comments from mail with encoded attachments
229
        comment = self.makeComment(msg)
10482.2.9 by Tim Penhey
Make the last few tests pass.
230
        mailer = CodeReviewCommentMailer.forCreation(comment)
7407.1.12 by Tim Penhey
Test that the mailer gets the correct attachments.
231
        # The attachments of the mailer should have only the diff.
9795.3.9 by Tim Penhey
Fix the test that was testing the old way attachments were added.
232
        [outgoing_attachment] = mailer.attachments
233
        self.assertEqual('inc.diff', outgoing_attachment[1])
234
        self.assertEqual('text/x-diff', outgoing_attachment[2])
10360.3.1 by Tim Penhey
Make the test fail.
235
        # The attachments are attached to the outgoing message.
10831.1.1 by Aaron Bentley
Fix code review comments from mail with encoded attachments
236
        person = comment.branch_merge_proposal.target_branch.owner
10360.3.1 by Tim Penhey
Make the test fail.
237
        message = mailer.generateEmail(
238
            person.preferredemail.email, person).makeMessage()
239
        self.assertTrue(message.is_multipart())
240
        attachment = message.get_payload()[1]
241
        self.assertEqual('inc.diff', attachment.get_filename())
242
        self.assertEqual('text/x-diff', attachment['content-type'])
7407.1.12 by Tim Penhey
Test that the mailer gets the correct attachments.
243
10831.1.1 by Aaron Bentley
Fix code review comments from mail with encoded attachments
244
    def test_encoded_attachments(self):
245
        msg = self.factory.makeEmailMessage(
246
            body='This is the body of the email.',
247
            attachments=[('inc.diff', 'text/x-diff', 'This is a diff.')],
10831.1.2 by Aaron Bentley
Style tweak.
248
            encode_attachments=True)
10831.1.1 by Aaron Bentley
Fix code review comments from mail with encoded attachments
249
        comment = self.makeComment(msg)
250
        mailer = CodeReviewCommentMailer.forCreation(comment)
251
        person = comment.branch_merge_proposal.target_branch.owner
252
        message = mailer.generateEmail(
253
            person.preferredemail.email, person).makeMessage()
254
        attachment = message.get_payload()[1]
255
        self.assertEqual(
256
            'This is a diff.', attachment.get_payload(decode=True))
257
8771.4.1 by Aaron Bentley
Add CodeReviewCommenMailer._getToAddresses.
258
    def makeCommentAndParticipants(self):
8771.5.4 by Aaron Bentley
Update for review.
259
        """Create a merge proposal and comment.
260
261
        Proposal registered by "Proposer" and comment added by "Commenter".
262
        """
8771.4.1 by Aaron Bentley
Add CodeReviewCommenMailer._getToAddresses.
263
        proposer = self.factory.makePerson(
264
            email='proposer@email.com', displayname='Proposer')
265
        bmp = self.factory.makeBranchMergeProposal(registrant=proposer)
266
        commenter = self.factory.makePerson(
267
            email='commenter@email.com', displayname='Commenter')
8771.4.4 by Aaron Bentley
Ensure to_addrs and real_to are set correctly for CodeReviewCommentMailer
268
        bmp.source_branch.subscribe(commenter,
269
            BranchSubscriptionNotificationLevel.NOEMAIL, None,
7675.708.3 by Tim Penhey
Add subscribed_by and unsubscribed_by to the subscribe and unsubscribe branch methods.
270
            CodeReviewNotificationLevel.FULL, commenter)
8771.4.1 by Aaron Bentley
Add CodeReviewCommenMailer._getToAddresses.
271
        comment = bmp.createComment(commenter, 'hello')
272
        return comment
273
274
    def test_getToAddresses_no_parent(self):
8771.5.4 by Aaron Bentley
Update for review.
275
        """To address for a comment with no parent should be the proposer."""
8771.4.1 by Aaron Bentley
Add CodeReviewCommenMailer._getToAddresses.
276
        comment = self.makeCommentAndParticipants()
277
        mailer = CodeReviewCommentMailer.forCreation(comment)
278
        to = mailer._getToAddresses(
279
            comment.message.owner, 'comment@gmail.com')
280
        self.assertEqual(['Proposer <proposer@email.com>'], to)
281
        to = mailer._getToAddresses(
282
            comment.branch_merge_proposal.registrant, 'propose@gmail.com')
283
        self.assertEqual(['Proposer <propose@gmail.com>'], to)
284
8771.4.4 by Aaron Bentley
Ensure to_addrs and real_to are set correctly for CodeReviewCommentMailer
285
    def test_generateEmail_addresses(self):
8771.5.4 by Aaron Bentley
Update for review.
286
        """The to_addrs but not envelope_to should follow getToAddress.
287
288
        We provide false to addresses to make filters happier, but this
289
        should not affect the actual recipient list.
290
        """
8771.4.4 by Aaron Bentley
Ensure to_addrs and real_to are set correctly for CodeReviewCommentMailer
291
        comment = self.makeCommentAndParticipants()
292
        mailer = CodeReviewCommentMailer.forCreation(comment)
293
        ctrl = mailer.generateEmail('commenter@email.com',
294
                                    comment.message.owner)
295
        self.assertEqual(['Proposer <proposer@email.com>'], ctrl.to_addrs)
8771.4.5 by Aaron Bentley
Rename real_to to envelope_to.
296
        self.assertEqual(['commenter@email.com'], ctrl.envelope_to)
8771.4.4 by Aaron Bentley
Ensure to_addrs and real_to are set correctly for CodeReviewCommentMailer
297
8771.4.1 by Aaron Bentley
Add CodeReviewCommenMailer._getToAddresses.
298
    def test_getToAddresses_with_parent(self):
8771.5.4 by Aaron Bentley
Update for review.
299
        """To address for a reply should be the parent comment author."""
8771.4.1 by Aaron Bentley
Add CodeReviewCommenMailer._getToAddresses.
300
        comment = self.makeCommentAndParticipants()
301
        second_commenter = self.factory.makePerson(
302
            email='commenter2@email.com', displayname='Commenter2')
303
        reply = comment.branch_merge_proposal.createComment(
304
            second_commenter, 'hello2', parent=comment)
305
        mailer = CodeReviewCommentMailer.forCreation(reply)
306
        to = mailer._getToAddresses(second_commenter, 'comment2@gmail.com')
307
        self.assertEqual(['Commenter <commenter@email.com>'], to)
308
        to = mailer._getToAddresses(
309
            comment.message.owner, 'comment@gmail.com')
310
        self.assertEqual(['Commenter <comment@gmail.com>'], to)
6334.3.4 by Aaron Bentley
Test CodeReviewMessage.forCreation
311
8899.1.2 by Aaron Bentley
Use BMP.address for code review comments if person hides their email.
312
    def test_getToAddresses_with_hidden_address(self):
313
        """Don't show address if Person.hide_email_addresses."""
314
        comment = self.makeCommentAndParticipants()
315
        removeSecurityProxy(comment.message.owner).hide_email_addresses = True
316
        second_commenter = self.factory.makePerson(
317
            email='commenter2@email.com', displayname='Commenter2')
318
        reply = comment.branch_merge_proposal.createComment(
319
            second_commenter, 'hello2', parent=comment)
320
        mailer = CodeReviewCommentMailer.forCreation(reply)
321
        to = mailer._getToAddresses(second_commenter, 'comment2@gmail.com')
322
        self.assertEqual([mailer.merge_proposal.address], to)