def test_leave_notLogin(): token = A['token'] auth_logout(token) channelID = Pub_channel['channel_id'] with pytest.raises(CF.AccessError): CF.channel_join(token, channelID)
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_message_react(): ''' Function tests for message_react ''' #Initialisation global_var.initialise_all() user = auth_functions.auth_register("*****@*****.**", "pass123", \ "Rayden", "Smith") token = user["token"] user_2 = auth_functions.auth_register("*****@*****.**", "password", \ "Bob", "Sally") token_2 = user_2["token"] #User creates a channel channel = channel_functions.channels_create(token, "Name", True) channel_id = channel["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(token, channel_id, \ "This is your message") == {"message_id" : 2} #Initialisation finished # User not a member of channel with pytest.raises(AccessError, match=\ "Authorised user is not a member of the channel"): funcs.message_react(token_2, 0, LIKE_REACT) channel_functions.channel_join(token_2, channel_id) #An invalid token is sent to the function with pytest.raises(AccessError, match="Invalid token"): funcs.message_react("111111", 0, LIKE_REACT) #A user successfully reacts assert funcs.message_react(token, 0, LIKE_REACT) == {} #A user successfully reacts - with a different react (Not implemented in frontend) assert funcs.message_react(token, 0, HEART_REACT) == {} #Another user also reacts to the same message assert funcs.message_react(token_2, 0, LIKE_REACT) == {} #An user tries to react to a invalid message based on message_id with pytest.raises(ValueError, match="Message does not exist"): funcs.message_react(token, -1, LIKE_REACT) #An user tries to react to a message already reacted to with pytest.raises(ValueError, match="Message contains an active react"): funcs.message_react(token, 0, LIKE_REACT) #An user uses a invalid react id with pytest.raises(ValueError, match="Invalid React ID"): funcs.message_react(token, 1, -1)
def test_standup_send(): ''' Test functions for standup_send ''' data.initialise_all() # A message is buffered in the standup queue - Owner owner = auth.auth_register("*****@*****.**", "valid_password", "a", "b") channel = channel_func.channels_create(owner["token"], "Owner", True) length = 2 # Channel is not currently in standup mode with pytest.raises(ValueError, match="Not Currently In Standup"): standup_send(owner["token"], channel["channel_id"], \ "correct_and_valid_message") standup_start(owner["token"], channel["channel_id"], length) assert standup_send(owner["token"], channel["channel_id"], \ "correct_and_valid_message") == {} # A invalid token is used to access the function with pytest.raises(AccessError, match="Invalid token"): standup_send("123456", channel["channel_id"], length) user = auth.auth_register("*****@*****.**", "valid_password", "a", "b") # User tries to send message but has not joined channel with pytest.raises(AccessError, match="Cannot Access Channel"): standup_send(user["token"], channel["channel_id"], \ "correct_and_valid_message") # A message is buffered in the standup queue - Member channel_func.channel_join(user["token"], channel["channel_id"]) assert standup_send(user["token"], channel["channel_id"], \ "correct_and_valid_message") == {} # Channel given does not exist with pytest.raises(ValueError, match="Channel Does Not Exist"): standup_send(user["token"], 523523523, \ "correct_and_valid_message") # The message sent was greater than max message length with pytest.raises(ValueError, match="Message Too Long"): long_string = "a" * 1001 standup_send(user["token"], channel["channel_id"], long_string) # Check still works in private channel private = channel_func.channels_create(owner["token"], "Owner", False) standup_start(owner["token"], private["channel_id"], length) assert standup_send(owner["token"], private["channel_id"], \ "correct_and_valid_message") == {} time.sleep(2)
def test_channel_Succjoined(): reset_data() A = auth_register("*****@*****.**", "wsad1990", "Good", "Morning") Pub_channel = CF.channels_create(A['token'], 'Num3', 'True') token = A['token'] channelID = Pub_channel['channel_id'] CF.channel_join(token, channelID)
def test_message_remove(): ''' Function tests for message_remove ''' #Initialisation global_var.initialise_all() assert global_var.data["users"] == [] #Create users owner = auth_functions.auth_register("*****@*****.**", "pass123", \ "Sally", "Bob") owner_token = owner["token"] user = auth_functions.auth_register("*****@*****.**", "pass123", \ "Rayden", "Smith") token = user["token"] assert global_var.data["users"] != [] #owner creates a channel and so is the owner channel = channel_functions.channels_create(owner_token, "Name", True) assert channel == {"channel_id": 0} channel_id = channel["channel_id"] #user joins the channel channel_functions.channel_join(token, channel_id) #3 messages are sent 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} #Initialisation finished #A invalid token is sent to the function with pytest.raises(AccessError, match="Invalid token"): funcs.message_remove("111111", 0) #owner successfully removes a message assert funcs.message_remove(owner_token, 0) == {} # As message was removed, it can not be removed again with pytest.raises(ValueError, match="Message does not exist"): funcs.message_remove(owner_token, 0) #user successfully removes his own message assert funcs.message_remove(token, 1) == {} #A owner tries to remove a invalid message based on message_id with pytest.raises(ValueError, match="Message does not exist"): funcs.message_remove(owner_token, -1) #A user tries to remove a invalid message based on message_id with pytest.raises(ValueError, match="Message does not exist"): funcs.message_remove(token, -1) #user is unable to remove a message not his/her's with pytest.raises(AccessError, match="User does not have permission"): funcs.message_remove(token, 2)
def test_channel_addowner(): ''' Function tests for channel_addowner ''' #Initialisation global_var.initialise_all() owner = auth_register("*****@*****.**", \ "valid_correct_password", "valid_correct_first_name", \ "valid_correct_last_name") token_owner = owner["token"] user1 = auth_register("*****@*****.**", \ "valid_correct_password", "valid_correct_first_name",\ "valid_correct_last_name") token1 = user1["token"] userid1 = user1["u_id"] user2 = auth_register("*****@*****.**",\ "valid_correct_password", "valid_correct_first_name", \ "valid_correct_last_name") userid2 = user2["u_id"] token2 = user2["token"] #owner creates a channel and is automatically the owner channel = func.channels_create(token_owner, "TestChannel", True) channel_id = channel["channel_id"] # Initialisation finished #if given an invalid channel_id with pytest.raises(ValueError, match="Channel does not exist"): func.channel_addowner(token_owner, 100, userid1) #token 1&2 joins channel that owner created func.channel_join(token1, channel_id) func.channel_join(token2, channel_id) #token1 is not an owner of the channel with pytest.raises(AccessError, match="User is not an owner of the channel"): func.channel_addowner(token1, channel_id, userid2) # owner successfully promotes user to owner assert func.channel_addowner(token_owner, channel_id, userid1) == {} # user 1 can now promote other to owner assert func.channel_addowner(token1, channel_id, userid2) == {} #token1/userid1 is already an owner of the channel with pytest.raises(ValueError, match="User is already an owner of the channel"): func.channel_addowner(token_owner, channel_id, userid1) # The function is called using a invalid token with pytest.raises(AccessError, match="Invalid token"): func.channel_addowner("12345", channel_id, userid1)
def test_delete_NotOwner(): reset_data() A = auth_register("*****@*****.**", "wsad1990", "Good", "Morning") B = auth_register("*****@*****.**", "wsad1990", "Bad", "Night") Pub_channel = CF.channels_create(A['token'], 'DeleteChannel', 'True') token = B['token'] channelID = Pub_channel['channel_id'] CF.channel_join(token, channelID) with pytest.raises(CF.ValueError): CF.channel_delete(token, channelID)
def test_noAdmain_join_list(): reset_data() A = auth_register("*****@*****.**", 'HoyaLee2019', "Hoya", "Lee") B = auth_register("*****@*****.**", "wsad1990", "Good", "Morning") Pub_channel = CF.channels_create(A['token'], 'Num2', 'True') channelID = Pub_channel['channel_id'] token1 = B['token'] CF.channel_join(token1, channelID) CF.channels_list(token1)
def test_memeber_addOwner(): reset_data() A = auth_register("*****@*****.**", 'HoyaLee2019', "Hoya", "Lee") B = auth_register("*****@*****.**", "wsad1990", "Good", "Morning") Pub_channel = CF.channels_create(A['token'], 'Test1', 'True') CF.channel_join(B['token'], Pub_channel['channel_id']) token = A['token'] u_id = B['u_id'] channelID = Pub_channel['channel_id'] CF.channel_addowner(token, channelID, u_id)
def test_search_otherMember_success(): reset_data() B = auth_register("*****@*****.**", "wsad1990", "Good", "Morning") A = auth_register("*****@*****.**", 'HoyaLee2019', 'Hoya', 'Lee') Pub_channel = CF.channels_create(A['token'], 'Num0', 'True') CF.channel_join(B['token'], Pub_channel['channel_id']) token = B['token'] query_str = 'Hello' OF.search(token, query_str)
def test_user_already_moved(): reset_data() B = auth_register("*****@*****.**", "wsad1990", "Tony", "Hex") A = auth_register("*****@*****.**", 'HoyaLee2019', 'Hoya', 'Lee') Pub_channel = CF.channels_create(A['token'], 'channelFirst', 'True') CF.channel_join(B['token'], Pub_channel['channel_id']) token = A['token'] u_id = B['u_id'] channelID = Pub_channel['channel_id'] with pytest.raises(CHF.AccessError): CF.channel_removeowner(token, channelID, u_id)
def test_standup_start(): ''' Test functions for standup_start ''' data.initialise_all() # A valid token and channel successfully starts a standup - Owner owner = auth.auth_register("*****@*****.**", "valid_password", "a", "b") channel1 = channel_func.channels_create(owner["token"], "Owner", True) channel2 = channel_func.channels_create(owner["token"], "User", True) length = 1 # Testing return time end_ex = get_standup_end(length) end_ac = standup_start(owner["token"], channel1["channel_id"], length) assert same_time(end_ex, end_ac["time_finish"]) time.sleep(1) # A invalid token is used to access the function with pytest.raises(AccessError, match="Invalid token"): standup_start("123456", channel2["channel_id"], length) # A valid token and channel successfully starts a standup - User user = auth.auth_register("*****@*****.**", "valid_password", "a", "b") # User tries to start standup but has not joined the channel with pytest.raises(AccessError, match="Cannot Access Channel"): standup_start(user["token"], channel2["channel_id"], length) # User starts standup channel_func.channel_join(user["token"], channel2["channel_id"]) end_ex = get_standup_end(length) end_ac = standup_start(user["token"], channel2["channel_id"], length) assert same_time(end_ex, end_ac["time_finish"]) # Channel id given does not exist with pytest.raises(ValueError, match="Channel Does Not Exist"): standup_start(user["token"], 21512512521512, length) # Start standup when already running with pytest.raises(ValueError, match="Standup Already Running"): standup_start(user["token"], channel2["channel_id"], length) # Standup works on private channel private = channel_func.channels_create(owner["token"], "Owner", False) end_ex = get_standup_end(length) end_ac = standup_start(owner["token"], private["channel_id"], length) assert same_time(end_ex, end_ac["time_finish"]) time.sleep(1)
def test_channel_removeowner(): ''' Function tests for channel_removeowner ''' #Initialisation global_var.initialise_all() user1 = auth_register("*****@*****.**", \ "valid_correct_password", "valid_correct_first_name", \ "valid_correct_last_name") token1 = user1["token"] userid1 = user1["u_id"] user2 = auth_register("*****@*****.**", \ "valid_correct_password", "valid_correct_first_name", \ "valid_correct_last_name") token2 = user2["token"] userid2 = user2["u_id"] #token1 create a channel channel = func.channels_create(token1, "TestChannel", True) channel_id = channel["channel_id"] #token2 joins the channel token1 made func.channel_join(token2, channel_id) # Initialisation finished #user2 is not the owner, thus trying to removeowner raises an error with pytest.raises(ValueError, match=\ "User id is not an owner of the channel"): func.channel_removeowner(token1, channel_id, userid2) # user2 is not the owner and tries to remove owner of channel with pytest.raises(AccessError, match=\ "User is not an owner of the channel or slackrowner"): func.channel_removeowner(token2, channel_id, userid1) #token1 makes token2 a owner func.channel_addowner(token1, channel_id, userid2) assert func.channel_removeowner(token1, channel_id, userid2) == {} #if given an invalid channel_id with pytest.raises(ValueError, match="Channel does not exist"): func.channel_removeowner(token1, 100, userid2) # The function is called using a invalid token with pytest.raises(AccessError, match="Invalid token"): func.channel_removeowner("12345", channel_id, userid2)
def test_channels_create(): ''' Function tests for channels_create ''' #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"] #user1 create a channel func.channels_create(token1, "TestChannel", True) # Initialisation finished assert func.channels_create(token1, "Channel1", True) == \ {"channel_id" : 1} assert func.channels_create(token1, "Channel2", False) == \ {"channel_id" : 2} # User can join public channel assert func.channel_join(token2, 1) == {} # User cannot join private channel with pytest.raises(AccessError, \ match="Channel is private and user is not admin"): func.channel_join(token2, 2) # Channel cannot be created by an invalid token with pytest.raises(AccessError, match="Invalid token"): func.channels_create("12345", "Channel3", True) #public channel, name longer than 20 characters with pytest.raises(ValueError, match=\ "Name is longer than 20 characters"): func.channels_create(token1, "a" * 21, True) #private channel, name longer than 20 characters with pytest.raises(ValueError, match=\ "Name is longer than 20 characters"): func.channels_create(token1, "a" * 21, False)
def channel_join(): """ Adds an authorised user to a channel """ token = request.form.get("token") channel_id = to_int(request.form.get("channel_id")) return dumps(channel.channel_join(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_invalid_token(): token = A['token'] + '10086' channelID = Pub_channel['channel_id'] with pytest.raises(TF.AccessError): CF.channel_join(token, channelID)
def test_message_edit(): ''' Function tests for message_edit ''' #Initialisation global_var.initialise_all() #Create users owner = auth_functions.auth_register("*****@*****.**", "pass123", \ "Sally", "Bob") owner_token = owner["token"] user = auth_functions.auth_register("*****@*****.**", "pass123", \ "Rayden", "Smith") token = user["token"] #owner creates a 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) #3 messages are sent 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} assert funcs.message_send(token, channel_id, \ "This is another valid message") == {"message_id" : 3} #Init finished #A owner edits a message assert funcs.message_edit(owner_token, 0, "This is a valid edit") == {} #A user edits his own message assert funcs.message_edit(token, 1, "This is another valid edit") == {} # A user edits a message with an empty string assert funcs.message_edit(token, 1, "") == {} assert funcs.message_edit(token, 3, " ") == {} #A invalid token is sent to the function with pytest.raises(AccessError, match="Invalid token"): funcs.message_edit("111111", 0, "This is a valid edit") #A owner tries to edit a invalid message based on message_id with pytest.raises(ValueError, match="Message does not exist"): funcs.message_edit(owner_token, -1, "This is a valid edit") #A user tries to edit a invalid message based on message_id with pytest.raises(ValueError, match="Message does not exist"): funcs.message_edit(token, -1, "A valid message edit") #A user tries to edit a message not his with pytest.raises(AccessError, match="User does not have permission"): funcs.message_edit(token, 2, "This is a valid message") #The edited message was too long with pytest.raises(ValueError, match="Message length too long"): funcs.message_edit(owner_token, 0, "1" + create_long_string())
def test_channel_details(): ''' Function tests for channel_details ''' #Initialisation global_var.initialise_all() user1 = auth_register("*****@*****.**", \ "valid_correct_password", "valid_correct_first_name", \ "valid_correct_last_name") token1 = user1["token"] userdict1 = { "u_id": 0, "name_first": "valid_correct_first_name", "name_last": "valid_correct_last_name", "profile_img_url": 'http://*****:*****@test.com", \ "valid_correct_password", "valid_correct_first_name", \ "valid_correct_last_name") token2 = user2["token"] userdict2 = { "u_id": 1, "name_first": "valid_correct_first_name", "name_last": "valid_correct_last_name", "profile_img_url": 'http://localhost:5001/imgurl/server/assets/images/default.jpg' } #token1 creates a channel and is automatically part of it as the owner channel = func.channels_create(token1, "TestChannel", True) channel_id = channel["channel_id"] # Initialisation finished #if user is not a member of the channel, has not joined yet with pytest.raises(AccessError, match=\ "Authorised user is not a member of the channel"): func.channel_details(token2, channel_id) assert func.channel_details(token1, channel_id) == { "name": "TestChannel", "owner_members": [userdict1], "all_members": [userdict1] } # If a user changes names, this is reflected in channel_details user_profile_setname(token1, "another", "name") userdict1["name_first"] = "another" userdict1["name_last"] = "name" assert func.channel_details(token1, channel_id) == { "name": "TestChannel", "owner_members": [userdict1], "all_members": [userdict1] } #if given an invalid channel_id with pytest.raises(ValueError, match="Channel does not exist"): func.channel_details(token1, 100) # The function is called using a invalid token with pytest.raises(AccessError, match="Invalid token"): func.channel_details("12345", channel_id) # A second user joins the channel func.channel_join(token2, channel_id) assert func.channel_details(token1, channel_id) == { "name": "TestChannel", "owner_members": [userdict1], "all_members": [userdict1, userdict2] }
def test_channel_id_invalid(): token = A['token'] channelID = Pub_channel['channel_id'] + 14301580495 with pytest.raises(CF.AccessError): CF.channel_join(token, channelID)
def test_channel_joined_failed(): token = A['token'] channelID = Prv_channel['channel_id'] with pytest.raises(CF.AccessError): CF.channel_join(token, channelID)
def test_channelisID_NULL(): token = A['token'] channelID = None with pytest.raises(CF.AccessError): CF.channel_join(token, channelID)
def test_channelisID_invalid(): token = A['token'] channelID = Pub_channel['channel_id'] + 10086 with pytest.raises(CF.AccessError): CF.channel_join(token, channelID)