Esempio n. 1
0
    def create_admin_room(self) -> str:
        admin_room_id = self.get_admin_room()
        if admin_room_id is not None:
            return admin_room_id

        try:
            self.create_user('0', 'Admin')
        except UserExistsException:
            pass

        channel_id = str(uuid())
        room_id = str(uuid())

        self.create_channel('Admins', channel_id, '0')

        self.redis.set(RedisKeys.admin_room(), room_id)
        self.redis.hset(RedisKeys.room_name_for_id(), room_id, 'Admins')
        self.redis.hset(RedisKeys.rooms(channel_id), room_id, 'Admins')
        self.redis.hset(RedisKeys.channel_for_rooms(), room_id, channel_id)
        self.redis.sadd(RedisKeys.non_ephemeral_rooms(), room_id)

        acls = {
            RoleKeys.ADMIN: '',
            RoleKeys.SUPER_USER: ''
        }
        samechannel = {
            'samechannel': ''
        }

        self.add_acls_in_channel_for_action(channel_id, ApiActions.LIST, acls)
        self.add_acls_in_channel_for_action(channel_id, ApiActions.JOIN, acls)
        self.add_acls_in_room_for_action(room_id, ApiActions.JOIN, acls)
        self.add_acls_in_room_for_action(room_id, ApiActions.LIST, acls)
        self.add_acls_in_room_for_action(room_id, ApiActions.CROSSROOM, samechannel)
        return room_id
Esempio n. 2
0
    def remove_room(self, channel_id: str, room_id: str) -> None:
        if self.env.cache.get_channel_exists(channel_id) is None:
            if not self.channel_exists(channel_id):
                raise NoSuchChannelException(channel_id)

        if not self.room_exists(channel_id, room_id):
            raise NoSuchRoomException(room_id)

        self.redis.srem(RedisKeys.non_ephemeral_rooms(), room_id)
        self.redis.hdel(RedisKeys.room_name_for_id(), room_id)
        self.redis.delete(RedisKeys.room_roles(room_id))
        self.redis.hdel(RedisKeys.rooms(channel_id), room_id)
        self.redis.hdel(RedisKeys.channel_for_rooms(), room_id)
Esempio n. 3
0
    def create_room(self, room_name: str, room_id: str, channel_id: str, user_id: str, user_name: str, ephemeral: bool=True, sort_order: int=False) -> None:
        if self.env.cache.get_channel_exists(channel_id) is None:
            if not self.channel_exists(channel_id):
                raise NoSuchChannelException(channel_id)

        if room_name is None or len(room_name.strip()) == 0:
            raise EmptyRoomNameException(room_id)

        if self.room_exists(channel_id, room_id):
            raise RoomExistsException(room_id)

        if self.room_name_exists(channel_id, room_name):
            raise RoomNameExistsForChannelException(channel_id, room_name)

        if ephemeral:
            self.redis.sadd(RedisKeys.non_ephemeral_rooms(), room_id)

        self.redis.hset(RedisKeys.room_name_for_id(), room_id, room_name)
        self.redis.hset(RedisKeys.rooms(channel_id), room_id, room_name)
        self.redis.hset(RedisKeys.channel_for_rooms(), room_id, channel_id)
        self.redis.hset(RedisKeys.user_names(), user_id, user_name)
        self.set_owner(room_id, user_id)
Esempio n. 4
0
File: redis.py Progetto: Xlzman/dino
 def is_room_ephemeral(self, room_id: str) -> bool:
     redis_key = RedisKeys.non_ephemeral_rooms()
     cache_key = '%s-%s' % (redis_key, room_id)
     return self.cache.get(cache_key)
Esempio n. 5
0
File: redis.py Progetto: Xlzman/dino
 def set_is_room_ephemeral(self, room_id: str, is_ephemeral: bool) -> None:
     redis_key = RedisKeys.non_ephemeral_rooms()
     cache_key = '%s-%s' % (redis_key, room_id)
     self.cache.set(cache_key, is_ephemeral)
Esempio n. 6
0
 def is_room_ephemeral(self, room_id: str) -> bool:
     return not self.redis.sismember(RedisKeys.non_ephemeral_rooms(), room_id)
Esempio n. 7
0
 def unset_ephemeral_room(self, room_id: str):
     self.redis.sadd(RedisKeys.non_ephemeral_rooms(), room_id)
Esempio n. 8
0
 def set_ephemeral_room(self, room_id: str):
     self.redis.srem(RedisKeys.non_ephemeral_rooms(), room_id)
Esempio n. 9
0
    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