def test_send_createlong(): testData = create_data(1) #Create 1001 character message longString = "x" * 1001 with pytest.raises(InputError): send(testData[0]['token'], testData[1]['channel_id'], longString)
def send_standup(token, channel_id, standup_index): ''' Uses message_send to send the standup message with standup_index ''' if data.standup_list[standup_index]['message'] == "": data.standup_list[standup_index][ 'message'] = "No one sent a message during the standup :(" else: data.standup_list[standup_index]['message'].lstrip() message.send(token, channel_id, data.standup_list[standup_index]['message'])
def _send_messages(details, ch_id, n): msg_ids = [] response = send(details["token"], ch_id, "First message") msg_ids.append(response["message_id"]) for i in range(1, n): msg_ids.append( send(details["token"], ch_id, f"another message {i}")["message_id"]) return msg_ids
def test_edit_invalid_token(): testData = create_data(1) testMessage = send(testData[0]['token'], testData[1]['channel_id'], "Message") with pytest.raises(AccessError): edit("NOT_VALID_TOKEN", testMessage, "new message")
def bot_send(messageX, channel_id): ''' Input - message < 1000 length, and valid channel_id. Sends a message with the bot to a channel. ''' # register and join the channel if haven't already auto_register() bot_id = discrete.find_uid("*****@*****.**") for channel in all_channels: if channel["channel_id"] == channel_id: if bot_id not in channel["all_members"]: bot_join(channel_id) # send the message token_bot = common.encode_token({"u_id": bot_id}) message.send(token_bot, channel_id, messageX)
def test_remove_invalid_token(): testData = create_data(1) testMessage = send(testData[0]['token'], testData[1]['channel_id'], "Message")['message_id'] testData[0]['token'] = "NOT_VALID_TOKEN" with pytest.raises(AccessError): remove("NOT_VALID_TOKEN", testMessage)
def test_search_no_match(): testUser = create_user() testChannel = channels.create(testUser['token'], "Test Channel", True) message.send(testUser['token'], testChannel['channel_id'], "Hello World") message.send(testUser['token'], testChannel['channel_id'], "Bye World") message.send(testUser['token'], testChannel['channel_id'], "Hello Dux Wu") message.send(testUser['token'], testChannel['channel_id'], "WuHan cool") result = search.search(testUser['token'], "Ayaya") assert result == {'messages': []}
def test_search1(): testUser = create_user() testChannel = channels.create(testUser['token'], "Test Channel", True) message.send(testUser['token'], testChannel['channel_id'], "Hello World") message.send(testUser['token'], testChannel['channel_id'], "Bye World") message.send(testUser['token'], testChannel['channel_id'], "Hello Dux Wu") message.send(testUser['token'], testChannel['channel_id'], "WuHan cool") result = search.search(testUser['token'], "Wu") # Don't know what order the result list is assert (result['messages'][0]['message'] == 'Hello Dux Wu' and result['messages'][1]['message'] == 'WuHan cool')\ or (result['messages'][0]['message'] == 'WuHan cool' and result['messages'][1]['message'] == 'Hello Dux Wu')
def test_remove_unauthorised_not_admin(): testData = create_data(1) testData2 = create_data(2) joi(testData2[0]['token'], testData[1]['channel_id']) # testData sent the message and is admin testMessage = send(testData2[0]['token'], testData[1]['channel_id'], "Message") # testData sent the message and testData is the owner/admin of BOTH the slackr and the channel so testData 2 shouldn't be able to remove with pytest.raises(AccessError): remove(testData2[0]['token'], testMessage)
def test_edit_unauthorised_not_admin(): testData = create_data(1) testData2 = create_data(2) joi(testData2[0]['token'], testData[1]['channel_id']) # The user of testData sent the message and is admin testMessage = send(testData2[0]['token'], testData[1]['channel_id'], "Message") # The user of testData sent the message and testData is the owner/admin so testData 2 shouldn't be able to remove with pytest.raises(AccessError): edit(testData2[0]['token'], testMessage, "newMessage")
def test_edit_authorised_not_admin(): testData = create_data(1) testData2 = create_data(2) joi(testData2[0]['token'], testData[1]['channel_id']) # The user of testData2 sent the message; but is not an admin testMessage = send(testData2[0]['token'], testData[1]['channel_id'], "Message") # The user of testData2 should still be able to edit the message result = edit(testData2[0]['token'], testMessage, "new msg") assert type(result) is dict assert result == {}
def test_search_multi_channel(): testUser = create_user() testChannel = channels.create(testUser['token'], "Test Channel", True) testChannel2 = channels.create(testUser['token'], "Test Channel2", True) message.send(testUser['token'], testChannel['channel_id'], "Hello World") message.send(testUser['token'], testChannel2['channel_id'], "Bye World") message.send(testUser['token'], testChannel2['channel_id'], "Hello Dux Wu") message.send(testUser['token'], testChannel2['channel_id'], "WuHan cool") result = search.search(testUser['token'], "World") result2 = search.search(testUser['token'], "Wu") assert (result['messages'][0]['message'] == 'Hello World' and result['messages'][1]['message'] == 'Bye World')\ or (result['messages'][0]['message'] == 'Bye World' and result['messages'][1]['message'] == 'Hello World') assert (result2['messages'][0]['message'] == 'Hello Dux Wu' and result2['messages'][1]['message'] == 'WuHan cool')\ or (result2['messages'][1]['message'] == 'WuHan cool' and result2['messages'][0]['message'] == 'Hello Dux Wu')
def test_remove_unauthorised_admin(): # Create 1 admin of channel and 1 member of channel testData = create_data(1) testData2 = create_data(2) # The user of testData is owner (joied first - see assumptions) joi(testData2[0]['token'], testData[1]['channel_id']) testMessage = send(testData2[0]['token'], testData[1]['channel_id'], "Test") # The user of testData2 sent the message but testData is the owner/admin of †he channel so should be able to remove result = remove(testData[0]['token'], testMessage) assert type(result) is dict assert result == {}
def test_remove_authorised_not_admin(): testData = create_data(1) testData2 = create_data(2) joi(testData2[0]['token'], testData[1]['channel_id']) # The user of testData2 sent the message; but is not an admin (did not joi the channel first - see assumptions) testMessage = send(testData2[0]['token'], testData[1]['channel_id'], "Message") # The user of testData2 should still be able to remove the message result = remove(testData2[0]['token'], testMessage) assert type(result) is dict assert result == {}
def test_edit_unauthorised_admin(): #C reate 1 admin of channel and 1 member of channel testData = create_data(1) testData2 = create_data(2) # The user of testData is owner joi(testData2[0]['token'], testData[1]['channel_id']) testMessage = send(testData2[0]['token'], testData[1]['channel_id'], "Test") # The user of testData2 sent the message but testData is the owner/admin so should be able to edit result = edit(testData[0]['token'], testMessage, "newmessage") assert type(result) is dict assert result == {}
def test_edit_empty_string(): testData = create_data(1) testMessage = send(testData[0]['token'], testData[1]['channel_id'], "This is a message that should be removed") result = edit( testData[0]['token'], testMessage, "") # Should NOT throw an error here - should remove the message # First, a quick sanity check on the return value and type assert type(result) is dict assert result == {} # Now, we can check the message is actually gone by trying to remove it (should throw AccessError - should have been deleted) with pytest.raises(InputError): remove(testData[0]['token'], testMessage)
def test_send_not_jonied(): testData = create_data(1) lev(testData[0]['token'], testData[1]['channel_id']) with pytest.raises(AccessError): send(testData[0]['token'], testData[1]['channel_id'], "The mainstream media!")
def test_send_createempty(): testData = create_data(1) #Create 1001 character message with pytest.raises(InputError): send(testData[0]['token'], testData[1]['channel_id'], "")
def test_send_createjust_short(): testData = create_data(1) #Create 1000 character message shortString = "x" * 1000 result = send(testData[0]['token'], testData[1]['channel_id'], shortString)
def test_send_invalid_channel_id(): testData = create_data(1) with pytest.raises(AccessError): send(testData[0]['token'], -1, "aaa")
def test_send_invalid_token(): testData = create_data(1) with pytest.raises(AccessError): send("NOT_VALID_TOKEN", testData[1]['channel_id'], "The Middle East is complicated business")
def test_send_basic(): testData = create_data(1) result = send(testData[0]['token'], testData[1]['channel_id'], "very legal and very cool")
def msg_send(): data = request.get_json() return dumps(message.send(data['token'], int(data['channel_id']), data['message']))
def guess(token, channel_id, X): ''' Input - valid token, channel ID and a guess X. Behavior: Given a message with some 2nd string 'X', determines whether the guess is correct or not. Output - {} ''' # ch_id is invalid -> InputError index = common.ch_id_exists(channel_id) if index == -1: raise InputError( description='The channel you are guessing on does not exist.') # check if token is valid authorized_caller = common.decode_token(token) if authorized_caller == {"u_id": "error_invalid_token"} or \ not common.user_in_channel(authorized_caller["u_id"], channel_id): raise AccessError( description='You do not have permission to do this.') # ensure channel has a hangman session # NOTE YOU must abstract these functions ASAP. index = None for session in hangman_sessions: if session["channel_id"] == channel_id: index = hangman_sessions.index(session) if index == None: raise InputError(description='A hangman session is not running right now') X = X[0].split(' ') # if word not specified (is list), generate a random word if len(X) != 1: X = X[1] # check if X has been guessed already if X in hangman_sessions[index]['guesses']: raise InputError(description='Your guess has already been guessed.') # catch empty input error if not isinstance(X, str): raise InputError(description='Usage: /guess [letter]') # SEND THE /guess X as a message to message data. message.send(token, channel_id, '/guess ' + X) # check if the guess results in the complete answer hm_word = hangman_sessions[index]['word'] guesses = hangman_sessions[index]['guesses'] guesses.append(X) prev_word = hangman_sessions[index]['decrement_word'] hangman_sessions[index]['decrement_word'] = hangman_sessions[index]['decrement_word'].replace(X, '') word_decrement = hangman_sessions[index]['decrement_word'] if prev_word == word_decrement: hangman_sessions[index]["bad_attempts"] += 1 # render ASCII or some image art. bot_message = _render_ascii( hangman_sessions[index]["bad_attempts"], channel_id, guesses, hm_word) if hm_word.replace(X, '') == hm_word: hangman_sessions[index]['remaining_guesses'] -= 1 # if guess results in complete answer, then done and send congratulations if word_decrement == '': hangman_sessions.pop(index) bot_message += "\n\(^O^)/\nYou've correctly guessed the word!" # decrement the amount of guesses available, and if no more guesses, # remove hangman session and send taunting message else: if hangman_sessions[index]['remaining_guesses'] == 0: hangman_sessions.pop(index) bot_message += f"\n(✖╭╮✖)\nThe word is {hm_word}, you have lost\nF to pay respects" else: bot_message += "\nLetters guessed:" for word in hangman_sessions[index]['guesses']: bot_message += f" {word}" # send message as the bot bot_send(bot_message, channel_id) return {}