def setUp(self): environ.env.db = FakeDb() self.auth = AuthRedis(host='mock', env=environ.env) environ.env.session = { SessionKeys.user_id.value: BaseAclTestValidator.USER_ID, SessionKeys.user_name.value: BaseAclTestValidator.USER_NAME, SessionKeys.age.value: BaseAclTestValidator.AGE, SessionKeys.gender.value: BaseAclTestValidator.GENDER, SessionKeys.membership.value: BaseAclTestValidator.MEMBERSHIP, SessionKeys.image.value: BaseAclTestValidator.IMAGE, SessionKeys.has_webcam.value: BaseAclTestValidator.HAS_WEBCAM, SessionKeys.fake_checked.value: BaseAclTestValidator.FAKE_CHECKED, SessionKeys.country.value: BaseAclTestValidator.COUNTRY, SessionKeys.city.value: BaseAclTestValidator.CITY, SessionKeys.token.value: BaseAclTestValidator.TOKEN } FakeDb._admins = dict() FakeDb._super_users = set() environ.env.config = { ConfigKeys.ACL: { 'room': { 'join': { 'ecludes': [], 'acls': ['gender', 'age', 'country'] }, 'message': { 'ecludes': [], 'acls': ['gender', 'age'] } }, 'available': { 'acls': ['gender', 'age'] }, 'validation': { 'country': { 'type': 'anything', 'value': AclStrInCsvValidator() }, 'gender': { 'type': 'str_in_csv', 'value': AclStrInCsvValidator('m,f') }, 'age': { 'type': 'range', 'value': AclRangeValidator() } } } } self.auth.redis.hmset(RedisKeys.auth_key(BaseAclTestValidator.USER_ID), environ.env.session) self.validator = AclValidator()
def init_acl_validators(gn_env: GNEnvironment) -> None: if len(gn_env.config) == 0 or gn_env.config.get(ConfigKeys.TESTING, False): # assume we're testing return acl_config = gn_env.config.get(ConfigKeys.ACL) validators = acl_config['validation'] for acl_type, validation_config in validators.items(): validation_type = validation_config['type'] if validation_type == 'str_in_csv': csv = None if 'value' in validation_config: csv = validation_config['value'] if csv == '##db##': try: csv = gn_env.db.get_acl_validation_value(acl_type, 'str_in_csv') except AclValueNotFoundException: logger.warning( 'acl config specifies to get value from db but no value found for type ' '"%s" and method "str_in_csv", will check for default value' % acl_type) if 'default' not in validation_config or len(validation_config['default'].strip()) == 0: raise RuntimeError('no default value found for type "%s" and method "str_in_csv"' % acl_type) csv = validation_config['default'] validation_config['value'] = AclStrInCsvValidator(csv) elif validation_type == 'range': validation_config['value'] = AclRangeValidator() elif validation_type == 'disallow': validation_config['value'] = AclDisallowValidator() elif validation_type == 'samechannel': validation_config['value'] = AclSameChannelValidator() elif validation_type == 'custom': validation_config['value'] = AclPatternValidator() elif validation_type == 'sameroom': validation_config['value'] = AclSameRoomValidator() elif validation_type == 'is_admin': validation_config['value'] = AclIsAdminValidator() elif validation_type == 'is_room_owner': validation_config['value'] = AclIsRoomOwnerValidator() elif validation_type == 'is_super_user': validation_config['value'] = AclIsSuperUserValidator() else: raise RuntimeError('unknown validation type "%s"' % validation_type) gn_env.config.set(ConfigKeys.ACL, MappingProxyType(acl_config))
def setUp(self): environ.env.db = FakeDb() FakeDb._channel_exists = { RequestSetAclTest.CHANNEL_ID: True, RequestSetAclTest.OTHER_CHANNEL_ID: False } FakeDb._room_exists = { RequestSetAclTest.ROOM_ID: True, RequestSetAclTest.OTHER_ROOM_ID: False } FakeDb._room_contains = { RequestSetAclTest.ROOM_ID: {RequestSetAclTest.USER_ID}, RequestSetAclTest.OTHER_ROOM_ID: set() } FakeDb._channel_for_room = { RequestSetAclTest.ROOM_ID: RequestSetAclTest.CHANNEL_ID, RequestSetAclTest.OTHER_ROOM_ID: RequestSetAclTest.OTHER_CHANNEL_ID } FakeDb._admins = dict() FakeDb._super_users = set() FakeDb._channel_owners = dict() FakeDb._owners = {RequestSetAclTest.ROOM_ID: ''} FakeDb._moderators = { RequestSetAclTest.ROOM_ID: {RequestSetAclTest.USER_ID}, RequestSetAclTest.OTHER_ROOM_ID: {} } self.auth = AuthRedis(host='mock', env=environ.env) environ.env.session = { SessionKeys.user_id.value: RequestSetAclTest.USER_ID, SessionKeys.user_name.value: RequestSetAclTest.USER_NAME, SessionKeys.age.value: RequestSetAclTest.AGE, SessionKeys.gender.value: RequestSetAclTest.GENDER, SessionKeys.membership.value: RequestSetAclTest.MEMBERSHIP, SessionKeys.image.value: RequestSetAclTest.IMAGE, SessionKeys.has_webcam.value: RequestSetAclTest.HAS_WEBCAM, SessionKeys.fake_checked.value: RequestSetAclTest.FAKE_CHECKED, SessionKeys.country.value: RequestSetAclTest.COUNTRY, SessionKeys.city.value: RequestSetAclTest.CITY, SessionKeys.token.value: RequestSetAclTest.TOKEN } self.set_owner() environ.env.config = { ConfigKeys.ACL: { 'room': { 'join': { 'acls': ['gender', 'age', 'country'] }, 'message': { 'acls': ['gender', 'age'] }, 'crossroom': { 'acls': ['samechannel'] } }, 'channel': { 'crossroom': { 'acls': ['samechannel'] } }, 'available': { 'acls': ['gender', 'age', 'samechannel'] }, 'validation': { 'samechannel': { 'type': 'samechannel', 'value': AclSameChannelValidator() }, 'country': { 'type': 'anything', 'value': AclStrInCsvValidator() }, 'gender': { 'type': 'str_in_csv', 'value': AclStrInCsvValidator('m,f') }, 'age': { 'type': 'range', 'value': AclRangeValidator() } } } } self.auth.redis.hmset(RedisKeys.auth_key(RequestSetAclTest.USER_ID), environ.env.session) self.validator = RequestValidator()
def setUp(self): environ.env.db = FakeDb() self.remote = FakeRemote() environ.env.remote = self.remote environ.env.cache = FakeCache() FakeDb._channel_exists = { RequestMessageTest.CHANNEL_ID: True, RequestMessageTest.OTHER_CHANNEL_ID: False, RequestMessageTest.WHISPER_NOT_ALLOWED_CHANNEL_ID: True } FakeDb._room_exists = { RequestMessageTest.ROOM_ID: True, RequestMessageTest.OTHER_ROOM_ID: False } FakeDb._room_contains = { RequestMessageTest.ROOM_ID: {RequestMessageTest.USER_ID}, RequestMessageTest.OTHER_ROOM_ID: set() } FakeDb._channel_names = { RequestMessageTest.CHANNEL_ID: RequestMessageTest.CHANNEL_NAME, RequestMessageTest.WHISPER_NOT_ALLOWED_CHANNEL_ID: RequestMessageTest.CHANNEL_NAME + ' no whisper' } FakeDb._channel_for_room = { RequestMessageTest.ROOM_ID: RequestMessageTest.CHANNEL_ID, RequestMessageTest.OTHER_ROOM_ID: RequestMessageTest.OTHER_CHANNEL_ID, } self.auth = AuthRedis(host='mock', env=environ.env) environ.env.session = { SessionKeys.user_id.value: RequestMessageTest.USER_ID, SessionKeys.user_name.value: RequestMessageTest.USER_NAME, SessionKeys.age.value: RequestMessageTest.AGE, SessionKeys.gender.value: RequestMessageTest.GENDER, SessionKeys.membership.value: RequestMessageTest.MEMBERSHIP, SessionKeys.image.value: RequestMessageTest.IMAGE, SessionKeys.has_webcam.value: RequestMessageTest.HAS_WEBCAM, SessionKeys.fake_checked.value: RequestMessageTest.FAKE_CHECKED, SessionKeys.country.value: RequestMessageTest.COUNTRY, SessionKeys.city.value: RequestMessageTest.CITY, SessionKeys.token.value: RequestMessageTest.TOKEN } environ.env.config = { ConfigKeys.VALIDATE_WHISPERS: True, ConfigKeys.ACL: { 'room': { 'join': { 'acls': ['gender', 'age', 'country'] }, 'message': { 'acls': ['gender', 'age'] }, 'crossroom': { 'acls': ['samechannel'] } }, 'channel': { 'whisper': { 'acls': ['disallow'] }, 'crossroom': { 'acls': ['samechannel'] } }, 'available': { 'acls': [ 'gender', 'age', 'samechannel', 'whisper', ] }, 'validation': { 'samechannel': { 'type': 'samechannel', 'value': AclSameChannelValidator() }, 'country': { 'type': 'anything', 'value': AclStrInCsvValidator() }, 'gender': { 'type': 'str_in_csv', 'value': AclStrInCsvValidator('m,f') }, 'age': { 'type': 'range', 'value': AclRangeValidator() }, 'disallow': { 'type': 'disallow', 'value': AclDisallowValidator() } } } } self.auth.redis.hmset(RedisKeys.auth_key(RequestMessageTest.USER_ID), environ.env.session) self.validator = RequestValidator()
def setUp(self): environ.env.db = FakeDb() FakeDb._room_contains = { BaseWithDb.ROOM_ID: { BaseWithDb.USER_ID }, BaseWithDb.OTHER_ROOM_ID: set() } FakeDb._bans = { 'global': '', 'channel': '', 'room': '' } FakeDb._room_acls = dict() FakeDb._channel_acls = dict() FakeDb._admins = dict() FakeDb._super_users = set() FakeDb._channel_owners = dict() FakeDb._owners = dict() FakeDb._moderators = dict() FakeDb._channel_names = { BaseWithDb.CHANNEL_ID: BaseWithDb.CHANNEL_NAME } environ.env.config = { ConfigKeys.ACL: { 'room': { 'crossroom': { 'acls': [ 'samechannel', 'disallow' ] }, 'message': { 'acls': [ 'gender', 'age', 'country', ] } }, 'channel': { 'crossroom': { 'acls': [ 'samechannel', 'disallow' ] }, 'message': { 'acls': [ 'gender', 'age', 'country' ] } }, 'available': { 'acls': [ 'gender', 'age', 'samechannel', 'disallow' ] }, 'validation': { 'disallow': { 'type': 'disallow', 'value': AclDisallowValidator() }, 'samechannel': { 'type': 'samechannel', 'value': AclSameChannelValidator() }, 'country': { 'type': 'anything', 'value': AclStrInCsvValidator() }, 'gender': { 'type': 'str_in_csv', 'value': AclStrInCsvValidator('m,f') }, 'age': { 'type': 'range', 'value': AclRangeValidator() } } } }
def setUp(self): environ.env.session = { SessionKeys.user_id.value: CustomPatternAclValidatorTest.USER_ID, SessionKeys.user_name.value: CustomPatternAclValidatorTest.USER_NAME, SessionKeys.age.value: CustomPatternAclValidatorTest.AGE, SessionKeys.gender.value: CustomPatternAclValidatorTest.GENDER, SessionKeys.membership.value: CustomPatternAclValidatorTest.MEMBERSHIP, SessionKeys.image.value: CustomPatternAclValidatorTest.IMAGE, SessionKeys.has_webcam.value: CustomPatternAclValidatorTest.HAS_WEBCAM, SessionKeys.fake_checked.value: CustomPatternAclValidatorTest.FAKE_CHECKED, SessionKeys.country.value: CustomPatternAclValidatorTest.COUNTRY, SessionKeys.city.value: CustomPatternAclValidatorTest.CITY, SessionKeys.token.value: CustomPatternAclValidatorTest.TOKEN } environ.env.config = { ConfigKeys.ACL: { 'room': { 'join': {}, 'message': {} }, 'available': {}, 'validation': {} } } environ.env.config = { ConfigKeys.ACL: { 'room': { 'join': { 'excludes': [], 'acls': ['gender', 'age', 'country'] }, 'message': { 'excludes': [], 'acls': ['gender', 'age'] } }, 'available': { 'acls': ['gender', 'age', 'custom', 'membership'] }, 'validation': { 'gender': { 'type': 'str_in_csv', 'value': AclStrInCsvValidator('m,f,ts') }, 'membership': { 'type': 'str_in_csv', 'value': AclStrInCsvValidator('n,tg,tg_p') }, 'custom': { 'type': 'accepted_pattern', 'value': AclPatternValidator() }, 'age': { 'type': 'range', 'value': AclRangeValidator() } } } } self.validator = AclPatternValidator()
def setUp(self): BaseTest.users_in_room.clear() BaseTest.emit_args.clear() BaseTest.emit_kwargs.clear() BaseTest.msgs_sent.clear() BaseTest.rendered_template = None self.session = { 'user_id': BaseTest.USER_ID, 'user_name': BaseTest.USER_NAME, 'age': BaseTest.AGE, 'gender': BaseTest.GENDER, 'membership': BaseTest.MEMBERSHIP, 'image': BaseTest.IMAGE, 'fake_checked': BaseTest.FAKE_CHECKED, 'has_webcam': BaseTest.HAS_WEBCAM, 'city': BaseTest.CITY, 'country': BaseTest.COUNTRY, 'token': '66968fad-2336-40c9-bc6d-0ecbcd91f4da' } environ.env.config = environ.ConfigDict() environ.env.config.set(ConfigKeys.TESTING, True) environ.env.config = environ.env.config.sub(**self.session) all_acls = [ 'age', 'gender', 'membership', 'group', 'country', 'city', 'image', 'has_webcam', 'fake_checked', 'owner', 'admin', 'moderator', 'superuser', 'crossroom', 'samechannel', 'sameroom', 'disallow' ] environ.env.config.set( ConfigKeys.ACL, { 'room': { 'join': { 'acls': all_acls }, 'message': { 'acls': all_acls }, 'history': { 'acls': all_acls }, 'crossroom': { 'acls': all_acls } }, 'channel': { 'message': { 'acls': all_acls }, 'list': { 'acls': all_acls }, 'crossroom': { 'acls': all_acls }, 'whisper': { 'acls': ['disallow'] }, }, 'available': { 'acls': all_acls }, 'validation': { 'superuser': { 'type': 'superuser', 'value': AclIsSuperUserValidator() }, 'admin': { 'type': 'admin', 'value': AclIsAdminValidator() }, 'samechannel': { 'type': 'samechannel', 'value': AclSameChannelValidator() }, 'sameroom': { 'type': 'sameroom', 'value': AclSameRoomValidator() }, 'country': { 'type': 'str_in_csv', 'value': AclStrInCsvValidator() }, 'disallow': { 'type': 'disallow', 'value': AclDisallowValidator() }, 'gender': { 'type': 'str_in_csv', 'value': AclStrInCsvValidator('m,f') }, 'membership': { 'type': 'str_in_csv', 'value': AclStrInCsvValidator() }, 'city': { 'type': 'str_in_csv', 'value': AclStrInCsvValidator() }, 'has_webcam': { 'type': 'str_in_csv', 'value': AclStrInCsvValidator('y,n') }, 'fake_checked': { 'type': 'str_in_csv', 'value': AclStrInCsvValidator('y,n') }, 'image': { 'type': 'str_in_csv', 'value': AclStrInCsvValidator('y,n') }, 'group': { 'type': 'str_in_csv', 'value': AclStrInCsvValidator('') }, 'age': { 'type': 'range', 'value': AclRangeValidator() } } }) environ.env.auth = AuthRedis('mock', env=environ.env) environ.env.storage = StorageRedis('mock') environ.env.db = DatabaseRedis(environ.env, 'mock') environ.env.storage.redis = environ.env.auth.redis environ.env.db.redis = environ.env.auth.redis environ.env.redis = environ.env.auth.redis environ.env.publish = BaseTest._mock_publish environ.env.disconnect = BaseTest._disconnect environ.env.stats = MockStats() environ.env.spam = MockSpam() environ.env.cache = CacheAllMiss() environ.env.auth.redis.flushall() environ.env.storage.redis.flushall() environ.env.db.redis.flushall() environ.env.cache._flushall() environ.env.auth.redis.hmset(RedisKeys.auth_key(BaseTest.USER_ID), self.session) environ.env.redis.hset(RedisKeys.room_name_for_id(), BaseTest.ROOM_ID, BaseTest.ROOM_NAME) environ.env.redis.sadd(RedisKeys.non_ephemeral_rooms(), BaseTest.ROOM_ID) environ.env.redis.hset(RedisKeys.channels(), BaseTest.CHANNEL_ID, BaseTest.CHANNEL_NAME) environ.env.db.redis.hset(RedisKeys.channels(), BaseTest.CHANNEL_ID, BaseTest.CHANNEL_NAME) environ.env.db.redis.hset(RedisKeys.auth_key(BaseTest.USER_ID), SessionKeys.user_name.value, BaseTest.USER_NAME) environ.env.db.redis.hset(RedisKeys.channel_for_rooms(), BaseTest.ROOM_ID, BaseTest.CHANNEL_ID) environ.env.db.redis.hset(RedisKeys.user_names(), BaseTest.USER_ID, BaseTest.USER_NAME) environ.env.db.redis.delete(RedisKeys.room_acl(BaseTest.ROOM_ID)) environ.env.render_template = BaseTest._render_template environ.env.emit = BaseTest._emit environ.env.join_room = BaseTest._join_room environ.env.send = BaseTest._send environ.env.leave_room = BaseTest._leave_room environ.env.redirect = BaseTest._redirect environ.env.url_for = BaseTest._url_for environ.env.send_from_directory = BaseTest._send_from_directory environ.env.request = BaseTest.Request() environ.env.SelectField = SelectField environ.env.SubmitField = SubmitField environ.env.StringField = StringField environ.env.DataRequired = DataRequired environ.env.Form = Form environ.env.logger = logger environ.env.session = self.session # TODO: don't do this here, but too many tests that doesn't do it themselves... should remove this base class # and only have test logic in each test class, separate it self.env = environ.env
def test_validate_new_acl_blank(self): AclStrInCsvValidator('a,b,c').validate_new_acl('')
def test_call_not_in_session(self): validator = AclStrInCsvValidator() is_valid, msg = validator(as_parser(self.json_act()), environ.env, 'other-stuff', 'a,b,c') self.assertFalse(is_valid)
def test_call_blank_values(self): validator = AclStrInCsvValidator() is_valid, msg = validator(as_parser(self.json_act()), environ.env, 'gender', '') self.assertTrue(is_valid)
def set_up_env(self): self.env = BaseFunctionalTest.FakeEnv() self.env.config.set(ConfigKeys.TESTING, False) all_acls = [ 'age', 'gender', 'membership', 'group', 'country', 'city', 'image', 'has_webcam', 'fake_checked', 'owner', 'admin', 'moderator', 'superuser', 'crossroom', 'samechannel', 'sameroom', 'disallow' ] self.env.config.set( ConfigKeys.ACL, { 'room': { 'join': { 'acls': all_acls }, 'message': { 'acls': all_acls }, 'history': { 'acls': all_acls }, 'crossroom': { 'acls': all_acls } }, 'channel': { 'message': { 'acls': all_acls }, 'list': { 'acls': all_acls }, 'crossroom': { 'acls': all_acls } }, 'available': { 'acls': all_acls }, 'validation': { 'samechannel': { 'type': 'samechannel', 'value': AclSameChannelValidator() }, 'sameroom': { 'type': 'sameroom', 'value': AclSameRoomValidator() }, 'country': { 'type': 'str_in_csv', 'value': AclStrInCsvValidator() }, 'disallow': { 'type': 'disallow', 'value': AclDisallowValidator() }, 'gender': { 'type': 'str_in_csv', 'value': AclStrInCsvValidator('m,f') }, 'membership': { 'type': 'str_in_csv', 'value': AclStrInCsvValidator() }, 'city': { 'type': 'str_in_csv', 'value': AclStrInCsvValidator() }, 'has_webcam': { 'type': 'str_in_csv', 'value': AclStrInCsvValidator('y,n') }, 'fake_checked': { 'type': 'str_in_csv', 'value': AclStrInCsvValidator('y,n') }, 'image': { 'type': 'str_in_csv', 'value': AclStrInCsvValidator('y,n') }, 'group': { 'type': 'str_in_csv', 'value': AclStrInCsvValidator('') }, 'age': { 'type': 'range', 'value': AclRangeValidator() }, 'admin': { 'type': 'is_admin', 'value': AclIsAdminValidator() }, 'superuser': { 'type': 'is_super_user', 'value': AclIsSuperUserValidator() } } }) self.env.session[SessionKeys.user_name.value] = BaseTest.USER_NAME self.env.config.set(ConfigKeys.DRIVER, 'sqlite', domain=ConfigKeys.DATABASE) self.db = DatabaseRdbms(self.env) environ.env = self.env environ.env.config = self.env.config environ.env.db = self.db environ.env.join_room = lambda x: None environ.env.leave_room = lambda x: None environ.env.emit = self.emit environ.env.observer = EventEmitter() environ.env.db.create_user(BaseFunctionalTest.USER_ID, BaseFunctionalTest.USER_NAME)