def test_standup_start(): '''test for start function''' clear() #create a user and take the token user1 = auth.auth_register('*****@*****.**', 'password', 'FirstN', 'LastN') user1 = auth.auth_login('*****@*****.**', 'password') u1_token = user1['token'] #create a channel by user1 in channels and return its channel id channel_1_id = channels.channels_create(u1_token, 'team', True).get('channel_id') channels_t = data.return_channels() time_str1 = channels_t[0]['standup'][ 'finish_time'] #get the finish time before the standup starts now = datetime.datetime.utcnow() timestamp = int(now.replace(tzinfo=datetime.timezone.utc).timestamp()) time_str3 = timestamp #start a standup standup.standup_start(u1_token, channel_1_id, 1) channels_t = data.return_channels() time_str2 = channels_t[0]['standup'][ 'finish_time'] #get the finish time after the standup starts assert time_difference(time_str1, time_str2) < 0 assert time_difference(time_str2, time_str3) == 1 time.sleep(2)
def test_channels_create(): clear() #create a user and take its id and token user1 = auth.auth_register('*****@*****.**', 'password', 'FirstN', 'LastN') u1_id = user1['u_id'] u1_token = user1['token'] #check the error when the channelname is too long with pytest.raises(InputError): channels.channels_create(u1_token, 'A_very_very_very_ver_long_name', True) #create a channel in channels and return its channel id channel_1_id = channels.channels_create(u1_token, 'team', True).get('channel_id') #assert channel_1_id is int assert data.return_channels()[-1]['name'] == 'team' assert data.return_channels()[-1]['channel_id'] == channel_1_id assert data.return_channels()[-1]['is_public'] == True assert data.return_channels()[-1]['owner_members'][0]['u_id'] == u1_id assert data.return_channels( )[-1]['owner_members'][0]['name_first'] == 'FirstN' assert data.return_channels( )[-1]['owner_members'][0]['name_last'] == 'LastN' assert data.return_channels()[-1]['all_members'][0]['u_id'] == u1_id assert data.return_channels( )[-1]['all_members'][0]['name_first'] == 'FirstN' assert data.return_channels()[-1]['all_members'][0]['name_last'] == 'LastN'
def test_create(url): ''' testing creation of channels ''' # clear out the databases requests.delete(url + 'clear', json={}) # register a new user token = register_user(url, '*****@*****.**', 'emilyisshort', 'Emily', 'Luo').get('token') # create a new channel resp = requests.post(url + '/channels/create', json={ 'token': token, 'name': 'wtv channel', 'is_public': True }) # load text text = json.loads(resp.text) # test that channel actually exists check = False for channel in data.return_channels(): if channel.get('channel_id') == text.get('channel_id'): check = True break assert check
def edit_msg_react_in_list(msg, uid, method): """Interate the messages list by its id, return the message after edit.""" # get the channels channels = data.return_channels() messages = data.return_messages() # modify in channel for i in channels: if i['channel_id'] == msg['channel_id']: for temp in i['message']: if temp['message_id'] == msg['message_id']: if method == 'add': temp['reacts'][0]["u_ids"].append(int(uid)) elif method == 'delete': temp['reacts'][0]["u_ids"].remove(int(uid)) # modify in msg.json for temp in messages: if temp['message_id'] == msg['message_id']: if method == 'add': temp['reacts'][0]["u_ids"].append(int(uid)) elif method == 'delete': temp['reacts'][0]["u_ids"].remove(int(uid)) # add it to memory data.replace_channels(channels) data.replace_messages(messages)
def test_invite(url): ''' testing channel_invite requests ''' # clear out the databases requests.delete(url + 'clear', json={}) # register a new user and create a new channel user1 = register_user(url, '*****@*****.**', 'emilyisshort', 'Emily', 'Luo') channels = create_channels(url, user1.get('token'), True, 1) # create second user to invite user2 = register_user(url, '*****@*****.**', 'emilyisshort2', 'Emily2', 'Luo2') # invite user2 invite_user(url, user1, channels[0].get('channel_id'), user2) # check if both members are in channel check = False user_list = [{'u_id': user1.get('u_id')}, {'u_id': user2.get('u_id')}] for channel in data.return_channels(): if channel['channel_id'] == channels[0]['channel_id']: check = check_user_list(channel['all_members'], user_list) break assert check
def create_channels(url, token, is_public, num): ''' create a specified number of channels ''' channel_list = [] for i in range(num): # add a channel channel_name = 'channel' + str(i) channel_id = json.loads( requests.post(url + '/channels/create', json={ 'token': token, 'name': channel_name, 'is_public': str(is_public) }).text).get('channel_id') # find the channel data structure channel = None for chan in data.return_channels(): if chan.get('channel_id') == channel_id: channel = chan break channel_list.append(channel) return channel_list
def send_random_messages(channel_id, num): ''' send a number of random messages''' # characters to use in the message characters = string.ascii_letters + string.digits + string.punctuation + string.whitespace # find which channels there are focus_channel = None channels = data.return_channels() for channel in channels: if channel['channel_id'] == channel_id: focus_channel = channel break # add random messages to list messages = [] for i in range(num): msg = create_random_message(characters, i) return_message = { 'message_id': i, 'u_id': str(random.choice(focus_channel['all_members'])['u_id']), 'message': msg, 'time_created': i, } messages.append(return_message) # add the message to channel for i in channels: if i['channel_id'] == channel_id: i['messages'] = messages break # add that to persistent storage data.replace_channels(channels) return messages
def find_channel(channel_id): """Interate the channels list by its id, return the channel we need.""" answer = None for i in data.return_channels(): if i['channel_id'] == channel_id: answer = i break return answer
def is_channel_public(channel_id): """To indicate if the channel is public.""" is_public = False for channel in data.return_channels(): if channel['channel_id'] == channel_id: is_public = channel['is_public'] return is_public
def number_of_owners(channel_id): """Return the total number of owners.""" test = None for chan in data.return_channels(): if chan['channel_id'] == channel_id: test = chan return len(test['owner_members'])
def add_owner_in_channel(channel_id, owners): """Add a member into the owner list.""" channels = data.return_channels() for users in channels: if users['channel_id'] == channel_id: users['owner_members'].append(owners) data.replace_channels(channels)
def remove_whole_channel(channel_id): """If no owner exist, remove the whole channel.""" channels = data.return_channels() for chan in channels: if chan['channel_id'] == channel_id: channels.remove(chan) data.replace_channels(channels)
def test_standup_all_message_sent_out_twice(): '''check if the message in message package has been sent out after ending''' clear() #create three user and take their id and token user1 = auth.auth_register('*****@*****.**', 'password', 'FirstN1', 'LastN1') user1 = auth.auth_login('*****@*****.**', 'password') u1_token = user1['token'] u1_id = user1['u_id'] user2 = auth.auth_register('*****@*****.**', 'password', 'FirstN2', 'LastN2') user2 = auth.auth_login('*****@*****.**', 'password') u2_token = user2['token'] u2_id = user2['u_id'] user3 = auth.auth_register('*****@*****.**', 'password', 'FirstN3', 'LastN3') user3 = auth.auth_login('*****@*****.**', 'password') u3_token = user3['token'] u3_id = user3['u_id'] #create a channel by user1 in channels and invite other two users channel_1_id = channels.channels_create(u1_token, 'team', True).get('channel_id') channel.channel_invite(u1_token, channel_1_id, u2_id) channel.channel_invite(u1_token, channel_1_id, u3_id) #start a standup by u1 standup.standup_start(u1_token, channel_1_id, 3) standup.standup_send(u1_token, channel_1_id, 'WE') standup.standup_send(u2_token, channel_1_id, 'ARE') standup.standup_send(u3_token, channel_1_id, 'FRIENDS') #sleep until the standup ending time.sleep(4) #start another standup by u2 standup.standup_start(u2_token, channel_1_id, 3) standup.standup_send(u1_token, channel_1_id, 'SHE') standup.standup_send(u2_token, channel_1_id, 'HE') standup.standup_send(u3_token, channel_1_id, 'THEY') #sleep until the standup ending time.sleep(4) channels_t = data.return_channels() m_c_1 = channels_t[0]['message'][1] m_c_2 = channels_t[0]['message'][0] #check the message has been sent assert m_c_1['u_id'] == u1_id assert m_c_2['u_id'] == u2_id assert m_c_1['message'] == 'FirstN1: WE\nFirstN2: ARE\nFirstN3: FRIENDS\n' assert m_c_2['message'] == 'FirstN1: SHE\nFirstN2: HE\nFirstN3: THEY\n' #check the message package assert channels_t[0]['standup'][ 'message_package'] == 'FirstN1: SHE\nFirstN2: HE\nFirstN3: THEY\n'
def rm_owner_in_channel(channel_id, owners): """Remove a member from the owner list.""" channels = data.return_channels() for users in channels: if users['channel_id'] == channel_id: for onrs in users['owner_members']: if onrs['u_id'] == owners: users['owner_members'].remove(onrs) data.replace_channels(channels)
def remove_a_member_in_channel(u_id, channel_id): """Remove the member by user if from the channel.""" channels = data.return_channels() for users in channels: if users['channel_id'] == channel_id: for member in users['all_members']: if member['u_id'] == u_id: users['all_members'].remove(member) data.replace_channels(channels)
def adding_message(return_message, channel_id): """Adding given return_message in the whole list.""" # get the channels channels = data.return_channels() # add user into memory for i in channels: if i['channel_id'] == channel_id: i['message'].insert(0, return_message) # add it to memory data.replace_channels(channels) data.insert_messages(return_message)
def add_one_in_channel(channel_id, user): """Adding a member into the channel.""" # get the channels channels = data.return_channels() # add user into memory for i in channels: if i['channel_id'] == channel_id: i['all_members'].append(user) if check_permission(user['u_id']) == 1: i['owner_members'].append(user) # add it to memory data.replace_channels(channels)
def delete_msg_in_list(msg): """Interate the messages list by its id, return the message we need.""" # get the channels channels = data.return_channels() messages = data.return_messages() # deleting message from memory for i in channels: if i['channel_id'] == msg['channel_id']: i['message'].remove(msg) messages.remove(msg) # add it to memory data.replace_channels(channels) data.replace_messages(messages)
def channels_create(token, name, is_public): """Create a new empty channel.""" if len(name) > 20: # The length of channel name should <= 20. raise InputError('The length of channel name should <= 20') i = owner_from_token(token) owner_id = i['u_id'] owner_fn = i['name_first'] owner_ln = i['name_last'] standup = { 'finish_time':-1, 'message_package':'', } channel_id = create_channel_id(data.return_channels()) channel_new = { # Initialize the new channel. 'name': name, 'channel_id':channel_id, 'owner_members': [ { 'u_id': owner_id, 'name_first': owner_fn, 'name_last': owner_ln, 'profile_img_url':'' } ], 'all_members': [ { 'u_id': owner_id, 'name_first': owner_fn, 'name_last': owner_ln, 'profile_img_url':'' } ], 'is_public': is_public, 'message':[], 'standup': standup } data.append_channels(channel_new) # Add this new channel into data. return { 'channel_id': channel_id }
def channels_listall(token): """Just return all channels? Sure about that?""" # check that token exists owner_from_token(token) channel_temp = data.return_channels() all_channels = [] for i in channel_temp: temp = { 'channel_id': i['channel_id'], 'name': i['name'], } all_channels.append(temp) return { 'channels': all_channels, }
def edit_msg_in_list(msg, text): """Interate the messages list by its id, return the message after edit.""" # get the channels channels = data.return_channels() messages = data.return_messages() # deleting message from memory for i in channels: if i['channel_id'] == msg['channel_id']: for temp in i['message']: if temp['message_id'] == msg['message_id']: temp['message'] = text for temp in messages: if temp['message_id'] == msg['message_id']: temp['message'] = text # add it to memory data.replace_channels(channels) data.replace_messages(messages)
def change_msg_pin(msg, sign): """Interate the messages list by its id, return the message after edit.""" # get the channels channels = data.return_channels() messages = data.return_messages() # deleting message from memory for i in channels: if i['channel_id'] == msg['channel_id']: for temp in i['message']: if temp['message_id'] == msg['message_id']: temp['is_pinned'] = sign for temp in messages: if temp['message_id'] == msg['message_id']: temp['is_pinned'] = sign # add it to memory data.replace_channels(channels) data.replace_messages(messages)
def search(token, query_str): """Search the message with the specific query_str.""" # check that token exists user = owner_from_token(token) id_from = user.get('u_id') mes_list = [] chan_list = [] channels = data.return_channels() for i in channels: for j in i['all_members']: if id_from == j['u_id']: chan_list.append(i['channel_id']) # make the query string to all_lower # this will make query case insensitive query_str = query_str.lower() # make query string to ignore whitespace query_str = "".join(query_str.split()) messages = data.return_messages() for i in messages: if i['channel_id'] in chan_list: # focus on the channels which is joinned by the user # make the message lowercase and ignoring whitespace working_msg = "".join(i['message'].lower().split()) if query_str in working_msg: added_message = { "message_id": i['message_id'], "u_id": i['u_id'], "message": i['message'], "time_created": i['time_created'], 'reacts': i['reacts'], 'is_pinned': i['is_pinned'], } mes_list.append(added_message) for msg in mes_list: msg['reacts'][0]['is_this_user_reacted'] = False if id_from in msg['reacts'][0]['u_ids']: msg['reacts'][0]['is_this_user_reacted'] = True return {'messages': mes_list}
def create_channels(url, token, is_public, num): ''' create a specified number of channels ''' channel_list = [] for i in range(num): # add a channel channel_name = 'channel' + str(i) channel_id = send_request('POST', url, 'channels/create', { 'token': token, 'name': channel_name, 'is_public': str(is_public) }).get('channel_id') # add the channel object, not just the channel itself for channel in data.return_channels(): if channel['channel_id'] == channel_id: # add it to the channel list channel_list.append(channel) return channel_list
def channels_list(token): """Need to fix implementation.""" # find the token u_id = owner_from_token(token).get('u_id') channel_temp = data.return_channels() list_channel = [] for i in channel_temp: for j in i['all_members']: if u_id == j['u_id']: temp = { 'channel_id': i['channel_id'], 'name': i['name'], } list_channel.append(temp) return { 'channels': list_channel, }
def test_standup_send(): '''test for the send function''' clear() #create three user and take their id and token user1 = auth.auth_register('*****@*****.**', 'password', 'FirstN1', 'LastN1') user1 = auth.auth_login('*****@*****.**', 'password') u1_token = user1['token'] user2 = auth.auth_register('*****@*****.**', 'password', 'FirstN2', 'LastN2') user2 = auth.auth_login('*****@*****.**', 'password') u2_token = user2['token'] u2_id = user2['u_id'] user3 = auth.auth_register('*****@*****.**', 'password', 'FirstN3', 'LastN3') user3 = auth.auth_login('*****@*****.**', 'password') u3_token = user3['token'] u3_id = user3['u_id'] #create a channel by user1 in channels and invite other two users channel_1_id = channels.channels_create(u1_token, 'team', True).get('channel_id') channel.channel_invite(u1_token, channel_1_id, u2_id) channel.channel_invite(u1_token, channel_1_id, u3_id) #start a standup standup.standup_start(u1_token, channel_1_id, 2) standup.standup_send(u1_token, channel_1_id, 'WE') standup.standup_send(u2_token, channel_1_id, 'ARE') standup.standup_send(u3_token, channel_1_id, 'FRIENDS') channels_t = data.return_channels() m_l = channels_t[0]['standup']['message_package'] assert m_l == 'FirstN1: WE\nFirstN2: ARE\nFirstN3: FRIENDS\n' time.sleep(3)