def pytest_runtest_teardown(): conn = DynamoDBConnection() conn.delete_table(Message.get_table_name()) conn.delete_table(ChannelJoinInfo.get_table_name()) conn.delete_table(ChannelUsageLog.get_table_name()) conn.delete_table(Channel.get_table_name()) conn.delete_table(ChannelWithdrawalLog.get_table_name()) Message.create_table() ChannelWithdrawalLog.create_table() ChannelJoinInfo.create_table() ChannelUsageLog.create_table() Channel.create_table()
def test_withdrawal(group_chat_channel2, user1, user2): with AuthenticatedClient(user1) as user1_sock: with AuthenticatedClient(user2) as user2_sock: user1_sock.sendobj(dict(method='withdrawal', channel=group_chat_channel2)) response = user1_sock.recvobj() assert response['method'] == 'withdrawal' response = user2_sock.recvobj() assert response['method'] == 'publish' assert response['type'] == 'withdrawal' try: ChannelJoinInfo.get_item(group_chat_channel2, user1) except ItemNotFoundException: pass else: assert False
def test_withdrawal(group_chat_channel2, user1, user2): with AuthenticatedClient(user1) as user1_sock: with AuthenticatedClient(user2) as user2_sock: user1_sock.sendobj( dict(method='withdrawal', channel=group_chat_channel2)) response = user1_sock.recvobj() assert response['method'] == 'withdrawal' response = user2_sock.recvobj() assert response['method'] == 'publish' assert response['type'] == 'withdrawal' try: ChannelJoinInfo.get_item(group_chat_channel2, user1) except ItemNotFoundException: pass else: assert False
def make_client_attend_channel(self, message): data = bson.loads(message['data']) channel_users = set(data['users']) applying_users = set(self.factory.clients_by_user_id.keys()) & channel_users for user_id in applying_users: mine = ChannelJoinInfo.get_item(data['channel'], user_id) user_clients = self.factory.clients_by_user_id[user_id] for client in user_clients: client.user.join_infos_dict[data['channel']] = mine self.factory.clients_by_channel_name.setdefault(data['channel'], []).append(client)
def test_withdrawal(group_chat_channel2, user1, user2): with AuthenticatedClient(user1) as user1_sock: with AuthenticatedClient(user2) as user2_sock: user2_sock.sendobj(dict(method="attend", channel=group_chat_channel2)) user2_sock.recvobj() user1_sock.sendobj(dict(method="withdrawal", channel=group_chat_channel2)) response = user1_sock.recvobj() assert response["method"] == "withdrawal" response = user2_sock.recvobj() assert response["method"] == "publish" assert response["type"] == "withdrawal" try: ChannelJoinInfo.get_item(group_chat_channel2, user1) except ItemNotFoundException: pass else: assert False
def make_client_attend_channel(self, message): data = bson.loads(message['data']) channel_users = set(data['users']) applying_users = set( self.factory.clients_by_user_id.keys()) & channel_users for user_id in applying_users: mine = ChannelJoinInfo.get_item(data['channel'], user_id) user_clients = self.factory.clients_by_user_id[user_id] for client in user_clients: client.user.join_infos_dict[data['channel']] = mine self.factory.clients_by_channel_name.setdefault( data['channel'], []).append(client)
def adapt(self): """ :return: Changed request object. If None returned, protocol will not be continued. """ if self.request.method == 'attend': for join_info in ChannelJoinInfo.by_channel(self.request['channel']): if join_info.user_id == self.protocol.user.id: self.protocol.attended_channel = self.request['channel'] self.protocol.transport.write(bson.dumps(dict( method=self.request.method, channel=join_info.channel, last_read=join_info.partners_last_read_at ))) return raise ProtocolError( 'Given channel "%s" is not a channel of user %s' % (self.request['channel'], self.protocol.user.id) ) elif self.request.method == 'exit': self.protocol.attended_channel = None elif self.request.method == 'publish': self.request._data['channel'] = self.protocol.attended_channel # Trick. Should not access _data directly return self.request
def adapt(self): """ :return: Changed request object. If None returned, protocol will not be continued. """ if self.request.method == 'attend': for join_info in ChannelJoinInfo.by_channel( self.request['channel']): if join_info.user_id == self.protocol.user.id: self.protocol.attended_channel = self.request['channel'] self.protocol.transport.write( bson.dumps( dict(method=self.request.method, channel=join_info.channel, last_read=join_info.partners_last_read_at))) return raise ProtocolError( 'Given channel "%s" is not a channel of user %s' % (self.request['channel'], self.protocol.user.id)) elif self.request.method == 'exit': self.protocol.attended_channel = None elif self.request.method == 'publish': self.request._data[ 'channel'] = self.protocol.attended_channel # Trick. Should not access _data directly return self.request
def get_activated_channels(cls, user): return [ji.channel for ji in ChannelJoinInfo.by_user(user.id)]