def test_search_multi_channel(): ''' Testing successful case of being able to obtain messages from multiple channels that the user is apart of ''' # Initialising global_var.initialise_all() # Creating user user = auth_register('*****@*****.**', 'password', 'search', 'test') token = get_user_token_by_u_id(user["u_id"]) # Creating channel 1 channel1 = channels_create(token, "chat1", True) channel1_id = channel1["channel_id"] # Creating channel 2 channel2 = channels_create(token, "chat2", True) channel2_id = channel2["channel_id"] # Adding messages to channel message_send(token, channel1_id, "121") message_send(token, channel2_id, "321") # Search messages = search.search(token, '21') # Checking if messages obtained assert messages["messages"][0]["message"] == "121" assert messages["messages"][0]["u_id"] == user["u_id"] assert messages["messages"][1]["message"] == "321" # Ensuring that no other messages are obtained with pytest.raises(IndexError, match="list index out of range"): message = messages["messages"][2]
def test_get_channel_by_channel_id(): ''' Ensures get_channel_by_channel_id returns correct channel id ''' # Initialisation global_var.initialise_all() # Creating a user user = auth.auth_register("*****@*****.**", "pass123", "Raydon", "Smith") token = user["token"] # Creating first channel channel_1 = channel_functions.channels_create(token, "Channel 1", True) channel_1_id = channel_1["channel_id"] # Creating second channel channel_2 = channel_functions.channels_create(token, "Channel 2", True) channel_2_id = channel_2["channel_id"] # Ensuring that first channel has id assert helpers.get_channel_by_channel_id(channel_1_id).id == 0 # Ensuring that second channel has id assert helpers.get_channel_by_channel_id(channel_2_id).id == 1 # Checking no such channel_id assert helpers.get_channel_by_channel_id(-1) is None
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_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_upload_base_error(): reset_data() A = auth_register("*****@*****.**", "wsad1990", "Good", "Morning") token = A['token'] CF.channels_create(token, 'UploadError', "True") img_url = "http://ichef.bbci.co.uk/onesport/cps/480/mcs/media/images/57210000/jpg/_57210683_57210682.jpg" x_start = '0' y_start = '0' x_end = '200' y_end = '200' base = None UF.user_profiles_uploadphoto(token, img_url, x_start, y_start, x_end, y_end, base)
def test_search_empty_channel(): ''' Test search with a search on an empty channel ''' # Initialising global_var.initialise_all() # Creating user user = auth_register('*****@*****.**', 'password', 'search', 'test') token = get_user_token_by_u_id(user["u_id"]) # Creating channel channels_create(token, "chat", True) assert search.search(token, 'hey') == {'messages': []}
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 test_addOwner_succ(): token = A['token'] u_id = B['u_id'] Pub_channel = CF.channels_create(A['token'], 'Test1', 'True') channelID = Pub_channel['channel_id'] CF.channel_addowner(token, channelID, u_id)
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_search_one(): ''' Testing successful case for a search for one message ''' # Initialising global_var.initialise_all() # Creating user user = auth_register('*****@*****.**', 'password', 'search', 'test') token = get_user_token_by_u_id(user["u_id"]) # Creating channel channel = channels_create(token, "chat", True) channel_id = channel["channel_id"] # Adding messages to channel message_send(token, channel_id, "121") message_send(token, channel_id, "321") message_send(token, channel_id, "342") message_send(token, channel_id, "499") # Searching message messages = search.search(token, '99') # Checking if message obtained assert messages["messages"][0]["message"] == "499" assert messages["messages"][0]["u_id"] == user["u_id"] # Ensuring that no other messages are obtained with pytest.raises(IndexError, match="list index out of range"): message = messages["messages"][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_details_highPermission_succ(): reset_data() A = auth_register("*****@*****.**", 'HoyaLee2019', "Hoya", "Lee") Pub_channel = CF.channels_create(A['token'], 'NextChannel', 'True') token = A['token'] channelID = Pub_channel['channel_id'] CF.channel_details(token, channelID)
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_set_channel_success(): reset_data() A = auth_register("*****@*****.**", 'HoyaLee2019', "Hoya", "Lee") Pub_channel = channels_create(A['token'], 'STDChannel', 'True') token = A['token'] channel_id = Pub_channel['channel_id'] OF.standup_active(token, channel_id)
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_change_success(): reset_data() O = auth_register("*****@*****.**", 'HoyaLee2019', "Hoya", "Lee") token = O['token'] Pub_channel = CF.channels_create(O['token'], 'NameChanging', 'True') channelID = Pub_channel['channel_id'] CF.channel_name_change(token, channelID, 'Name1')
def test_delete_success(): reset_data() A = auth_register("*****@*****.**", "wsad1990", "Good", "Morning") Pub_channel = CF.channels_create(A['token'], 'DeleteChannel', 'True') token = A['token'] channelID = Pub_channel['channel_id'] CF.channel_delete(token, channelID)
def test_search_invalid_token(): ''' Testing search with an invalid token ''' with pytest.raises(AccessError, match="Invalid token"): # Initialising global_var.initialise_all() # Creating user user = auth_register('*****@*****.**', 'password', 'search', 'test') token = get_user_token_by_u_id(user["u_id"]) # Creating channel channels_create(token, "chat", True) # Searching using an invalid token search.search("invalid token", "123")
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_set_success(): reset_data() A = auth_register("*****@*****.**", "wsad1990", "Good", "Morning") Pub_channel = channels_create(A['token'], 'StdChannel', 'True') token = A['token'] channelID = Pub_channel['channel_id'] length = '15' OF.standup_start(token, channelID, length)
def test_cant_promote(): reset_data() A = auth_register("*****@*****.**", 'HoyaLee2019', "Hoya", "Lee") Pub_channel = CF.channels_create(A['token'], 'Test1', 'True') token = A['token'] u_id = 1234567 channelID = Pub_channel['channel_id'] CF.channel_addowner(token, channelID, u_id)
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_validationOFtoken(): reset_data() A = auth_register("*****@*****.**", 'HoyaLee2019', "Hoya", "Lee") Pub_channel = CF.channels_create(A['token'], 'NextChannel', 'True') token = A['token'] + '45645' channelID = Pub_channel['channel_id'] with pytest.raises(TF.AccessError): CF.channel_details(token, channelID)
def test_canNot_addOwner(): auth_login("*****@*****.**", 'HoyaLee2019') token = A['token'] u_id = A['u_id'] Pub_channel = CF.channels_create(A['token'], 'Test1', 'True') channelID = Pub_channel['channel_id'] with pytest.raises(CHF.ValueError): CF.channel_addowner(token, channelID, u_id)
def test_invalid_channelID(): reset_data() A = auth_register("*****@*****.**", 'HoyaLee2019', "Hoya", "Lee") Pub_channel = CF.channels_create(A['token'], 'NextChannel', 'True') token = A['token'] channelID = Pub_channel['channel_id'] + 45645 with pytest.raises(CHF.ValueError): CF.channel_details(token, channelID)
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_set_successful(): reset_data() A = auth_register("*****@*****.**", 'HoyaLee2019', "Hoya", "Lee") B = auth_register("*****@*****.**", "wsad1990", "Good", "Morning") Pub_channel = channels_create(A['token'], 'STDChannel', 'True') token = A['token'] channelID = Pub_channel['channel_id'] length = '15' OF.standup_start(token, channelID, length)
def test_channel_Invalid(): reset_data() A = auth_register("*****@*****.**", 'HoyaLee2019', "Hoya", "Lee") Pub_channel = channels_create(A['token'], 'STDChannel', 'True') token = A['token'] channelID = Pub_channel['channel_id'] + 10086 length = '15' with pytest.raises(CHF.ValueError): OF.standup_start(token, channelID, length)
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_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)