~launchpad-pqm/launchpad/devel

« back to all changes in this revision

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

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-09-16 02:11:33 UTC
  • mfrom: (13939.3.20 private-bug-5)
  • Revision ID: launchpad@pqm.canonical.com-20110916021133-ck2uixqh1wa93n92
[r=jcsackett][bug=797697] Use BugSet.createBug() a create bugs via
 email to ensure private and security rules are enforced.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright 2009-2011 Canonical Ltd.  This software is licensed under the
2
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3
3
 
4
 
from canonical.testing.layers import DatabaseFunctionalLayer
 
4
from lazr.lifecycle.interfaces import (
 
5
    IObjectCreatedEvent,
 
6
    IObjectModifiedEvent,
 
7
    )
 
8
 
 
9
from canonical.testing.layers import (
 
10
    DatabaseFunctionalLayer,
 
11
    LaunchpadFunctionalLayer,
 
12
    )
 
13
from lp.bugs.interfaces.bug import CreateBugParams
5
14
from lp.bugs.mail.commands import (
6
15
    AffectsEmailCommand,
7
 
    )
8
 
from lp.services.mail.interfaces import BugTargetNotFound
 
16
    BugEmailCommand,
 
17
    CVEEmailCommand,
 
18
    DuplicateEmailCommand,
 
19
    PrivateEmailCommand,
 
20
    SecurityEmailCommand,
 
21
    SubscribeEmailCommand,
 
22
    SummaryEmailCommand,
 
23
    TagEmailCommand,
 
24
    UnsubscribeEmailCommand,
 
25
    )
 
26
from lp.services.mail.interfaces import (
 
27
    BugTargetNotFound,
 
28
    EmailProcessingError,
 
29
    )
9
30
from lp.testing import (
10
31
    login_celebrity,
11
32
    login_person,
143
164
        self.assertRaisesWithContent(
144
165
            BugTargetNotFound, message,
145
166
            AffectsEmailCommand.getBugTarget, 'fnord/pting/snarf/thrup')
 
167
 
 
168
    def test_execute_bug(self):
 
169
        bug = self.factory.makeBug()
 
170
        product = self.factory.makeProduct(name='fnord')
 
171
        login_person(bug.bugtasks[0].target.owner)
 
172
        command = AffectsEmailCommand('affects', ['fnord'])
 
173
        bugtask, bugtask_event, bug_event = command.execute(bug, None)
 
174
        self.assertEqual(bug, bugtask.bug)
 
175
        self.assertEqual(product, bugtask.target)
 
176
        self.assertTrue(IObjectCreatedEvent.providedBy(bugtask_event))
 
177
        self.assertEqual(None, bug_event)
 
178
 
 
179
    def test_execute_bug_params_product(self):
 
180
        user = self.factory.makePerson()
 
181
        login_person(user)
 
182
        product = self.factory.makeProduct(name='fnord')
 
183
        message = self.factory.makeMessage(
 
184
            subject='bug title', content='borked\n affects fnord')
 
185
        command = AffectsEmailCommand('affects', ['fnord'])
 
186
        bug_params = CreateBugParams(
 
187
            title='bug title', msg=message, owner=user)
 
188
        bugtask, bugtask_event, bug_event = command.execute(bug_params, None)
 
189
        self.assertEqual(product, bugtask.target)
 
190
        self.assertEqual('bug title', bugtask.bug.title)
 
191
        self.assertEqual('borked\n affects fnord', bugtask.bug.description)
 
192
        self.assertEqual(user, bugtask.bug.owner)
 
193
        self.assertTrue(IObjectCreatedEvent.providedBy(bugtask_event))
 
194
        self.assertTrue(IObjectCreatedEvent.providedBy(bug_event))
 
195
 
 
196
    def test_execute_bug_params_productseries(self):
 
197
        product = self.factory.makeProduct(name='fnord')
 
198
        login_person(product.owner)
 
199
        series = self.factory.makeProductSeries(name='pting', product=product)
 
200
        message = self.factory.makeMessage(
 
201
            subject='bug title', content='borked\n affects fnord/pting')
 
202
        command = AffectsEmailCommand('affects', ['fnord/pting'])
 
203
        bug_params = CreateBugParams(
 
204
            title='bug title', msg=message, owner=product.owner)
 
205
        bugtask, bugtask_event, bug_event = command.execute(bug_params, None)
 
206
        self.assertEqual(series, bugtask.target)
 
207
        self.assertEqual('bug title', bugtask.bug.title)
 
208
        self.assertEqual(2, len(bugtask.bug.bugtasks))
 
209
        self.assertTrue(IObjectCreatedEvent.providedBy(bugtask_event))
 
210
        self.assertTrue(IObjectCreatedEvent.providedBy(bug_event))
 
211
 
 
212
    def test_execute_bug_params_distribution(self):
 
213
        user = self.factory.makePerson()
 
214
        login_person(user)
 
215
        distribution = self.factory.makeDistribution(name='fnord')
 
216
        message = self.factory.makeMessage(
 
217
            subject='bug title', content='borked\n affects fnord')
 
218
        command = AffectsEmailCommand('affects', ['fnord'])
 
219
        bug_params = CreateBugParams(
 
220
            title='bug title', msg=message, owner=user)
 
221
        bugtask, bugtask_event, bug_event = command.execute(bug_params, None)
 
222
        self.assertEqual(distribution, bugtask.target)
 
223
        self.assertEqual('bug title', bugtask.bug.title)
 
224
        self.assertTrue(IObjectCreatedEvent.providedBy(bugtask_event))
 
225
        self.assertTrue(IObjectCreatedEvent.providedBy(bug_event))
 
226
 
 
227
    def test_execute_bug_params_dsp(self):
 
228
        user = self.factory.makePerson()
 
229
        login_person(user)
 
230
        distribution = self.factory.makeDistribution(name='fnord')
 
231
        series = self.factory.makeDistroSeries(
 
232
            name='pting', distribution=distribution)
 
233
        package = self.factory.makeSourcePackage(
 
234
            sourcepackagename='snarf', distroseries=series, publish=True)
 
235
        dsp = distribution.getSourcePackage(package.name)
 
236
        message = self.factory.makeMessage(
 
237
            subject='bug title', content='borked\n affects fnord/snarf')
 
238
        command = AffectsEmailCommand('affects', ['fnord/snarf'])
 
239
        bug_params = CreateBugParams(
 
240
            title='bug title', msg=message, owner=user)
 
241
        bugtask, bugtask_event, bug_event = command.execute(bug_params, None)
 
242
        self.assertEqual(dsp, bugtask.target)
 
243
        self.assertEqual('bug title', bugtask.bug.title)
 
244
        self.assertTrue(IObjectCreatedEvent.providedBy(bugtask_event))
 
245
        self.assertTrue(IObjectCreatedEvent.providedBy(bug_event))
 
246
 
 
247
    def test_execute_bug_params_distroseries(self):
 
248
        distribution = self.factory.makeDistribution(name='fnord')
 
249
        login_person(distribution.owner)
 
250
        series = self.factory.makeDistroSeries(
 
251
            name='pting', distribution=distribution)
 
252
        message = self.factory.makeMessage(
 
253
            subject='bug title', content='borked\n affects fnord/pting')
 
254
        command = AffectsEmailCommand('affects', ['fnord/pting'])
 
255
        bug_params = CreateBugParams(
 
256
            title='bug title', msg=message, owner=distribution.owner)
 
257
        bugtask, bugtask_event, bug_event = command.execute(bug_params, None)
 
258
        self.assertEqual(series, bugtask.target)
 
259
        self.assertEqual('bug title', bugtask.bug.title)
 
260
        self.assertEqual(2, len(bugtask.bug.bugtasks))
 
261
        self.assertTrue(IObjectCreatedEvent.providedBy(bugtask_event))
 
262
        self.assertTrue(IObjectCreatedEvent.providedBy(bug_event))
 
263
 
 
264
    def test_execute_bug_params_distroseries_sourcepackage(self):
 
265
        distribution = self.factory.makeDistribution(name='fnord')
 
266
        login_person(distribution.owner)
 
267
        series = self.factory.makeDistroSeries(
 
268
            name='pting', distribution=distribution)
 
269
        package = self.factory.makeSourcePackage(
 
270
            sourcepackagename='snarf', distroseries=series, publish=True)
 
271
        message = self.factory.makeMessage(
 
272
            subject='bug title', content='borked\n affects fnord/pting/snarf')
 
273
        command = AffectsEmailCommand('affects', ['fnord/pting/snarf'])
 
274
        bug_params = CreateBugParams(
 
275
            title='bug title', msg=message, owner=distribution.owner)
 
276
        bugtask, bugtask_event, bug_event = command.execute(bug_params, None)
 
277
        self.assertEqual(package, bugtask.target)
 
278
        self.assertEqual('bug title', bugtask.bug.title)
 
279
        self.assertEqual(2, len(bugtask.bug.bugtasks))
 
280
        self.assertTrue(IObjectCreatedEvent.providedBy(bugtask_event))
 
281
        self.assertTrue(IObjectCreatedEvent.providedBy(bug_event))
 
282
 
 
283
 
 
284
class BugEmailCommandTestCase(TestCaseWithFactory):
 
285
 
 
286
    layer = LaunchpadFunctionalLayer
 
287
 
 
288
    def test_execute_bug_id(self):
 
289
        bug = self.factory.makeBug()
 
290
        command = BugEmailCommand('bug', [str(bug.id)])
 
291
        self.assertEqual((bug, None), command.execute(None, None))
 
292
 
 
293
    def test_execute_bug_id_wrong_type(self):
 
294
        command = BugEmailCommand('bug', ['nickname'])
 
295
        error = self.assertRaises(
 
296
            EmailProcessingError, command.execute, None, None)
 
297
        message = str(error).split('\n')
 
298
        self.assertEqual(
 
299
            "The 'bug' command expects either 'new' or a bug id.", message[0])
 
300
 
 
301
    def test_execute_bug_id_not_found(self):
 
302
        command = BugEmailCommand('bug', ['9999999'])
 
303
        error = self.assertRaises(
 
304
            EmailProcessingError, command.execute, None, None)
 
305
        message = str(error).split('\n')
 
306
        self.assertEqual(
 
307
            "There is no such bug in Launchpad: 9999999", message[0])
 
308
 
 
309
    def test_execute_bug_id_new(self):
 
310
        user = self.factory.makePerson()
 
311
        login_person(user)
 
312
        message = self.factory.makeSignedMessage(
 
313
            body='borked\n affects fnord',
 
314
            subject='title borked',
 
315
            to_address='new@bugs.launchpad.dev')
 
316
        filealias = self.factory.makeLibraryFileAlias()
 
317
        command = BugEmailCommand('bug', ['new'])
 
318
        params, event = command.execute(message, filealias)
 
319
        self.assertEqual(None, event)
 
320
        self.assertEqual(user, params.owner)
 
321
        self.assertEqual('title borked', params.title)
 
322
        self.assertEqual(message['Message-Id'], params.msg.rfc822msgid)
 
323
 
 
324
 
 
325
class PrivateEmailCommandTestCase(TestCaseWithFactory):
 
326
 
 
327
    layer = DatabaseFunctionalLayer
 
328
 
 
329
    def test_execute_bug(self):
 
330
        bug = self.factory.makeBug()
 
331
        login_person(bug.bugtasks[0].target.owner)
 
332
        command = PrivateEmailCommand('private', ['yes'])
 
333
        exec_bug, event = command.execute(bug, None)
 
334
        self.assertEqual(bug, exec_bug)
 
335
        self.assertEqual(True, bug.private)
 
336
        self.assertTrue(IObjectModifiedEvent.providedBy(event))
 
337
 
 
338
    def test_execute_bug_params(self):
 
339
        user = self.factory.makePerson()
 
340
        login_person(user)
 
341
        bug_params = CreateBugParams(title='bug title', owner=user)
 
342
        command = PrivateEmailCommand('private', ['yes'])
 
343
        dummy_event = object()
 
344
        params, event = command.execute(bug_params, dummy_event)
 
345
        self.assertEqual(bug_params, params)
 
346
        self.assertEqual(True, bug_params.private)
 
347
        self.assertEqual(dummy_event, event)
 
348
 
 
349
    def test_execute_bug_params_with_security(self):
 
350
        # BugSet.createBug() requires new security bugs to be private.
 
351
        user = self.factory.makePerson()
 
352
        login_person(user)
 
353
        bug_params = CreateBugParams(
 
354
            title='bug title', owner=user, security_related='yes')
 
355
        command = PrivateEmailCommand('private', ['no'])
 
356
        dummy_event = object()
 
357
        params, event = command.execute(bug_params, dummy_event)
 
358
        self.assertEqual(bug_params, params)
 
359
        self.assertEqual(True, bug_params.private)
 
360
        self.assertEqual(dummy_event, event)
 
361
 
 
362
 
 
363
class SecurityEmailCommandTestCase(TestCaseWithFactory):
 
364
 
 
365
    layer = DatabaseFunctionalLayer
 
366
 
 
367
    def test_execute_bug(self):
 
368
        bug = self.factory.makeBug()
 
369
        login_person(bug.bugtasks[0].target.owner)
 
370
        command = SecurityEmailCommand('security', ['yes'])
 
371
        exec_bug, event = command.execute(bug, None)
 
372
        self.assertEqual(bug, exec_bug)
 
373
        self.assertEqual(True, bug.security_related)
 
374
        self.assertTrue(IObjectModifiedEvent.providedBy(event))
 
375
 
 
376
    def test_execute_bug_params(self):
 
377
        user = self.factory.makePerson()
 
378
        login_person(user)
 
379
        bug_params = CreateBugParams(title='bug title', owner=user)
 
380
        command = SecurityEmailCommand('security', ['yes'])
 
381
        dummy_event = object()
 
382
        params, event = command.execute(bug_params, dummy_event)
 
383
        self.assertEqual(bug_params, params)
 
384
        self.assertEqual(True, bug_params.security_related)
 
385
        self.assertEqual(True, bug_params.private)
 
386
        self.assertEqual(dummy_event, event)
 
387
 
 
388
 
 
389
class SubscribeEmailCommandTestCase(TestCaseWithFactory):
 
390
 
 
391
    layer = DatabaseFunctionalLayer
 
392
 
 
393
    def test_execute_bug_with_user_name(self):
 
394
        bug = self.factory.makeBug()
 
395
        login_person(bug.bugtasks[0].target.owner)
 
396
        subscriber = self.factory.makePerson()
 
397
        command = SubscribeEmailCommand('subscribe', [subscriber.name])
 
398
        dummy_event = object()
 
399
        exec_bug, event = command.execute(bug, dummy_event)
 
400
        self.assertEqual(bug, exec_bug)
 
401
        self.assertContentEqual(
 
402
            [bug.owner, subscriber], bug.getDirectSubscribers())
 
403
        self.assertEqual(dummy_event, event)
 
404
 
 
405
    def test_execute_bug_without_user_name(self):
 
406
        bug = self.factory.makeBug()
 
407
        target_owner = bug.bugtasks[0].target.owner
 
408
        login_person(target_owner)
 
409
        command = SubscribeEmailCommand('subscribe', [])
 
410
        dummy_event = object()
 
411
        exec_bug, event = command.execute(bug, dummy_event)
 
412
        self.assertEqual(bug, exec_bug)
 
413
        self.assertContentEqual(
 
414
            [bug.owner, target_owner], bug.getDirectSubscribers())
 
415
        self.assertEqual(dummy_event, event)
 
416
 
 
417
    def test_execute_bug_params_one_subscriber(self):
 
418
        user = self.factory.makePerson()
 
419
        login_person(user)
 
420
        subscriber = self.factory.makePerson()
 
421
        bug_params = CreateBugParams(title='bug title', owner=user)
 
422
        command = SubscribeEmailCommand('subscribe', [subscriber.name])
 
423
        dummy_event = object()
 
424
        params, event = command.execute(bug_params, dummy_event)
 
425
        self.assertEqual(bug_params, params)
 
426
        self.assertContentEqual([subscriber], bug_params.subscribers)
 
427
        self.assertEqual(dummy_event, event)
 
428
 
 
429
    def test_execute_bug_params_many_subscriber(self):
 
430
        user = self.factory.makePerson()
 
431
        login_person(user)
 
432
        subscriber_1 = self.factory.makePerson()
 
433
        subscriber_2 = self.factory.makePerson()
 
434
        bug_params = CreateBugParams(
 
435
            title='bug title', owner=user, subscribers=[subscriber_1])
 
436
        command = SubscribeEmailCommand('subscribe', [subscriber_2.name])
 
437
        dummy_event = object()
 
438
        params, event = command.execute(bug_params, dummy_event)
 
439
        self.assertEqual(bug_params, params)
 
440
        self.assertContentEqual(
 
441
            [subscriber_1, subscriber_2], bug_params.subscribers)
 
442
        self.assertEqual(dummy_event, event)
 
443
 
 
444
 
 
445
class UnsubscribeEmailCommandTestCase(TestCaseWithFactory):
 
446
 
 
447
    layer = DatabaseFunctionalLayer
 
448
 
 
449
    def test_execute_bug_with_user_name(self):
 
450
        bug = self.factory.makeBug()
 
451
        target_owner = bug.bugtasks[0].target.owner
 
452
        login_person(target_owner)
 
453
        bug.subscribe(target_owner, target_owner)
 
454
        command = UnsubscribeEmailCommand('unsubscribe', [target_owner.name])
 
455
        dummy_event = object()
 
456
        exec_bug, event = command.execute(bug, dummy_event)
 
457
        self.assertEqual(bug, exec_bug)
 
458
        self.assertContentEqual(
 
459
            [bug.owner], bug.getDirectSubscribers())
 
460
        self.assertEqual(dummy_event, event)
 
461
 
 
462
    def test_execute_bug_without_user_name(self):
 
463
        bug = self.factory.makeBug()
 
464
        target_owner = bug.bugtasks[0].target.owner
 
465
        login_person(target_owner)
 
466
        bug.subscribe(target_owner, target_owner)
 
467
        command = UnsubscribeEmailCommand('unsubscribe', [])
 
468
        dummy_event = object()
 
469
        exec_bug, event = command.execute(bug, dummy_event)
 
470
        self.assertEqual(bug, exec_bug)
 
471
        self.assertContentEqual(
 
472
            [bug.owner], bug.getDirectSubscribers())
 
473
        self.assertEqual(dummy_event, event)
 
474
 
 
475
    def test_execute_bug_params(self):
 
476
        # Unsubscribe does nothing because the is not yet a bug.
 
477
        # Any value can be used for the user name.
 
478
        user = self.factory.makePerson()
 
479
        login_person(user)
 
480
        bug_params = CreateBugParams(title='bug title', owner=user)
 
481
        command = UnsubscribeEmailCommand('unsubscribe', ['non-existent'])
 
482
        dummy_event = object()
 
483
        params, event = command.execute(bug_params, dummy_event)
 
484
        self.assertEqual(bug_params, params)
 
485
        self.assertEqual(dummy_event, event)
 
486
 
 
487
 
 
488
class SummaryEmailCommandTestCase(TestCaseWithFactory):
 
489
 
 
490
    layer = DatabaseFunctionalLayer
 
491
 
 
492
    def test_execute_bug(self):
 
493
        bug = self.factory.makeBug()
 
494
        login_person(bug.bugtasks[0].target.owner)
 
495
        command = SummaryEmailCommand('summary', ['new title'])
 
496
        exec_bug, event = command.execute(bug, None)
 
497
        self.assertEqual(bug, exec_bug)
 
498
        self.assertEqual('new title', bug.title)
 
499
        self.assertTrue(IObjectModifiedEvent.providedBy(event))
 
500
 
 
501
    def test_execute_bug_params(self):
 
502
        user = self.factory.makePerson()
 
503
        login_person(user)
 
504
        bug_params = CreateBugParams(title='bug title', owner=user)
 
505
        command = SummaryEmailCommand('summary', ['new title'])
 
506
        dummy_event = object()
 
507
        params, event = command.execute(bug_params, dummy_event)
 
508
        self.assertEqual(bug_params, params)
 
509
        self.assertEqual('new title', bug_params.title)
 
510
        self.assertEqual(dummy_event, event)
 
511
 
 
512
 
 
513
class DuplicateEmailCommandTestCase(TestCaseWithFactory):
 
514
 
 
515
    layer = DatabaseFunctionalLayer
 
516
 
 
517
    def test_execute_bug(self):
 
518
        master_bug = self.factory.makeBug()
 
519
        bug = self.factory.makeBug()
 
520
        login_person(bug.bugtasks[0].target.owner)
 
521
        command = DuplicateEmailCommand('duplicate', [str(master_bug.id)])
 
522
        exec_bug, event = command.execute(bug, None)
 
523
        self.assertEqual(master_bug, exec_bug)
 
524
        self.assertEqual(master_bug, bug.duplicateof)
 
525
        self.assertTrue(IObjectModifiedEvent.providedBy(event))
 
526
 
 
527
    def test_execute_bug_params(self):
 
528
        # duplicate does nothing because the is not yet a bug.
 
529
        # Any value can be used for the bug is.
 
530
        user = self.factory.makePerson()
 
531
        login_person(user)
 
532
        bug_params = CreateBugParams(title='bug title', owner=user)
 
533
        command = DuplicateEmailCommand('duplicate', ['non-existent'])
 
534
        dummy_event = object()
 
535
        params, event = command.execute(bug_params, dummy_event)
 
536
        self.assertEqual(bug_params, params)
 
537
        self.assertEqual(dummy_event, event)
 
538
 
 
539
 
 
540
class CVEEmailCommandTestCase(TestCaseWithFactory):
 
541
 
 
542
    layer = DatabaseFunctionalLayer
 
543
 
 
544
    def test_execute_bug(self):
 
545
        bug = self.factory.makeBug()
 
546
        login_person(bug.bugtasks[0].target.owner)
 
547
        cve = self.factory.makeCVE('1999-1717')
 
548
        command = CVEEmailCommand('cve', ['1999-1717'])
 
549
        dummy_event = object()
 
550
        exec_bug, event = command.execute(bug, dummy_event)
 
551
        self.assertEqual(bug, exec_bug)
 
552
        self.assertEqual([cve], [cve_link.cve for cve_link in bug.cve_links])
 
553
        self.assertEqual(dummy_event, event)
 
554
 
 
555
    def test_execute_bug_params(self):
 
556
        user = self.factory.makePerson()
 
557
        login_person(user)
 
558
        cve = self.factory.makeCVE('1999-1717')
 
559
        bug_params = CreateBugParams(title='bug title', owner=user)
 
560
        command = CVEEmailCommand('cve', ['1999-1717'])
 
561
        dummy_event = object()
 
562
        params, event = command.execute(bug_params, dummy_event)
 
563
        self.assertEqual(bug_params, params)
 
564
        self.assertEqual(cve, params.cve)
 
565
        self.assertEqual(dummy_event, event)
 
566
 
 
567
 
 
568
class TagEmailCommandTestCase(TestCaseWithFactory):
 
569
 
 
570
    layer = DatabaseFunctionalLayer
 
571
 
 
572
    def test_execute_bug(self):
 
573
        bug = self.factory.makeBug()
 
574
        login_person(bug.bugtasks[0].target.owner)
 
575
        bug.tags = ['form']
 
576
        command = TagEmailCommand('tag', ['ui', 'trivial'])
 
577
        dummy_event = object()
 
578
        exec_bug, event = command.execute(bug, dummy_event)
 
579
        self.assertEqual(bug, exec_bug)
 
580
        self.assertContentEqual(['form', 'ui', 'trivial'], bug.tags)
 
581
        self.assertEqual(dummy_event, event)
 
582
 
 
583
    def test_execute_bug_params(self):
 
584
        user = self.factory.makePerson()
 
585
        login_person(user)
 
586
        bug_params = CreateBugParams(title='bug title', owner=user)
 
587
        command = TagEmailCommand('tag', ['ui', 'trivial'])
 
588
        dummy_event = object()
 
589
        params, event = command.execute(bug_params, dummy_event)
 
590
        self.assertEqual(bug_params, params)
 
591
        self.assertContentEqual(['ui', 'trivial'], bug_params.tags)
 
592
        self.assertEqual(dummy_event, event)