def test_accessibilityQuery(self): """ Ensure that asAccessibleTo returns only items actually accessible to the given role. """ for i in range(10): self.addSomeThings() query = self.store.query(PrivateThing) aliceQuery = self.assertWarns( PendingDeprecationWarning, "Use Role.asAccessibleTo() instead of sharing.asAccessibleTo().", __file__, lambda : list(sharing.asAccessibleTo(self.alice, query))) bobQuery = list(sharing.asAccessibleTo(self.bob, query)) self.assertEqual(map(sharing.itemFromProxy, bobQuery), map(lambda x: x.sharedItem, self.bobThings)) self.assertEqual(map(sharing.itemFromProxy, aliceQuery), map(lambda x: x.sharedItem, self.aliceThings)) self.assertEqual([p.sharedInterfaces for p in aliceQuery], [[IPrivateThing]] * 10) self.assertEqual([p.sharedInterfaces for p in bobQuery], [[IReadOnly]] * 10)
def test_twoInterfacesTwoGroupsQuery(self): """ Verify that when an item is shared to two roles that a user is a member of, and then retrieved by an asAccessibleTo query, both interfaces will be accessible on each object in the query result, and the same number of items will be accessible in the query as were shared. """ us = sharing.getPrimaryRole(self.store, u'us', True) them = sharing.getPrimaryRole(self.store, u'them', True) self.bob.becomeMemberOf(us) self.bob.becomeMemberOf(them) for x in range(3): it = PrivateThing(store=self.store, publicData=x) sharing.shareItem(it, toRole=us, shareID=u'q', interfaces=[IPrivateThing]) sharing.shareItem(it, toRole=them, shareID=u'q', interfaces=[IReadOnly]) # sanity check self.assertEquals(self.store.query(PrivateThing).count(), 3) aat = list(sharing.asAccessibleTo(self.bob, self.store.query( PrivateThing, sort=PrivateThing.publicData.descending))) aat2 = list(sharing.asAccessibleTo(self.bob, self.store.query( PrivateThing, sort=PrivateThing.publicData.ascending))) # sanity check x2 for acc in aat: acc.mutateSomeState() expectedData = [x + 5 for x in reversed(range(3))] self.assertEquals([acc.retrieveSomeState() for acc in aat], expectedData) self.assertEquals([acc.retrieveSomeState() for acc in aat2], list(reversed(expectedData)))
def test_twoInterfacesTwoGroupsUnsortedQuery(self): """ Verify that when duplicate shares exist for the same item and an asAccessibleTo query is made with no specified sort, the roles are still deduplicated properly. """ us = sharing.getPrimaryRole(self.store, u'us', True) them = sharing.getPrimaryRole(self.store, u'them', True) self.bob.becomeMemberOf(us) self.bob.becomeMemberOf(them) for x in range(3): it = PrivateThing(store=self.store, publicData=x) sharing.shareItem(it, toRole=us, shareID=u'q', interfaces=[IPrivateThing]) sharing.shareItem(it, toRole=them, shareID=u'q', interfaces=[IReadOnly]) # sanity check self.assertEquals(self.store.query(PrivateThing).count(), 3) aat = list(sharing.asAccessibleTo(self.bob, self.store.query( PrivateThing))) # sanity check x2 for acc in aat: acc.mutateSomeState() expectedData = [x + 5 for x in range(3)] aat.sort(key=lambda i: i.retrieveSomeState()) self.assertEquals([acc.retrieveSomeState() for acc in aat], expectedData)
def test_limitWithPrivateStuff(self): """ Verify that a limited query with some un-shared items will return up to the provided limit number of shared items. """ L = [] def makeThing(shared): t = PrivateThing(store=self.store, publicData=self.i) self.i += 1 if shared: sharing.shareItem( t, toRole=self.bob, interfaces=[IPrivateThing], shareID=unicode(self.i)) L.append(t) # 0, 1, 2: shared for x in range(3): makeThing(True) # 3, 4, 5: private for x in range(3): makeThing(False) # 6, 7, 8: shared again for x in range(3): makeThing(True) self.assertEquals( map(sharing.itemFromProxy, sharing.asAccessibleTo( self.bob, self.store.query( PrivateThing, limit=5))), [L[0], L[1], L[2], L[6], L[7]])
def getTopLevelFor(self, role): """ Return an iterator of all top-level Blurbs in this store. @param role: a L{xmantissa.sharing.Role}. """ blogs = self.store.query(Blurb, Blurb.parent == None) return sharing.asAccessibleTo(role, blogs)
def view(self, role): """ Collect the children of this blurb that are visible to this role. @param role: a L{Role} which can observe some children of this blurb. @return: an iterable of L{xmantissa.sharing.SharedProxy} instances. """ children = self.store.query(Blurb, Blurb.parent == self, sort=Blurb.dateCreated.descending) return asAccessibleTo(role, children)
def test_limit(self): """ Ensure that asAccessibleTo respects query limits. """ for i in range(10): self.addSomeThings() query = self.store.query(PrivateThing, limit=3) bobQuery = list(sharing.asAccessibleTo(self.bob, query)) self.assertEquals(len(bobQuery), 3)
def test_sortOrdering(self): """ Ensure that asAccessibleTo respects query sort order. """ for i in range(10): self.addSomeThings() query = self.store.query(PrivateThing, sort=PrivateThing.publicData.ascending) # Sanity check. self.assertEquals([x.publicData for x in query], range(-9, 1, 1)) bobQuery = list(sharing.asAccessibleTo(self.bob, query)) self.assertEquals([x.retrieveSomeState() for x in bobQuery], range(-9, 1, 1)) query2 = self.store.query(PrivateThing, sort=PrivateThing.publicData.descending) # Sanity check #2 self.assertEquals([x.publicData for x in query2], range(-9, 1, 1)[::-1]) bobQuery2 = list(sharing.asAccessibleTo(self.bob, query2)) self.assertEquals([x.retrieveSomeState() for x in bobQuery2], range(-9, 1, 1)[::-1])
def _charactersForViewer(self, store, role): """ Find the characters the given role is allowed to play. This will load any L{Thing}s from C{store} which are shared to C{role}. It then unwraps them from their sharing wrapper and returns them (XXX there should really be a way for this to work without the unwrapping, no? See #2909. -exarkun). """ characters = [] things = store.query(Thing) actors = asAccessibleTo(role, things) characters.extend(map(itemFromProxy, actors)) return characters
def test_limitGetsAllInterfaces(self): """ asAccessibleTo should always collate interfaces together, regardless of its limit parameter. """ t = PrivateThing(store=self.store, publicData=self.i) sharing.shareItem(t, toName=u'*****@*****.**', interfaces=[IPrivateThing], shareID=u'test') sharing.shareItem(t, toName=u'Everyone', interfaces=[IReadOnly], shareID=u'test') L = list(sharing.asAccessibleTo( self.bob, self.store.query(PrivateThing, limit=1))) self.assertEquals(len(L), 1) self.assertEquals(set(L[0].sharedInterfaces), set([IReadOnly, IPrivateThing]))
def viewByTag(self, role, tag): """ Collect the children of this blurb that are visible to this role, and have been tagged with C{tag} @param role: a L{Role} which can observe some children of this blurb. @param tag: the tag name @type tag: C{unicode} @return: an iterable of L{xmantissa.sharing.SharedProxy} instances. """ children = self.store.query( Blurb, AND(Blurb.parent == self, Tag.object == Blurb.storeID, Tag.name == tag), sort=Blurb.dateCreated.descending, ) return asAccessibleTo(role, children)
def viewByTag(self, role, tag): """ Collect the children of this blurb that are visible to this role, and have been tagged with C{tag} @param role: a L{Role} which can observe some children of this blurb. @param tag: the tag name @type tag: C{unicode} @return: an iterable of L{xmantissa.sharing.SharedProxy} instances. """ children = self.store.query(Blurb, AND(Blurb.parent == self, Tag.object == Blurb.storeID, Tag.name == tag), sort=Blurb.dateCreated.descending) return asAccessibleTo(role, children)
def test_limitEfficiency(self): """ Verify that querying a limited number of shared items does not become slower as more items are shared. """ zomg = QueryCounter(self.store) for i in range(10): self.addSomeThings() query = self.store.query( PrivateThing, limit=3, sort=PrivateThing.publicData.ascending) checkit = lambda : list(sharing.asAccessibleTo(self.bob, query)) before = zomg.measure(checkit) for i in range(10): self.addSomeThings() after = zomg.measure(checkit) self.assertEquals(before, after)
def test_limitMultiShare(self): """ asAccessibleTo should stop after yielding the limit number of results, even if there are more shares examined than results. """ L = [] for x in range(10): t = PrivateThing(store=self.store, publicData=self.i) L.append(t) self.i += 1 sharing.shareItem(t, toName=u'*****@*****.**', interfaces=[IPrivateThing], shareID=unicode(x)) sharing.shareItem(t, toName=u'Everyone', interfaces=[IReadOnly], shareID=unicode(x)) proxies = list(sharing.asAccessibleTo( self.bob, self.store.query(PrivateThing, limit=5, sort=PrivateThing.publicData.ascending))) self.assertEquals(map(sharing.itemFromProxy, proxies), L[:5]) for proxy in proxies: self.assertEquals(set(proxy.sharedInterfaces), set([IPrivateThing, IReadOnly]))
def inequalityQuery(self, constraint, count, isAscending): """ Perform the query in L{ScrollingElement} in a slightly different way: wrap it in L{asAccessibleTo} for this L{ShareScrollingElement}'s role. @param constraint: an additional constraint to apply to the query. @type constraint: L{axiom.iaxiom.IComparison}. @param count: the maximum number of rows to return. @type count: C{int} @param isAscending: a boolean describing whether the query should be yielding ascending or descending results. @type isAscending: C{bool} @return: an query which will yield some results from this model. @rtype: L{axiom.iaxiom.IQuery} """ theQuery = super(ShareScrollingElement, self).inequalityQuery( constraint, count, isAscending) return sharing.asAccessibleTo(self.role, theQuery.query)