def test_inherit_parent_roles(): class RoleMixin(object): __roles__ = { 'id_only': ['id', ] } class Parent(Mapper, RoleMixin): __type__ = TestType id = TestField() name = TestField() __roles__ = { 'parent': ['name'], 'overview': ['id', 'name'] } class Child(Parent): __type__ = TestType __roles__ = { 'overview': ['name', ] } mapper = Child(data={}) assert mapper.roles == { '__default__': whitelist('id', 'name'), 'overview': whitelist('name', ), 'parent': whitelist('name', ), 'id_only': whitelist('id', ), }
def test_inherit_parent_roles(): class RoleMixin(object): __roles__ = { 'id_only': [ 'id', ] } class Parent(Mapper, RoleMixin): __type__ = TestType id = TestField() name = TestField() __roles__ = {'parent': ['name'], 'overview': ['id', 'name']} class Child(Parent): __type__ = TestType __roles__ = { 'overview': [ 'name', ] } mapper = Child(data={}) assert mapper.roles == { '__default__': whitelist('id', 'name'), 'overview': whitelist('name', ), 'parent': whitelist('name', ), 'id_only': whitelist('id', ), }
def test_union_whitelist_roles(): role = whitelist('name', 'id') | whitelist('name', 'email') assert 'name' in role assert 'id' in role assert 'email' in role assert role.whitelist
class MapperB(MapperA): __roles__ = { '__default__': whitelist('id', 'name'), 'type': whitelist('object_type'), } __mapper_args__ = { 'polymorphic_name': 'B', }
class EventMapper(SchedulableMapper): __type__ = TestType location = field.String() __mapper_args__ = { 'polymorphic_name': 'event' } __roles__ = { 'public': SchedulableMapper.__roles__['public'] | whitelist('location'), 'event_only_role': whitelist('id', 'location'), 'event_blacklist': blacklist('location') }
class TaskMapper(SchedulableMapper): __type__ = TestType status = field.String() __mapper_args__ = { 'polymorphic_name': 'task' } __roles__ = { 'public': SchedulableMapper.__roles__['public'] | whitelist('status'), 'task_only_role': whitelist('id', 'status'), 'task_blacklist': blacklist('status') }
class MapperA(PolymorphicMapper): __type__ = TestType id = Integer() name = String() object_type = String() __roles__ = { '__default__': whitelist('id'), 'overview': whitelist('id', 'name', 'object_type'), } __mapper_args__ = { 'polymorphic_on': 'object_type', }
def test_union_blacklist_and_whitelist(): role = blacklist('name', 'id') | whitelist('name', 'email') assert 'email' in role assert 'name' not in role assert 'id' not in role assert role.whitelist
class CompanyMapper(Mapper): __type__ = User id = field.String(read_only=True) name = field.String() __roles__ = {'public': whitelist('name', 'id')}
class MapperBase(Mapper): __type__ = TestType id = TestField() name = TestField() __roles__ = {'__default__': whitelist('id', )}
class UserMapper(Mapper): __type__ = dict id = String(required=True, read_only=True) name = String() __roles__ = {'private': whitelist('name')}
class UserMapper(Mapper): __type__ = User id = field.String(read_only=True) name = field.String() password = field.String() is_admin = field.Boolean(required=False, default=False) __roles__ = {'public': whitelist('name', 'id', 'is_admin')}
class MapperBase(Mapper): __type__ = TestType id = Integer() name = String() user = Nested(UserMapper, role='private') __roles__ = {'private': whitelist('name', 'user')}
class UserMapper(Mapper): __type__ = User id = field.Integer(read_only=True) name = field.String() is_admin = field.Boolean(required=False, default=False) company = field.Nested(CompanyMapper, getter=company_getter) __roles__ = { 'overview': whitelist('id', 'name') }
def test_polymorphic_mappers_role_inheritance(): class MapperA(PolymorphicMapper): __type__ = TestType id = Integer() name = String() object_type = String() __roles__ = { '__default__': whitelist('id'), 'overview': whitelist('id', 'name', 'object_type'), } __mapper_args__ = { 'polymorphic_on': 'object_type', } class MapperB(MapperA): __roles__ = { '__default__': whitelist('id', 'name'), 'type': whitelist('object_type'), } __mapper_args__ = { 'polymorphic_name': 'B', } mapper = MapperA(data={}) assert mapper.roles == { '__default__': whitelist('id'), 'overview': whitelist('id', 'name', 'object_type') } mapperb = MapperB(data={}) assert mapperb.roles == { '__default__': whitelist('id', 'name'), 'overview': whitelist('id', 'name', 'object_type'), 'type': whitelist('object_type'), }
def test_override_default_role(): class MapperBase(Mapper): __type__ = TestType id = TestField() name = TestField() __roles__ = {'__default__': whitelist('id', )} mapper = MapperBase(data={}) assert mapper.roles == {'__default__': whitelist('id', )}
def test_polymorphic_mappers_role_inheritance(): class MapperA(PolymorphicMapper): __type__ = TestType id = Integer() name = String() object_type = String() __roles__ = { '__default__': whitelist('id'), 'overview': whitelist('id', 'name', 'object_type'), } __mapper_args__ = { 'polymorphic_on': 'object_type', } class MapperB(MapperA): __roles__ = { '__default__': whitelist('id', 'name'), 'type': whitelist('object_type'), } __mapper_args__ = { 'polymorphic_name': 'B', } mapper = MapperA(data={}) assert mapper.roles == { '__default__': whitelist('id'), 'overview': whitelist('id', 'name', 'object_type') } mapperb = MapperB(data={}) assert mapperb.roles == { '__default__': whitelist('id', 'name'), 'overview': whitelist('id', 'name', 'object_type'), 'type': whitelist('object_type'), }
def test_override_default_role(): class MapperBase(Mapper): __type__ = TestType id = TestField() name = TestField() __roles__ = { '__default__': whitelist('id', ) } mapper = MapperBase(data={}) assert mapper.roles == {'__default__': whitelist('id', )}
class SchedulableMapper(PolymorphicMapper): __type__ = TestType id = field.Integer(read_only=True) name = field.String() object_type = field.String(choices=['event', 'task'], read_only=True) __mapper_args__ = { 'polymorphic_on': object_type, 'allow_polymorphic_marshal': True, } __roles__ = { 'name_only': ['name'], 'public': whitelist('name', 'id'), }
def test_new_mapper_sets_roles(): class MapperBase(Mapper): __type__ = TestType id = TestField() name = TestField() class MyMapper(MapperBase): email = TestField() __roles__ = { 'overview': [ 'email', ] } class OtherMapper(MyMapper): __roles__ = { 'private': [ 'id', ] } mapper = MapperBase(data={}) assert mapper.roles == {'__default__': whitelist('id', 'name')} mapper = MyMapper(data={}) assert mapper.roles == { 'overview': whitelist('email', ), '__default__': whitelist('id', 'name', 'email') } mapper = OtherMapper(data={}) assert mapper.roles == { '__default__': whitelist('id', 'name', 'email'), 'overview': whitelist('email'), 'private': whitelist('id', ) }
def test_new_mapper_sets_roles(): class MapperBase(Mapper): __type__ = TestType id = TestField() name = TestField() class MyMapper(MapperBase): email = TestField() __roles__ = { 'overview': ['email', ] } class OtherMapper(MyMapper): __roles__ = { 'private': ['id', ] } mapper = MapperBase(data={}) assert mapper.roles == {'__default__': whitelist('id', 'name')} mapper = MyMapper(data={}) assert mapper.roles == { 'overview': whitelist('email', ), '__default__': whitelist('id', 'name', 'email')} mapper = OtherMapper(data={}) assert mapper.roles == { '__default__': whitelist('id', 'name', 'email'), 'overview': whitelist('email'), 'private': whitelist('id', ) }
def test_whitelist_membership(): role = whitelist('name', 'id') assert 'name' in role assert 'id' in role assert 'email' not in role