Example #1
0
    def testDeleteNamespace(self):
        """L{Facade.deleteNamespace} deletes a L{Namespace}."""
        namespaces = NamespaceAPI(self.user)
        namespaces.create([(u'username/name', u'A namespace.')])
        self.store.commit()

        with login(u'username', self.user.objectID, self.transact) as session:
            yield self.facade.deleteNamespace(session, u'username/name')

        self.store.rollback()
        self.assertEqual({}, namespaces.get([u'username/name']))
Example #2
0
    def testDeleteNamespace(self):
        """L{Facade.deleteNamespace} deletes a L{Namespace}."""
        namespaces = NamespaceAPI(self.user)
        namespaces.create([(u'username/name', u'A namespace.')])
        self.store.commit()

        with login(u'username', self.user.objectID, self.transact) as session:
            yield self.facade.deleteNamespace(session, u'username/name')

        self.store.rollback()
        self.assertEqual({}, namespaces.get([u'username/name']))
Example #3
0
    def testGetNamespaceWithNamespaces(self):
        """
        L{Facade.getNamespace} includes child L{Namespace.name}s, if they were
        requested.
        """
        namespaces = NamespaceAPI(self.user)
        namespaces.create([(u'username/name', u'A namespace.')])
        self.store.commit()

        with login(u'username', self.user.objectID, self.transact) as session:
            result = yield self.facade.getNamespace(session, u'username',
                                                    False, True, False)
        self.assertEqual([u'name', u'private'], sorted(result.namespaces))
Example #4
0
    def testGetNamespaceWithDescription(self):
        """
        L{Facade.getNamespace} includes the L{Namespace.description}, if it
        was requested.
        """
        namespaces = NamespaceAPI(self.user)
        namespaces.create([(u'username/name', u'A namespace.')])
        self.store.commit()

        with login(u'username', self.user.objectID, self.transact) as session:
            result = yield self.facade.getNamespace(session, u'username/name',
                                                    True, False, False)
        self.assertEqual(u'A namespace.', result.description)
Example #5
0
    def testGetNamespaceWithDescription(self):
        """
        L{Facade.getNamespace} includes the L{Namespace.description}, if it
        was requested.
        """
        namespaces = NamespaceAPI(self.user)
        namespaces.create([(u'username/name', u'A namespace.')])
        self.store.commit()

        with login(u'username', self.user.objectID, self.transact) as session:
            result = yield self.facade.getNamespace(session, u'username/name',
                                                    True, False, False)
        self.assertEqual(u'A namespace.', result.description)
Example #6
0
    def testGetNamespaceWithNamespaces(self):
        """
        L{Facade.getNamespace} includes child L{Namespace.name}s, if they were
        requested.
        """
        namespaces = NamespaceAPI(self.user)
        namespaces.create([(u'username/name', u'A namespace.')])
        self.store.commit()

        with login(u'username', self.user.objectID, self.transact) as session:
            result = yield self.facade.getNamespace(session, u'username',
                                                    False, True, False)
        self.assertEqual([u'name', u'private'], sorted(result.namespaces))
Example #7
0
    def testDeleteNamespaceWithData(self):
        """
        L{Facade.deleteNamespace} raises a L{TNamespaceNotEmpty} exception if
        the requested L{Namespace} has child data such as other L{Namespace}s
        or L{Tag}s.
        """
        namespaces = NamespaceAPI(self.user)
        namespaces.create([(u'username/parent', u'A parent namespace.')])
        namespaces.create([(u'username/parent/child', u'A child namespace.')])
        self.store.commit()

        with login(u'username', self.user.objectID, self.transact) as session:
            deferred = self.facade.deleteNamespace(session, u'username/parent')
            yield self.assertFailure(deferred, TNamespaceNotEmpty)
Example #8
0
    def testDeleteNamespaceWithData(self):
        """
        L{Facade.deleteNamespace} raises a L{TNamespaceNotEmpty} exception if
        the requested L{Namespace} has child data such as other L{Namespace}s
        or L{Tag}s.
        """
        namespaces = NamespaceAPI(self.user)
        namespaces.create([(u'username/parent', u'A parent namespace.')])
        namespaces.create([(u'username/parent/child', u'A child namespace.')])
        self.store.commit()

        with login(u'username', self.user.objectID, self.transact) as session:
            deferred = self.facade.deleteNamespace(session, u'username/parent')
            yield self.assertFailure(deferred, TNamespaceNotEmpty)
Example #9
0
def apply(store):
    # Using model code in a patch isn't ideal, but writing this patch with
    # pure SQL will be heinous.
    for user in getUsers():
        if user.username in ('fluiddb', 'anon'):
            continue
        namespaces = NamespaceAPI(user)
        path = '%s/private' % user.username
        if not namespaces.get([path]):
            namespaces.create([
                (path, u'Private namespace for user %s' % user.username)
            ])
            namespace = getNamespaces(paths=[path]).one()
            permission = namespace.permission
            permission.set(Operation.LIST_NAMESPACE, Policy.CLOSED, [user.id])
Example #10
0
    def testDeleteIsDenied(self):
        """
        L{Facade.deleteNamespace} raises a L{TPathPermissionDenied} exception
        if the user doesn't have C{DELETE} permissions on the specified
        L{Namespace}.
        """
        namespaces = NamespaceAPI(self.user)
        namespaces.create([(u'username/test', u'description')])
        self.permissions.set([(u'username/test', Operation.DELETE_NAMESPACE,
                               Policy.OPEN, [u'username'])])
        self.store.commit()

        with login(u'username', self.user.objectID, self.transact) as session:
            deferred = self.facade.deleteNamespace(session, u'username/test')
            yield self.assertFailure(deferred, TPathPermissionDenied)
Example #11
0
    def testDeleteIsDenied(self):
        """
        L{Facade.deleteNamespace} raises a L{TPathPermissionDenied} exception
        if the user doesn't have C{DELETE} permissions on the specified
        L{Namespace}.
        """
        namespaces = NamespaceAPI(self.user)
        namespaces.create([(u'username/test', u'description')])
        self.permissions.set([(u'username/test', Operation.DELETE_NAMESPACE,
                               Policy.OPEN, [u'username'])])
        self.store.commit()

        with login(u'username', self.user.objectID, self.transact) as session:
            deferred = self.facade.deleteNamespace(session, u'username/test')
            yield self.assertFailure(deferred, TPathPermissionDenied)
Example #12
0
class NamespaceAPITest(NamespaceAPITestMixin, FluidinfoTestCase):

    resources = [('config', ConfigResource()), ('store', DatabaseResource())]

    def setUp(self):
        super(NamespaceAPITest, self).setUp()
        self.system = createSystemData()
        UserAPI().create([(u'username', u'password', u'User',
                           u'*****@*****.**')])
        self.user = getUser(u'username')
        self.namespaces = NamespaceAPI(self.user)
        self.permissions = PermissionAPI(self.user)

    def testCreateWithoutData(self):
        """
        L{NamespaceAPI.create} returns an empty C{list} if no L{Namespace}
        data is available.
        """
        result = self.namespaces.create([])
        self.assertEqual([], result)
        ignored = (self.system.namespaces.keys() +
                   [u'username', u'username/private'])
        result = self.store.find(Namespace, Not(Namespace.path.is_in(ignored)))
        self.assertIdentical(None, result.one())

    def testPermissionsAreNotCreatedIfCreateFails(self):
        """
        If L{NamespaceAPI.create} fails, no permissions should be created.
        """
        self.assertRaises(MalformedPathError, self.namespaces.create,
                          [(u'!!!!/test', u'description')])

        result = getNamespacePermissions([u'username/test'])
        self.assertTrue(result.is_empty())
Example #13
0
class NamespaceAPITest(NamespaceAPITestMixin, FluidinfoTestCase):

    resources = [('config', ConfigResource()),
                 ('store', DatabaseResource())]

    def setUp(self):
        super(NamespaceAPITest, self).setUp()
        self.system = createSystemData()
        UserAPI().create([(u'username', u'password', u'User',
                           u'*****@*****.**')])
        self.user = getUser(u'username')
        self.namespaces = NamespaceAPI(self.user)
        self.permissions = PermissionAPI(self.user)

    def testCreateWithoutData(self):
        """
        L{NamespaceAPI.create} returns an empty C{list} if no L{Namespace}
        data is available.
        """
        result = self.namespaces.create([])
        self.assertEqual([], result)
        ignored = (
            self.system.namespaces.keys() + [u'username', u'username/private'])
        result = self.store.find(Namespace, Not(Namespace.path.is_in(ignored)))
        self.assertIdentical(None, result.one())

    def testPermissionsAreNotCreatedIfCreateFails(self):
        """
        If L{NamespaceAPI.create} fails, no permissions should be created.
        """
        self.assertRaises(MalformedPathError, self.namespaces.create,
                          [(u'!!!!/test', u'description')])

        result = getNamespacePermissions([u'username/test'])
        self.assertTrue(result.is_empty())
Example #14
0
    def testUpdateNamespace(self):
        """
        L{Facade.updateNamespace} updates the description for an existing
        L{Namespace}.
        """
        namespaces = NamespaceAPI(self.user)
        namespaces.create([(u'username/name', u'A namespace.')])
        self.store.commit()

        with login(u'username', self.user.objectID, self.transact) as session:
            yield self.facade.updateNamespace(session, u'username/name',
                                              u'A new description.')

        self.store.rollback()
        result = namespaces.get([u'username/name'], withDescriptions=True)
        self.assertEqual(u'A new description.',
                         result[u'username/name']['description'])
Example #15
0
    def testUpdateNamespace(self):
        """
        L{Facade.updateNamespace} updates the description for an existing
        L{Namespace}.
        """
        namespaces = NamespaceAPI(self.user)
        namespaces.create([(u'username/name', u'A namespace.')])
        self.store.commit()

        with login(u'username', self.user.objectID, self.transact) as session:
            yield self.facade.updateNamespace(session, u'username/name',
                                              u'A new description.')

        self.store.rollback()
        result = namespaces.get([u'username/name'], withDescriptions=True)
        self.assertEqual(u'A new description.',
                         result[u'username/name']['description'])
Example #16
0
class CachingNamespaceAPI(object):
    """The public API to cached namespace-related logic in the model.

    @param user: The L{User} to perform operations on behalf of.
    """
    def __init__(self, user):
        self._api = NamespaceAPI(user, factory=CachingAPIFactory())

    def create(self, values):
        """See L{NamespaceAPI.create}."""
        return self._api.create(values)

    def delete(self, paths):
        """See L{NamespaceAPI.delete}.

        Permissions for deleted L{Namespace}s are removed from the cache.
        """
        if isgenerator(paths):
            paths = list(paths)
        cache = PermissionCache()
        cache.clearNamespacePermissions(paths)
        return self._api.delete(paths)

    def get(self,
            paths,
            withDescriptions=None,
            withNamespaces=None,
            withTags=None):
        """See L{NamespaceAPI.get}."""
        return self._api.get(paths,
                             withDescriptions=withDescriptions,
                             withNamespaces=withNamespaces,
                             withTags=withTags)

    def set(self, values):
        """Set or update L{Namespace}s.

        @param values: A C{dict} mapping L{Namespace.path}s to descriptions.
        @return: A C{list} of C{(objectID, Namespace.path)} 2-tuples
            representing the L{Namespace}s that were updated.
        """
        return self._api.set(values)
Example #17
0
class CachingNamespaceAPI(object):
    """The public API to cached namespace-related logic in the model.

    @param user: The L{User} to perform operations on behalf of.
    """

    def __init__(self, user):
        self._api = NamespaceAPI(user, factory=CachingAPIFactory())

    def create(self, values):
        """See L{NamespaceAPI.create}."""
        return self._api.create(values)

    def delete(self, paths):
        """See L{NamespaceAPI.delete}.

        Permissions for deleted L{Namespace}s are removed from the cache.
        """
        if isgenerator(paths):
            paths = list(paths)
        cache = PermissionCache()
        cache.clearNamespacePermissions(paths)
        return self._api.delete(paths)

    def get(self, paths, withDescriptions=None, withNamespaces=None,
            withTags=None):
        """See L{NamespaceAPI.get}."""
        return self._api.get(paths, withDescriptions=withDescriptions,
                             withNamespaces=withNamespaces, withTags=withTags)

    def set(self, values):
        """Set or update L{Namespace}s.

        @param values: A C{dict} mapping L{Namespace.path}s to descriptions.
        @return: A C{list} of C{(objectID, Namespace.path)} 2-tuples
            representing the L{Namespace}s that were updated.
        """
        return self._api.set(values)