def message_send(token, channel_id, text): data = channel_utils.get_data() # Check valid token if authentication.decode_token(token) == None: raise ValueError("Invalid token provided.") u_id = authentication.decode_token(token)["u_id"] # check the message <= 1000 characters if len(text) > 1000: raise ValueError("Message is more than 1000 characters") # check if user is an admin, owner or a member of the channel channel_deats = channel_utils.channel_details(token, channel_id) if u_id not in channel_deats['all_members']: raise errors.AccessError( "The user isn't authorised to post in this channel") # generate a message_id, and post it message = message_create(u_id, text, datetime.now()) # put the message onto a channel for channel in data['channels']: if channel['channelID'] == channel_id: break channel['messages'].insert(0, message) storage.dump_data_store(data) return message['message_id']
def message_remove(token, message_id): data = channel_utils.get_data() # Check valid token if authentication.decode_token(token) == None: raise ValueError("Invalid token provided.") u_id = authentication.decode_token(token)["u_id"] # find message through channel/list, then channel messages to find the correct message i = 0 while i < len(data["channels"]): j = 0 if u_id in data["channels"][i]["membersUID"]: while j < len(data["channels"][i]["messages"]): if message_id == data["channels"][i]["messages"][j]["message_id"]: if (u_id not in data["channels"][i]['ownersUID']) and \ (u_id not in data["channels"][i]["messages"][j]['u_id']): raise ValueError( "Neither the original poster of this message or an owner") del data["channels"][i]["messages"][j] storage.dump_data_store(data) return j += 1 i += 1 raise errors.AccessError("Message_id isn't in any channel you have access too") pass
def message_sendlater(token, channel_id, message, time_sent): data = channel_utils.get_data() # Check valid token if authentication.decode_token(token) == None: raise ValueError("Invalid token provided.") u_id = authentication.decode_token(token)["u_id"] # check the message <= 1000 characters if len(message) > 1000: raise ValueError("Message is more than 1000 characters.") # check if user is an admin, owner or a member of the channel channel_deats = channel_utils.channel_details(token, channel_id) if u_id not in channel_deats['all_members']: raise errors.AccessError( "The user isn't authorised to post in this channel.") # time in the future if time_sent < datetime.now(): raise ValueError("Time sent is in the past.") # generate a message_id, and set to post at correct time message = message_create(u_id, message, time_sent) # put the message into the channel for channel in data['channels']: if channel['channelID'] == channel_id: break channel['messages'].insert(0, message) # HOW TO DO TIME? storage.dump_data_store(data) return message['message_id']
def message_unpin(token, message_id): data = channel_utils.get_data() # Check valid token if authentication.decode_token(token) == None: raise ValueError("Invalid token provided.") u_id = authentication.decode_token(token)["u_id"] # if the token owner is an owner or an admin (p_id 1 and 2) # (search through all users for permission level) # look through all channels the user is apart of #print(channels['channels'][i]['channel_id']) i = 0 while i < len(data["channels"]): j = 0 while j < len(data["channels"][i]["messages"]): if message_id == data["channels"][i]["messages"][j]["message_id"]: if u_id in data["channels"][i]["ownersUID"]: if data["channels"][i]["messages"][j]["is_pinned"] == False: raise ValueError( "This message is already pinned") data["channels"][i]["messages"][j]["is_pinned"] = False storage.dump_data_store(data) return if u_id in data["channels"][i]["membersUID"]: raise ValueError("User isn't an admin") raise errors.AccessError("User isn't a member of the channel the message is in") j += 1 i += 1 raise ValueError("message_id isn't a valid message") pass
def message_unreact(token, message_id, react_id): data = channel_utils.get_data() # Check valid token if authentication.decode_token(token) == None: raise ValueError("Invalid token provided.") u_id = authentication.decode_token(token)["u_id"] # Valid react_id (Only 1 is currently valid) if react_id != 1: raise ValueError("not a valid react_id") # search through channel/list, then channel messages to find the correct message i = 0 #print(channels['channels'][i]['channel_id']) while i < len(data["channels"]): j = 0 if u_id in data["channels"][i]["membersUID"]: while j < len(data["channels"][i]["messages"]): if message_id == data["channels"][i]["messages"][j]["message_id"]: if data["channels"][i]["messages"][j]["reacts"][0]['is_this_user_reacted'] == False: raise ValueError( "You haven't reacted to this message with this reaction") data["channels"][i]["messages"][j]["reacts"][0]['is_this_user_reacted'] = False data["channels"][i]["messages"][j]["reacts"][0]['u_ids'].remove(u_id) storage.dump_data_store(data) return j += 1 i += 1 # check react id's raise ValueError("Not a valid message in a channel that the user is apart of")
def standup_start(token, channel_id): data = channel_utils.get_data() # Check valid token if authentication.decode_token(token) == None: raise ValueError("Invalid token provided.") u_id = authentication.decode_token(token)["u_id"] i = 0 while i < len(data["channels"]): if data["channels"][i]["channelID"] == channel_id: # check if currently running a stand_up if data["channels"][i]["standup_time"] >= datetime.datetime.now(): raise ValueError("This channel already has a standup started") elif u_id not in data["channels"][i]["membersUID"]: raise errors.AccessError( "This user isn't a member of the channel") else: # return time + 15 mins time = datetime.datetime.now() + datetime.timedelta(minutes=15) data["channels"][i]["standup_time"] = time storage.dump_data_store(data) return time i += 1 raise ValueError("Channel ID is not a valid channel")
def standup_send(token, channel_id, message): data = channel_utils.get_data() # Check valid token if authentication.decode_token(token) == None: raise ValueError("Invalid token provided.") u_id = authentication.decode_token(token)["u_id"] # check the message <= 1000 characters if len(message) > 1000: raise ValueError("Message is more than 1000 characters.") i = 0 while i < len(data["channels"]): if data["channels"][i]["channelID"] == channel_id: # check if currently running a stand_up if data["channels"][i]["standup_time"] <= datetime.datetime.now(): raise ValueError("This channel doesn't have a standup") elif u_id not in data["channels"][i]["membersUID"]: raise errors.AccessError( "This user isn't a member of the channel") else: timesent = data["channels"][i]["standup_time"] message_utils.message_sendlater(token, channel_id, message, timesent) return i += 1 raise ValueError("Channel ID is not a valid channel")
def search(token, query_str): data = channel_utils.get_data() # Check valid token if authentication.decode_token(token) == None: raise ValueError("Invalid token provided.") u_id = authentication.decode_token(token)["u_id"] if len(query_str) > 1000: raise ValueError("Messages cannot be more than 1000 characters") messages = [] # find the channels the user is apart of. i = 0 #print(channels['channels'][i]['channel_id']) while i < len(data["channels"]): j = 0 if u_id in data["channels"][i]["membersUID"]: while j < len(data["channels"][i]["messages"]): if query_str in data["channels"][i]["messages"][j]["message"]: messages.append(data["channels"][i]["messages"][j]) j += 1 i += 1 return messages
def test_message_edit(): # set up user storage.clear_data_store() dic1 = authentication.auth_register('*****@*****.**', 'password1', 'Bob', 'James') token1 = dic1['token'] # creat a valid channel in the name of user1 dic = channel_utils.channels_create(token1, 'name', False) channel_id_valid = dic['channel_id'] # Message to be edited message = "Unedited message" message_id = message_utils.message_send(token1, channel_id_valid, message) # When all of the following isn't true: # Meassage with message_id wasn't sent by the authorised user making the request # Meassage with message_id was not sent by an owner of this channel # Meassage with message_id was not sent by an admin or owner of the slack # VALUE ERROR dic2 = authentication.auth_register('*****@*****.**', 'password2', 'Bobby', 'Jamesy') token2 = dic2['token'] token = token2 message = "Editted message." with pytest.raises(errors.AccessError): message_utils.message_edit(token, message_id, message) # Message is too long (over 1000 characters). token = token1 message = "0123456789" * 101 with pytest.raises(ValueError): message_utils.message_edit(token, message_id, message) # Message_id is invalid. token = token1 bad_message_id = -99 message = "Editted message." with pytest.raises(errors.AccessError): message_utils.message_edit(token, bad_message_id, message) # Everything valid. token = token1 message = "Editted message." message_utils.message_edit(token, message_id, message) # Prove that it worked data = channel_utils.get_data() assert data["channels"][0]['messages'][0] == "Editted message." pass
def test_standup_start(): storage.clear_data_store() # set up user dic1 = authentication.auth_register('*****@*****.**', 'password1', 'Bob', 'James') token1 = dic1['token'] # creat a valid channel in the name of user1 dic = channel_utils.channels_create(token1, 'name', False) channel_id_valid = dic['channel_id'] # set up 2nd user dic2 = authentication.auth_register('*****@*****.**', 'password11', 'Bobby', 'Jamesy') token2 = dic2['token'] # Valid token, valid channel_id, but no access: usertoken = token2 channel_id = channel_id_valid with pytest.raises(errors.AccessError): standup.standup_start(usertoken, channel_id) # Valid token, valid channel_id: usertoken = token1 channel_id = channel_id_valid time = standup.standup_start(usertoken, channel_id) data = channel_utils.get_data() assert time < datetime.datetime.now() + datetime.timedelta(minutes=16) assert time > datetime.datetime.now() + datetime.timedelta(minutes=14) assert time == data["channels"][0]["standup_time"] # invalid token, valid channel_id: usertoken = "invalidToken" channel_id = channel_id_valid with pytest.raises(ValueError): standup.standup_start(usertoken, channel_id) # valid token, invalid channel_id: usertoken = token1 channel_id = 9191 with pytest.raises(ValueError): standup.standup_start(usertoken, channel_id)
def test_standup_send(): storage.clear_data_store() # set up user dic1 = authentication.auth_register('*****@*****.**', 'password1', 'Bob', 'James') token1 = dic1['token'] # creat a valid channel in the name of user1 dic = channel_utils.channels_create(token1, 'name', False) channel_id_valid = dic['channel_id'] # No active standup usertoken = token1 channel_id = channel_id_valid message = "No start up" with pytest.raises(ValueError): standup.standup_send(usertoken, channel_id, message) # Start the standup standup.standup_start(usertoken, channel_id) # Valid token, valid channel_id: usertoken = token1 channel_id = channel_id_valid message = "Stand up active" standup.standup_send(usertoken, channel_id, message) data = channel_utils.get_data() assert data["channels"][0]["standup_time"] == data["channels"][0][ "messages"][0]["time_created"] # Error when Channel does not exist message = "Hello There" usertoken = "mytoken1234" channel_id = 12123 # this is an invalid channel with pytest.raises(ValueError): standup.standup_send(usertoken, channel_id, message) # Error when message > 1000 chars message = "H" * 1001 usertoken = "mytoken1234" channel_id = 1 with pytest.raises(ValueError): standup.standup_send(usertoken, channel_id, message) # set up 2nd user, which isn't a member of the channel dic2 = authentication.auth_register('*****@*****.**', 'password12', 'Boby', 'Jamesy') token2 = dic2['token'] # AccessError if the user is authorised, but not a member of the channel message = "Hello There" usertoken = token2 channel_id = channel_id_valid with pytest.raises(errors.AccessError): standup.standup_send(usertoken, channel_id, message) # Error if user doesn't exist message = "Hello There" usertoken = "mytoken1234" channel_id = channel_id_valid with pytest.raises(ValueError): standup.standup_send(usertoken, channel_id, message)