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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
# 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
__all__ = [
'pop_questionemailjobs',
]
from unittest import TestCase
from zope.component import getUtility
from zope.interface import implements
from zope.security.proxy import removeSecurityProxy
from lp.testing.layers import DatabaseFunctionalLayer
from lp.answers.enums import QuestionRecipientSet
from lp.answers.interfaces.questioncollection import IQuestionSet
from lp.answers.model.questionjob import QuestionEmailJob
from lp.answers.notification import (
QuestionAddedNotification,
QuestionModifiedDefaultNotification,
QuestionModifiedOwnerNotification,
QuestionNotification,
QuestionUnsupportedLanguageNotification,
)
from lp.registry.interfaces.person import IPerson
from lp.services.worlddata.interfaces.language import ILanguageSet
from lp.testing import TestCaseWithFactory
def pop_questionemailjobs():
jobs = sorted(
QuestionEmailJob.iterReady(),
key=lambda job: job.metadata["recipient_set"])
for job in jobs:
job.start()
job.complete()
return jobs
class FakeQuestionModifiedNotification(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()
self.messages = []
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()
object_before_modification = StubQuestion()
class QuestionModifiedDefaultNotificationTestCase(TestCase):
"""Test cases for mail notifications about modified questions."""
def setUp(self):
"""Create a notification with a fake question."""
self.notification = FakeQuestionModifiedNotification(
StubQuestion(), FakeEvent())
def test_recipient_set(self):
self.assertEqual(
QuestionRecipientSet.SUBSCRIBER,
self.notification.recipient_set)
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 = FakeQuestionModifiedNotification(question, event)
self.assertEqual(event.user, notification.user)
self.assertNotEqual(question.owner, notification.user)
class FakeQuestionModifiedOwnerNotification(
QuestionModifiedOwnerNotification):
"""A subclass that does not send emails."""
def shouldNotify(self):
return False
class QuestionModifiedOwnerNotificationTestCase(TestCase):
"""Test cases for mail notifications about owner modified questions."""
def setUp(self):
self.question = StubQuestion()
self.event = FakeEvent()
self.notification = FakeQuestionModifiedOwnerNotification(
self.question, self.event)
def test_recipient_set(self):
self.assertEqual(
QuestionRecipientSet.ASKER,
self.notification.recipient_set)
class FakeQuestionAddedNotification(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):
self.question = StubQuestion()
self.event = FakeEvent()
self.notification = FakeQuestionAddedNotification(
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)
class FakeQuestionUnsupportedLanguageNotification(
QuestionUnsupportedLanguageNotification):
"""A subclass that does not send emails."""
def shouldNotify(self):
return False
class QuestionUnsupportedLanguageNotificationTestCase(TestCase):
"""Test notifications about questions with unsupported languages."""
def setUp(self):
self.question = StubQuestion()
self.event = FakeEvent()
self.notification = FakeQuestionUnsupportedLanguageNotification(
self.question, self.event)
def test_recipient_set(self):
self.assertEqual(
QuestionRecipientSet.CONTACT,
self.notification.recipient_set)
class FakeQuestionNotification(QuestionNotification):
"""A subclass to exercise question notifcations."""
recipient_set = QuestionRecipientSet.ASKER_SUBSCRIBER
def getBody(self):
return 'body'
class QuestionNotificationTestCase(TestCaseWithFactory):
"""Test common question notification behavior."""
layer = DatabaseFunctionalLayer
def makeQuestion(self):
"""Create question that does not trigger a notification."""
asker = self.factory.makePerson()
product = self.factory.makeProduct()
naked_question_set = removeSecurityProxy(getUtility(IQuestionSet))
question = naked_question_set.new(
title='title', description='description', owner=asker,
language=getUtility(ILanguageSet)['en'],
product=product, distribution=None, sourcepackagename=None)
return question
def test_init_enqueue(self):
# Creating a question notification creates a queation email job.
question = self.makeQuestion()
event = FakeEvent()
event.user = self.factory.makePerson()
notification = FakeQuestionNotification(question, event)
self.assertEqual(
notification.recipient_set.name,
notification.job.metadata['recipient_set'])
self.assertEqual(notification.question, notification.job.question)
self.assertEqual(notification.user, notification.job.user)
self.assertEqual(notification.getSubject(), notification.job.subject)
self.assertEqual(notification.getBody(), notification.job.body)
self.assertEqual(notification.getHeaders(), notification.job.headers)
|