~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/bugs/mail/tests/test_bug_task_modification.py

[r=mars][ui=none][bug=605340] Add in X-Launchpad-Bug-Modifier header,
        identifying the event creator's display name and name,
        to bug mail generated by bugnotification.py.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2010 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
"""Test for emails sent after bug task modification."""
 
5
 
 
6
from unittest import TestLoader
 
7
 
 
8
import transaction
 
9
 
 
10
from zope.component import getUtility
 
11
from zope.interface import providedBy
 
12
from zope.event import notify
 
13
 
 
14
from canonical.testing import DatabaseFunctionalLayer
 
15
from canonical.launchpad.database import BugNotification
 
16
from canonical.launchpad.webapp.interfaces import ILaunchBag
 
17
 
 
18
 
 
19
from lp.bugs.interfaces.bugtask import BugTaskStatus
 
20
from lp.bugs.scripts.bugnotification import construct_email_notifications
 
21
from lp.testing import TestCaseWithFactory
 
22
 
 
23
from lazr.lifecycle.event import ObjectModifiedEvent
 
24
from lazr.lifecycle.snapshot import Snapshot
 
25
 
 
26
 
 
27
class TestModificationNotification(TestCaseWithFactory):
 
28
    """Test email notifications when a bug task is modified."""
 
29
 
 
30
    layer = DatabaseFunctionalLayer
 
31
 
 
32
    def setUp(self):
 
33
        # Run the tests as a logged-in user.
 
34
        super(TestModificationNotification, self).setUp(
 
35
            user='test@canonical.com')
 
36
        self.user = getUtility(ILaunchBag).user
 
37
        self.product = self.factory.makeProduct(owner=self.user)
 
38
        self.bug = self.factory.makeBug(product=self.product)
 
39
        self.bug_task = self.bug.getBugTask(self.product)
 
40
        self.bug_task_before_modification = Snapshot(self.bug_task,
 
41
            providing=providedBy(self.bug_task))
 
42
 
 
43
    def test_for_bug_modifier_header(self):
 
44
        """Test X-Launchpad-Bug-Modifier appears when a bug is modified."""
 
45
        self.bug_task.transitionToStatus(BugTaskStatus.CONFIRMED, self.user)
 
46
        notify(ObjectModifiedEvent(
 
47
            self.bug_task, self.bug_task_before_modification,
 
48
            ['status'], user=self.user))
 
49
        transaction.commit()
 
50
        latest_notification = BugNotification.selectFirst(orderBy='-id')
 
51
        notifications, messages = construct_email_notifications(
 
52
            [latest_notification])
 
53
        self.assertEqual(len(notifications), 1,
 
54
                         'email notification not created')
 
55
        headers = [msg['X-Launchpad-Bug-Modifier'] for msg in messages]
 
56
        self.assertEqual(len(headers), len(messages))
 
57
 
 
58
 
 
59
def test_suite():
 
60
    return TestLoader().loadTestsFromName(__name__)