def test_message_remove(): helper.reset_data() # Valid parameters token = helper.token_account_1()["token"] channel_id = helper.get_valid_channel(token) message_id = helper.get_valid_message(token, channel_id) message = "This message was produced by the get_valid_message function." # Invalid parameters unauthorised_token = helper.token_account_2()["token"] stub.channel_leave(unauthorised_token, channel_id) # Test that it raises an access erorr if the user is not in the channel # to remove a message with pytest.raises(validation_helper.AccessError): stub.message_remove(unauthorised_token, message_id) # Test that it raises an access erorr if the user does not have the proper # authority to remove a message with pytest.raises(validation_helper.AccessError): stub.channel_join(unauthorised_token, channel_id) stub.message_remove(unauthorised_token, message_id) # Attempt to successfully remove a message assert stub.message_remove(token, message_id) == {} # Test that it raises an error if trying to remove a already removed message with pytest.raises(validation_helper.ValueError): stub.message_remove(token, message_id) # Test that the message disappears from search assert message_id not in stub.search(token, message)
def test_message_send(): helper.reset_data() # Valid parameters token = helper.token_account_1()["token"] channel_id = helper.get_valid_channel(token) message = "Testing the function message_send." # Invalid parameters invalid_channel_id = -00000 invalid_message = message*3000 unauthorised_token = helper.token_account_2()["token"] # Test that it raises an erorr if the channel does not exist with pytest.raises(validation_helper.ValueError): stub.message_send(token, invalid_channel_id, message) # Test that it raises an erorr if the message is >1000 characters with pytest.raises(validation_helper.ValueError): stub.message_send(token, channel_id, invalid_message) # Test that it raises an access erorr if the token is not an authorised user. # Note: we assume the second account is not part of the channel. This will fail if the second # account is in the channel with pytest.raises(validation_helper.AccessError): stub.channel_leave(unauthorised_token, channel_id) stub.message_send(unauthorised_token, channel_id, message) # Test that the function successfully completes with valid parameters # (this may require manual verification) assert stub.message_send(token, channel_id, message)["message_id"] > -1
def test_channel_invite(): helper.reset_data() # Create users for testing user1 = helper.token_account_1() user2 = helper.token_account_2() # create a new channel for testing channel_id = helper.channelid_public(user1["token"]) # Check that error is raised if an invalid token is passed in with pytest.raises(validation_helper.AccessError): stub.channel_invite("fake token", channel_id, user1["u_id"]) # Invalid channel_id with pytest.raises(validation_helper.ValueError): stub.channel_invite(user1["token"], -1, user2["u_id"]) # Invalid u_id with pytest.raises(validation_helper.ValueError): stub.channel_invite(user1["token"], channel_id, -1) # valid call assert stub.channel_invite(user1["token"], channel_id, user2["u_id"]) == {} # Test that the user is in the channel message_id = stub.message_send(user2["token"], channel_id, "Hi")["message_id"] # check that nothing happens if the user is already in the channel stub.channel_invite(user1["token"], channel_id, user1["u_id"]) # check that if an admin/owner of the slackr is invited, they become an owner stub.channel_leave(user1["token"], channel_id) stub.channel_invite(user2["token"], channel_id, user1["u_id"]) stub.message_remove(user1["token"], message_id) # can do this since they should be an owner
def test_channel_messages(): helper.reset_data() # Create users for testing user1 = helper.token_account_1() user1_token = user1["token"] user2 = helper.token_account_2() user2_token = user2["token"] # Create a new channel for testing channel_id = helper.channelid_public(user1_token) # Leave the channel in case user2 is in the new channel stub.channel_leave(user2_token, channel_id) # Setting the start & end start = 0 # Check that error is raised if an invalid token is passed in with pytest.raises(validation_helper.AccessError): stub.channel_messages("fake token", channel_id, start) # Start is a invalid number with pytest.raises(validation_helper.ValueError): stub.channel_messages(user1_token, channel_id, -1) # Channel doesn't exist with pytest.raises(validation_helper.ValueError): stub.channel_messages(user1_token, -1, start) # Index of start greater then messages in channel with pytest.raises(validation_helper.ValueError): stub.channel_messages(user1_token, channel_id, 1000) # user2 is not a member with pytest.raises(validation_helper.AccessError): stub.channel_messages(user2_token, channel_id, start) # valid call message_id = helper.get_valid_message(user1_token, channel_id) message = get_info_helper.get_info_about_message(message_id) assert stub.channel_messages(user1_token, channel_id, start) == {"messages": [message], "start": 0, "end": -1} # now test it with >50 messages sent in the channel i = 0 while i < 70: stub.message_send(user1_token, channel_id, "Hello") i += 1 messages = stub.channel_messages(user1_token, channel_id, 10) assert messages["start"] == 10 assert messages["end"] == 60 assert len(messages["messages"]) == 50
def test_channel_details(): helper.reset_data() # Create users for testing user1 = helper.token_account_1() user1_token = user1["token"] user1_id = user1["u_id"] user2 = helper.token_account_2() user2_token = user2["token"] # Create a new channel for testing channel_id = helper.channelid_public(user1_token) # this is to test things channel_name = "Public Channel" channel_owner_members = [{"name_first": "Mommy", "name_last": "Pig", "u_id": user1_id, "profile_img_url": "/static/default.jpg"}] channel_all_members = [{"name_first": "Mommy", "name_last": "Pig", "u_id": user1_id, "profile_img_url": "/static/default.jpg"}] # Check that error is raised if an invalid token is passed in with pytest.raises(validation_helper.AccessError): stub.channel_details("fake token", channel_id) # Channel doesn't exist with pytest.raises(validation_helper.ValueError): stub.channel_details(user1_token, -1) # user2 is not a member with pytest.raises(validation_helper.AccessError): stub.channel_details(user2_token, channel_id) # Valid call assert stub.channel_details(user1_token, channel_id) == {"name": channel_name, "owner_members": channel_owner_members, "all_members": channel_all_members}
def test_message_edit(): helper.reset_data() # Valid parameters token = helper.token_account_1()["token"] channel_id = helper.get_valid_channel(token) message_id = helper.get_valid_message(token, channel_id) message = "This message was edited by the message_edit function." # Invalid parameters invalid_message = message*3000 invalid_message_id = -1 unauthorised_token = helper.token_account_2()["token"] # Test that it raises an error if a user edits a message that is not theirs. with pytest.raises(validation_helper.AccessError): stub.channel_join(unauthorised_token, channel_id) stub.message_edit(unauthorised_token, message_id, message) stub.channel_leave(unauthorised_token, channel_id) # Test that it raises an error if invalid message_id with pytest.raises(validation_helper.ValueError): stub.message_edit(token, invalid_message_id, message) # Test that it raises an error if it is a invalid message being edited with pytest.raises(validation_helper.ValueError): stub.message_edit(token, message_id, invalid_message) # Attempt to successfully edit a message assert stub.message_edit(token, message_id, message) == {} # Test that the message is edited in search assert stub.search(token, message)["messages"] != [] # Test that message is deleted if edited with an empty string stub.message_edit(token, message_id, "") assert stub.channel_messages(token, channel_id, 0)["messages"] == []
def test_message_sendlater(): helper.reset_data() # Valid parameters token = helper.token_account_1()["token"] channel_id = helper.get_valid_channel(token) message = "Testing the function message_sendlater." time_sent = helper.in_one_second() # Invalid parameters unauthorised_token = helper.token_account_2()["token"] invalid_channel_id = -00000 invalid_message = message*3000 invalid_time_sent = datetime(2018, 12, 12) stub.channel_leave(unauthorised_token, channel_id) # Test that it raises a error if the channel does not exist with pytest.raises(validation_helper.ValueError): stub.message_sendlater(token, invalid_channel_id, message, time_sent) # Test that it raises an error if the message is >1000 characters with pytest.raises(validation_helper.ValueError): stub.message_sendlater(token, channel_id, invalid_message, time_sent) # Test that it raises an error if the time sent is a time in the past with pytest.raises(validation_helper.ValueError): stub.message_sendlater(token, channel_id, message, invalid_time_sent) # Test that it raises an access error if the token is not an authorised user with pytest.raises(validation_helper.AccessError): stub.message_sendlater(unauthorised_token, channel_id, message, time_sent) # Test that the function successfully completes with valid parameters # (this may require manual verification) message_id = stub.message_sendlater(token, channel_id, message, time_sent)["message_id"] assert message_id > -1