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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
|
# Copyright 2009-2011 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
"""Tests for recording changes done to a bug."""
from lazr.lifecycle.event import (
ObjectCreatedEvent,
ObjectModifiedEvent,
)
from lazr.lifecycle.snapshot import Snapshot
from testtools.matchers import StartsWith
from zope.component import getUtility
from zope.event import notify
from zope.interface import providedBy
from lp.services.webapp.interfaces import ILaunchBag
from lp.services.webapp.publisher import canonical_url
from lp.testing.layers import LaunchpadFunctionalLayer
from lp.bugs.enum import BugNotificationLevel
from lp.bugs.interfaces.bugtask import (
BugTaskImportance,
BugTaskStatus,
)
from lp.bugs.interfaces.cve import ICveSet
from lp.bugs.model.bugnotification import BugNotification
from lp.bugs.scripts.bugnotification import construct_email_notifications
from lp.services.librarian.browser import ProxiedLibraryFileAlias
from lp.testing import (
celebrity_logged_in,
login_person,
person_logged_in,
TestCaseWithFactory,
)
class TestBugChanges(TestCaseWithFactory):
layer = LaunchpadFunctionalLayer
def setUp(self):
super(TestBugChanges, self).setUp('foo.bar@canonical.com')
self.admin_user = getUtility(ILaunchBag).user
self.user = self.factory.makePerson(
displayname='Arthur Dent',
selfgenerated_bugnotifications=True)
self.product = self.factory.makeProduct(
owner=self.user, official_malone=True)
self.bug = self.factory.makeBug(product=self.product, owner=self.user)
self.bug_task = self.bug.bugtasks[0]
# Add some structural subscribers to show that notifications
# aren't sent to LIFECYCLE subscribers by default.
self.product_lifecycle_subscriber = self.newSubscriber(
self.product, "product-lifecycle",
BugNotificationLevel.LIFECYCLE)
self.product_metadata_subscriber = self.newSubscriber(
self.product, "product-metadata",
BugNotificationLevel.METADATA)
self.saveOldChanges()
def newSubscriber(self, target, name, level):
# Create a new bug subscription with a new person.
subscriber = self.factory.makePerson(name=name)
subscription = target.addBugSubscription(subscriber, subscriber)
with person_logged_in(subscriber):
filter = subscription.bug_filters.one()
filter.bug_notification_level = level
return subscriber
def saveOldChanges(self, bug=None, append=False):
"""Save old activity and notifications for a test.
This method should be called after setup. Removing the
initial bug-created activity and notification messages
allows for a more accurate check of new activity and
notifications.
The append parameter can be used to save activity/notifications
for more than one bug in a single test, as when dealing
with duplicates.
"""
if bug is None:
bug = self.bug
old_activities = set(bug.activity)
old_notification_ids = set(
notification.id for notification in (
BugNotification.selectBy(bug=bug)))
if append:
self.old_activities.update(old_activities)
self.old_notification_ids.update(old_notification_ids)
else:
self.old_activities = old_activities
self.old_notification_ids = old_notification_ids
def changeAttribute(self, obj, attribute, new_value):
"""Set the value of `attribute` on `obj` to `new_value`.
:return: The value of `attribute` before modification.
"""
obj_before_modification = Snapshot(obj, providing=providedBy(obj))
if attribute == 'duplicateof':
obj.markAsDuplicate(new_value)
else:
setattr(obj, attribute, new_value)
notify(ObjectModifiedEvent(
obj, obj_before_modification, [attribute], self.user))
return getattr(obj_before_modification, attribute)
def getNewNotifications(self, bug=None):
if bug is None:
bug = self.bug
bug_notifications = BugNotification.selectBy(
bug=bug, orderBy='id')
new_notifications = [
notification for notification in bug_notifications
if notification.id not in self.old_notification_ids]
return new_notifications
def assertRecordedChange(self, expected_activity=None,
expected_notification=None, bug=None):
"""Assert that things were recorded as expected."""
if bug is None:
bug = self.bug
new_activities = [
activity for activity in bug.activity
if activity not in self.old_activities]
new_notifications = self.getNewNotifications(bug)
if expected_activity is None:
self.assertEqual(0, len(new_activities))
else:
if isinstance(expected_activity, dict):
expected_activities = [expected_activity]
else:
expected_activities = expected_activity
self.assertEqual(len(expected_activities), len(new_activities))
for expected_activity in expected_activities:
added_activity = new_activities.pop(0)
self.assertEqual(
expected_activity['person'], added_activity.person)
self.assertEqual(
expected_activity['whatchanged'],
added_activity.whatchanged)
self.assertEqual(
expected_activity.get('oldvalue'),
added_activity.oldvalue)
self.assertEqual(
expected_activity.get('newvalue'),
added_activity.newvalue)
self.assertEqual(
expected_activity.get('message'), added_activity.message)
if expected_notification is None:
self.assertEqual(0, len(new_notifications))
else:
if isinstance(expected_notification, dict):
expected_notifications = [expected_notification]
else:
expected_notifications = expected_notification
self.assertEqual(
len(expected_notifications), len(new_notifications))
for expected_notification in expected_notifications:
added_notification = new_notifications.pop(0)
self.assertEqual(
expected_notification['text'],
added_notification.message.text_contents)
self.assertEqual(
expected_notification['person'],
added_notification.message.owner)
self.assertEqual(
expected_notification.get('is_comment', False),
added_notification.is_comment)
expected_recipients = expected_notification.get('recipients')
expected_recipient_reasons = (
expected_notification.get('recipient_reasons'))
if expected_recipients is None:
expected_recipients = bug.getBugNotificationRecipients(
level=BugNotificationLevel.METADATA)
self.assertEqual(
set(expected_recipients),
set(recipient.person
for recipient in added_notification.recipients))
if expected_recipient_reasons:
self.assertEqual(
set(expected_recipient_reasons),
set(recipient.reason_header
for recipient in added_notification.recipients))
def assertRecipients(self, expected_recipients):
notifications = self.getNewNotifications()
notifications, omitted, messages = construct_email_notifications(
notifications)
recipients = set(message['to'] for message in messages)
self.assertEqual(
set(recipient.preferredemail.email
for recipient in expected_recipients),
recipients)
def test_subscribe(self):
# Subscribing someone to a bug adds an item to the activity log,
# but doesn't send an e-mail notification.
subscriber = self.factory.makePerson(displayname='Mom')
bug_subscription = self.bug.subscribe(self.user, subscriber)
notify(ObjectCreatedEvent(bug_subscription, user=subscriber))
subscribe_activity = dict(
whatchanged='bug',
message='added subscriber Arthur Dent',
person=subscriber)
self.assertRecordedChange(expected_activity=subscribe_activity)
def test_unsubscribe(self):
# Unsubscribing someone from a bug adds an item to the activity
# log, but doesn't send an e-mail notification.
subscriber = self.factory.makePerson(displayname='Mom')
self.bug.subscribe(self.user, subscriber)
self.saveOldChanges()
# Only the user can unsubscribe him or her self.
self.bug.unsubscribe(self.user, self.user)
# This checks the activity's attribute and target attributes.
activity = self.bug.activity[-1]
self.assertEqual(activity.attribute, 'removed_subscriber')
self.assertEqual(activity.target, None)
unsubscribe_activity = dict(
whatchanged='removed subscriber Arthur Dent',
person=self.user)
self.assertRecordedChange(expected_activity=unsubscribe_activity)
def test_unsubscribe_private_bug(self):
# Test that a person can unsubscribe themselves from a private bug
# that they are not assigned to.
subscriber = self.factory.makePerson(displayname='Mom')
# Create the private bug.
bug = self.factory.makeBug(
product=self.product, owner=self.user, private=True)
bug.subscribe(subscriber, self.user)
self.saveOldChanges(bug=bug)
bug.unsubscribe(subscriber, subscriber)
unsubscribe_activity = dict(
whatchanged=u'removed subscriber Mom',
person=subscriber)
self.assertRecordedChange(
expected_activity=unsubscribe_activity, bug=bug)
def test_title_changed(self):
# Changing the title of a Bug adds items to the activity log and
# the Bug's notifications.
old_title = self.changeAttribute(self.bug, 'title', '42')
# This checks the activity's attribute and target attributes.
activity = self.bug.activity[-1]
self.assertEqual(activity.attribute, 'title')
self.assertEqual(activity.target, None)
title_change_activity = {
'whatchanged': 'summary',
'oldvalue': old_title,
'newvalue': "42",
'person': self.user,
}
title_change_notification = {
'text': (
"** Summary changed:\n\n"
"- %s\n"
"+ 42" % old_title),
'person': self.user,
}
self.assertRecordedChange(
expected_activity=title_change_activity,
expected_notification=title_change_notification)
def test_description_changed(self):
# Changing the description of a Bug adds items to the activity
# log and the Bug's notifications.
old_description = self.changeAttribute(
self.bug, 'description', 'Hello, world')
description_change_activity = {
'person': self.user,
'whatchanged': 'description',
'oldvalue': old_description,
'newvalue': 'Hello, world',
}
description_change_notification = {
'text': (
"** Description changed:\n\n"
"- %s\n"
"+ Hello, world" % old_description),
'person': self.user,
}
self.assertRecordedChange(
expected_notification=description_change_notification,
expected_activity=description_change_activity)
def test_bugwatch_added(self):
# Adding a BugWatch to a bug adds items to the activity
# log and the Bug's notifications.
bugtracker = self.factory.makeBugTracker()
bug_watch = self.bug.addWatch(bugtracker, '42', self.user)
# This checks the activity's attribute and target attributes.
activity = self.bug.activity[-1]
self.assertEqual(activity.attribute, 'watches')
self.assertEqual(activity.target, None)
bugwatch_activity = {
'person': self.user,
'whatchanged': 'bug watch added',
'newvalue': bug_watch.url,
}
bugwatch_notification = {
'text': (
"** Bug watch added: %s #%s\n"
" %s" % (
bug_watch.bugtracker.title, bug_watch.remotebug,
bug_watch.url)),
'person': self.user,
}
self.assertRecordedChange(
expected_notification=bugwatch_notification,
expected_activity=bugwatch_activity)
def test_bugwatch_added_from_comment(self):
# Adding a bug comment containing a URL that looks like a link
# to a remote bug causes a BugWatch to be added to the
# bug. This adds to the activity log and sends a notification.
self.assertEqual(self.bug.watches.count(), 0)
self.bug.newMessage(
content="http://bugs.example.com/view.php?id=1234",
owner=self.user)
self.assertEqual(self.bug.watches.count(), 1)
[bug_watch] = self.bug.watches
bugwatch_activity = {
'person': self.user,
'whatchanged': 'bug watch added',
'newvalue': bug_watch.url,
}
bugwatch_notification = {
'text': (
"** Bug watch added: %s #%s\n"
" %s" % (
bug_watch.bugtracker.title, bug_watch.remotebug,
bug_watch.url)),
'person': self.user,
'recipients': [
self.user, self.product_metadata_subscriber],
}
comment_notification = {
'text': "http://bugs.example.com/view.php?id=1234",
'person': self.user,
'is_comment': True,
'recipients': [self.user],
}
self.assertRecordedChange(
expected_activity=bugwatch_activity,
expected_notification=[
bugwatch_notification, comment_notification])
def test_bugwatch_removed(self):
# Removing a BugWatch from a bug adds items to the activity
# log and the Bug's notifications.
bugtracker = self.factory.makeBugTracker()
bug_watch = self.bug.addWatch(bugtracker, '42', self.user)
self.saveOldChanges()
self.bug.removeWatch(bug_watch, self.user)
# This checks the activity's attribute and target attributes.
activity = self.bug.activity[-1]
self.assertEqual(activity.attribute, 'watches')
self.assertEqual(activity.target, None)
bugwatch_activity = {
'person': self.user,
'whatchanged': 'bug watch removed',
'oldvalue': bug_watch.url,
}
bugwatch_notification = {
'text': (
"** Bug watch removed: %s #%s\n"
" %s" % (
bug_watch.bugtracker.title, bug_watch.remotebug,
bug_watch.url)),
'person': self.user,
}
self.assertRecordedChange(
expected_notification=bugwatch_notification,
expected_activity=bugwatch_activity)
def test_bugwatch_modified(self):
# Modifying a BugWatch is like removing and re-adding it.
bugtracker = self.factory.makeBugTracker()
bug_watch = self.bug.addWatch(bugtracker, '42', self.user)
old_url = bug_watch.url
self.saveOldChanges()
old_remotebug = self.changeAttribute(bug_watch, 'remotebug', '84')
bugwatch_removal_activity = {
'person': self.user,
'whatchanged': 'bug watch removed',
'oldvalue': old_url,
}
bugwatch_addition_activity = {
'person': self.user,
'whatchanged': 'bug watch added',
'newvalue': bug_watch.url,
}
bugwatch_removal_notification = {
'text': (
"** Bug watch removed: %s #%s\n"
" %s" % (
bug_watch.bugtracker.title, old_remotebug,
old_url)),
'person': self.user,
}
bugwatch_addition_notification = {
'text': (
"** Bug watch added: %s #%s\n"
" %s" % (
bug_watch.bugtracker.title, bug_watch.remotebug,
bug_watch.url)),
'person': self.user,
}
self.assertRecordedChange(
expected_notification=[bugwatch_removal_notification,
bugwatch_addition_notification],
expected_activity=[bugwatch_removal_activity,
bugwatch_addition_activity])
def test_bugwatch_not_modified(self):
# Firing off a modified event without actually modifying
# anything intersting doesn't cause anything to be added to the
# activity log.
bug_watch = self.factory.makeBugWatch(bug=self.bug)
self.saveOldChanges()
self.changeAttribute(bug_watch, 'remotebug', bug_watch.remotebug)
self.assertRecordedChange()
def test_link_branch(self):
# Linking a branch to a bug adds both to the activity log and
# sends an e-mail notification.
branch = self.factory.makeBranch()
self.bug.linkBranch(branch, self.user)
# This checks the activity's attribute and target attributes.
activity = self.bug.activity[-1]
self.assertEqual(activity.attribute, 'linked_branches')
self.assertEqual(activity.target, None)
added_activity = {
'person': self.user,
'whatchanged': 'branch linked',
'newvalue': branch.bzr_identity,
}
added_notification = {
'text': "** Branch linked: %s" % branch.bzr_identity,
'person': self.user,
}
self.assertRecordedChange(
expected_activity=added_activity,
expected_notification=added_notification)
def test_link_branch_to_complete_bug(self):
# Linking a branch to a bug that is "complete" (see
# IBug.is_complete) adds to the activity log but does *not*
# send an e-mail notification.
for bug_task in self.bug.bugtasks:
bug_task.transitionToStatus(
BugTaskStatus.FIXRELEASED, user=self.user)
self.failUnless(self.bug.is_complete)
self.saveOldChanges()
branch = self.factory.makeBranch()
self.bug.linkBranch(branch, self.user)
expected_activity = {
'person': self.user,
'whatchanged': 'branch linked',
'newvalue': branch.bzr_identity,
}
self.assertRecordedChange(
expected_activity=expected_activity)
def test_link_private_branch(self):
# Linking a *private* branch to a bug adds *nothing* to the
# activity log and does *not* send an e-mail notification.
branch = self.factory.makeBranch(private=True)
self.bug.linkBranch(branch, self.user)
self.assertRecordedChange()
def test_unlink_branch(self):
# Unlinking a branch from a bug adds both to the activity log and
# sends an e-mail notification.
branch = self.factory.makeBranch()
self.bug.linkBranch(branch, self.user)
self.saveOldChanges()
self.bug.unlinkBranch(branch, self.user)
# This checks the activity's attribute and target attributes.
activity = self.bug.activity[-1]
self.assertEqual(activity.attribute, 'linked_branches')
self.assertEqual(activity.target, None)
added_activity = {
'person': self.user,
'whatchanged': 'branch unlinked',
'oldvalue': branch.bzr_identity,
}
added_notification = {
'text': "** Branch unlinked: %s" % branch.bzr_identity,
'person': self.user,
}
self.assertRecordedChange(
expected_activity=added_activity,
expected_notification=added_notification)
def test_unlink_branch_from_complete_bug(self):
# Unlinking a branch from a bug that is "complete" (see
# IBug.is_complete) adds to the activity log but does *not*
# send an e-mail notification.
for bug_task in self.bug.bugtasks:
bug_task.transitionToStatus(
BugTaskStatus.FIXRELEASED, user=self.user)
self.failUnless(self.bug.is_complete)
branch = self.factory.makeBranch()
self.bug.linkBranch(branch, self.user)
self.saveOldChanges()
self.bug.unlinkBranch(branch, self.user)
expected_activity = {
'person': self.user,
'whatchanged': 'branch unlinked',
'oldvalue': branch.bzr_identity,
}
self.assertRecordedChange(
expected_activity=expected_activity)
def test_unlink_private_branch(self):
# Unlinking a *private* branch from a bug adds *nothing* to
# the activity log and does *not* send an e-mail notification.
branch = self.factory.makeBranch(private=True)
self.bug.linkBranch(branch, self.user)
self.saveOldChanges()
self.bug.unlinkBranch(branch, self.user)
self.assertRecordedChange()
def test_make_private(self):
# Marking a bug as private adds items to the bug's activity log
# and notifications.
self.bug.setPrivate(True, self.user)
visibility_change_activity = {
'person': self.user,
'whatchanged': 'visibility',
'oldvalue': 'public',
'newvalue': 'private',
}
visibility_change_notification = {
'text': '** Visibility changed to: Private',
'person': self.user,
}
self.assertRecordedChange(
expected_activity=visibility_change_activity,
expected_notification=visibility_change_notification)
def test_make_public(self):
# Marking a bug as public adds items to the bug's activity log
# and notifications.
private_bug = self.factory.makeBug(private=True)
self.saveOldChanges(private_bug)
self.assertTrue(private_bug.private)
private_bug.setPrivate(False, self.user)
visibility_change_activity = {
'person': self.user,
'whatchanged': 'visibility',
'oldvalue': 'private',
'newvalue': 'public',
}
visibility_change_notification = {
'text': '** Visibility changed to: Public',
'person': self.user,
}
self.assertRecordedChange(
expected_activity=visibility_change_activity,
expected_notification=visibility_change_notification,
bug=private_bug)
def test_tags_added(self):
# Adding tags to a bug will add BugActivity and BugNotification
# entries.
self.changeAttribute(
self.bug, 'tags', ['first-new-tag', 'second-new-tag'])
tag_change_activity = {
'person': self.user,
'whatchanged': 'tags',
'oldvalue': '',
'newvalue': 'first-new-tag second-new-tag',
}
tag_change_notification = {
'person': self.user,
'text': '** Tags added: first-new-tag second-new-tag',
}
self.assertRecordedChange(
expected_activity=tag_change_activity,
expected_notification=tag_change_notification)
def test_tags_removed(self):
# Removing tags from a bug adds BugActivity and BugNotification
# entries.
self.bug.tags = ['first-new-tag', 'second-new-tag']
self.saveOldChanges()
self.changeAttribute(
self.bug, 'tags', ['first-new-tag'])
tag_change_activity = {
'person': self.user,
'whatchanged': 'tags',
'oldvalue': 'first-new-tag second-new-tag',
'newvalue': 'first-new-tag',
}
tag_change_notification = {
'person': self.user,
'text': '** Tags removed: second-new-tag',
}
self.assertRecordedChange(
expected_activity=tag_change_activity,
expected_notification=tag_change_notification)
def test_mark_as_security_vulnerability(self):
# Marking a bug as a security vulnerability adds to the bug's
# activity log and sends a notification.
self.bug.setSecurityRelated(False, self.user)
self.changeAttribute(self.bug, 'security_related', True)
security_change_activity = {
'person': self.user,
'whatchanged': 'security vulnerability',
'oldvalue': 'no',
'newvalue': 'yes',
}
security_change_notification = {
'text': (
'** This bug has been flagged as '
'a security vulnerability'),
'person': self.user,
}
self.assertRecordedChange(
expected_activity=security_change_activity,
expected_notification=security_change_notification)
def test_unmark_as_security_vulnerability(self):
# Unmarking a bug as a security vulnerability adds to the
# bug's activity log and sends a notification.
self.bug.setSecurityRelated(True, self.user)
self.saveOldChanges()
self.changeAttribute(self.bug, 'security_related', False)
security_change_activity = {
'person': self.user,
'whatchanged': 'security vulnerability',
'oldvalue': 'yes',
'newvalue': 'no',
}
security_change_notification = {
'text': (
'** This bug is no longer flagged as '
'a security vulnerability'),
'person': self.user,
}
self.assertRecordedChange(
expected_activity=security_change_activity,
expected_notification=security_change_notification)
def test_link_cve(self):
# Linking a CVE to a bug adds to the bug's activity log and
# sends a notification.
cve = getUtility(ICveSet)['1999-8979']
self.bug.linkCVE(cve, self.user)
# This checks the activity's attribute and target attributes.
activity = self.bug.activity[-1]
self.assertEqual(activity.attribute, 'cves')
self.assertEqual(activity.target, None)
cve_linked_activity = {
'person': self.user,
'whatchanged': 'cve linked',
'oldvalue': None,
'newvalue': cve.sequence,
}
cve_linked_notification = {
'text': (
'** CVE added: http://www.cve.mitre.org/'
'cgi-bin/cvename.cgi?name=1999-8979'),
'person': self.user,
}
self.assertRecordedChange(
expected_activity=cve_linked_activity,
expected_notification=cve_linked_notification)
def test_unlink_cve(self):
# Unlinking a CVE from a bug adds to the bug's activity log and
# sends a notification.
cve = getUtility(ICveSet)['1999-8979']
self.bug.linkCVE(cve, self.user)
self.saveOldChanges()
self.bug.unlinkCVE(cve, self.user)
# This checks the activity's attribute and target attributes.
activity = self.bug.activity[-1]
self.assertEqual(activity.attribute, 'cves')
self.assertEqual(activity.target, None)
cve_unlinked_activity = {
'person': self.user,
'whatchanged': 'cve unlinked',
'oldvalue': cve.sequence,
'newvalue': None,
}
cve_unlinked_notification = {
'text': (
'** CVE removed: http://www.cve.mitre.org/'
'cgi-bin/cvename.cgi?name=1999-8979'),
'person': self.user,
}
self.assertRecordedChange(
expected_activity=cve_unlinked_activity,
expected_notification=cve_unlinked_notification)
def test_attachment_added(self):
# Adding an attachment to a bug adds entries in both BugActivity
# and BugNotification.
message = self.factory.makeMessage(owner=self.user)
self.bug.linkMessage(message)
self.saveOldChanges()
attachment = self.factory.makeBugAttachment(
bug=self.bug, owner=self.user, comment=message)
# This checks the activity's attribute and target attributes.
activity = self.bug.activity[-1]
self.assertEqual(activity.attribute, 'attachments')
self.assertEqual(activity.target, None)
attachment_added_activity = {
'person': self.user,
'whatchanged': 'attachment added',
'oldvalue': None,
'newvalue': '%s %s' % (
attachment.title,
ProxiedLibraryFileAlias(
attachment.libraryfile, attachment).http_url),
}
attachment_added_notification = {
'person': self.user,
'text': '** Attachment added: "%s"\n %s' % (
attachment.title,
ProxiedLibraryFileAlias(
attachment.libraryfile, attachment).http_url),
}
self.assertRecordedChange(
expected_notification=attachment_added_notification,
expected_activity=attachment_added_activity)
def test_attachment_removed(self):
# Removing an attachment from a bug adds entries in both BugActivity
# and BugNotification.
attachment = self.factory.makeBugAttachment(
bug=self.bug, owner=self.user)
self.saveOldChanges()
download_url = ProxiedLibraryFileAlias(
attachment.libraryfile, attachment).http_url
attachment.removeFromBug(user=self.user)
# This checks the activity's attribute and target attributes.
activity = self.bug.activity[-1]
self.assertEqual(activity.attribute, 'attachments')
self.assertEqual(activity.target, None)
attachment_removed_activity = {
'person': self.user,
'whatchanged': 'attachment removed',
'newvalue': None,
'oldvalue': '%s %s' % (
attachment.title, download_url),
}
attachment_removed_notification = {
'person': self.user,
'text': '** Attachment removed: "%s"\n %s' % (
attachment.title, download_url),
}
self.assertRecordedChange(
expected_notification=attachment_removed_notification,
expected_activity=attachment_removed_activity)
def test_bugtask_added(self):
# Adding a bug task adds entries in both BugActivity and
# BugNotification.
target = self.factory.makeProduct()
added_task = self.bug.addTask(self.user, target)
notify(ObjectCreatedEvent(added_task, user=self.user))
task_added_activity = {
'person': self.user,
'whatchanged': 'bug task added',
'newvalue': target.bugtargetname,
}
task_added_notification = {
'person': self.user,
'text': (
'** Also affects: %s\n'
' Importance: %s\n'
' Status: %s' % (
target.bugtargetname, added_task.importance.title,
added_task.status.title)),
}
self.assertRecordedChange(
expected_notification=task_added_notification,
expected_activity=task_added_activity)
def test_bugtask_added_with_assignee(self):
# Adding an assigned bug task adds entries in both BugActivity
# and BugNotification.
target = self.factory.makeProduct()
added_task = self.bug.addTask(self.user, target)
added_task.transitionToAssignee(self.factory.makePerson())
notify(ObjectCreatedEvent(added_task, user=self.user))
task_added_activity = {
'person': self.user,
'whatchanged': 'bug task added',
'newvalue': target.bugtargetname,
}
task_added_notification = {
'person': self.user,
'text': (
'** Also affects: %s\n'
' Importance: %s\n'
' Assignee: %s (%s)\n'
' Status: %s' % (
target.bugtargetname, added_task.importance.title,
added_task.assignee.displayname, added_task.assignee.name,
added_task.status.title)),
}
self.assertRecordedChange(
expected_notification=task_added_notification,
expected_activity=task_added_activity)
def test_bugtask_added_with_bugwatch(self):
# Adding a bug task with a bug watch adds entries in both
# BugActivity and BugNotification.
target = self.factory.makeProduct()
bug_watch = self.factory.makeBugWatch(bug=self.bug)
self.saveOldChanges()
added_task = self.bug.addTask(self.user, target)
added_task.bugwatch = bug_watch
notify(ObjectCreatedEvent(added_task, user=self.user))
task_added_activity = {
'person': self.user,
'whatchanged': 'bug task added',
'newvalue': target.bugtargetname,
}
task_added_notification = {
'person': self.user,
'text': (
'** Also affects: %s via\n'
' %s\n'
' Importance: %s\n'
' Status: %s' % (
target.bugtargetname, bug_watch.url,
added_task.importance.title, added_task.status.title)),
}
self.assertRecordedChange(
expected_notification=task_added_notification,
expected_activity=task_added_activity)
def test_change_bugtask_importance(self):
# When a bugtask's importance is changed, BugActivity and
# BugNotification get updated.
bug_task_before_modification = Snapshot(
self.bug_task, providing=providedBy(self.bug_task))
self.bug_task.transitionToImportance(
BugTaskImportance.HIGH, user=self.user)
notify(ObjectModifiedEvent(
self.bug_task, bug_task_before_modification,
['importance'], user=self.user))
# This checks the activity's attribute and target attributes.
activity = self.bug.activity[-1]
self.assertEqual(activity.attribute, 'importance')
self.assertThat(activity.target, StartsWith(u'product-name'))
expected_activity = {
'person': self.user,
'whatchanged': '%s: importance' % self.bug_task.bugtargetname,
'oldvalue': 'Undecided',
'newvalue': 'High',
'message': None,
}
expected_notification = {
'text': (
u'** Changed in: %s\n Importance: Undecided => High' %
self.bug_task.bugtargetname),
'person': self.user,
}
self.assertRecordedChange(
expected_activity=expected_activity,
expected_notification=expected_notification)
def test_change_bugtask_status(self):
# When a bugtask's status is changed, BugActivity and
# BugNotification get updated.
bug_task_before_modification = Snapshot(
self.bug_task, providing=providedBy(self.bug_task))
self.bug_task.transitionToStatus(
BugTaskStatus.FIXCOMMITTED, user=self.user)
notify(ObjectModifiedEvent(
self.bug_task, bug_task_before_modification, ['status'],
user=self.user))
expected_activity = {
'person': self.user,
'whatchanged': '%s: status' % self.bug_task.bugtargetname,
'oldvalue': 'New',
'newvalue': 'Fix Committed',
'message': None,
}
expected_notification = {
'text': (
u'** Changed in: %s\n Status: New => Fix Committed' %
self.bug_task.bugtargetname),
'person': self.user,
}
self.assertRecordedChange(
expected_activity=expected_activity,
expected_notification=expected_notification)
def test_target_bugtask_to_product(self):
# When a bugtask's target is changed, BugActivity and
# BugNotification get updated.
bug_task_before_modification = Snapshot(
self.bug_task, providing=providedBy(self.bug_task))
new_target = self.factory.makeProduct(owner=self.user)
self.bug_task.transitionToTarget(new_target)
notify(ObjectModifiedEvent(
self.bug_task, bug_task_before_modification,
['target', 'product'], user=self.user))
expected_activity = {
'person': self.user,
'whatchanged': 'affects',
'oldvalue': bug_task_before_modification.bugtargetname,
'newvalue': self.bug_task.bugtargetname,
}
expected_notification = {
'text': u"** Project changed: %s => %s" % (
bug_task_before_modification.bugtargetname,
self.bug_task.bugtargetname),
'person': self.user,
'recipients': [
self.user, self.product_metadata_subscriber],
}
# The person who was subscribed to meta data changes for the old
# product was notified.
self.assertRecordedChange(
expected_activity=expected_activity,
expected_notification=expected_notification)
def _test_retarget_private_security_bug_to_product(self,
bug, maintainer,
bug_supervisor=None):
# When a private security related bug has a bugtask retargetted to a
# different product, a notification is sent to the new bug supervisor
# and maintainer. If they are the same person, only one notification
# is sent. They only get notifications if they can see the bug.
# Create the private bug.
bug_task = bug.bugtasks[0]
new_target = self.factory.makeProduct(
owner=maintainer, bug_supervisor=bug_supervisor)
self.saveOldChanges(bug)
bug_task_before_modification = Snapshot(
bug_task, providing=providedBy(bug_task))
bug_task.transitionToTarget(new_target)
notify(ObjectModifiedEvent(
bug_task, bug_task_before_modification,
['target', 'product'], user=self.user))
expected_activity = {
'person': self.user,
'whatchanged': 'affects',
'oldvalue': bug_task_before_modification.bugtargetname,
'newvalue': bug_task.bugtargetname,
}
expected_recipients = [self.user]
expected_reasons = ['Subscriber']
if bug.userCanView(maintainer):
expected_recipients.append(maintainer)
expected_reasons.append('Maintainer')
if (bug_supervisor and not bug_supervisor.inTeam(maintainer)
and bug.userCanView(bug_supervisor)):
expected_recipients.append(bug_supervisor)
expected_reasons.append('Bug Supervisor')
expected_notification = {
'text': u"** Project changed: %s => %s" % (
bug_task_before_modification.bugtargetname,
bug_task.bugtargetname),
'person': self.user,
'recipients': expected_recipients,
'recipient_reasons': expected_reasons
}
self.assertRecordedChange(
expected_activity=expected_activity,
expected_notification=expected_notification, bug=bug)
def test_retarget_private_security_bug_to_product(self):
# A series of tests for re-targetting a private bug task.
bug = self.factory.makeBug(
product=self.product, owner=self.user, private=True)
maintainer = self.factory.makePerson()
bug_supervisor = self.factory.makePerson()
# Test with no bug supervisor
self._test_retarget_private_security_bug_to_product(bug, maintainer)
# Test with bug supervisor = maintainer.
self._test_retarget_private_security_bug_to_product(
bug, maintainer, maintainer)
# Test with different bug supervisor
self._test_retarget_private_security_bug_to_product(
bug, maintainer, bug_supervisor)
# Now make the bug visible to the bug supervisor and re-test.
with celebrity_logged_in('admin'):
bug.default_bugtask.transitionToAssignee(bug_supervisor)
# Test with bug supervisor = maintainer.
self._test_retarget_private_security_bug_to_product(
bug, maintainer, maintainer)
# Test with different bug supervisor
self._test_retarget_private_security_bug_to_product(
bug, maintainer, bug_supervisor)
def test_target_bugtask_to_sourcepackage(self):
# When a bugtask's target is changed, BugActivity and
# BugNotification get updated.
target = self.factory.makeDistributionSourcePackage()
metadata_subscriber = self.newSubscriber(
target, "dsp-metadata", BugNotificationLevel.METADATA)
self.newSubscriber(
target, "dsp-lifecycle", BugNotificationLevel.LIFECYCLE)
new_target = self.factory.makeDistributionSourcePackage(
distribution=target.distribution)
source_package_bug = self.factory.makeBug(
owner=self.user)
source_package_bug_task = source_package_bug.addTask(
owner=self.user, target=target)
self.saveOldChanges(source_package_bug)
bug_task_before_modification = Snapshot(
source_package_bug_task,
providing=providedBy(source_package_bug_task))
source_package_bug_task.transitionToTarget(new_target)
notify(ObjectModifiedEvent(
source_package_bug_task, bug_task_before_modification,
['target', 'sourcepackagename'], user=self.user))
expected_activity = {
'person': self.user,
'whatchanged': 'affects',
'oldvalue': bug_task_before_modification.bugtargetname,
'newvalue': source_package_bug_task.bugtargetname,
}
expected_recipients = [self.user, metadata_subscriber]
expected_recipients.extend(
bug_task.pillar.owner
for bug_task in source_package_bug.bugtasks
if bug_task.pillar.official_malone)
expected_notification = {
'text': u"** Package changed: %s => %s" % (
bug_task_before_modification.bugtargetname,
source_package_bug_task.bugtargetname),
'person': self.user,
'recipients': expected_recipients,
}
self.assertRecordedChange(
expected_activity=expected_activity,
expected_notification=expected_notification,
bug=source_package_bug)
def test_add_bugwatch_to_bugtask(self):
# Adding a BugWatch to a bug task records an entry in
# BugActivity and BugNotification.
bug_watch = self.factory.makeBugWatch()
self.saveOldChanges()
self.changeAttribute(self.bug_task, 'bugwatch', bug_watch)
expected_activity = {
'person': self.user,
'whatchanged': '%s: remote watch' % self.product.bugtargetname,
'oldvalue': None,
'newvalue': bug_watch.title,
}
expected_notification = {
'text': (
u'** Changed in: %s\n Remote watch: None => %s' % (
self.bug_task.bugtargetname, bug_watch.title)),
'person': self.user,
}
self.assertRecordedChange(
expected_activity=expected_activity,
expected_notification=expected_notification)
def test_remove_bugwatch_from_bugtask(self):
# Removing a BugWatch from a bug task records an entry in
# BugActivity and BugNotification.
bug_watch = self.factory.makeBugWatch()
self.changeAttribute(self.bug_task, 'bugwatch', bug_watch)
self.saveOldChanges()
self.changeAttribute(self.bug_task, 'bugwatch', None)
expected_activity = {
'person': self.user,
'whatchanged': '%s: remote watch' % self.product.bugtargetname,
'oldvalue': bug_watch.title,
'newvalue': None,
}
expected_notification = {
'text': (
u'** Changed in: %s\n Remote watch: %s => None' % (
self.bug_task.bugtargetname, bug_watch.title)),
'person': self.user,
}
self.assertRecordedChange(
expected_activity=expected_activity,
expected_notification=expected_notification)
def test_assign_bugtask(self):
# Assigning a bug task to someone adds entries to the bug
# activity and notifications sets.
bug_task_before_modification = Snapshot(
self.bug_task, providing=providedBy(self.bug_task))
self.bug_task.transitionToAssignee(self.user)
notify(ObjectModifiedEvent(
self.bug_task, bug_task_before_modification,
['assignee'], user=self.user))
expected_activity = {
'person': self.user,
'whatchanged': '%s: assignee' % self.bug_task.bugtargetname,
'oldvalue': None,
'newvalue': self.user.unique_displayname,
'message': None,
}
expected_notification = {
'text': (
u'** Changed in: %s\n'
u' Assignee: (unassigned) => %s' % (
self.bug_task.bugtargetname,
self.user.unique_displayname)),
'person': self.user,
}
self.assertRecordedChange(
expected_activity=expected_activity,
expected_notification=expected_notification)
def _test_unassign_bugtask(self, bug_task, expected_recipients):
# A helper method used by tests for unassigning public and private bug
# tasks.
# Unassigning a bug task assigned to someone adds entries to the
# bug activity and notifications sets.
old_assignee = bug_task.assignee
bug_task_before_modification = Snapshot(
bug_task, providing=providedBy(bug_task))
bug_task.transitionToAssignee(None)
notify(ObjectModifiedEvent(
bug_task, bug_task_before_modification,
['assignee'], user=self.user))
expected_activity = {
'person': self.user,
'whatchanged': '%s: assignee' % bug_task.bugtargetname,
'oldvalue': old_assignee.unique_displayname,
'newvalue': None,
'message': None,
}
expected_notification = {
'text': (
u'** Changed in: %s\n'
u' Assignee: %s => (unassigned)' % (
bug_task.bugtargetname,
old_assignee.unique_displayname)),
'person': self.user,
'recipients': expected_recipients,
}
self.assertRecordedChange(
expected_activity=expected_activity,
expected_notification=expected_notification,
bug=bug_task.bug)
def test_unassign_bugtask(self):
# Test that unassigning a public bug task adds entries to the
# bug activity and notifications sets.
old_assignee = self.factory.makePerson()
self.bug_task.transitionToAssignee(old_assignee)
self.saveOldChanges()
# The old assignee got notified about the change, in addition
# to the default recipients.
expected_recipients = [
self.user, self.product_metadata_subscriber, old_assignee]
self._test_unassign_bugtask(self.bug_task, expected_recipients)
def test_unassign_private_bugtask(self):
# Test that unassigning a private bug task adds entries to the
# bug activity and notifications sets. This test creates a private bug
# that the user can only see because they are assigned to it. The user
# then unassigns themselves.
# Create the private bug.
bug = self.factory.makeBug(
product=self.product, owner=self.user, private=True)
bug_task = bug.bugtasks[0]
# Create a test assignee.
old_assignee = self.factory.makePerson()
# As the bug owner, assign the test assignee..
with person_logged_in(self.user):
bug_task.transitionToAssignee(old_assignee)
self.saveOldChanges(bug=bug)
# Only the bug owner will get notified about the change.
expected_recipients = [self.user]
with person_logged_in(old_assignee):
self._test_unassign_bugtask(bug_task, expected_recipients)
def test_target_bugtask_to_milestone(self):
# When a bugtask is targetted to a milestone BugActivity and
# BugNotification records will be created.
milestone = self.factory.makeMilestone(product=self.product)
self.changeAttribute(self.bug_task, 'milestone', milestone)
expected_activity = {
'person': self.user,
'whatchanged': '%s: milestone' % self.bug_task.bugtargetname,
'oldvalue': None,
'newvalue': milestone.name,
'message': None,
}
expected_notification = {
'text': (
u'** Changed in: %s\n Milestone: None => %s' % (
self.bug_task.bugtargetname, milestone.name)),
'person': self.user,
}
self.assertRecordedChange(
expected_activity=expected_activity,
expected_notification=expected_notification)
def test_untarget_bugtask_from_milestone(self):
# When a bugtask is untargetted from a milestone both
# BugActivity and BugNotification records will be created.
milestone = self.factory.makeMilestone(product=self.product)
self.changeAttribute(self.bug_task, 'milestone', milestone)
self.saveOldChanges()
old_milestone_subscriber = self.factory.makePerson()
milestone.addBugSubscription(
old_milestone_subscriber, old_milestone_subscriber)
self.changeAttribute(self.bug_task, 'milestone', None)
expected_activity = {
'person': self.user,
'whatchanged': '%s: milestone' % self.bug_task.bugtargetname,
'newvalue': None,
'oldvalue': milestone.name,
'message': None,
}
expected_notification = {
'text': (
u'** Changed in: %s\n Milestone: %s => None' % (
self.bug_task.bugtargetname, milestone.name)),
'person': self.user,
'recipients': [
self.user, self.product_metadata_subscriber,
old_milestone_subscriber,
],
}
self.assertRecordedChange(
expected_activity=expected_activity,
expected_notification=expected_notification)
def test_change_bugtask_milestone(self):
# When a bugtask is retargeted from one milestone to another,
# both BugActivity and BugNotification records are created.
old_milestone = self.factory.makeMilestone(product=self.product)
old_milestone_subscriber = self.factory.makePerson()
old_milestone.addBugSubscription(
old_milestone_subscriber, old_milestone_subscriber)
new_milestone = self.factory.makeMilestone(product=self.product)
new_milestone_subscriber = self.factory.makePerson()
new_milestone.addBugSubscription(
new_milestone_subscriber, new_milestone_subscriber)
self.changeAttribute(self.bug_task, 'milestone', old_milestone)
self.saveOldChanges()
self.changeAttribute(self.bug_task, 'milestone', new_milestone)
expected_activity = {
'person': self.user,
'whatchanged': '%s: milestone' % self.bug_task.bugtargetname,
'newvalue': new_milestone.name,
'oldvalue': old_milestone.name,
}
expected_notification = {
'text': (
u'** Changed in: %s\n'
u' Milestone: %s => %s' % (
self.bug_task.bugtargetname,
old_milestone.name, new_milestone.name)),
'person': self.user,
'recipients': [
self.user, self.product_metadata_subscriber,
old_milestone_subscriber, new_milestone_subscriber,
],
}
self.assertRecordedChange(
expected_activity=expected_activity,
expected_notification=expected_notification)
def test_bugtask_deleted(self):
# Deleting a bug task adds entries in both BugActivity and
# BugNotification.
target = self.factory.makeProduct()
task_to_delete = self.bug.addTask(self.user, target)
self.saveOldChanges()
login_person(self.user)
task_to_delete.delete()
task_deleted_activity = {
'person': self.user,
'whatchanged': 'bug task deleted',
'oldvalue': target.bugtargetname,
}
task_deleted_notification = {
'person': self.user,
'text': (
"** No longer affects: %s" % target.bugtargetname),
}
self.assertRecordedChange(
expected_notification=task_deleted_notification,
expected_activity=task_deleted_activity)
def test_product_series_nominated(self):
# Nominating a bug to be fixed in a product series adds an item
# to the activity log only.
product = self.factory.makeProduct()
series = self.factory.makeProductSeries(product=product)
self.bug.addTask(self.user, product)
self.saveOldChanges()
nomination = self.bug.addNomination(self.user, series)
self.assertFalse(nomination.isApproved())
expected_activity = {
'person': self.user,
'whatchanged': 'nominated for series',
'newvalue': series.bugtargetname,
}
self.assertRecordedChange(expected_activity=expected_activity)
def test_distro_series_nominated(self):
# Nominating a bug to be fixed in a product series adds an item
# to the activity log only.
distribution = self.factory.makeDistribution()
series = self.factory.makeDistroSeries(distribution=distribution)
self.bug.addTask(self.user, distribution)
self.saveOldChanges()
nomination = self.bug.addNomination(self.user, series)
self.assertFalse(nomination.isApproved())
expected_activity = {
'person': self.user,
'whatchanged': 'nominated for series',
'newvalue': series.bugtargetname,
}
self.assertRecordedChange(expected_activity=expected_activity)
def test_nomination_approved(self):
# When a nomination is approved, it's like adding a new bug
# task for the series directly.
product = self.factory.makeProduct()
product.driver = product.owner
series = self.factory.makeProductSeries(product=product)
self.bug.addTask(self.user, product)
nomination = self.bug.addNomination(self.user, series)
self.assertFalse(nomination.isApproved())
self.saveOldChanges()
nomination.approve(product.owner)
expected_activity = {
'person': product.owner,
'newvalue': series.bugtargetname,
'whatchanged': 'bug task added',
'newvalue': series.bugtargetname,
}
task_added_notification = {
'person': product.owner,
'text': (
'** Also affects: %s\n'
' Importance: Undecided\n'
' Status: New' % (
series.bugtargetname)),
}
self.assertRecordedChange(
expected_activity=expected_activity,
expected_notification=task_added_notification)
def test_marked_as_duplicate(self):
# When a bug is marked as a duplicate, activity is recorded
# and a notification is sent.
duplicate_bug = self.factory.makeBug()
self.saveOldChanges(duplicate_bug)
self.saveOldChanges(self.bug, append=True)
# Save the initial "bug created" notifications before
# marking this bug a duplicate, so that we don't get
# extra notifications by mistake.
duplicate_bug_recipients = duplicate_bug.getBugNotificationRecipients(
level=BugNotificationLevel.METADATA).getRecipients()
self.changeAttribute(duplicate_bug, 'duplicateof', self.bug)
# This checks the activity's attribute and target attributes.
activity = duplicate_bug.activity[-1]
self.assertEqual(activity.attribute, 'duplicateof')
self.assertEqual(activity.target, None)
expected_activity = {
'person': self.user,
'whatchanged': 'marked as duplicate',
'oldvalue': None,
'newvalue': str(self.bug.id),
}
expected_notification = {
'person': self.user,
'text': ("** This bug has been marked a duplicate of bug %d\n"
" %s" % (self.bug.id, self.bug.title)),
'recipients': duplicate_bug_recipients,
}
self.assertRecordedChange(
expected_activity=expected_activity,
expected_notification=expected_notification,
bug=duplicate_bug)
# Ensure that only the people subscribed to the bug that
# gets marked as a duplicate are notified.
master_notifications = BugNotification.selectBy(
bug=self.bug, orderBy='id')
new_notifications = [
notification for notification in master_notifications
if notification.id not in self.old_notification_ids]
self.assertEqual(len(list(new_notifications)), 0)
def test_unmarked_as_duplicate(self):
# When a bug is unmarked as a duplicate, activity is recorded
# and a notification is sent.
duplicate_bug = self.factory.makeBug()
duplicate_bug_recipients = duplicate_bug.getBugNotificationRecipients(
level=BugNotificationLevel.METADATA).getRecipients()
duplicate_bug.markAsDuplicate(self.bug)
self.saveOldChanges(duplicate_bug)
self.changeAttribute(duplicate_bug, 'duplicateof', None)
# This checks the activity's attribute and target attributes.
activity = duplicate_bug.activity[-1]
self.assertEqual(activity.attribute, 'duplicateof')
self.assertEqual(activity.target, None)
expected_activity = {
'person': self.user,
'whatchanged': 'removed duplicate marker',
'oldvalue': str(self.bug.id),
'newvalue': None,
}
expected_notification = {
'person': self.user,
'text': ("** This bug is no longer a duplicate of bug %d\n"
" %s" % (self.bug.id, self.bug.title)),
'recipients': duplicate_bug_recipients,
}
self.assertRecordedChange(
expected_activity=expected_activity,
expected_notification=expected_notification,
bug=duplicate_bug)
def test_changed_duplicate(self):
# When a bug is changed from being a duplicate of one bug to
# being a duplicate of another, activity is recorded and a
# notification is sent.
bug_one = self.factory.makeBug()
bug_two = self.factory.makeBug()
bug_recipients = self.bug.getBugNotificationRecipients(
level=BugNotificationLevel.METADATA).getRecipients()
self.bug.markAsDuplicate(bug_one)
self.saveOldChanges()
self.changeAttribute(self.bug, 'duplicateof', bug_two)
# This checks the activity's attribute and target attributes.
activity = self.bug.activity[-1]
self.assertEqual(activity.attribute, 'duplicateof')
self.assertEqual(activity.target, None)
expected_activity = {
'person': self.user,
'whatchanged': 'changed duplicate marker',
'oldvalue': str(bug_one.id),
'newvalue': str(bug_two.id),
}
expected_notification = {
'person': self.user,
'text': ("** This bug is no longer a duplicate of bug %d\n"
" %s\n"
"** This bug has been marked a duplicate of bug %d\n"
" %s" % (bug_one.id, bug_one.title,
bug_two.id, bug_two.title)),
'recipients': bug_recipients,
}
self.assertRecordedChange(
expected_activity=expected_activity,
expected_notification=expected_notification)
def test_duplicate_private_bug(self):
# When a bug is marked as the duplicate of a private bug the
# private bug's summary won't be included in the notification.
private_bug = self.factory.makeBug()
private_bug.setPrivate(True, self.user)
public_bug = self.factory.makeBug()
self.saveOldChanges(private_bug)
self.saveOldChanges(public_bug)
# Save the initial "bug created" notifications before
# marking this bug a duplicate, so that we don't get
# extra notifications by mistake.
public_bug_recipients = public_bug.getBugNotificationRecipients(
level=BugNotificationLevel.METADATA).getRecipients()
self.changeAttribute(public_bug, 'duplicateof', private_bug)
# This checks the activity's attribute and target attributes.
activity = public_bug.activity[-1]
self.assertEqual(activity.attribute, 'duplicateof')
self.assertEqual(activity.target, None)
expected_activity = {
'person': self.user,
'whatchanged': 'marked as duplicate',
'oldvalue': None,
'newvalue': str(private_bug.id),
}
expected_notification = {
'person': self.user,
'text': (
"** This bug has been marked a duplicate of private bug %d"
% private_bug.id),
'recipients': public_bug_recipients,
}
self.assertRecordedChange(
expected_activity=expected_activity,
expected_notification=expected_notification,
bug=public_bug)
def test_unmarked_as_duplicate_of_private_bug(self):
# When a bug is unmarked as a duplicate of a private bug,
# the private bug's summary isn't sent in the notification.
private_bug = self.factory.makeBug()
private_bug.setPrivate(True, self.user)
public_bug = self.factory.makeBug()
# Save the initial "bug created" notifications before
# marking this bug a duplicate, so that we don't get
# extra notifications by mistake.
public_bug_recipients = public_bug.getBugNotificationRecipients(
level=BugNotificationLevel.METADATA).getRecipients()
self.changeAttribute(public_bug, 'duplicateof', private_bug)
self.saveOldChanges(private_bug)
self.saveOldChanges(public_bug)
self.changeAttribute(public_bug, 'duplicateof', None)
# This checks the activity's attribute and target attributes.
activity = public_bug.activity[-1]
self.assertEqual(activity.attribute, 'duplicateof')
self.assertEqual(activity.target, None)
expected_activity = {
'person': self.user,
'whatchanged': 'removed duplicate marker',
'oldvalue': str(private_bug.id),
'newvalue': None,
}
expected_notification = {
'person': self.user,
'text': (
"** This bug is no longer a duplicate of private bug %d"
% private_bug.id),
'recipients': public_bug_recipients,
}
self.assertRecordedChange(
expected_activity=expected_activity,
expected_notification=expected_notification,
bug=public_bug)
def test_changed_private_duplicate(self):
# When a bug is change from being the duplicate of a private bug
# to being the duplicate of a public bug, the private bug's
# summary won't be sent in the notification.
private_bug = self.factory.makeBug()
private_bug.setPrivate(True, self.user)
duplicate_bug = self.factory.makeBug()
public_bug = self.factory.makeBug()
bug_recipients = duplicate_bug.getBugNotificationRecipients(
level=BugNotificationLevel.METADATA).getRecipients()
self.changeAttribute(duplicate_bug, 'duplicateof', private_bug)
self.saveOldChanges(duplicate_bug)
self.changeAttribute(duplicate_bug, 'duplicateof', public_bug)
# This checks the activity's attribute and target attributes.
activity = duplicate_bug.activity[-1]
self.assertEqual(activity.attribute, 'duplicateof')
self.assertEqual(activity.target, None)
expected_activity = {
'person': self.user,
'whatchanged': 'changed duplicate marker',
'oldvalue': str(private_bug.id),
'newvalue': str(public_bug.id),
}
expected_notification = {
'person': self.user,
'text': (
"** This bug is no longer a duplicate of private bug %d\n"
"** This bug has been marked a duplicate of bug %d\n"
" %s" % (private_bug.id, public_bug.id,
public_bug.title)),
'recipients': bug_recipients,
}
self.assertRecordedChange(
expected_activity=expected_activity,
expected_notification=expected_notification,
bug=duplicate_bug)
def test_convert_to_question_no_comment(self):
# When a bug task is converted to a question, its status is
# first set to invalid, which causes the normal notifications for
# that to be added to the activity log and sent out as e-mail
# notification. After that another item is added to the activity
# log saying that the bug was converted to a question.
self.product.official_answers = True
self.bug.convertToQuestion(self.user)
converted_question = self.bug.getQuestionCreatedFromBug()
conversion_activity = {
'person': self.user,
'whatchanged': 'converted to question',
'newvalue': str(converted_question.id),
}
status_activity = {
'person': self.user,
'whatchanged': '%s: status' % self.bug_task.bugtargetname,
'newvalue': 'Invalid',
'oldvalue': 'New',
}
conversion_notification = {
'person': self.user,
'text': (
'** Converted to question:\n'
' %s' % canonical_url(converted_question)),
}
status_notification = {
'text': (
'** Changed in: %s\n'
' Status: New => Invalid' %
self.bug_task.bugtargetname),
'person': self.user,
'recipients': self.bug.getBugNotificationRecipients(
level=BugNotificationLevel.LIFECYCLE),
}
self.assertRecordedChange(
expected_activity=[status_activity, conversion_activity],
expected_notification=[status_notification,
conversion_notification])
def test_create_bug(self):
# When a bug is created, activity is recorded and a comment
# notification is sent.
new_bug = self.factory.makeBug(
product=self.product, owner=self.user, comment="ENOTOWEL")
expected_activity = {
'person': self.user,
'whatchanged': 'bug',
'message': u"added bug",
}
expected_notification = {
'person': self.user,
'text': u"ENOTOWEL",
'is_comment': True,
'recipients': new_bug.getBugNotificationRecipients(
level=BugNotificationLevel.COMMENTS),
}
self.assertRecordedChange(
expected_activity=expected_activity,
expected_notification=expected_notification,
bug=new_bug)
def test_description_changed_no_self_email(self):
# Users who have selfgenerated_bugnotifications set to False
# do not get any bug email that they generated themselves.
self.user.selfgenerated_bugnotifications = False
self.changeAttribute(
self.bug, 'description', 'New description')
# self.user is not included among the recipients.
self.assertRecipients(
[self.product_metadata_subscriber])
def test_description_changed_no_self_email_indirect(self):
# Users who have selfgenerated_bugnotifications set to False
# do not get any bug email that they generated themselves,
# even if a subscription is through a team membership.
team = self.factory.makeTeam()
team.addMember(self.user, team.teamowner)
self.bug.subscribe(team, self.user)
self.user.selfgenerated_bugnotifications = False
self.changeAttribute(
self.bug, 'description', 'New description')
# self.user is not included among the recipients.
self.assertRecipients(
[self.product_metadata_subscriber, team.teamowner])
def test_description_changed_no_muted_email(self):
# Users who have muted a bug do not get any bug email for a bug,
# even if they are subscribed through a team membership.
team = self.factory.makeTeam()
team.addMember(self.user, team.teamowner)
self.bug.subscribe(team, self.user)
self.bug.mute(self.user, self.user)
self.changeAttribute(
self.bug, 'description', 'New description')
# self.user is not included among the recipients.
self.assertRecipients(
[self.product_metadata_subscriber, team.teamowner])
def test_no_lifecycle_email_despite_structural_subscription(self):
# If a person has a structural METADATA subscription,
# and a direct LIFECYCLE subscription, they should
# get no emails for a non-LIFECYCLE change (bug 713382).
self.bug.subscribe(self.product_metadata_subscriber,
self.product_metadata_subscriber,
level=BugNotificationLevel.LIFECYCLE)
self.changeAttribute(
self.bug, 'description', 'New description')
# self.product_metadata_subscriber is not included among the
# recipients.
self.assertRecipients([self.user])
|