~launchpad-pqm/launchpad/devel

11149.3.1 by Jeroen Vermeulen
Test cruft in Answers.
1
# Copyright 2009-2010 Canonical Ltd.  This software is licensed under the
8687.15.11 by Karl Fogel
Add the copyright header block to files under lib/lp/answers/.
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3859.2.1 by Francis J. Lacoste
Replace old [Support #dd] prefix when sending out notification.
3
4
""" Unit-tests for the Answer Tracker Mail Notifications. """
5
6
__metaclass__ = type
7
11149.3.1 by Jeroen Vermeulen
Test cruft in Answers.
8
from unittest import TestCase
3859.2.1 by Francis J. Lacoste
Replace old [Support #dd] prefix when sending out notification.
9
7876.3.12 by Francis J. Lacoste
Event.user is a principal, so we need an IPerson case.
10
from zope.interface import implements
11
12952.5.1 by Curtis Hovey
Added recipient_set property to QuestionAddedNotification.
12
from lp.answers.enums import QuestionRecipientSet
12906.1.2 by Curtis Hovey
Always send notfications about added questions as though they come from the owner.
13
from lp.answers.notification import (
14
    QuestionAddedNotification,
15
    QuestionModifiedDefaultNotification,
16
    )
7675.110.3 by Curtis Hovey
Ran the migration script to move registry code to lp.registry.
17
from lp.registry.interfaces.person import IPerson
3859.2.1 by Francis J. Lacoste
Replace old [Support #dd] prefix when sending out notification.
18
19
20
class TestQuestionModifiedNotification(QuestionModifiedDefaultNotification):
21
    """Subclass that do not send emails and with simpler initialization.
22
23
    Since notifications are handlers that accomplish their action on
24
    initialization, override the relevant method to make them easier to test.
25
    """
26
27
    def initialize(self):
28
        """Leave the fixture to initialize the notification properly."""
29
        self.new_message = None
30
31
    def shouldNotify(self):
32
        """Do not send emails!"""
33
        return False
34
35
36
class StubQuestion:
37
    """Question with a only an id and title attributes."""
38
39
    def __init__(self, id=1, title="Question title"):
40
        self.id = id
41
        self.title = title
12906.1.2 by Curtis Hovey
Always send notfications about added questions as though they come from the owner.
42
        self.owner = FakeUser()
3859.2.1 by Francis J. Lacoste
Replace old [Support #dd] prefix when sending out notification.
43
5821.16.2 by Francis J. Lacoste
Provide a fake ZStorm to please @block_implicit_flushes.
44
3859.2.1 by Francis J. Lacoste
Replace old [Support #dd] prefix when sending out notification.
45
class StubQuestionMessage:
46
    """Question message with only a subject attribute."""
47
48
    def __init__(self, subject='Message subject'):
49
        self.subject = subject
50
51
7876.3.12 by Francis J. Lacoste
Event.user is a principal, so we need an IPerson case.
52
class FakeUser:
53
    """A fake user."""
54
    implements(IPerson)
55
11149.3.1 by Jeroen Vermeulen
Test cruft in Answers.
56
7876.3.12 by Francis J. Lacoste
Event.user is a principal, so we need an IPerson case.
57
class FakeEvent:
58
    """A fake event."""
59
    user = FakeUser()
60
61
3859.2.1 by Francis J. Lacoste
Replace old [Support #dd] prefix when sending out notification.
62
class QuestionModifiedDefaultNotificationTestCase(TestCase):
63
    """Test cases for mail notifications about modified questions."""
64
65
    def setUp(self):
5821.16.4 by Francis J. Lacoste
block_implicit_flushes hurts QuestionNotification.
66
        """Create a notification with a fake question."""
3859.2.1 by Francis J. Lacoste
Replace old [Support #dd] prefix when sending out notification.
67
        self.notification = TestQuestionModifiedNotification(
7876.3.12 by Francis J. Lacoste
Event.user is a principal, so we need an IPerson case.
68
            StubQuestion(), FakeEvent())
3859.2.1 by Francis J. Lacoste
Replace old [Support #dd] prefix when sending out notification.
69
12952.5.2 by Curtis Hovey
Added recipient_set to QuestionModifiedDefaultNotification.
70
    def test_recipient_set(self):
71
        self.assertEqual(
72
            QuestionRecipientSet.SUBSCRIBER,
73
            self.notification.recipient_set)
74
12906.2.2 by Curtis Hovey
Ensure the separator check checks for a a new line.
75
    def test_buildBody_with_separator(self):
76
        # A body with a separator is preserved.
77
        formatted_body = self.notification.buildBody(
78
            "body\n-- ", "rationale")
79
        self.assertEqual(
80
            "body\n-- \nrationale", formatted_body)
81
82
    def test_buildBody_without_separator(self):
83
        # A separator will added to body if one is not present.
84
        formatted_body = self.notification.buildBody(
85
            "body -- mdash", "rationale")
86
        self.assertEqual(
87
            "body -- mdash\n-- \nrationale", formatted_body)
88
12906.2.5 by Curtis Hovey
Removed unneeded tests for the simpler method.
89
    def test_getSubject(self):
11149.3.1 by Jeroen Vermeulen
Test cruft in Answers.
90
        """getSubject() when there is no message added to the question."""
3859.2.1 by Francis J. Lacoste
Replace old [Support #dd] prefix when sending out notification.
91
        self.assertEquals(
12906.2.5 by Curtis Hovey
Removed unneeded tests for the simpler method.
92
            'Re: [Question #1]: Question title',
3859.2.1 by Francis J. Lacoste
Replace old [Support #dd] prefix when sending out notification.
93
            self.notification.getSubject())
12906.1.2 by Curtis Hovey
Always send notfications about added questions as though they come from the owner.
94
95
    def test_user_is_event_user(self):
96
        """The notification user is always the event user."""
97
        question = StubQuestion()
98
        event = FakeEvent()
99
        notification = TestQuestionModifiedNotification(question, event)
100
        self.assertEqual(event.user, notification.user)
101
        self.assertNotEqual(question.owner, notification.user)
102
103
104
class TestQuestionAddedNotification(QuestionAddedNotification):
105
    """A subclass that does not send emails."""
106
107
    def shouldNotify(self):
108
        return False
109
110
12952.5.1 by Curtis Hovey
Added recipient_set property to QuestionAddedNotification.
111
class QuestionAddedNotificationTestCase(TestCase):
12906.1.2 by Curtis Hovey
Always send notfications about added questions as though they come from the owner.
112
    """Test cases for mail notifications about created questions."""
113
12952.5.2 by Curtis Hovey
Added recipient_set to QuestionModifiedDefaultNotification.
114
    def setUp(self):
115
        """Create a notification with a fake question."""
116
        self.question = StubQuestion()
117
        self.event = FakeEvent()
118
        self.notification = TestQuestionAddedNotification(
119
            self.question, self.event)
120
12952.5.1 by Curtis Hovey
Added recipient_set property to QuestionAddedNotification.
121
    def test_recipient_set(self):
122
        self.assertEqual(
123
            QuestionRecipientSet.ASKER_SUBSCRIBER,
12952.5.2 by Curtis Hovey
Added recipient_set to QuestionModifiedDefaultNotification.
124
            self.notification.recipient_set)
12952.5.1 by Curtis Hovey
Added recipient_set property to QuestionAddedNotification.
125
12906.1.2 by Curtis Hovey
Always send notfications about added questions as though they come from the owner.
126
    def test_user_is_question_owner(self):
127
        """The notification user is always the question owner."""
12952.5.2 by Curtis Hovey
Added recipient_set to QuestionModifiedDefaultNotification.
128
        self.assertEqual(self.question.owner, self.notification.user)
129
        self.assertNotEqual(self.event.user, self.notification.user)