~launchpad-pqm/launchpad/devel

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
# Copyright 2009-2010 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

""" Unit-tests for the Answer Tracker Mail Notifications. """

__metaclass__ = type

from unittest import TestCase

from zope.interface import implements

from lp.answers.enums import QuestionRecipientSet
from lp.answers.notification import (
    QuestionAddedNotification,
    QuestionModifiedDefaultNotification,
    )
from lp.registry.interfaces.person import IPerson


class TestQuestionModifiedNotification(QuestionModifiedDefaultNotification):
    """Subclass that do not send emails and with simpler initialization.

    Since notifications are handlers that accomplish their action on
    initialization, override the relevant method to make them easier to test.
    """

    def initialize(self):
        """Leave the fixture to initialize the notification properly."""
        self.new_message = None

    def shouldNotify(self):
        """Do not send emails!"""
        return False


class StubQuestion:
    """Question with a only an id and title attributes."""

    def __init__(self, id=1, title="Question title"):
        self.id = id
        self.title = title
        self.owner = FakeUser()


class StubQuestionMessage:
    """Question message with only a subject attribute."""

    def __init__(self, subject='Message subject'):
        self.subject = subject


class FakeUser:
    """A fake user."""
    implements(IPerson)


class FakeEvent:
    """A fake event."""
    user = FakeUser()


class QuestionModifiedDefaultNotificationTestCase(TestCase):
    """Test cases for mail notifications about modified questions."""

    def setUp(self):
        """Create a notification with a fake question."""
        self.notification = TestQuestionModifiedNotification(
            StubQuestion(), FakeEvent())

    def test_recipient_set(self):
        self.assertEqual(
            QuestionRecipientSet.SUBSCRIBER,
            self.notification.recipient_set)

    def test_buildBody_with_separator(self):
        # A body with a separator is preserved.
        formatted_body = self.notification.buildBody(
            "body\n-- ", "rationale")
        self.assertEqual(
            "body\n-- \nrationale", formatted_body)

    def test_buildBody_without_separator(self):
        # A separator will added to body if one is not present.
        formatted_body = self.notification.buildBody(
            "body -- mdash", "rationale")
        self.assertEqual(
            "body -- mdash\n-- \nrationale", formatted_body)

    def test_getSubject(self):
        """getSubject() when there is no message added to the question."""
        self.assertEquals(
            'Re: [Question #1]: Question title',
            self.notification.getSubject())

    def test_user_is_event_user(self):
        """The notification user is always the event user."""
        question = StubQuestion()
        event = FakeEvent()
        notification = TestQuestionModifiedNotification(question, event)
        self.assertEqual(event.user, notification.user)
        self.assertNotEqual(question.owner, notification.user)


class TestQuestionAddedNotification(QuestionAddedNotification):
    """A subclass that does not send emails."""

    def shouldNotify(self):
        return False


class QuestionAddedNotificationTestCase(TestCase):
    """Test cases for mail notifications about created questions."""

    def setUp(self):
        """Create a notification with a fake question."""
        self.question = StubQuestion()
        self.event = FakeEvent()
        self.notification = TestQuestionAddedNotification(
            self.question, self.event)

    def test_recipient_set(self):
        self.assertEqual(
            QuestionRecipientSet.ASKER_SUBSCRIBER,
            self.notification.recipient_set)

    def test_user_is_question_owner(self):
        """The notification user is always the question owner."""
        self.assertEqual(self.question.owner, self.notification.user)
        self.assertNotEqual(self.event.user, self.notification.user)