def get_valid_message(token, channel_id): """ This function returns a valid message_id sent from the provided token. It is expected that the user is a part of the channel it is posting messages to. usage: pass in a valid token, and the channel in which you want to send your message Notes: It will try and find a valid channel to join, else it will create one and return that. This is to prevent creating more unnecessesary channels This makes sure the channel_id is valid for the user. """ # send a message message = "This message was produced by the get_valid_message function." stub.message_send(token, channel_id, message) # Get a list of messages in the channel start = 0 messages = stub.channel_messages(token, channel_id, start)["messages"] # Grab the most recent message # We assume our message is the first one. # assert messages[0]["message"] == message message_id = messages[0]["message_id"] return message_id
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_search(): helper.reset_data() # SETUP START admin = helper.token_admin() channel_id = helper.channelid_public(admin["token"]) keyword = "Hello" + str(random()) # SETUP END # Checks that nothing is returned if the message does not exist assert stub.search(admin["token"], "this message does not exist" + str(random())) == {"messages": []} # Checks that messages are successfully returned stub.message_send(admin["token"], channel_id, keyword) assert keyword in stub.search(admin["token"], keyword)["messages"][0]["message"]
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_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 message_send(): """ uses stub.py interface function to send a message immediately """ return dumps( stub.message_send(request.form.get('token'), int(request.form.get('channel_id')), request.form.get('message')))