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 createBlog(self, title, description): """ Create a top-level BLOG-flavored Blurb with the given title and description, shared for edit with the owner of this store and for viewing with everyone, and return it. @param title: the blog title @type title: C{unicode} @param description: the blog description @type description: C{unicode} """ store = self.store now = Time() blog = Blurb(store=self.store, dateCreated=now, dateLastEdited=now, title=title, body=description, flavor=FLAVOR.BLOG, author=sharing.getSelfRole(self.store)) authorsRole = sharing.getPrimaryRole(store, title + u' blog', True) sharing.getSelfRole(store).becomeMemberOf(authorsRole) sharing.shareItem(blog, authorsRole, shareID=u'blog') everyoneRole = sharing.getEveryoneRole(store) sharing.shareItem(blog, everyoneRole, shareID=u'blog', interfaces=[IViewable]) # this should be configurable blog.permitChildren(everyoneRole, FLAVOR.BLOG_POST, IViewable)
def test_differentUserSameID(self): """ Verify that if different facets of the same item are shared to different users with the same shareID, each user will receive the correct respective facet with only the correct methods exposed. """ t = PrivateThing(store=self.store, publicData=789) toBob = sharing.shareItem(t, toName=u'*****@*****.**', interfaces=[IReadOnly]) toAlice = sharing.shareItem(t, toName=u'*****@*****.**', shareID=toBob.shareID, interfaces=[IPrivateThing]) # Sanity check. self.assertEquals(toBob.shareID, toAlice.shareID) asBob = sharing.getShare(self.store, sharing.getPrimaryRole( self.store, u'*****@*****.**'), toBob.shareID) asAlice = sharing.getShare(self.store, sharing.getPrimaryRole( self.store, u'*****@*****.**'), toBob.shareID) self.assertEquals(asBob.retrieveSomeState(), 789) self.assertRaises(AttributeError, lambda : asBob.mutateSomeState) self.assertRaises(AttributeError, lambda : asAlice.retrieveSomeState) asAlice.mutateSomeState() # Make sure they're both seeing the same item. self.assertEquals(asBob.retrieveSomeState(), 789+5)
def installApp(self, ss1): """ Create a public item and share it as the default. """ fpi = FakePublicItem(store=ss1) shareItem(fpi, toRole=getEveryoneRole(ss1), shareID=getDefaultShareID(ss1))
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 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)
def addSomeThings(self): privateThing = PrivateThing(store=self.store, publicData=-self.i) self.i += 1 self.things.append(privateThing) self.bobThings.append(sharing.shareItem( privateThing, toName=u'*****@*****.**', interfaces=[IReadOnly])) self.aliceThings.append(sharing.shareItem( privateThing, toName=u'*****@*****.**', interfaces=[IPrivateThing]))
def test_defaultShareIDInteractionNoMatch(self): """ Verify that L{websharing.linkTo} explicitly includes a share ID in the URL if the ID of the share it is passed doesn't match the default. """ websharing.addDefaultShareID(self.s, u'share-id', 0) shareable = Shareable(store=self.s) sharing.shareItem(Shareable(store=self.s), shareID=u'not-the-share-id') share = sharing.getShare( self.s, sharing.getEveryoneRole(self.s), u'not-the-share-id') url = websharing.linkTo(share) self.assertEqual(str(url), '/users/right/not-the-share-id')
def test_defaultShareIDInteractionNoMatch(self): """ Verify that L{websharing.linkTo} explicitly includes a share ID in the URL if the ID of the share it is passed doesn't match the default. """ websharing.addDefaultShareID(self.s, u'share-id', 0) shareable = Shareable(store=self.s) sharing.shareItem(Shareable(store=self.s), shareID=u'not-the-share-id') share = sharing.getShare(self.s, sharing.getEveryoneRole(self.s), u'not-the-share-id') url = websharing.linkTo(share) self.assertEqual(str(url), '/users/right/not-the-share-id')
def test_defaultShareIDInteractionMatching(self): """ Verify that L{websharing.linkTo} does not explicitly include a share ID in the URL if the ID of the share it is passed matches the default. """ websharing.addDefaultShareID(self.s, u'share-id', 0) sharing.shareItem(Shareable(store=self.s), shareID=u'share-id') share = sharing.getShare( self.s, sharing.getEveryoneRole(self.s), u'share-id') url = websharing.linkTo(share) self.assertEqual(str(url), '/users/right/') # and if we call child() self.assertEqual(str(url.child('child')), '/users/right/share-id/child')
def test_defaultShareIDInteractionMatching(self): """ Verify that L{websharing.linkTo} does not explicitly include a share ID in the URL if the ID of the share it is passed matches the default. """ websharing.addDefaultShareID(self.s, u'share-id', 0) sharing.shareItem(Shareable(store=self.s), shareID=u'share-id') share = sharing.getShare(self.s, sharing.getEveryoneRole(self.s), u'share-id') url = websharing.linkTo(share) self.assertEqual(str(url), '/users/right/') # and if we call child() self.assertEqual(str(url.child('child')), '/users/right/share-id/child')
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 test_twoInterfacesTwoGroups(self): """ Verify that when an item is shared to two roles that a user is a member of, they will have access to both interfaces when it is retrieved with getShare. """ self.addSomeThings() us = sharing.getPrimaryRole(self.store, u'us', True) them = sharing.getPrimaryRole(self.store, u'them', True) self.bob.becomeMemberOf(us) self.bob.becomeMemberOf(them) it = PrivateThing(store=self.store, publicData=1234) sharing.shareItem(it, toRole=us, shareID=u'q', interfaces=[IPrivateThing]) sharing.shareItem(it, toRole=them, shareID=u'q', interfaces=[IReadOnly]) that = sharing.getShare(self.store, self.bob, u'q') self.assertEquals(that.retrieveSomeState(), 1234) that.mutateSomeState() self.assertEquals(that.retrieveSomeState(), 1239)
def setUp(self): self.siteStore = Store(filesdir=self.mktemp()) Mantissa().installSite(self.siteStore, u'example.com', u"", False) self.loginSystem = self.siteStore.findUnique(LoginSystem) userAccount = self.loginSystem.addAccount(u'alice', u'example.com', u'password') self.userStore = userAccount.avatars.open() product = Product( store=self.siteStore, types=[qual(hyperbola_model.HyperbolaPublicPresence)]) product.installProductOn(self.userStore) self.publicPresence = self.userStore.findUnique( hyperbola_model.HyperbolaPublicPresence) self.me = Role(store=self.userStore, externalID=u'*****@*****.**', description=u'foobar') self.you = Role(store=self.userStore, externalID=u'*****@*****.**', description=u'rad yo') blog = self.blog = hyperblurb.Blurb(store=self.userStore, title=u"Hello World", body=u"Hello World!~!!", author=self.me, hits=0, dateCreated=Time(), dateLastEdited=Time(), flavor=hyperblurb.FLAVOR.BLOG) blog.permitChildren(self.me, hyperblurb.FLAVOR.BLOG_POST, ihyperbola.ICommentable) blog.permitChildren(self.me, hyperblurb.FLAVOR.BLOG_COMMENT, ihyperbola.ICommentable) blog.permitChildren(self.you, hyperblurb.FLAVOR.BLOG_POST, ihyperbola.ICommentable) blog.permitChildren(self.you, hyperblurb.FLAVOR.BLOG_COMMENT, ihyperbola.ICommentable) shareItem(blog, getEveryoneRole(self.userStore), shareID=u'blog', interfaces=[ihyperbola.IViewable])
def _setBlurbPermissions(self, blurb, roleToPerms): # We want the shareIDs of the same post for different roles to all be # the same, so that users can trade URLs - since "None" will allocate a # new one, we just use this value for the first iteration... firstShareID = None for role, interfaceList in roleToPerms.items(): shareObj = shareItem(blurb, interfaces=interfaceList, toRole=role, shareID=firstShareID) # ... and then save the initially allocated shareID for each # subsequent share. firstShareID = shareObj.shareID return firstShareID
def setUp(self): """ Set up some state. """ self.s = Store() self.ls = LoginSystem(store=self.s) installOn(self.ls, self.s) acct = self.ls.addAccount( u'right', u'host', u'', verified=True, internal=True) acct.addLoginMethod( u'wrong', u'host', internal=False, verified=False) self.share = sharing.shareItem(self.ls, shareID=u'loginsystem')
def _shareAndGetProxy(self, blurb): """ Share C{blurb} to everyone and return a shared proxy @param blurb: a blurb @type blurb: L{hyperbola.hyperblurb.Blurb} @rtype: L{xmantissa.sharing.SharedProxy} """ share = sharing.shareItem(blurb) return sharing.getShare(self.userStore, sharing.getEveryoneRole(self.userStore), share.shareID)
def test_unauthorizedAccessNoShare(self): """ Verify that NoSuchShare is raised when getShare is called with a user who is not allowed to access a shared item. """ t = PrivateThing(store=self.store, publicData=345) theShare = sharing.shareItem(t, toName=u'*****@*****.**') self.assertRaises(sharing.NoSuchShare, sharing.getShare, self.store, sharing.getPrimaryRole(self.store, u'*****@*****.**'), theShare.shareID)
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 test_deletedOriginalNoShare(self): """ NoSuchShare should be raised when getShare is called with an item who is not allowed to access a shared item. """ t = PrivateThing(store=self.store, publicData=234) theShare = sharing.shareItem(t, toName=u'*****@*****.**') t.deleteFromStore() self.assertRaises(sharing.NoSuchShare, sharing.getShare, self.store, sharing.getPrimaryRole(self.store, u'*****@*****.**'), theShare.shareID)
def test_getShareProxyWithAdapter(self): """ When you share an item with an interface that has an adapter for that interface, the object that results from getShare should provide the interface by exposing the adapter rather than the original item. """ privateThing = PrivateThing(store=self.store) shared = sharing.shareItem(privateThing, toName=u'testshare', interfaces=[IExternal]) proxy = sharing.getShare(self.store, sharing.getPrimaryRole(self.store, u'testshare'), shared.shareID) proxy.doExternal() self.assertTrue(privateThing.externalized)
def setUp(self): """ Create an additional user for UserIndexPage, and share a single item with a shareID of the empty string. """ _UserIdentificationMixin.setUp(self) self.magicValue = 123412341234 self.signup.createUser(u'', self.username, self.domain, u'', u'username@internet') self.userStore = websharing._storeFromUsername(self.siteStore, self.username) self.shareable = Shareable(store=self.userStore, magicValue=self.magicValue) self.share = sharing.shareItem(self.shareable, shareID=self.shareID)
def _shareAndGetProxy(self, blurb): """ Share C{blurb} to everyone and return a shared proxy @param blurb: a blurb @type blurb: L{hyperbola.hyperblurb.Blurb} @rtype: L{xmantissa.sharing.SharedProxy} """ share = sharing.shareItem(blurb) return sharing.getShare( self.userStore, sharing.getEveryoneRole(self.userStore), share.shareID)
def setUp(self): """ Create an additional user for UserIndexPage, and share a single item with a shareID of the empty string. """ _UserIdentificationMixin.setUp(self) self.magicValue = 123412341234 self.signup.createUser( u'', self.username, self.domain, u'', u'username@internet') self.userStore = websharing._storeFromUsername( self.siteStore, self.username) self.shareable = Shareable(store=self.userStore, magicValue=self.magicValue) self.share = sharing.shareItem(self.shareable, shareID=self.shareID)
def setUp(self): """ Set up some state. """ self.s = Store() self.ls = LoginSystem(store=self.s) installOn(self.ls, self.s) acct = self.ls.addAccount(u'right', u'host', u'', verified=True, internal=True) acct.addLoginMethod(u'wrong', u'host', internal=False, verified=False) self.share = sharing.shareItem(self.ls, shareID=u'loginsystem')
def setUp(self): self.siteStore = Store(filesdir=self.mktemp()) Mantissa().installSite(self.siteStore, u'example.com', u"", False) self.loginSystem = self.siteStore.findUnique(LoginSystem) userAccount = self.loginSystem.addAccount( u'alice', u'example.com', u'password') self.userStore = userAccount.avatars.open() product = Product( store=self.siteStore, types=[qual(hyperbola_model.HyperbolaPublicPresence)]) product.installProductOn(self.userStore) self.publicPresence = self.userStore.findUnique( hyperbola_model.HyperbolaPublicPresence) self.me = Role(store=self.userStore, externalID=u'*****@*****.**', description=u'foobar') self.you = Role(store=self.userStore, externalID=u'*****@*****.**', description=u'rad yo') blog = self.blog = hyperblurb.Blurb( store=self.userStore, title=u"Hello World", body=u"Hello World!~!!", author=self.me, hits=0, dateCreated=Time(), dateLastEdited=Time(), flavor=hyperblurb.FLAVOR.BLOG) blog.permitChildren( self.me, hyperblurb.FLAVOR.BLOG_POST, ihyperbola.ICommentable) blog.permitChildren( self.me, hyperblurb.FLAVOR.BLOG_COMMENT, ihyperbola.ICommentable) blog.permitChildren( self.you, hyperblurb.FLAVOR.BLOG_POST, ihyperbola.ICommentable) blog.permitChildren( self.you, hyperblurb.FLAVOR.BLOG_COMMENT, ihyperbola.ICommentable) shareItem(blog, getEveryoneRole(self.userStore), shareID=u'blog', interfaces=[ihyperbola.IViewable])
def test_editLinkIfEditable(self): """ Test that L{hyperbola_view.BlogPostBlurbViewer} renders an 'edit' link if the underlying blurb is editable. """ post = self._makeBlurb(hyperblurb.FLAVOR.BLOG_POST) authorShareID = sharing.shareItem( post, toRole=sharing.getSelfRole(self.userStore), interfaces=[ihyperbola.IEditable]).shareID authorPostShare = sharing.getShare( self.userStore, sharing.getSelfRole(self.userStore), authorShareID) authorPostView = hyperbola_view.blurbViewDispatcher(authorPostShare) tag = tags.invisible(foo='bar') result = authorPostView.editLink(None, tag) self.assertIdentical(result, tag)
def test_shareAndAdapt(self): """ Verify that when an item is shared to a particular user with a particular interface, retrieving it for that user results in methods on the given interface being callable and other methods being restricted. """ t = PrivateThing(store=self.store, publicData=789) # Sanity check. self.failUnless(IPublicThing(t).isMethodAvailable()) shared = sharing.shareItem(t, toName=u'testshare', interfaces=[IReadOnly]) proxy = sharing.getShare(self.store, sharing.getPrimaryRole(self.store, u'testshare'), shared.shareID) self.assertFalse(IPublicThing(proxy).isMethodAvailable()) self.assertRaises(AttributeError, IPublicThing(proxy).callMethod)
def test_validShareID(self): """ Verify that we get the right resource and segments from L{websharing.SharingIndex.locateChild} if we access a valid share ID. """ websharing.addDefaultShareID(self.userStore, u'', 0) otherShareable = Shareable(store=self.userStore, magicValue=self.magicValue + 3) for _shareID in [u'foo', u'f\xf6\xf6']: otherShare = sharing.shareItem(otherShareable, shareID=_shareID) sharingIndex = self.makeSharingIndex(None) SEGMENTS = (_shareID.encode('utf-8'), 'bar') (res, segments) = sharingIndex.locateChild(None, SEGMENTS) self.assertEqual( res.wrappedFragment.showMagicValue(), self.magicValue + 3) self.assertEqual(segments, SEGMENTS[1:])
def test_validShareID(self): """ Verify that we get the right resource and segments from L{websharing.SharingIndex.locateChild} if we access a valid share ID. """ websharing.addDefaultShareID(self.userStore, u'', 0) otherShareable = Shareable(store=self.userStore, magicValue=self.magicValue + 3) for _shareID in [u'foo', u'f\xf6\xf6']: otherShare = sharing.shareItem(otherShareable, shareID=_shareID) sharingIndex = self.makeSharingIndex(None) SEGMENTS = (_shareID.encode('utf-8'), 'bar') (res, segments) = sharingIndex.locateChild(None, SEGMENTS) self.assertEqual(res.wrappedFragment.showMagicValue(), self.magicValue + 3) self.assertEqual(segments, SEGMENTS[1:])
def test_coalesceInheritedAdapters(self): """ If multiple interfaces that are part of the same inheritance hierarchy are specified, only the leaf interfaces should be adapted to, and provided for all interfaces it inherits from. """ extraPrivateThing = ExtraPrivateThing(store=self.store) role = sharing.getPrimaryRole(self.store, u'testshare') extraProxy = sharing.getShare( self.store, role, sharing.shareItem( extraPrivateThing, toRole=role, interfaces=[IExternal, IExtraExternal]).shareID) externalTag, externalObj = extraProxy.doExternal() extraExternalTag, extraExternalObj = extraProxy.doExternalExtra() self.assertIdentical(externalObj, extraExternalObj) self.assertEquals(externalTag, 'external') self.assertEquals(extraExternalTag, 'external-extra')
def test_simpleShare(self): """ Verify that an item which is shared with shareItem can be retrieved and manipulated with getShare. This is an older-style API, on its way to deprecation. """ t = PrivateThing(store=self.store, publicData=456) shareItemResult = self.assertWarns( PendingDeprecationWarning, "Use Role.shareItem() instead of sharing.shareItem().", __file__, lambda : sharing.shareItem(t, toName=u'*****@*****.**')) bob = sharing.getPrimaryRole(self.store, u'*****@*****.**') gotShare = self.assertWarns( PendingDeprecationWarning, "Use Role.getShare() instead of sharing.getShare().", __file__, lambda : sharing.getShare(self.store, bob, shareItemResult.shareID)) gotShare.mutateSomeState() self.assertEquals(t.publicData, 456 + 5)