def setUp(self): self.db = StorageRedis('mock') self.db.redis.flushall() environ.env.session = { 'user_id': RedisStorageTest.USER_ID, 'user_name': RedisStorageTest.USER_NAME }
class RedisStorageTest(TestCase): USER_ID = '1234' ROOM_ID = '4321' USER_NAME = 'Batman' MESSAGE = str(uuid()) MESSAGE_ID = str(uuid()) def setUp(self): self.db = StorageRedis('mock') self.db.redis.flushall() environ.env.session = { 'user_id': RedisStorageTest.USER_ID, 'user_name': RedisStorageTest.USER_NAME } def test_get_empty_history(self): history = self.db.get_history(RedisStorageTest.ROOM_ID) self.assertEqual(0, len(history)) def test_store_message(self): self.db.store_message(as_parser(self.act())) history = self.db.get_history(RedisStorageTest.ROOM_ID) self.assertEqual(1, len(history)) self.assertIsNotNone(history[0]['message_id']) self.assertEqual(RedisStorageTest.USER_ID, history[0]['from_user_id']) self.assertEqual(RedisStorageTest.MESSAGE, history[0]['body']) def test_delete_message(self): self.db.store_message(as_parser(self.act())) history = self.db.get_history(RedisStorageTest.ROOM_ID) self.assertEqual(1, len(history)) self.db.delete_message(RedisStorageTest.MESSAGE_ID, RedisStorageTest.ROOM_ID) history = self.db.get_history(RedisStorageTest.ROOM_ID) self.assertEqual(0, len(history)) def act(self): return { 'actor': { 'id': RedisStorageTest.USER_ID, 'summary': RedisStorageTest.USER_NAME }, 'verb': 'send', 'object': { 'content': b64e(RedisStorageTest.MESSAGE) }, 'target': { 'id': RedisStorageTest.ROOM_ID }, 'id': RedisStorageTest.MESSAGE_ID, 'published': datetime.utcnow().strftime(ConfigKeys.DEFAULT_DATE_FORMAT) }
def init_storage_engine(gn_env: GNEnvironment) -> None: if len(gn_env.config) == 0 or gn_env.config.get(ConfigKeys.TESTING, False): # assume we're testing return storage_engine = gn_env.config.get(ConfigKeys.STORAGE, None) if storage_engine is None: raise RuntimeError('no storage engine specified') storage_type = storage_engine.get(ConfigKeys.TYPE) if storage_type is None: raise RuntimeError('no storage type specified, use redis, cassandra, mysql etc.') if storage_type == 'redis': from dino.storage.redis import StorageRedis storage_host, storage_port = storage_engine.get(ConfigKeys.HOST), None if ':' in storage_host: storage_host, storage_port = storage_host.split(':', 1) storage_db = storage_engine.get(ConfigKeys.DB, 0) gn_env.storage = StorageRedis(host=storage_host, port=storage_port, db=storage_db) elif storage_type == 'cassandra': from dino.storage.cassandra import CassandraStorage storage_hosts = storage_engine.get(ConfigKeys.HOST) strategy = storage_engine.get(ConfigKeys.STRATEGY, None) replication = storage_engine.get(ConfigKeys.REPLICATION, None) key_space = gn_env.config.get(ConfigKeys.ENVIRONMENT, 'dino') gn_env.storage = CassandraStorage(storage_hosts, replications=replication, strategy=strategy, key_space=key_space) gn_env.storage.init() else: raise RuntimeError('unknown storage engine type "%s"' % storage_type)
def __init__(self): super(BaseFunctional.FakeEnv, self).__init__(None, ConfigDict(), skip_init=True) self.config = ConfigDict() self.cache = CacheRedis(self, 'mock') self.storage = StorageRedis(host='mock', env=self) self.session = dict() self.node = 'test' self.request = BaseFunctionalTest.FakeRequest()
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