Exemple #1
0
class DatasetSerializerTestCase(BaseTestCase):
    def set_up_managers(self):
        super(DatasetSerializerTestCase, self).set_up_managers()
        self.dataset_manager = DatasetManager(self.app)
        self.dataset_serializer = DatasetSerializer(self.app)
        self.role_manager = RoleManager(self.app)

    def test_views(self):
        dataset = self.dataset_manager.create()

        self.log('should have a summary view')
        summary_view = self.dataset_serializer.serialize_to_view(
            dataset, view='summary')
        self.assertKeys(summary_view, self.dataset_serializer.views['summary'])

        self.log('should have the summary view as default view')
        self.dataset_serializer.serialize_to_view(dataset,
                                                  default_view='summary')
        self.assertKeys(summary_view, self.dataset_serializer.views['summary'])

        self.log('should have a serializer for all serializable keys')
        for key in self.dataset_serializer.serializable_keyset:
            instantiated_attribute = getattr(dataset, key, None)
            if not ((key in self.dataset_serializer.serializers) or
                    (isinstance(instantiated_attribute,
                                self.TYPES_NEEDING_NO_SERIALIZERS))):
                self.fail('no serializer for: %s (%s)' %
                          (key, instantiated_attribute))
        else:
            self.assertTrue(True, 'all serializable keys have a serializer')

    def test_views_and_keys(self):
        dataset = self.dataset_manager.create()

        self.log('should be able to use keys with views')
        serialized = self.dataset_serializer.serialize_to_view(
            dataset,
            # file_name is exposed using app.config.expose_dataset_path = True
            view='summary',
            keys=['file_name'])
        self.assertKeys(
            serialized,
            self.dataset_serializer.views['summary'] + ['file_name'])

        self.log('should be able to use keys on their own')
        serialized = self.dataset_serializer.serialize_to_view(
            dataset, keys=['purgable', 'file_size'])
        self.assertKeys(serialized, ['purgable', 'file_size'])

    def test_serialize_permissions(self):
        dataset = self.dataset_manager.create()
        who_manages = self.user_manager.create(**user2_data)
        self.dataset_manager.permissions.manage.grant(dataset, who_manages)

        self.log(
            'serialized permissions should be returned for the user who can manage and be well formed'
        )
        permissions = self.dataset_serializer.serialize_permissions(
            dataset, 'perms', user=who_manages)
        self.assertIsInstance(permissions, dict)
        self.assertKeys(permissions, ['manage', 'access'])
        self.assertIsInstance(permissions['manage'], list)
        self.assertIsInstance(permissions['access'], list)

        manage_perms = permissions['manage']
        self.assertTrue(len(manage_perms) == 1)
        role_id = manage_perms[0]
        self.assertEncodedId(role_id)
        role_id = self.app.security.decode_id(role_id)
        role = self.role_manager.get(self.trans, role_id)
        self.assertTrue(
            who_manages in [user_role.user for user_role in role.users])
        # wat

        self.log('permissions should be not returned for non-managing users')
        not_my_supervisor = self.user_manager.create(**user3_data)
        self.assertRaises(SkipAttribute,
                          self.dataset_serializer.serialize_permissions,
                          dataset,
                          'perms',
                          user=not_my_supervisor)

        self.log('permissions should not be returned for anon users')
        self.assertRaises(SkipAttribute,
                          self.dataset_serializer.serialize_permissions,
                          dataset,
                          'perms',
                          user=None)

    def test_serializers(self):
        # self.user_manager.create( **user2_data )
        dataset = self.dataset_manager.create()
        all_keys = list(self.dataset_serializer.serializable_keyset)
        serialized = self.dataset_serializer.serialize(dataset, all_keys)

        self.log('everything serialized should be of the proper type')
        self.assertEncodedId(serialized['id'])
        self.assertDate(serialized['create_time'])
        self.assertDate(serialized['update_time'])

        self.assertUUID(serialized['uuid'])
        self.assertIsInstance(serialized['state'], basestring)
        self.assertIsInstance(serialized['deleted'], bool)
        self.assertIsInstance(serialized['purged'], bool)
        self.assertIsInstance(serialized['purgable'], bool)

        # # TODO: no great way to do these with mocked dataset
        # self.assertIsInstance( serialized[ 'file_size' ], int )
        # self.assertIsInstance( serialized[ 'total_size' ], int )

        self.log('serialized should jsonify well')
        self.assertIsJsonifyable(serialized)
class DatasetSerializerTestCase( BaseTestCase ):

    def set_up_managers( self ):
        super( DatasetSerializerTestCase, self ).set_up_managers()
        self.dataset_manager = DatasetManager( self.app )
        self.dataset_serializer = DatasetSerializer( self.app )
        self.role_manager = RoleManager( self.app )

    def test_views( self ):
        dataset = self.dataset_manager.create()

        self.log( 'should have a summary view' )
        summary_view = self.dataset_serializer.serialize_to_view( dataset, view='summary' )
        self.assertKeys( summary_view, self.dataset_serializer.views[ 'summary' ] )

        self.log( 'should have the summary view as default view' )
        self.dataset_serializer.serialize_to_view( dataset, default_view='summary' )
        self.assertKeys( summary_view, self.dataset_serializer.views[ 'summary' ] )

        self.log( 'should have a serializer for all serializable keys' )
        for key in self.dataset_serializer.serializable_keyset:
            instantiated_attribute = getattr( dataset, key, None )
            if not ( ( key in self.dataset_serializer.serializers ) or
                     ( isinstance( instantiated_attribute, self.TYPES_NEEDING_NO_SERIALIZERS ) ) ):
                self.fail( 'no serializer for: %s (%s)' % ( key, instantiated_attribute ) )
        else:
            self.assertTrue( True, 'all serializable keys have a serializer' )

    def test_views_and_keys( self ):
        dataset = self.dataset_manager.create()

        self.log( 'should be able to use keys with views' )
        serialized = self.dataset_serializer.serialize_to_view( dataset,
            # file_name is exposed using app.config.expose_dataset_path = True
            view='summary', keys=[ 'file_name' ] )
        self.assertKeys( serialized,
            self.dataset_serializer.views[ 'summary' ] + [ 'file_name' ] )

        self.log( 'should be able to use keys on their own' )
        serialized = self.dataset_serializer.serialize_to_view( dataset,
            keys=[ 'purgable', 'file_size' ] )
        self.assertKeys( serialized, [ 'purgable', 'file_size' ] )

    def test_serialize_permissions( self ):
        dataset = self.dataset_manager.create()
        who_manages = self.user_manager.create( **user2_data )
        self.dataset_manager.permissions.manage.grant( dataset, who_manages )

        self.log( 'serialized permissions should be returned for the user who can manage and be well formed' )
        permissions = self.dataset_serializer.serialize_permissions( dataset, 'perms', user=who_manages )
        self.assertIsInstance( permissions, dict )
        self.assertKeys( permissions, [ 'manage', 'access' ] )
        self.assertIsInstance( permissions[ 'manage' ], list )
        self.assertIsInstance( permissions[ 'access' ], list )

        manage_perms = permissions[ 'manage' ]
        self.assertTrue( len( manage_perms ) == 1 )
        role_id = manage_perms[0]
        self.assertEncodedId( role_id )
        role_id = self.app.security.decode_id( role_id )
        role = self.role_manager.get( self.trans, role_id )
        self.assertTrue( who_manages in [ user_role.user for user_role in role.users ])
        # wat

        self.log( 'permissions should be not returned for non-managing users' )
        not_my_supervisor = self.user_manager.create( **user3_data )
        self.assertRaises( SkipAttribute, self.dataset_serializer.serialize_permissions,
            dataset, 'perms', user=not_my_supervisor )

        self.log( 'permissions should not be returned for anon users' )
        self.assertRaises( SkipAttribute, self.dataset_serializer.serialize_permissions,
            dataset, 'perms', user=None )

    def test_serializers( self ):
        # self.user_manager.create( **user2_data )
        dataset = self.dataset_manager.create()
        all_keys = list( self.dataset_serializer.serializable_keyset )
        serialized = self.dataset_serializer.serialize( dataset, all_keys )

        self.log( 'everything serialized should be of the proper type' )
        self.assertEncodedId( serialized[ 'id' ] )
        self.assertDate( serialized[ 'create_time' ] )
        self.assertDate( serialized[ 'update_time' ] )

        self.assertUUID( serialized[ 'uuid' ] )
        self.assertIsInstance( serialized[ 'state' ], basestring )
        self.assertIsInstance( serialized[ 'deleted' ], bool )
        self.assertIsInstance( serialized[ 'purged' ], bool )
        self.assertIsInstance( serialized[ 'purgable' ], bool )

        # # TODO: no great way to do these with mocked dataset
        # self.assertIsInstance( serialized[ 'file_size' ], int )
        # self.assertIsInstance( serialized[ 'total_size' ], int )

        self.log( 'serialized should jsonify well' )
        self.assertIsJsonifyable( serialized )
Exemple #3
0
class DatasetSerializerTestCase(BaseTestCase):
    def set_up_managers(self):
        super(DatasetSerializerTestCase, self).set_up_managers()
        self.dataset_manager = DatasetManager(self.app)
        self.dataset_serializer = DatasetSerializer(self.app)

    def test_views(self):
        dataset = self.dataset_manager.create()

        self.log('should have a summary view')
        summary_view = self.dataset_serializer.serialize_to_view(
            dataset, view='summary')
        self.assertKeys(summary_view, self.dataset_serializer.views['summary'])

        self.log('should have the summary view as default view')
        default_view = self.dataset_serializer.serialize_to_view(
            dataset, default_view='summary')
        self.assertKeys(summary_view, self.dataset_serializer.views['summary'])

        self.log('should have a serializer for all serializable keys')
        for key in self.dataset_serializer.serializable_keyset:
            instantiated_attribute = getattr(dataset, key, None)
            if not ((key in self.dataset_serializer.serializers) or
                    (isinstance(instantiated_attribute,
                                self.TYPES_NEEDING_NO_SERIALIZERS))):
                self.fail('no serializer for: %s (%s)' %
                          (key, instantiated_attribute))
        else:
            self.assertTrue(True, 'all serializable keys have a serializer')

    def test_views_and_keys(self):
        dataset = self.dataset_manager.create()

        self.log('should be able to use keys with views')
        serialized = self.dataset_serializer.serialize_to_view(
            dataset, view='summary', keys=['permissions'])
        self.assertKeys(
            serialized,
            self.dataset_serializer.views['summary'] + ['permissions'])

        self.log('should be able to use keys on their own')
        serialized = self.dataset_serializer.serialize_to_view(
            dataset, keys=['purgable', 'file_size'])
        self.assertKeys(serialized, ['purgable', 'file_size'])

    def test_serialize_permissions(self):
        dataset = self.dataset_manager.create()
        self.log('serialized permissions should be well formed')

    def test_serializers(self):
        user2 = self.user_manager.create(**user2_data)
        dataset = self.dataset_manager.create()
        all_keys = list(self.dataset_serializer.serializable_keyset)
        serialized = self.dataset_serializer.serialize(dataset, all_keys)

        self.log('everything serialized should be of the proper type')
        self.assertEncodedId(serialized['id'])
        self.assertDate(serialized['create_time'])
        self.assertDate(serialized['update_time'])

        self.assertUUID(serialized['uuid'])
        self.assertIsInstance(serialized['state'], basestring)
        self.assertIsInstance(serialized['deleted'], bool)
        self.assertIsInstance(serialized['purged'], bool)
        self.assertIsInstance(serialized['purgable'], bool)

        # # TODO: no great way to do these with mocked dataset
        # self.assertIsInstance( serialized[ 'file_size' ], int )
        # self.assertIsInstance( serialized[ 'total_size' ], int )

        self.log('serialized should jsonify well')
        self.assertIsJsonifyable(serialized)
class DatasetSerializerTestCase( BaseTestCase ):

    def set_up_managers( self ):
        super( DatasetSerializerTestCase, self ).set_up_managers()
        self.dataset_manager = DatasetManager( self.app )
        self.dataset_serializer = DatasetSerializer( self.app )

    def test_views( self ):
        dataset = self.dataset_manager.create()

        self.log( 'should have a summary view' )
        summary_view = self.dataset_serializer.serialize_to_view( dataset, view='summary' )
        self.assertKeys( summary_view, self.dataset_serializer.views[ 'summary' ] )

        self.log( 'should have the summary view as default view' )
        default_view = self.dataset_serializer.serialize_to_view( dataset, default_view='summary' )
        self.assertKeys( summary_view, self.dataset_serializer.views[ 'summary' ] )

        self.log( 'should have a serializer for all serializable keys' )
        for key in self.dataset_serializer.serializable_keyset:
            instantiated_attribute = getattr( dataset, key, None )
            if not ( ( key in self.dataset_serializer.serializers )
                  or ( isinstance( instantiated_attribute, self.TYPES_NEEDING_NO_SERIALIZERS ) ) ):
                self.fail( 'no serializer for: %s (%s)' % ( key, instantiated_attribute ) )
        else:
            self.assertTrue( True, 'all serializable keys have a serializer' )

    def test_views_and_keys( self ):
        dataset = self.dataset_manager.create()

        self.log( 'should be able to use keys with views' )
        serialized = self.dataset_serializer.serialize_to_view( dataset,
            view='summary', keys=[ 'permissions' ] )
        self.assertKeys( serialized,
            self.dataset_serializer.views[ 'summary' ] + [ 'permissions' ] )

        self.log( 'should be able to use keys on their own' )
        serialized = self.dataset_serializer.serialize_to_view( dataset,
            keys=[ 'purgable', 'file_size' ] )
        self.assertKeys( serialized, [ 'purgable', 'file_size' ] )

    def test_serialize_permissions( self ):
        dataset = self.dataset_manager.create()
        self.log( 'serialized permissions should be well formed' )

    def test_serializers( self ):
        user2 = self.user_manager.create( **user2_data )
        dataset = self.dataset_manager.create()
        all_keys = list( self.dataset_serializer.serializable_keyset )
        serialized = self.dataset_serializer.serialize( dataset, all_keys )

        self.log( 'everything serialized should be of the proper type' )
        self.assertEncodedId( serialized[ 'id' ] )
        self.assertDate( serialized[ 'create_time' ] )
        self.assertDate( serialized[ 'update_time' ] )

        self.assertUUID( serialized[ 'uuid' ] )
        self.assertIsInstance( serialized[ 'state' ], basestring )
        self.assertIsInstance( serialized[ 'deleted' ], bool )
        self.assertIsInstance( serialized[ 'purged' ], bool )
        self.assertIsInstance( serialized[ 'purgable' ], bool )

        # # TODO: no great way to do these with mocked dataset
        # self.assertIsInstance( serialized[ 'file_size' ], int )
        # self.assertIsInstance( serialized[ 'total_size' ], int )

        self.log( 'serialized should jsonify well' )
        self.assertIsJsonifyable( serialized )