374
377
with person_logged_in(person):
375
378
self.assertRaises(Unauthorized, getattr, other, 'canWrite')
380
def makeSubscribedDistroSourcePackages(self):
381
# Create a person, a distribution and four
382
# DistributionSourcePacakage. Subscribe the person to two
383
# DSPs, and subscribe another person to another DSP.
384
user = self.factory.makePerson()
385
distribution = self.factory.makeDistribution()
386
dsp1 = self.factory.makeDistributionSourcePackage(
387
sourcepackagename='sp-b', distribution=distribution)
388
distribution = self.factory.makeDistribution()
389
dsp2 = self.factory.makeDistributionSourcePackage(
390
sourcepackagename='sp-a', distribution=distribution)
391
dsp3 = self.factory.makeDistributionSourcePackage(
392
sourcepackagename='sp-c', distribution=distribution)
393
with person_logged_in(user):
394
dsp1.addSubscription(user, subscribed_by=user)
395
dsp2.addSubscription(user, subscribed_by=user)
396
dsp4 = self.factory.makeDistributionSourcePackage(
397
sourcepackagename='sp-d', distribution=distribution)
398
other_user = self.factory.makePerson()
399
with person_logged_in(other_user):
400
dsp4.addSubscription(other_user, subscribed_by=other_user)
401
return user, dsp1, dsp2
403
def test_getBugSubscriberPackages(self):
404
# getBugSubscriberPackages() returns the DistributionSourcePackages
405
# to which a user is subscribed.
406
user, dsp1, dsp2 = self.makeSubscribedDistroSourcePackages()
408
# We cannot directly compare the objects returned by
409
# getBugSubscriberPackages() with the expected DSPs:
410
# These are different objects and the class does not have
411
# an __eq__ operator. So we compare the attributes distribution
412
# and sourcepackagename.
414
def get_distribution(dsp):
415
return dsp.distribution
418
return dsp.sourcepackagename
420
result = user.getBugSubscriberPackages()
422
[get_distribution(dsp) for dsp in (dsp2, dsp1)],
423
[get_distribution(dsp) for dsp in result])
425
[get_spn(dsp) for dsp in (dsp2, dsp1)],
426
[get_spn(dsp) for dsp in result])
428
def test_getBugSubscriberPackages__one_query(self):
429
# getBugSubscriberPackages() retrieves all objects
430
# needed to build the DistributionSourcePackages in
432
user, dsp1, dsp2 = self.makeSubscribedDistroSourcePackages()
433
Store.of(user).invalidate()
434
with StormStatementRecorder() as recorder:
435
list(user.getBugSubscriberPackages())
436
self.assertThat(recorder, HasQueryCount(Equals(1)))
378
439
class TestPersonStates(TestCaseWithFactory):