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
|
# Copyright 2011 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
"""Tests for the personsubscriptioninfo module."""
__metaclass__ = type
from testtools.matchers import LessThan
from zope.security.proxy import removeSecurityProxy
from canonical.testing import DatabaseFunctionalLayer
from lp.bugs.interfaces.personsubscriptioninfo import (
IRealSubscriptionInfo,
IRealSubscriptionInfoCollection,
IVirtualSubscriptionInfo,
IVirtualSubscriptionInfoCollection,
)
from lp.bugs.model.personsubscriptioninfo import PersonSubscriptions
from lp.registry.interfaces.person import TeamSubscriptionPolicy
from lp.registry.interfaces.teammembership import TeamMembershipStatus
from lp.testing import (
person_logged_in,
StormStatementRecorder,
TestCaseWithFactory,
)
from lp.testing.matchers import (
HasQueryCount,
Provides,
)
class TestPersonSubscriptionInfo(TestCaseWithFactory):
layer = DatabaseFunctionalLayer
def setUp(self):
super(TestPersonSubscriptionInfo, self).setUp()
self.subscriber = self.factory.makePerson()
self.bug = self.factory.makeBug()
self.subscriptions = PersonSubscriptions(self.subscriber, self.bug)
def makeDuplicates(self, count=1, subscriber=None):
if subscriber is None:
subscriber = self.subscriber
if subscriber.is_team:
subscribed_by = subscriber.teamowner
else:
subscribed_by = subscriber
duplicates = [self.factory.makeBug() for i in range(count)]
with person_logged_in(subscribed_by):
for duplicate in duplicates:
duplicate.markAsDuplicate(self.bug)
duplicate.subscribe(subscriber, subscribed_by)
return duplicates
def assertCollectionsAreEmpty(self, except_=None):
names = ('direct', 'from_duplicate', 'as_owner', 'as_assignee')
assert except_ is None or except_ in names
for name in names:
collection = getattr(self.subscriptions, name)
if name == except_:
self.assertEqual(self.subscriptions.count, collection.count)
else:
self.assertEqual(collection.count, 0)
def assertCollectionContents(
self, collection,
personal=0, as_team_member=0, as_team_admin=0):
# Make sure that the collection has the values we expect.
self.assertEqual(collection.count,
personal + as_team_member + as_team_admin)
for name, expected in (('personal', personal),
('as_team_member', as_team_member),
('as_team_admin', as_team_admin)):
actual = getattr(collection, name)
self.assertEqual(expected, len(actual))
if IVirtualSubscriptionInfoCollection.providedBy(collection):
expected_interface = IVirtualSubscriptionInfo
else:
self.assertThat(collection,
Provides(IRealSubscriptionInfoCollection))
expected_interface = IRealSubscriptionInfo
for info in actual:
self.assertThat(info, Provides(expected_interface))
def assertVirtualSubscriptionInfoMatches(
self, info, bug, principal, pillar, bugtasks):
# Make sure that the virtual subscription info has expected values.
self.assertEqual(info.bug, bug)
self.assertEqual(info.principal, principal)
self.assertEqual(info.pillar, pillar)
self.assertContentEqual(info.tasks, bugtasks)
def assertRealSubscriptionInfoMatches(
self, info, bug, principal,
principal_is_reporter, security_contact_tasks, bug_supervisor_tasks):
# Make sure that the real subscription info has expected values.
self.assertEqual(info.bug, bug)
self.assertEqual(info.principal, principal)
self.assertEqual(info.principal_is_reporter, principal_is_reporter)
self.assertContentEqual(
info.bug_supervisor_tasks, bug_supervisor_tasks)
self.assertContentEqual(
info.security_contact_tasks, security_contact_tasks)
def test_no_subscriptions(self):
# Load a `PersonSubscriptionInfo`s for a subscriber and a bug.
self.subscriptions.reload()
self.assertCollectionsAreEmpty()
self.failIf(self.subscriptions.muted)
def test_no_subscriptions_getDataForClient(self):
self.subscriptions.reload()
subscriptions, references = self.subscriptions.getDataForClient()
self.assertEqual(references, {})
self.assertEqual(subscriptions['count'], 0)
self.assertEqual(subscriptions['muted'], False)
self.assertEqual(subscriptions['direct']['count'], 0)
self.assertEqual(subscriptions['from_duplicate']['count'], 0)
self.assertEqual(subscriptions['as_owner']['count'], 0)
self.assertEqual(subscriptions['as_assignee']['count'], 0)
self.assertEqual(subscriptions['bug_id'], self.bug.id)
def test_assignee(self):
with person_logged_in(self.subscriber):
self.bug.default_bugtask.transitionToAssignee(self.subscriber)
self.subscriptions.reload()
self.assertCollectionsAreEmpty(except_='as_assignee')
self.failIf(self.subscriptions.muted)
self.assertCollectionContents(
self.subscriptions.as_assignee, personal=1)
self.assertVirtualSubscriptionInfoMatches(
self.subscriptions.as_assignee.personal[0],
self.bug, self.subscriber,
self.bug.default_bugtask.target, [self.bug.default_bugtask])
def test_assignee_getDataForClient(self):
with person_logged_in(self.subscriber):
self.bug.default_bugtask.transitionToAssignee(self.subscriber)
self.subscriptions.reload()
subscriptions, references = self.subscriptions.getDataForClient()
self.assertEqual(len(references), 3)
self.assertEqual(subscriptions['count'], 1)
self.assertEqual(subscriptions['muted'], False)
self.assertEqual(subscriptions['direct']['count'], 0)
self.assertEqual(subscriptions['from_duplicate']['count'], 0)
self.assertEqual(subscriptions['as_owner']['count'], 0)
self.assertEqual(subscriptions['as_assignee']['count'], 1)
personal = subscriptions['as_assignee']['personal'][0]
self.assertEqual(references[personal['bug']], self.bug)
self.assertEqual(references[personal['principal']], self.subscriber)
self.assertEqual(references[personal['pillar']],
self.bug.default_bugtask.target)
def test_assignee_through_team(self):
team = self.factory.makeTeam(members=[self.subscriber])
with person_logged_in(self.subscriber):
self.bug.bugtasks[0].transitionToAssignee(team)
self.subscriptions.reload()
self.assertCollectionsAreEmpty(except_='as_assignee')
self.failIf(self.subscriptions.muted)
self.assertCollectionContents(
self.subscriptions.as_assignee, as_team_member=1)
self.assertVirtualSubscriptionInfoMatches(
self.subscriptions.as_assignee.as_team_member[0],
self.bug, team,
self.bug.default_bugtask.target, [self.bug.default_bugtask])
def test_assignee_through_team_getDataForClient(self):
team = self.factory.makeTeam(members=[self.subscriber])
with person_logged_in(self.subscriber):
self.bug.bugtasks[0].transitionToAssignee(team)
self.subscriptions.reload()
subscriptions, references = self.subscriptions.getDataForClient()
personal = subscriptions['as_assignee']['as_team_member'][0]
self.assertEqual(references[personal['principal']], team)
def test_assignee_through_team_as_admin(self):
team = self.factory.makeTeam()
with person_logged_in(team.teamowner):
team.addMember(self.subscriber, team.teamowner,
status=TeamMembershipStatus.ADMIN)
self.bug.bugtasks[0].transitionToAssignee(team)
self.subscriptions.reload()
self.assertCollectionsAreEmpty(except_='as_assignee')
self.failIf(self.subscriptions.muted)
self.assertCollectionContents(
self.subscriptions.as_assignee, as_team_admin=1)
self.assertVirtualSubscriptionInfoMatches(
self.subscriptions.as_assignee.as_team_admin[0],
self.bug, team,
self.bug.default_bugtask.target, [self.bug.default_bugtask])
def test_assignee_through_team_as_admin_getDataForClient(self):
team = self.factory.makeTeam()
with person_logged_in(team.teamowner):
team.addMember(self.subscriber, team.teamowner,
status=TeamMembershipStatus.ADMIN)
self.bug.bugtasks[0].transitionToAssignee(team)
self.subscriptions.reload()
subscriptions, references = self.subscriptions.getDataForClient()
personal = subscriptions['as_assignee']['as_team_admin'][0]
self.assertEqual(references[personal['principal']], team)
def test_direct(self):
# Subscribed directly to the bug.
with person_logged_in(self.subscriber):
self.bug.subscribe(self.subscriber, self.subscriber)
# Load a `PersonSubscriptionInfo`s for subscriber and a bug.
self.subscriptions.reload()
self.assertCollectionsAreEmpty(except_='direct')
self.failIf(self.subscriptions.muted)
self.assertCollectionContents(
self.subscriptions.direct, personal=1)
self.assertRealSubscriptionInfoMatches(
self.subscriptions.direct.personal[0],
self.bug, self.subscriber, False, [], [])
def test_direct_getDataForClient(self):
# Subscribed directly to the bug.
with person_logged_in(self.subscriber):
subscription = self.bug.subscribe(
self.subscriber, self.subscriber)
self.subscriptions.reload()
subscriptions, references = self.subscriptions.getDataForClient()
personal = subscriptions['direct']['personal'][0]
self.assertEqual(references[personal['principal']], self.subscriber)
self.assertEqual(references[personal['bug']], self.bug)
self.assertEqual(references[personal['subscription']], subscription)
self.assertEqual(personal['principal_is_reporter'], False)
self.assertEqual(personal['security_contact_pillars'], [])
self.assertEqual(personal['bug_supervisor_pillars'], [])
def test_direct_through_team(self):
# Subscribed to the bug through membership in a team.
team = self.factory.makeTeam(members=[self.subscriber])
with person_logged_in(self.subscriber):
self.bug.subscribe(team, self.subscriber)
# Load a `PersonSubscriptionInfo`s for subscriber and a bug.
self.subscriptions.reload()
self.assertCollectionsAreEmpty(except_='direct')
self.failIf(self.subscriptions.muted)
self.assertCollectionContents(
self.subscriptions.direct, as_team_member=1)
self.assertRealSubscriptionInfoMatches(
self.subscriptions.direct.as_team_member[0],
self.bug, team, False, [], [])
def test_direct_through_team_getDataForClient(self):
# Subscribed to the bug through membership in a team.
team = self.factory.makeTeam(members=[self.subscriber])
with person_logged_in(self.subscriber):
self.bug.subscribe(team, self.subscriber)
self.subscriptions.reload()
subscriptions, references = self.subscriptions.getDataForClient()
personal = subscriptions['direct']['as_team_member'][0]
self.assertEqual(references[personal['principal']], team)
def test_direct_through_team_as_admin(self):
# Subscribed to the bug through membership in a team
# as an admin of that team.
team = self.factory.makeTeam()
with person_logged_in(team.teamowner):
team.addMember(self.subscriber, team.teamowner,
status=TeamMembershipStatus.ADMIN)
self.bug.subscribe(team, team.teamowner)
# Load a `PersonSubscriptionInfo`s for subscriber and a bug.
self.subscriptions.reload()
self.assertCollectionsAreEmpty(except_='direct')
self.failIf(self.subscriptions.muted)
self.assertCollectionContents(
self.subscriptions.direct, as_team_admin=1)
self.assertRealSubscriptionInfoMatches(
self.subscriptions.direct.as_team_admin[0],
self.bug, team, False, [], [])
def test_direct_through_team_as_admin_getDataForClient(self):
# Subscribed to the bug through membership in a team
# as an admin of that team.
team = self.factory.makeTeam()
with person_logged_in(team.teamowner):
team.addMember(self.subscriber, team.teamowner,
status=TeamMembershipStatus.ADMIN)
self.bug.subscribe(team, team.teamowner)
self.subscriptions.reload()
subscriptions, references = self.subscriptions.getDataForClient()
personal = subscriptions['direct']['as_team_admin'][0]
self.assertEqual(references[personal['principal']], team)
def test_duplicate_direct(self):
# Subscribed directly to the duplicate bug.
[duplicate] = self.makeDuplicates(count=1)
# Load a `PersonSubscriptionInfo`s for subscriber and a bug.
self.subscriptions.reload()
self.assertCollectionsAreEmpty(except_='from_duplicate')
self.failIf(self.subscriptions.muted)
self.assertCollectionContents(
self.subscriptions.from_duplicate, personal=1)
self.assertRealSubscriptionInfoMatches(
self.subscriptions.from_duplicate.personal[0],
duplicate, self.subscriber, False, [], [])
def test_duplicate_direct_reverse(self):
# Subscribed directly to the primary bug, and a duplicate bug changes.
primary = self.factory.makeBug()
with person_logged_in(self.subscriber):
self.bug.markAsDuplicate(primary)
primary.subscribe(self.subscriber, self.subscriber)
# Load a `PersonSubscriptionInfo`s for subscriber and a bug.
self.subscriptions.reload()
# This means no subscriptions on the duplicate bug.
self.assertCollectionsAreEmpty()
self.failIf(self.subscriptions.muted)
self.assertCollectionContents(
self.subscriptions.from_duplicate, personal=0)
def test_duplicate_multiple(self):
# Subscribed directly to more than one duplicate bug.
duplicate1 = self.factory.makeBug()
duplicate2 = self.factory.makeBug()
with person_logged_in(self.subscriber):
duplicate1.markAsDuplicate(self.bug)
duplicate1.subscribe(self.subscriber, self.subscriber)
duplicate2.markAsDuplicate(self.bug)
duplicate2.subscribe(self.subscriber, self.subscriber)
# Load a `PersonSubscriptionInfo`s for subscriber and a bug.
self.subscriptions.reload()
self.assertCollectionsAreEmpty(except_='from_duplicate')
self.failIf(self.subscriptions.muted)
self.assertCollectionContents(
self.subscriptions.from_duplicate, personal=2)
self.assertRealSubscriptionInfoMatches(
self.subscriptions.from_duplicate.personal[0],
duplicate1, self.subscriber, False, [], [])
self.assertRealSubscriptionInfoMatches(
self.subscriptions.from_duplicate.personal[1],
duplicate2, self.subscriber, False, [], [])
def test_duplicate_through_team(self):
# Subscribed to a duplicate bug through team membership.
team = self.factory.makeTeam(members=[self.subscriber])
duplicate = self.factory.makeBug()
with person_logged_in(self.subscriber):
duplicate.markAsDuplicate(self.bug)
duplicate.subscribe(team, self.subscriber)
# Load a `PersonSubscriptionInfo`s for subscriber and a bug.
self.subscriptions.reload()
self.assertCollectionsAreEmpty(except_='from_duplicate')
self.failIf(self.subscriptions.muted)
self.assertCollectionContents(
self.subscriptions.from_duplicate, as_team_member=1)
self.assertRealSubscriptionInfoMatches(
self.subscriptions.from_duplicate.as_team_member[0],
duplicate, team, False, [], [])
def test_duplicate_through_team_as_admin(self):
# Subscribed to a duplicate bug through team membership
# as an admin of that team.
team = self.factory.makeTeam()
with person_logged_in(team.teamowner):
team.addMember(self.subscriber, team.teamowner,
status=TeamMembershipStatus.ADMIN)
duplicate = self.factory.makeBug()
with person_logged_in(self.subscriber):
duplicate.markAsDuplicate(self.bug)
duplicate.subscribe(team, self.subscriber)
# Load a `PersonSubscriptionInfo`s for subscriber and a bug.
self.subscriptions.reload()
self.assertCollectionsAreEmpty(except_='from_duplicate')
self.failIf(self.subscriptions.muted)
self.assertCollectionContents(
self.subscriptions.from_duplicate, as_team_admin=1)
self.assertRealSubscriptionInfoMatches(
self.subscriptions.from_duplicate.as_team_admin[0],
duplicate, team, False, [], [])
def test_subscriber_is_reporter(self):
self.bug = self.factory.makeBug(owner=self.subscriber)
self.subscriptions = PersonSubscriptions(self.subscriber, self.bug)
# Subscribed directly to the bug.
with person_logged_in(self.subscriber):
self.bug.subscribe(self.subscriber, self.subscriber)
# Load a `PersonSubscriptionInfo`s for subscriber and a bug.
self.subscriptions.reload()
self.assertRealSubscriptionInfoMatches(
self.subscriptions.direct.personal[0],
self.bug, self.subscriber, True, [], [])
def test_subscriber_is_security_contact(self):
target = self.bug.default_bugtask.target
removeSecurityProxy(target).security_contact = self.subscriber
# Subscribed directly to the bug.
with person_logged_in(self.subscriber):
self.bug.subscribe(self.subscriber, self.subscriber)
# Load a `PersonSubscriptionInfo`s for subscriber and a bug.
self.subscriptions.reload()
self.assertRealSubscriptionInfoMatches(
self.subscriptions.direct.personal[0],
self.bug, self.subscriber, False,
[{'task': self.bug.default_bugtask, 'pillar': target}], [])
def test_subscriber_is_bug_supervisor(self):
target = self.bug.default_bugtask.target
removeSecurityProxy(target).bug_supervisor = self.subscriber
# Subscribed directly to the bug.
with person_logged_in(self.subscriber):
self.bug.subscribe(self.subscriber, self.subscriber)
# Load a `PersonSubscriptionInfo`s for subscriber and a bug.
self.subscriptions.reload()
self.assertRealSubscriptionInfoMatches(
self.subscriptions.direct.personal[0],
self.bug, self.subscriber, False,
[], [{'task': self.bug.default_bugtask, 'pillar': target}])
def test_owner(self):
# Bug is targeted to a pillar with no supervisor set.
target = self.bug.default_bugtask.target
# Load a `PersonSubscriptionInfo`s for target.owner and a bug.
self.subscriptions.loadSubscriptionsFor(target.owner, self.bug)
self.assertCollectionsAreEmpty(except_='as_owner')
self.failIf(self.subscriptions.muted)
self.assertCollectionContents(
self.subscriptions.as_owner, personal=1)
self.assertVirtualSubscriptionInfoMatches(
self.subscriptions.as_owner.personal[0],
self.bug, target.owner,
self.bug.default_bugtask.target, [self.bug.default_bugtask])
def test_owner_as_bug_supervisor_is_empty(self):
target = self.bug.default_bugtask.target
removeSecurityProxy(target).bug_supervisor = target.owner
# Subscribed directly to the bug.
self.subscriptions.loadSubscriptionsFor(target.owner, self.bug)
self.assertCollectionsAreEmpty()
self.failIf(self.subscriptions.muted)
def test_owner_through_team(self):
# Bug is targeted to a pillar with no supervisor set.
target = self.bug.default_bugtask.target
team = self.factory.makeTeam(
members=[self.subscriber],
subscription_policy=TeamSubscriptionPolicy.RESTRICTED)
removeSecurityProxy(target).owner = team
# Load a `PersonSubscriptionInfo`s for target.owner and a bug.
self.subscriptions.reload()
self.assertCollectionsAreEmpty(except_='as_owner')
self.failIf(self.subscriptions.muted)
self.assertCollectionContents(
self.subscriptions.as_owner, as_team_member=1)
self.assertVirtualSubscriptionInfoMatches(
self.subscriptions.as_owner.as_team_member[0],
self.bug, target.owner,
self.bug.default_bugtask.target, [self.bug.default_bugtask])
def test_owner_through_team_as_admin(self):
# Bug is targeted to a pillar with no supervisor set.
target = self.bug.default_bugtask.target
team = self.factory.makeTeam(
subscription_policy=TeamSubscriptionPolicy.RESTRICTED)
with person_logged_in(team.teamowner):
team.addMember(self.subscriber, team.teamowner,
status=TeamMembershipStatus.ADMIN)
removeSecurityProxy(target).owner = team
# Load a `PersonSubscriptionInfo`s for target.owner and a bug.
self.subscriptions.reload()
self.assertCollectionsAreEmpty(except_='as_owner')
self.failIf(self.subscriptions.muted)
self.assertCollectionContents(
self.subscriptions.as_owner, as_team_admin=1)
self.assertVirtualSubscriptionInfoMatches(
self.subscriptions.as_owner.as_team_admin[0],
self.bug, target.owner,
self.bug.default_bugtask.target, [self.bug.default_bugtask])
def test_is_muted(self):
# Subscribed directly to the bug, muted.
with person_logged_in(self.subscriber):
self.bug.mute(self.subscriber, self.subscriber)
# Load a `PersonSubscriptionInfo`s for subscriber and a bug.
self.subscriptions.reload()
self.failUnless(self.subscriptions.muted)
def test_many_duplicate_team_admin_subscriptions_few_queries(self):
# This is related to bug 811447. The user is subscribed to a
# duplicate bug through team membership in which the user is an admin.
team = self.factory.makeTeam()
with person_logged_in(team.teamowner):
team.addMember(self.subscriber, team.teamowner,
status=TeamMembershipStatus.ADMIN)
self.makeDuplicates(count=1, subscriber=team)
with StormStatementRecorder() as recorder:
self.subscriptions.reload()
# This should produce a very small number of queries.
self.assertThat(recorder, HasQueryCount(LessThan(5)))
count_with_one_subscribed_duplicate = recorder.count
# It should have the correct result.
self.assertCollectionsAreEmpty(except_='from_duplicate')
self.assertCollectionContents(
self.subscriptions.from_duplicate, as_team_admin=1)
# If we increase the number of duplicates subscribed via the team that
# the user administers...
self.makeDuplicates(count=4, subscriber=team)
with StormStatementRecorder() as recorder:
self.subscriptions.reload()
# ...then the query count should remain the same.
count_with_five_subscribed_duplicates = recorder.count
self.assertEqual(
count_with_one_subscribed_duplicate,
count_with_five_subscribed_duplicates)
# We should still have the correct result.
self.assertCollectionsAreEmpty(except_='from_duplicate')
self.assertCollectionContents(
self.subscriptions.from_duplicate, as_team_admin=5)
|