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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
# Copyright 2011 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
"""Tests for QuestionJobs classes."""
__metaclass__ = type
from testtools.content import Content
from testtools.content_type import UTF8_TEXT
import transaction
from zope.component import getUtility
from zope.security.proxy import removeSecurityProxy
from lp.services.database.lpstorm import IStore
from lp.services.scripts import log
from lp.testing.layers import DatabaseFunctionalLayer
from lp.answers.enums import (
QuestionJobType,
QuestionRecipientSet,
)
from lp.answers.interfaces.questioncollection import IQuestionSet
from lp.answers.interfaces.questionjob import (
IQuestionEmailJob,
IQuestionEmailJobSource,
IQuestionJob,
)
from lp.answers.model.questionjob import (
QuestionEmailJob,
QuestionJob,
)
from lp.services.job.interfaces.job import JobStatus
from lp.services.log.logger import BufferLogger
from lp.services.mail import stub
from lp.services.mail.sendmail import (
format_address,
format_address_for_person,
)
from lp.services.worlddata.interfaces.language import ILanguageSet
from lp.testing import (
person_logged_in,
run_script,
TestCaseWithFactory,
)
class QuestionJobTestCase(TestCaseWithFactory):
"""Test case for base QuestionJob class."""
layer = DatabaseFunctionalLayer
def test_instantiate(self):
question = self.factory.makeQuestion()
metadata = ('some', 'arbitrary', 'metadata')
job = QuestionJob(
question, QuestionJobType.EMAIL, metadata)
self.assertEqual(QuestionJobType.EMAIL, job.job_type)
self.assertEqual(question, job.question)
# Metadata is unserialized from JSON.
metadata_expected = list(metadata)
self.assertEqual(metadata_expected, job.metadata)
self.assertProvides(job, IQuestionJob)
def test_repr(self):
question = self.factory.makeQuestion()
metadata = []
question_job = QuestionJob(
question, QuestionJobType.EMAIL, metadata)
self.assertEqual(
'<QuestionJob for question %s; status=Waiting>' % question.id,
repr(question_job))
class QuestionEmailJobTestCase(TestCaseWithFactory):
"""Test case for QuestionEmailJob class."""
layer = DatabaseFunctionalLayer
def makeUserSubjectBodyHeaders(self):
user = self.factory.makePerson()
subject = 'email subject'
body = 'email body'
headers = {'X-Launchpad-Question': 'question metadata'}
return user, subject, body, headers
def addAnswerContact(self, question):
contact = self.factory.makePerson()
with person_logged_in(contact):
lang_set = getUtility(ILanguageSet)
contact.addLanguage(lang_set['en'])
question.target.addAnswerContact(contact, contact)
return contact
def test_create(self):
# The create class method converts the extra job arguments
# to metadata.
question = self.factory.makeQuestion()
user, subject, body, headers = self.makeUserSubjectBodyHeaders()
job = QuestionEmailJob.create(
question, user, QuestionRecipientSet.SUBSCRIBER,
subject, body, headers)
self.assertEqual(QuestionJobType.EMAIL, job.job_type)
self.assertEqual(question, job.question)
self.assertContentEqual(
['body', 'headers', 'recipient_set', 'subject', 'user'],
job.metadata.keys())
self.assertEqual(user.id, job.metadata['user'])
self.assertEqual(
QuestionRecipientSet.SUBSCRIBER.name,
job.metadata['recipient_set'])
self.assertEqual(subject, job.metadata['subject'])
self.assertEqual(body, job.metadata['body'])
self.assertEqual(
headers['X-Launchpad-Question'],
job.metadata['headers']['X-Launchpad-Question'])
self.assertProvides(job, IQuestionEmailJob)
def test_iterReady(self):
# Jobs in the ready state are returned by the iterator.
# Creating a question implicitly created an question email job.
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)
user, subject, ignore, headers = self.makeUserSubjectBodyHeaders()
job_1 = QuestionEmailJob.create(
question, user, QuestionRecipientSet.SUBSCRIBER,
subject, 'one', headers)
job_2 = QuestionEmailJob.create(
question, user, QuestionRecipientSet.SUBSCRIBER,
subject, 'two', headers)
job_3 = QuestionEmailJob.create(
question, user, QuestionRecipientSet.SUBSCRIBER,
subject, 'three', headers)
job_1.start()
job_1.complete()
job_ids = [job.id for job in QuestionEmailJob.iterReady()]
self.assertContentEqual([job_2.id, job_3.id], job_ids)
def test_user(self):
# The user property matches the user passed to create().
question = self.factory.makeQuestion()
user, subject, body, headers = self.makeUserSubjectBodyHeaders()
job = QuestionEmailJob.create(
question, user, QuestionRecipientSet.SUBSCRIBER,
subject, body, headers)
self.assertEqual(user, job.user)
def test_subject(self):
# The subject property matches the subject passed to create().
question = self.factory.makeQuestion()
user, subject, body, headers = self.makeUserSubjectBodyHeaders()
job = QuestionEmailJob.create(
question, user, QuestionRecipientSet.SUBSCRIBER,
subject, body, headers)
self.assertEqual(body, job.body)
def test_body(self):
# The body property matches the body passed to create().
question = self.factory.makeQuestion()
user, subject, body, headers = self.makeUserSubjectBodyHeaders()
job = QuestionEmailJob.create(
question, user, QuestionRecipientSet.SUBSCRIBER,
subject, body, headers)
self.assertEqual(body, job.body)
def test_headers(self):
# The headers property matches the headers passed to create().
question = self.factory.makeQuestion()
user, subject, body, headers = self.makeUserSubjectBodyHeaders()
job = QuestionEmailJob.create(
question, user, QuestionRecipientSet.SUBSCRIBER,
subject, body, headers)
self.assertEqual(headers, job.headers)
def test_from_address(self):
# The from_address is the question with the user displayname.
question = self.factory.makeQuestion()
user, subject, body, headers = self.makeUserSubjectBodyHeaders()
job = QuestionEmailJob.create(
question, user, QuestionRecipientSet.SUBSCRIBER,
subject, body, headers)
address = format_address(
user.displayname,
"question%s@answers.launchpad.net" % question.id)
self.assertEqual(address, job.from_address)
def test_log_name(self):
# The log_name property matches the class name.
question = self.factory.makeQuestion()
user, subject, body, headers = self.makeUserSubjectBodyHeaders()
job = QuestionEmailJob.create(
question, user, QuestionRecipientSet.SUBSCRIBER,
subject, body, headers)
self.assertEqual(job.__class__.__name__, job.log_name)
def test_getOopsVars(self):
# The getOopsVars() method adds the question and user to the vars.
question = self.factory.makeQuestion()
user, subject, body, headers = self.makeUserSubjectBodyHeaders()
job = QuestionEmailJob.create(
question, user, QuestionRecipientSet.SUBSCRIBER,
subject, body, headers)
oops_vars = job.getOopsVars()
self.assertTrue(('question', question.id) in oops_vars)
self.assertTrue(('user', user.name) in oops_vars)
def test_getErrorRecipients(self):
# The getErrorRecipients method matches the user.
question = self.factory.makeQuestion()
user, subject, body, headers = self.makeUserSubjectBodyHeaders()
job = QuestionEmailJob.create(
question, user, QuestionRecipientSet.SUBSCRIBER,
subject, body, headers)
self.assertEqual(
[format_address_for_person(job.user)], job.getErrorRecipients())
def test_recipients_asker(self):
# The recipients property contains the question owner.
question = self.factory.makeQuestion()
self.addAnswerContact(question)
user, subject, body, headers = self.makeUserSubjectBodyHeaders()
job = QuestionEmailJob.create(
question, user, QuestionRecipientSet.ASKER,
subject, body, headers)
recipients = [
person for email, person in job.recipients.getRecipientPersons()]
self.assertEqual(1, len(recipients))
self.assertEqual(question.owner, recipients[0])
def test_recipients_subscriber(self):
# The recipients property matches the question recipients,
# excluding the question owner.
question = self.factory.makeQuestion()
self.addAnswerContact(question)
user, subject, body, headers = self.makeUserSubjectBodyHeaders()
job = QuestionEmailJob.create(
question, user, QuestionRecipientSet.SUBSCRIBER,
subject, body, headers)
recipients = [
person for email, person in job.recipients.getRecipientPersons()]
self.assertFalse(question.owner in recipients)
question_recipients = [
person
for em, person in question.getRecipients().getRecipientPersons()
if person != question.owner]
self.assertContentEqual(
question_recipients, recipients)
def test_recipients_asker_subscriber(self):
# The recipients property matches the question recipients.
question = self.factory.makeQuestion()
self.addAnswerContact(question)
user, subject, body, headers = self.makeUserSubjectBodyHeaders()
job = QuestionEmailJob.create(
question, user, QuestionRecipientSet.ASKER_SUBSCRIBER,
subject, body, headers)
self.assertContentEqual(
question.getRecipients().getRecipientPersons(),
job.recipients.getRecipientPersons())
def test_recipients_contact(self):
# The recipients property matches the question target answer contacts.
question = self.factory.makeQuestion()
self.addAnswerContact(question)
user, subject, body, headers = self.makeUserSubjectBodyHeaders()
job = QuestionEmailJob.create(
question, user, QuestionRecipientSet.CONTACT,
subject, body, headers)
recipients = [
person for email, person in job.recipients.getRecipientPersons()]
self.assertContentEqual(
question.target.getAnswerContactRecipients(None),
recipients)
def test_buildBody_with_separator(self):
# A body with a separator is preserved.
question = self.factory.makeQuestion()
user, subject, body, headers = self.makeUserSubjectBodyHeaders()
body = 'body\n-- '
job = QuestionEmailJob.create(
question, user, QuestionRecipientSet.SUBSCRIBER,
subject, body, headers)
formatted_body = job.buildBody('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.
question = self.factory.makeQuestion()
user, subject, body, headers = self.makeUserSubjectBodyHeaders()
body = 'body -- mdash'
job = QuestionEmailJob.create(
question, user, QuestionRecipientSet.SUBSCRIBER,
subject, body, headers)
formatted_body = job.buildBody('rationale')
self.assertEqual(
'body -- mdash\n-- \nrationale', formatted_body)
def test_buildBody_wrapping(self):
# The rationale is wrapped and added to the body.
question = self.factory.makeQuestion()
user, subject, body, headers = self.makeUserSubjectBodyHeaders()
body = 'body\n-- '
job = QuestionEmailJob.create(
question, user, QuestionRecipientSet.SUBSCRIBER,
subject, body, headers)
rationale_1 = (
'You received this email because you are indirectly subscribed '
'to this')
rationale_2 = 'question via the ~illuminati team.'
rationale = '%s %s' % (rationale_1, rationale_2)
formatted_body = job.buildBody(rationale)
expected_rationale = '%s\n%s' % (rationale_1, rationale_2)
self.assertEqual(
body + '\n' + expected_rationale, formatted_body)
def test_run(self):
# The email is sent to all the recipients.
question = self.factory.makeQuestion()
self.addAnswerContact(question)
user, subject, body, headers = self.makeUserSubjectBodyHeaders()
job = QuestionEmailJob.create(
question, user, QuestionRecipientSet.ASKER_SUBSCRIBER,
subject, body, headers)
logger = BufferLogger()
with log.use(logger):
job.run()
self.assertEqual(
["DEBUG QuestionEmailJob will send email for question %s." %
question.id,
"DEBUG QuestionEmailJob has sent email for question %s." %
question.id],
logger.getLogBuffer().splitlines())
transaction.commit()
self.assertEqual(2, len(stub.test_emails))
def test_run_cronscript(self):
# The cronscript is configured: schema-lazr.conf and security.cfg.
question = self.factory.makeQuestion()
with person_logged_in(question.target.owner):
question.linkBug(self.factory.makeBug(product=question.target))
question.linkFAQ(
question.target.owner,
self.factory.makeFAQ(target=question.target),
'test FAQ link')
self.addAnswerContact(question)
user, subject, body, headers = self.makeUserSubjectBodyHeaders()
with person_logged_in(user):
lang_set = getUtility(ILanguageSet)
user.addLanguage(lang_set['en'])
question.target.addAnswerContact(user, user)
job = QuestionEmailJob.create(
question, user, QuestionRecipientSet.ASKER_SUBSCRIBER,
subject, body, headers)
transaction.commit()
out, err, exit_code = run_script(
"LP_DEBUG_SQL=1 cronscripts/process-job-source.py -vv %s" % (
IQuestionEmailJobSource.getName()))
self.addDetail("stdout", Content(UTF8_TEXT, lambda: out))
self.addDetail("stderr", Content(UTF8_TEXT, lambda: err))
self.assertEqual(0, exit_code)
self.assertTrue(
'Traceback (most recent call last)' not in err)
message = (
'QuestionEmailJob has sent email for question %s.' % question.id)
self.assertTrue(
message in err,
'Cound not find "%s" in err log:\n%s.' % (message, err))
IStore(job.job).invalidate()
self.assertEqual(JobStatus.COMPLETED, job.job.status)
|