def test_message_pin(): ''' Function tests for message_pin ''' #Initialisation global_var.initialise_all() #Create users owner = auth_functions.auth_register("*****@*****.**", "pass123", \ "Sally", "Bob") assert owner == {"u_id": 0, "token": encode_token_for_u_id(0)} owner_token = owner["token"] user = auth_functions.auth_register("*****@*****.**", "pass123", \ "Rayden", "Smith") token = user["token"] #owner that creates the channel and so is the owner channel = channel_functions.channels_create(owner_token, "Name", True) channel_id = channel["channel_id"] #user joins the channel channel_functions.channel_join(token, channel_id) #user sends 3 messages assert funcs.message_send(token, channel_id, "This is a valid message") \ == {"message_id" : 0} assert funcs.message_send(token, channel_id, \ "This is another valid message") == {"message_id" : 1} assert funcs.message_send(owner_token, channel_id, \ "This is not your message") == {"message_id" : 2} #Init finished #A invalid token is sent to the function # (A invalid user is trying to use the function) with pytest.raises(AccessError, match="Invalid token"): funcs.message_pin("111111", 0) #A user is not an admin with pytest.raises(ValueError, match="User is not an admin"): funcs.message_pin(token, 0) #A admin user successfully pins a message assert funcs.message_pin(owner_token, 0) == {} #Message is invalid based on message_id with pytest.raises(ValueError, match="Message does not exist"): funcs.message_pin(owner_token, -1) #Message is already pinned with pytest.raises(ValueError, match="Message is currently pinned"): funcs.message_pin(owner_token, 0) #Admin leaves channel channel_functions.channel_leave(owner_token, channel_id) #Admin is not a member of the channel with pytest.raises(AccessError, match=\ "Authorised user is not a member of the channel"): funcs.message_pin(owner_token, 1)
def test_succ_left(): reset_data() A = auth_register("*****@*****.**", 'HoyaLee2019', "Hoya", "Lee") Pub_channel = CF.channels_create(A['token'], 'Num4', 'True') token = A['token'] channelID = Pub_channel['channel_id'] CF.channel_leave(token, channelID)
def test_message_send(): ''' Function tests for message_send ''' #Initialisation global_var.initialise_all() assert global_var.data["users"] == [] #Creates an user user = auth_functions.auth_register("*****@*****.**", "pass123", \ "Rayden", "Smith") assert user == {"u_id": 0, "token": encode_token_for_u_id(0)} token = user["token"] assert global_var.data["users"] != [] #User creates a channel channel = channel_functions.channels_create(token, "ChannelName", True) assert channel == {"channel_id": 0} channel_id = channel["channel_id"] #Initialisation finished #User successfully sends message to created channel assert funcs.message_send(token, channel_id, \ "This is a valid message") == {"message_id": 0} #A invalid token is sent to the function with pytest.raises(AccessError, match="Invalid token"): funcs.message_send("111111", channel_id, "This is a valid message") #The channel based on ID does not exist with pytest.raises(ValueError, match="Invalid Channel ID"): funcs.message_send(token, -1, "This is a valid message") #Sending a message of length 1000 is valid assert funcs.message_send(token, channel_id, create_long_string()) \ == {"message_id" : 1} #A message of length greater than 1000 with pytest.raises(ValueError, match="Message length too long"): funcs.message_send(token, channel_id, "1" + create_long_string()) #Thrown multiple errors, token error is caught first with pytest.raises(AccessError, match="Invalid token"): funcs.message_send("111111", -1, "1" + create_long_string()) #User leaves channel channel_functions.channel_leave(token, channel_id) #User cannot send message to channel he is not apart of with pytest.raises(AccessError, \ match="Authorised user is not a member of the channel"): funcs.message_send(token, channel_id, "This is a valid message")
def channel_leave(): """ Removes a user from a channel """ token = request.form.get("token") channel_id = to_int(request.form.get("channel_id")) return dumps(channel.channel_leave(token, channel_id))
def test_channel_join(): ''' Function tests for channel_join ''' #Initialisation global_var.initialise_all() user1 = auth_register("*****@*****.**", \ "valid_correct_password", "valid_correct_first_name",\ "valid_correct_last_name") token1 = user1["token"] user2 = auth_register("*****@*****.**", \ "valid_correct_password", "valid_correct_first_name", \ "valid_correct_last_name") token2 = user2["token"] #user 2 create a channel channel = func.channels_create(token1, "PublicChannel", True) channel_ids = channel["channel_id"] # Initialisation finished #user 2 create a private channel channel2 = func.channels_create(token1, "PrivateChannel", False) channel_private = channel2["channel_id"] assert func.channel_join(token2, channel_ids) == {} #if given an invalid channel_id with pytest.raises(ValueError, match="Channel does not exist"): func.channel_join(token1, 100) # The function is called using a invalid token with pytest.raises(AccessError, match="Invalid token"): func.channel_join("1234asdasd5", 1) #if channel is private and user is not the admin with pytest.raises(AccessError, match=\ "Channel is private and user is not admin"): func.channel_join(token2, channel_private) # A slackr owner leaves a private channel and can join back in func.channel_leave(token1, channel_ids) func.channel_join(token1, channel_ids)
def test_channel_invite(): ''' Testing for inviting a user to a channel ''' #Initialisation global_var.initialise_all() # Create an user who owns the channel user1 = auth_register("*****@*****.**", "password123", \ "Rayden", "Smith") token_1 = user1["token"] # Create user that is invited to channel user2 = auth_register("*****@*****.**", "thisisapassword", \ "Bob", "Sally") token_2 = user2["token"] userid_2 = user2["u_id"] # Create user that will not be part of channel user3 = auth_register("*****@*****.**", "arandompassword", \ "Coen", "Kevin") token_3 = user3["token"] # Create a channel channel = func.channels_create(token_1, "TestChannel1", True) channel_id = channel["channel_id"] # Initialisation finished # User2 is successfully invited to channel assert func.channel_invite(token_1, channel_id, userid_2) == {} # If the invite was successful, user2 can send a message assert message_send(token_2, channel_id, "A successful message") \ == {"message_id" : 0} # User leaves the channel assert func.channel_leave(token_2, channel_id) == {} # User is not invited if given an invalid token with pytest.raises(AccessError, match="Invalid token"): func.channel_invite("12345", channel_id, userid_2) # User is not inivited if given an invalid channel_id with pytest.raises(ValueError, match="Channel does not exist"): func.channel_invite(token_1, 100, userid_2) # Inviting user is not apart of the channel with pytest.raises(AccessError, \ match="Authorised user is not a member of the channel"): func.channel_invite(token_3, channel_id, userid_2) # If user being invited is not an user with pytest.raises(ValueError, match="Invalid User ID"): func.channel_invite(token_1, channel_id, 111111)
def test_channel_leave(): ''' Function tests for channel_leave ''' #Initialisation global_var.initialise_all() user1 = auth_register("*****@*****.**", \ "valid_correct_password", "valid_correct_first_name", \ "valid_correct_last_name") token1 = user1["token"] #create a channel channel = func.channels_create(token1, "TestChannel", True) channel_id = channel["channel_id"] # Initialisation finished # User is in channel so can send message message_send(token1, channel_id, "can send message") # User has left channel and so can't send messages assert func.channel_leave(token1, channel_id) == {} with pytest.raises(AccessError, \ match="Authorised user is not a member of the channel"): message_send(token1, channel_id, "can't send message") # The function is called using a invalid token with pytest.raises(AccessError, match="Invalid token"): func.channel_leave("12345", channel_id) #If given an invalid channel_id with pytest.raises(ValueError, match="Channel does not exist"): func.channel_leave(token1, 100) # The function is called using a invalid token with pytest.raises(AccessError, match="Invalid token"): func.channel_leave("12345", channel_id) # User is already removed, but will not cause Error assert func.channel_leave(token1, channel_id) == {}
def test_message_sendlater(): ''' Function tests for message_sendlater ''' #Initialisation global_var.initialise_all() assert global_var.data["users"] == [] #Create a user user = auth_functions.auth_register("*****@*****.**", "pass123", \ "Rayden", "Smith") assert global_var.data["users"] != [] token = user["token"] #User creates a channel channel = channel_functions.channels_create(token, "ChannelName", True) channel_id = channel["channel_id"] #Init finished #User sends message to created channel assert funcs.message_sendlater(token, channel_id, \ "This is a valid message", datetime.datetime.now().timestamp() + 1) \ == {"message_id": 0} time.sleep(1) #Sending a message of length 1000 is valid assert funcs.message_sendlater(token, channel_id, create_long_string(), \ datetime.datetime.now().timestamp() + 1) == {"message_id": 1} time.sleep(1) #A message of length greater than 1000 is valid with pytest.raises(ValueError, match="Message length too long"): funcs.message_sendlater(token, channel_id, "1" + create_long_string(), \ datetime.datetime.now().timestamp() + 1) #An exception is thrown if a invalid token is given with pytest.raises(AccessError, match="Invalid token"): funcs.message_sendlater("111111", channel_id, \ "This is a valid message", datetime.datetime.now().timestamp() + 1) #The channel based on ID does not exist with pytest.raises(ValueError, match="Invalid Channel ID"): funcs.message_sendlater(token, -1, "This is a valid message", \ datetime.datetime.now().timestamp() + 1) #Time sent is a time in the past with pytest.raises(ValueError, match="Time sent was in the past"): funcs.message_sendlater(token, channel_id, "This is a valid message", \ datetime.datetime.now().timestamp() -1) #All errors (Token error is caught first) with pytest.raises(AccessError, match="Invalid token"): funcs.message_sendlater("111111", -1, "1" + create_long_string(), \ datetime.datetime.now().timestamp() + 1) #User leaves channel channel_functions.channel_leave(token, channel_id) #User cannot send message to channel he is not apart of with pytest.raises(AccessError, \ match="Authorised user is not a member of the channel"): funcs.message_sendlater(token, channel_id, "This is a valid message", \ datetime.datetime.now().timestamp() + 1)