Ejemplo n.º 1
0
class DatasetManagerTestCase( BaseTestCase ):

    def set_up_managers( self ):
        super( DatasetManagerTestCase, self ).set_up_managers()
        self.dataset_mgr = DatasetManager( self.app )

    def test_create( self ):
        self.log( "should be able to create a new Dataset" )
        dataset1 = self.dataset_mgr.create( self.trans )
        self.assertIsInstance( dataset1, model.Dataset )
        self.assertEqual( dataset1, self.trans.sa_session.query( model.Dataset ).get( dataset1.id ) )

    def test_base( self ):
        dataset1 = self.dataset_mgr.create( self.trans )
        dataset2 = self.dataset_mgr.create( self.trans )

        self.log( "should be able to query" )
        datasets = self.trans.sa_session.query( model.Dataset ).all()
        self.assertEqual( self.dataset_mgr.list( self.trans ), datasets )
        self.assertEqual( self.dataset_mgr.one( self.trans, filters=( model.Dataset.id == dataset1.id ) ), dataset1 )
        self.assertEqual( self.dataset_mgr.by_id( self.trans, dataset1.id ), dataset1 )
        self.assertEqual( self.dataset_mgr.by_ids( self.trans, [ dataset2.id, dataset1.id ] ), [ dataset2, dataset1 ] )

        self.log( "should be able to limit and offset" )
        self.assertEqual( self.dataset_mgr.list( self.trans, limit=1 ), datasets[0:1] )
        self.assertEqual( self.dataset_mgr.list( self.trans, offset=1 ), datasets[1:] )
        self.assertEqual( self.dataset_mgr.list( self.trans, limit=1, offset=1 ), datasets[1:2] )

        self.assertEqual( self.dataset_mgr.list( self.trans, limit=0 ), [] )
        self.assertEqual( self.dataset_mgr.list( self.trans, offset=3 ), [] )

        self.log( "should be able to order" )
        self.assertEqual( self.dataset_mgr.list( self.trans, order_by=sqlalchemy.desc( model.Dataset.create_time ) ),
            [ dataset2, dataset1 ] )

    def test_delete( self ):
        item1 = self.dataset_mgr.create( self.trans )

        self.log( "should be able to delete and undelete an hda" )
        self.assertFalse( item1.deleted )
        self.assertEqual( self.dataset_mgr.delete( self.trans, item1 ), item1 )
        self.assertTrue( item1.deleted )
        self.assertEqual( self.dataset_mgr.undelete( self.trans, item1 ), item1 )
        self.assertFalse( item1.deleted )

    def test_purge_allowed( self ):
        self.trans.app.config.allow_user_dataset_purge = True
        item1 = self.dataset_mgr.create( self.trans )

        self.log( "should purge an hda if config does allow" )
        self.assertFalse( item1.purged )
        self.assertEqual( self.dataset_mgr.purge( self.trans, item1 ), item1 )
        self.assertTrue( item1.purged )

    def test_purge_not_allowed( self ):
        self.trans.app.config.allow_user_dataset_purge = False
        item1 = self.dataset_mgr.create( self.trans )

        self.log( "should raise an error when purging an hda if config does not allow" )
        self.assertFalse( item1.purged )
        self.assertRaises( exceptions.ConfigDoesNotAllowException, self.dataset_mgr.purge, self.trans, item1 )
        self.assertFalse( item1.purged )

    ##TODO: I'm unclear as to how these work, so I'm kicking this down the road a bit....
    #def test_access_permission( self ):
    #    owner = self.user_mgr.create( self.trans, **user2_data )
    #    dataset = self.dataset_mgr.create( self.trans )
    #    # giving one user access permission makes it non-public, removing access for anyone else
    #    self.dataset_mgr.give_access_permission( self.trans, dataset, owner )
    #
    #    user3 = self.user_mgr.create( self.trans, **user3_data )
    #    user4 = self.user_mgr.create( self.trans,
    #        email='[email protected]', username='******', password=default_password )
    #
    #    self.assertTrue( self.dataset_mgr.has_access_permission( self.trans, dataset, owner ) )
    #    self.assertFalse( self.dataset_mgr.has_access_permission( self.trans, dataset, user3 ) )
    #    self.assertFalse( self.dataset_mgr.has_access_permission( self.trans, dataset, user4 ) )
    #
    #    # should be able to progressively add more roles without removing the previous
    #    self.dataset_mgr.give_access_permission( self.trans, dataset, user3 )
    #    self.assertTrue( self.dataset_mgr.has_access_permission( self.trans, dataset, user3 ) )
    #    self.assertTrue( self.dataset_mgr.has_access_permission( self.trans, dataset, owner ) )
    #    self.assertFalse( self.dataset_mgr.has_access_permission( self.trans, dataset, user4 ) )
    #
    #    #self.assertTrue( self.dataset_mgr.is_accessible( self.trans, dataset, owner ) )
    #    #self.assertFalse( self.dataset_mgr.is_accessible( self.trans, dataset, non_owner ) )

    def test_accessible( self ):
        owner = self.user_mgr.create( self.trans, **user2_data )
        non_owner = self.user_mgr.create( self.trans, **user3_data )

        dataset = self.dataset_mgr.create( self.trans )

        self.log( "(by default, dataset permissions are lax) should be accessible to all" )
        for user in self.user_mgr.list( self.trans ):
            self.assertTrue( self.dataset_mgr.is_accessible( self.trans, dataset, user ) )
Ejemplo n.º 2
0
class DatasetManagerTestCase(BaseTestCase):
    def set_up_managers(self):
        super(DatasetManagerTestCase, self).set_up_managers()
        self.dataset_manager = DatasetManager(self.app)

    def test_create(self):
        self.log("should be able to create a new Dataset")
        dataset1 = self.dataset_manager.create()
        self.assertIsInstance(dataset1, model.Dataset)
        self.assertEqual(
            dataset1,
            self.trans.sa_session.query(model.Dataset).get(dataset1.id))

    def test_base(self):
        dataset1 = self.dataset_manager.create()
        dataset2 = self.dataset_manager.create()

        self.log("should be able to query")
        datasets = self.trans.sa_session.query(model.Dataset).all()
        self.assertEqual(self.dataset_manager.list(), datasets)
        self.assertEqual(
            self.dataset_manager.one(
                filters=(model.Dataset.id == dataset1.id)), dataset1)
        self.assertEqual(self.dataset_manager.by_id(dataset1.id), dataset1)
        self.assertEqual(
            self.dataset_manager.by_ids([dataset2.id, dataset1.id]),
            [dataset2, dataset1])

        self.log("should be able to limit and offset")
        self.assertEqual(self.dataset_manager.list(limit=1), datasets[0:1])
        self.assertEqual(self.dataset_manager.list(offset=1), datasets[1:])
        self.assertEqual(self.dataset_manager.list(limit=1, offset=1),
                         datasets[1:2])

        self.assertEqual(self.dataset_manager.list(limit=0), [])
        self.assertEqual(self.dataset_manager.list(offset=3), [])

        self.log("should be able to order")
        self.assertEqual(
            self.dataset_manager.list(
                order_by=sqlalchemy.desc(model.Dataset.create_time)),
            [dataset2, dataset1])

    def test_delete(self):
        item1 = self.dataset_manager.create()

        self.log("should be able to delete and undelete a dataset")
        self.assertFalse(item1.deleted)
        self.assertEqual(self.dataset_manager.delete(item1), item1)
        self.assertTrue(item1.deleted)
        self.assertEqual(self.dataset_manager.undelete(item1), item1)
        self.assertFalse(item1.deleted)

    def test_purge_allowed(self):
        self.trans.app.config.allow_user_dataset_purge = True
        item1 = self.dataset_manager.create()

        self.log("should purge a dataset if config does allow")
        self.assertFalse(item1.purged)
        self.assertEqual(self.dataset_manager.purge(item1), item1)
        self.assertTrue(item1.purged)

        self.log("should delete a dataset when purging")
        self.assertTrue(item1.deleted)

    def test_purge_not_allowed(self):
        self.trans.app.config.allow_user_dataset_purge = False
        item1 = self.dataset_manager.create()

        self.log(
            "should raise an error when purging a dataset if config does not allow"
        )
        self.assertFalse(item1.purged)
        self.assertRaises(exceptions.ConfigDoesNotAllowException,
                          self.dataset_manager.purge, item1)
        self.assertFalse(item1.purged)

    def test_create_with_no_permissions(self):
        self.log(
            "should be able to create a new Dataset without any permissions")
        dataset = self.dataset_manager.create()

        permissions = self.dataset_manager.permissions.get(dataset)
        self.assertIsInstance(permissions, tuple)
        self.assertEqual(len(permissions), 2)
        manage_permissions, access_permissions = permissions
        self.assertEqual(manage_permissions, [])
        self.assertEqual(access_permissions, [])

        user3 = self.user_manager.create(**user3_data)
        self.log(
            "a dataset without permissions shouldn't be manageable to just anyone"
        )
        self.assertFalse(
            self.dataset_manager.permissions.manage.is_permitted(
                dataset, user3))
        self.log("a dataset without permissions should be accessible")
        self.assertTrue(
            self.dataset_manager.permissions.access.is_permitted(
                dataset, user3))

    def test_create_public_dataset(self):
        self.log(
            "should be able to create a new Dataset and give it some permissions that actually, you know, "
            "might work if there's any justice in this universe")
        owner = self.user_manager.create(**user2_data)
        owner_private_role = self.user_manager.private_role(owner)
        dataset = self.dataset_manager.create(
            manage_roles=[owner_private_role])

        permissions = self.dataset_manager.permissions.get(dataset)
        self.assertIsInstance(permissions, tuple)
        self.assertEqual(len(permissions), 2)
        manage_permissions, access_permissions = permissions
        self.assertIsInstance(manage_permissions, list)
        self.assertIsInstance(manage_permissions[0], model.DatasetPermissions)
        self.assertEqual(access_permissions, [])

        user3 = self.user_manager.create(**user3_data)
        self.log("a public dataset should be manageable to it's owner")
        self.assertTrue(
            self.dataset_manager.permissions.manage.is_permitted(
                dataset, owner))
        self.log("a public dataset shouldn't be manageable to just anyone")
        self.assertFalse(
            self.dataset_manager.permissions.manage.is_permitted(
                dataset, user3))
        self.log("a public dataset should be accessible")
        self.assertTrue(
            self.dataset_manager.permissions.access.is_permitted(
                dataset, user3))

    def test_create_private_dataset(self):
        self.log(
            "should be able to create a new Dataset and give it private permissions"
        )
        owner = self.user_manager.create(**user2_data)
        owner_private_role = self.user_manager.private_role(owner)
        dataset = self.dataset_manager.create(
            manage_roles=[owner_private_role],
            access_roles=[owner_private_role])

        permissions = self.dataset_manager.permissions.get(dataset)
        self.assertIsInstance(permissions, tuple)
        self.assertEqual(len(permissions), 2)
        manage_permissions, access_permissions = permissions
        self.assertIsInstance(manage_permissions, list)
        self.assertIsInstance(manage_permissions[0], model.DatasetPermissions)
        self.assertIsInstance(access_permissions, list)
        self.assertIsInstance(access_permissions[0], model.DatasetPermissions)

        self.log("a private dataset should be manageable by it's owner")
        self.assertTrue(
            self.dataset_manager.permissions.manage.is_permitted(
                dataset, owner))
        self.log("a private dataset should be accessible to it's owner")
        self.assertTrue(
            self.dataset_manager.permissions.access.is_permitted(
                dataset, owner))

        user3 = self.user_manager.create(**user3_data)
        self.log("a private dataset shouldn't be manageable to just anyone")
        self.assertFalse(
            self.dataset_manager.permissions.manage.is_permitted(
                dataset, user3))
        self.log("a private dataset shouldn't be accessible to just anyone")
        self.assertFalse(
            self.dataset_manager.permissions.access.is_permitted(
                dataset, user3))
class DatasetManagerTestCase( BaseTestCase ):

    def set_up_managers( self ):
        super( DatasetManagerTestCase, self ).set_up_managers()
        self.dataset_manager = DatasetManager( self.app )

    def test_create( self ):
        self.log( "should be able to create a new Dataset" )
        dataset1 = self.dataset_manager.create()
        self.assertIsInstance( dataset1, model.Dataset )
        self.assertEqual( dataset1, self.trans.sa_session.query( model.Dataset ).get( dataset1.id ) )

    def test_base( self ):
        dataset1 = self.dataset_manager.create()
        dataset2 = self.dataset_manager.create()

        self.log( "should be able to query" )
        datasets = self.trans.sa_session.query( model.Dataset ).all()
        self.assertEqual( self.dataset_manager.list(), datasets )
        self.assertEqual( self.dataset_manager.one( filters=( model.Dataset.id == dataset1.id ) ), dataset1 )
        self.assertEqual( self.dataset_manager.by_id( dataset1.id ), dataset1 )
        self.assertEqual( self.dataset_manager.by_ids( [ dataset2.id, dataset1.id ] ), [ dataset2, dataset1 ] )

        self.log( "should be able to limit and offset" )
        self.assertEqual( self.dataset_manager.list( limit=1 ), datasets[0:1] )
        self.assertEqual( self.dataset_manager.list( offset=1 ), datasets[1:] )
        self.assertEqual( self.dataset_manager.list( limit=1, offset=1 ), datasets[1:2] )

        self.assertEqual( self.dataset_manager.list( limit=0 ), [] )
        self.assertEqual( self.dataset_manager.list( offset=3 ), [] )

        self.log( "should be able to order" )
        self.assertEqual( self.dataset_manager.list( order_by=sqlalchemy.desc( model.Dataset.create_time ) ),
            [ dataset2, dataset1 ] )

    def test_delete( self ):
        item1 = self.dataset_manager.create()

        self.log( "should be able to delete and undelete a dataset" )
        self.assertFalse( item1.deleted )
        self.assertEqual( self.dataset_manager.delete( item1 ), item1 )
        self.assertTrue( item1.deleted )
        self.assertEqual( self.dataset_manager.undelete( item1 ), item1 )
        self.assertFalse( item1.deleted )

    def test_purge_allowed( self ):
        self.trans.app.config.allow_user_dataset_purge = True
        item1 = self.dataset_manager.create()

        self.log( "should purge a dataset if config does allow" )
        self.assertFalse( item1.purged )
        self.assertEqual( self.dataset_manager.purge( item1 ), item1 )
        self.assertTrue( item1.purged )

        self.log( "should delete a dataset when purging" )
        self.assertTrue( item1.deleted )

    def test_purge_not_allowed( self ):
        self.trans.app.config.allow_user_dataset_purge = False
        item1 = self.dataset_manager.create()

        self.log( "should raise an error when purging a dataset if config does not allow" )
        self.assertFalse( item1.purged )
        self.assertRaises( exceptions.ConfigDoesNotAllowException, self.dataset_manager.purge, item1 )
        self.assertFalse( item1.purged )

    def test_create_with_no_permissions( self ):
        self.log( "should be able to create a new Dataset without any permissions" )
        dataset = self.dataset_manager.create()

        permissions = self.dataset_manager.permissions.get( dataset )
        self.assertIsInstance( permissions, tuple )
        self.assertEqual( len( permissions ), 2 )
        manage_permissions, access_permissions = permissions
        self.assertEqual( manage_permissions, [] )
        self.assertEqual( access_permissions, [] )

        user3 = self.user_manager.create( **user3_data )
        self.log( "a dataset without permissions shouldn't be manageable to just anyone" )
        self.assertFalse( self.dataset_manager.permissions.manage.is_permitted( dataset, user3 ) )
        self.log( "a dataset without permissions should be accessible" )
        self.assertTrue( self.dataset_manager.permissions.access.is_permitted( dataset, user3 ) )

    def test_create_public_dataset( self ):
        self.log( "should be able to create a new Dataset and give it some permissions that actually, you know, "
            "might work if there's any justice in this universe" )
        owner = self.user_manager.create( **user2_data )
        owner_private_role = self.user_manager.private_role( owner )
        dataset = self.dataset_manager.create( manage_roles=[ owner_private_role ] )

        permissions = self.dataset_manager.permissions.get( dataset )
        self.assertIsInstance( permissions, tuple )
        self.assertEqual( len( permissions ), 2 )
        manage_permissions, access_permissions = permissions
        self.assertIsInstance( manage_permissions, list )
        self.assertIsInstance( manage_permissions[0], model.DatasetPermissions )
        self.assertEqual( access_permissions, [] )

        user3 = self.user_manager.create( **user3_data )
        self.log( "a public dataset should be manageable to it's owner" )
        self.assertTrue( self.dataset_manager.permissions.manage.is_permitted( dataset, owner ) )
        self.log( "a public dataset shouldn't be manageable to just anyone" )
        self.assertFalse( self.dataset_manager.permissions.manage.is_permitted( dataset, user3 ) )
        self.log( "a public dataset should be accessible" )
        self.assertTrue( self.dataset_manager.permissions.access.is_permitted( dataset, user3 ) )

    def test_create_private_dataset( self ):
        self.log( "should be able to create a new Dataset and give it private permissions" )
        owner = self.user_manager.create( **user2_data )
        owner_private_role = self.user_manager.private_role( owner )
        dataset = self.dataset_manager.create(
            manage_roles=[ owner_private_role ], access_roles=[ owner_private_role ] )

        permissions = self.dataset_manager.permissions.get( dataset )
        self.assertIsInstance( permissions, tuple )
        self.assertEqual( len( permissions ), 2 )
        manage_permissions, access_permissions = permissions
        self.assertIsInstance( manage_permissions, list )
        self.assertIsInstance( manage_permissions[0], model.DatasetPermissions )
        self.assertIsInstance( access_permissions, list )
        self.assertIsInstance( access_permissions[0], model.DatasetPermissions )

        self.log( "a private dataset should be manageable by it's owner" )
        self.assertTrue( self.dataset_manager.permissions.manage.is_permitted( dataset, owner ) )
        self.log( "a private dataset should be accessible to it's owner" )
        self.assertTrue( self.dataset_manager.permissions.access.is_permitted( dataset, owner ) )

        user3 = self.user_manager.create( **user3_data )
        self.log( "a private dataset shouldn't be manageable to just anyone" )
        self.assertFalse( self.dataset_manager.permissions.manage.is_permitted( dataset, user3 ) )
        self.log( "a private dataset shouldn't be accessible to just anyone" )
        self.assertFalse( self.dataset_manager.permissions.access.is_permitted( dataset, user3 ) )