def test_search_empty_sting(): """ test if the empty string is giving, the InputError shoud appear """ login_infor = message_test.test_message_send() with pytest.raises(error.InputError): other.search(login_infor['token'], '')
def test_standup_send_during_standup(): ''' Test that messages sent during the standup period are not actually sent untill after, and then, all as one message ''' clear() user = auth_register("*****@*****.**", "password", "Firstname", "Lastname") channel = channels_create(user['token'], "Channel", True) standup_start(user['token'], channel['channel_id'], 1) standup_send(user['token'], channel['channel_id'], "message1") check = search(user['token'], "message1") assert len(check['messages']) == 0 standup_send(user['token'], channel['channel_id'], "message2") check = search(user['token'], "message2") assert len(check['messages']) == 0 time.sleep(4) check = search(user['token'], "message") assert len(check['messages']) == 1 name = user_profile(user['token'], user['u_id'])['user']['handle_str'] assert check['messages'][0][ 'message'] == name + ": message1" + '\n' + name + ": message2"
def test_search_invalid_search_string(reset, create_public_channel): new_public_channel, user_ab = create_public_channel message_send( user_ab['token'], new_public_channel['channel_id'], "My first message") with pytest.raises(InputError): search(user_ab['token'], "My first message" + "a" * 999)
def test_search_valid_v(): """ test if token is bad it should appear a AccessError. """ login_infor = message_test.test_message_send() auth.auth_logout(login_infor['token']) with pytest.raises(error.AccessError): other.search(login_infor['token'], 'a' * 99)
def test_before_message(): mess = message_function("Hellooo world") token = mess['token'] mid = mess['mid'] b4_edit = search(token, "Hello? This is the edited version") assert len(b4_edit['messages']) == 0 message_edit(token, mid, "Hello? This is the edited version") after_edit = search(token,"Hellooo world") assert len(after_edit['messages']) == 0
def test_token_token(): clear() user = auth_register("*****@*****.**", "password", "Firstname", "Lastname") channels_return = channels_create(user.get('token'), "username", True) channels_return.get("channel_id") message_send(user.get('token'), channels_return.get('channel_id'), "Testthismessage").get("message_id") with pytest.raises(AccessError): search("invalid", "Test")
def test_hangmne_guess_fail(): # if __name__ == "__main__": """ Test if guess can start with incorrect input and fail """ user_infor = auth_register("*****@*****.**", "ccc337992611", "Min", "Li") channel_infor = channels_create(user_infor['token'], 'test_one', True) message_send(user_infor['token'], channel_infor['channel_id'], "/hangman") current_game = hangman_evil_answer(channel_infor['channel_id']) #print(f"Answer is {answer}") has_guessed = [] game_answer = current_game['word'] for letter in string.ascii_lowercase: if current_game is not None and letter not in game_answer: has_guessed.append(letter) message_id = message_send(user_infor['token'], channel_infor['channel_id'], f"/guess {letter}") time_create_date = datetime.datetime.now().replace(microsecond=0) time_create = time_create_date.timestamp() current_game = hangman_evil_answer(channel_infor['channel_id']) if current_game is not None: state = '' for char in current_game['word']: if char in current_game['guesses']: state += char else: state += '_' state += '\n' bot_msg = state + \ f"You have {9 - current_game['num_guesses']} incorrect guesses left\n" + \ "Guessed Characters: " + str(has_guessed) assert search(BOT_TOKEN, bot_msg) == { "messages": [{ 'message_id': message_id["message_id"] + 1, 'u_id': -1, 'message': bot_msg, 'time_created': time_create, 'reacts': [set_reacts()], 'is_pinned': False }] } bot_loss = f"Game Lost! The correct word was {game_answer}" assert search(BOT_TOKEN, bot_loss) == { "messages": [{ 'message_id': message_id["message_id"] + 1, 'u_id': -1, 'message': bot_loss, 'time_created': time_create, 'reacts': [set_reacts()], 'is_pinned': False }] }
def test_hangmne_guess_noraml(): """ Test if guess can start with correct input and success """ user_infor = auth_register("*****@*****.**", "ccc337992611", "Min", "Li") channel_infor = channels_create(user_infor['token'], 'test_one', True) message_send(user_infor['token'], channel_infor['channel_id'], "/hangman") current_game = hangman_evil_answer(channel_infor['channel_id']) #print(f"Answer is {answer}") has_guessed = [] game_answer = current_game['word'] for letter in game_answer: if current_game is not None and letter not in current_game['guesses']: has_guessed.append(letter) time_create_date = datetime.datetime.now().replace(microsecond=0) time_create = time_create_date.timestamp() message_id = message_send(user_infor['token'], channel_infor['channel_id'], f"/guess {letter}") current_game = hangman_evil_answer(channel_infor['channel_id']) if current_game is not None: state = '' for char in current_game['word']: if char in current_game['guesses']: state += char else: state += '_' state += '\n' bot_msg = state + \ f"You have {9 - current_game['num_guesses']} incorrect guesses left\n" + \ "Guessed Characters: " + str(has_guessed) assert search(BOT_TOKEN, bot_msg) == { "messages": [{ 'message_id': message_id["message_id"] + 1, 'u_id': -1, 'message': bot_msg, 'time_created': time_create, 'reacts': [set_reacts()], 'is_pinned': False }] } bot_win = f'Congratultions! You correctly guessed that the word was {game_answer}' assert search(BOT_TOKEN, bot_win) == { "messages": [{ 'message_id': message_id["message_id"] + 1, 'u_id': -1, 'message': bot_win, 'time_created': time_create, 'reacts': [set_reacts()], 'is_pinned': False }] }
def test_message_sendlater(): """ test whether message_sendlater works as expect. """ def helper_func(token): """ Run search in a seperate thread, so it checks before message is sent. We use threads here because the flask side of thing uses local threads for routes, so we just use time.sleep inside the functions. This means we need a seperate thread to check before sendlater finishes. """ messages = search(token, 'a' * 99) assert messages['messages'] == [] user_infor = auth_register("*****@*****.**", "ccc337992611", "Min", "Li") channel_infor = channels_create(user_infor['token'], 'test_one', True) time_sent_date = datetime.datetime.now().replace( microsecond=0) + datetime.timedelta(0, 4) time_sent = time_sent_date.timestamp() # Run test check empty a second after message_sendlater has been # called (but hasn't finished executing) new_thread = threading.Timer(1, helper_func, args=(user_infor['token'])) new_thread.start() message_id = message_sendlater(user_infor['token'], channel_infor['channel_id'], 'a' * 99, time_sent) messages = search(user_infor['token'], 'a' * 99) assert messages['messages'][0]['message_id'] == message_id['message_id'] assert messages['messages'][0]['time_created'] == time_sent
def test_search_multiple_users(): user1_info = auth.auth_register('a@gmail', '123', 'aName', 'aLastname') user2_info = auth.auth_register('b@gmail', '321', 'bName', 'bLastname') # Creating the channel channels.channels_create(user1_info['token'], 'a', True) # User 2 joining the channel channel.channel_join(user2_info['token'], 1) # User 1 & 2 messaging the channel message.message_send(user1_info['token'], 1, "hello there my name is User1") message.message_send(user2_info['token'], 1, "hello User1 my name is User2") # Finding a timestamp tmp = channel.channel_messages(user2_info['token'], 1, 0) message1 = tmp['messages'][0] timestamp1 = message1['time_created'] message2 = tmp['messages'][1] timestamp2 = message2['time_created'] # Giving all messages containing the query string assert other.search(user1_info['token'], "hello") == { 'messages': [{ 'message_id': 1, 'u_id': 1, 'message': 'hello there my name is User1', 'time_created': timestamp1, }, { 'message_id': 2, 'u_id': 2, 'message': 'hello User1 my name is User2', 'time_created': timestamp2, }], }
def test_mesasge_send_2(): """ Test the message_send function works as two message is send """ user_infor = auth_register("*****@*****.**", "ccc337992611", "Min", "Li") channel_infor = channels_create(user_infor['token'], 'test_one', True) time_create_date = datetime.datetime.now().replace(microsecond=0) time_create = time_create_date.timestamp() message_id_1 = message_send(user_infor['token'], channel_infor['channel_id'], 'a' * 99) message_id_2 = message_send(user_infor['token'], channel_infor['channel_id'], 'a' * 99) assert search(user_infor['token'], 'a' * 99) == { "messages": [{ 'message_id': message_id_2["message_id"], 'u_id': user_infor["u_id"], 'message': 'a' * 99, 'time_created': time_create, 'reacts': [set_reacts()], 'is_pinned': False }, { 'message_id': message_id_1["message_id"], 'u_id': user_infor["u_id"], 'message': 'a' * 99, 'time_created': time_create, 'reacts': [set_reacts()], 'is_pinned': False }] }
def test_owner_edit_access(): ''' Testing for a valid message edited by a channel owner. ''' clear() auth_register("*****@*****.**", "password", "User_0", "User_last_0") user_1 = auth_register("*****@*****.**", "password", "User_1", "User_last_1") user_2 = auth_register("*****@*****.**", "password", "User_2", "User_last_2") auth_register("*****@*****.**", "password", "User_3", "User_last_3") channel_1 = channels_create(user_1.get('token'), 'channel_1', True) channel_join(user_2.get('token'), channel_1.get('channel_id')) message_info = message_send(user_2.get('token'), channel_1.get('channel_id'), 'Hello world') print(message_info) message_edit(user_2.get('token'), message_info.get('message_id'), 'Hellooo Worlldddd!!!!') message_search = search(user_1['token'], 'Hellooo Worlldddd!!!!') assert message_search['messages'][0].get('message_id') == message_info.get( 'message_id') assert message_search['messages'][0].get('u_id') == user_2['u_id'] assert message_search['messages'][0].get( 'message') == 'Hellooo Worlldddd!!!!'
def test_search_complex(): user_info = auth.auth_register('m@gmail', 'sadsad', 'name', 'lastname') # Creating the channel channels.channels_create(user_info['token'], 'a', True) # Messaging the channel message.message_send(user_info['token'], 1, "hello there my name is John") message.message_send(user_info['token'], 1, "hello John my name is John") message.message_send(user_info['token'], 1, "you aren't John, I'm John") # Finding a timestamp tmp = channel.channel_messages(user_info['token'], 1, 0) message1 = tmp['messages'][0] timestamp1 = message1['time_created'] message2 = tmp['messages'][1] timestamp2 = message2['time_created'] # Giving all messages containing the query string assert other.search(user_info['token'], "hello") == { 'messages': [{ 'message_id': 1, 'u_id': 1, 'message': 'hello there my name is John', 'time_created': timestamp1, }, { 'message_id': 2, 'u_id': 1, 'message': 'hello John my name is John', 'time_created': timestamp2, }], }
def test_search_invalid_token(): boyu_dict, _ = initialise_data() channel_1 = channels_create(boyu_dict['token'], 'channel_1', True) message_send(boyu_dict['token'], channel_1['channel_id'], 'hello1') message_send(boyu_dict['token'], channel_1['channel_id'], 'hello1') with pytest.raises(AccessError): assert search("invalid.token", 'hello1')
def test_search_normal_string(reset, create_public_channel): new_public_channel, user_ab = create_public_channel msg1 = message_send( user_ab['token'], new_public_channel['channel_id'], "Symbols & words") msg2 = message_send( user_ab['token'], new_public_channel['channel_id'], "& with more stuff") message_send( user_ab['token'], new_public_channel['channel_id'], "hola amigos!") # result_list is of type list (list of dictionaries) result_list = search(user_ab['token'], "& w")['messages'] # getting the message id and user ids that sent them messages_ids = [message['message_id'] for message in result_list] # getting the message string messages = [message['message'] for message in result_list] # asserting we found the messages assert msg1['message_id'] in messages_ids assert msg2['message_id'] in messages_ids # asserting the messages haven't been truncated or tampered with assert 'Symbols & words' in messages assert '& with more stuff' in messages
def test_owner_pin_from_outside_channel(): clear() auth_register("*****@*****.**", "password", "User_0", "User_last_0") user_1 = auth_register("*****@*****.**", "password", "User_1", "User_last_1") channel_1 = channels_create(user_1.get('token'), 'channel_1', True) message_info = message_send(user_1.get('token'), channel_1.get('channel_id'), 'Hello world') message_search = search(user_1['token'], 'Hello world') assert message_search['messages'][0].get('message') == 'Hello world' assert message_search['messages'][0].get('is_pinned') == False message_pin(user_1.get('token'), message_info.get('message_id')) message_search = search(user_1['token'], 'Hello world') assert message_search['messages'][0].get('message') == 'Hello world' assert message_search['messages'][0].get('is_pinned') == True
def test_messge_send_later_valid(): ''' Testing for message sent later. ''' clear() auth_register("*****@*****.**", "password", "First", "Last") user_1 = auth_register("*****@*****.**", "password", "First_1", "Last_1") channel_1 = channels_create(user_1.get('token'), 'channel_1', True) time = datetime.datetime.now().replace(microsecond=0) + datetime.timedelta( 0, 5) time_sent = time.timestamp() print(time_sent) def test_sent_later_check(token, message): messages = search(token, message) assert messages == {'messages': []} timer = threading.Timer(3, test_sent_later_check, args=(user_1['token'], 'Hello world')) timer.start() message_id = message_sendlater(user_1["token"], channel_1.get('channel_id'), 'Hello world', time_sent) messages_search = search(user_1['token'], 'Hello world') print(messages_search) assert messages_search['messages'][0]['message_id'] == message_id[ 'message_id'] assert messages_search['messages'][0]['time_created'] == time_sent
def test_search_invalid_token(): """ Tests that given a single channel that the user is a member of, and an invalid token, raises an Access Error """ clear() # Registering a user and creating a channel user = auth_register("*****@*****.**", "password", "firstName", "lastName") channel = channels_create(user['token'], "channel", True) # Forcing a message into channel['channel_messages']['messages'] message_send(user['token'], channel['channel_id'], "message string") # Calling a search with invalid token with pytest.raises(AccessError): search(-1, "message")
def search_query(): """ Returns a list of messages the user has sent matching the search query using http """ token = request.args.get("token") query_str = request.args.get("query_str") return other.search(token, query_str)
def test_search_multiple_channels_single_return(): """ Tests that given a channel the user is a member of, and a valid token, a message can be matched from across multiple channels """ clear() # Registering a user and creating a channel user = auth_register("*****@*****.**", "password", "firstName", "lastName") channel1 = channels_create(user['token'], "channel1", True) channel2 = channels_create(user['token'], "channel2", True) channel3 = channels_create(user['token'], "channel3", True) # Forcing messages into channel['channel_messages']['messages'] message_send(user['token'], channel1['channel_id'], "message string") message_send(user['token'], channel2['channel_id'], "message int") message_send(user['token'], channel3['channel_id'], "message bool") # Calling a search with the query string "bool" result = search(user['token'], "bool") del result['messages'][0]['time_created'] assert result['messages'] == [{ 'message_id': 2, 'u_id': user['u_id'], 'message': "message bool", 'is_pinned': False, 'reacts': [] }]
def test_search_valid(): ch_id = channels_create(hamish['token'], 'comp', True) # Send a message new_message_id = message_send(hamish['token'], ch_id['channel_id'], 'yes') # Save the time created message_store = get_messages_store() for id in message_store['Messages']: if id['message_id'] == new_message_id['message_id']: time_created = id['time_created'] reacts = id['reacts'] pin_status = id['is_pinned'] assert(search(hamish['token'], 'yes') == { 'messages': [ { 'message_id': new_message_id['message_id'], 'u_id': hamish['u_id'], 'message': 'yes', 'time_created': str(time_created), 'reacts': reacts, 'is_pinned': pin_status, } ] } ) # Valid Display
def test_search_string_in_multiple_channels( reset, create_public_channel, make_user_cd): # user_ab creates public channel and sends a message to it new_public_channel, user_ab = create_public_channel message_send( user_ab['token'], new_public_channel['channel_id'], "ab's public channel message") # user_cd creates private channel and sends a message to it user_cd = make_user_cd new_private_channel = channels_create( user_cd['token'], "cd_private", False) message_send( user_cd['token'], new_private_channel['channel_id'], "cd's private channel message") # user_cd invites user_ab to private channel channel_invite(user_cd['token'], new_private_channel['channel_id'], user_ab['u_id']) result_list = search(user_ab['token'], "channel message")['messages'] # getting the message string and user ids that sent them messages = [message['message'] for message in result_list] # u_ids = [message['u_id'] for message in result_list] # asserting the u_ids relate to the messages assert user_cd['u_id'] in [1, 3] assert user_ab['u_id'] in [1, 3] # asserting the messages are in the results assert "ab's public channel message" in messages assert "cd's private channel message" in messages
def test_message_send(): """ Assume that message could not be empty string. If empty string was sended The InputError should raisedcd test whether the message_send funtion worke as ervery input is correct. """ user_infor = auth_register("*****@*****.**", "ccc337992611", "Min", "Li") channel_infor = channels_create(user_infor['token'], 'test_one', True) time_create_date = datetime.datetime.now().replace(microsecond=0) time_create = time_create_date.timestamp() message_id = message_send(user_infor['token'], channel_infor['channel_id'], 'a' * 99) # To test that message actually exists. assert search(user_infor['token'], 'a' * 99) == { "messages": [{ 'message_id': message_id["message_id"], 'u_id': user_infor["u_id"], 'message': 'a' * 99, 'time_created': time_create, 'reacts': [set_reacts()], 'is_pinned': False }] } # the user_infor was return for test_search. return user_infor
def test_other_search(): ''' Test that a user can search for messages in channels that he is a part of ''' other.clear() messages_test = [ 'test message 1', 'test message 2', 'test message 3' ] user1 = auth.auth_register('*****@*****.**', 'abcd1234', 'Will', 'Smith') user2 = auth.auth_register('*****@*****.**', 'abcd1234', 'John', 'Smith') channel_1 = channels.channels_create(user1['token'], 'channel_1', True) channels.channels_create(user1['token'], 'channel_2', True) channel.channel_join(user2['token'], channel_1['channel_id']) message.message_send(user2['token'], channel_1['channel_id'], 'test message 1') message.message_send(user2['token'], channel_1['channel_id'], 'test message 2') message.message_send(user2['token'], channel_1['channel_id'], 'test message 3') message.message_send(user2['token'], channel_1['channel_id'], 'this message should not be found') query_messages = other.search(user2['token'], 'test') i = 0 for messages_found in query_messages['messages']: assert messages_found['message'] == messages_test[i] i += 1 assert len(query_messages['messages']) == 3
def test_clear(): boyu_dict, _ = initialise_data() channel_1 = channels_create(boyu_dict['token'], 'channel_1', True) id_1 = message_send(boyu_dict['token'], channel_1['channel_id'], '1') message_send(boyu_dict['token'], channel_1['channel_id'], '2') message_send(boyu_dict['token'], channel_1['channel_id'], '3') # clear original data clear() # initialise data boyu_dict, _ = initialise_data() users_dict = users_all(boyu_dict['token']) # check user details already update assert len(users_dict['users']) == 2 assert users_dict['users'][0]['u_id'] == boyu_dict['u_id'] assert users_dict['users'][0]['email'] == '*****@*****.**' assert users_dict['users'][0]['name_first'] == 'Boyu' assert users_dict['users'][0]['name_last'] == 'Cai' # check channels list is already update channel_1_id = channels_create(boyu_dict['token'], 'channel_1', True) assert channels_listall(boyu_dict['token']) == channels_list( boyu_dict['token']) # send two message in a channel id_1 = message_send(boyu_dict['token'], channel_1_id['channel_id'], 'hello!') message_send(boyu_dict['token'], channel_1_id['channel_id'], 'he_llo') message_send(boyu_dict['token'], channel_1_id['channel_id'], 'hi there') search_result = search(boyu_dict['token'], "hello") # search message order is correct assert len(search_result['messages']) == 1 assert search_result['messages'][0]["message"] == 'hello!' assert search_result['messages'][0]["u_id"] == boyu_dict['u_id'] assert search_result['messages'][0]["message_id"] == id_1['message_id']
def test_message_remove(): mess = message_function("Hellooo world") token = mess['token'] mid = mess['mid'] message_remove(token, mid) result = search(token, "Hellooo world") assert len(result['messages']) == 0
def test_search_other_members(): """ Test that given messages sent by other user in the channel, Messages can returned by search when a relevant query string is given. """ clear() # Register users user = auth_register("*****@*****.**", "Reanxdi920", "Anesthesia", "Rory") user2 = auth_register("*****@*****.**", "Ascniw101", "Pennicilin", "Mirin") user3 = auth_register("*****@*****.**", "Cpasmc20", "Borgeis", "Filin") # Create a channel and let users join channel = channels_create(user['token'], "Public_channel", True) channel_join(user2['token'], channel['channel_id']) channel_join(user3['token'], channel['channel_id']) # Send some messages (A conversation between 2 members) message_send(user2['token'], channel['channel_id'], 'Hello there') message_send(user3['token'], channel['channel_id'], 'Wassup') msg3 = message_send(user2['token'], channel['channel_id'], 'Who is the owner?') message_send(user3['token'], channel['channel_id'], 'Not me') # Calling search for user with the query string 'owner' result = search(user['token'], "owner") assert result['messages'][0]['message_id'] == msg3['message_id'] assert result['messages'][0]['u_id'] == user2['u_id'] assert result['messages'][0]['message'] == "Who is the owner?"
def test_search_results_multiple(user1): """ Tests successful uses of search, focusing on queries that return multiple messages """ user2 = auth.auth_register("*****@*****.**", "chocfudge222", "first_name", "last_name") test_channel = channels.channels_create(user1["token"], "test_name", True) channel.channel_join(user2["token"], test_channel["channel_id"]) test_message = "This is a test message." test_message2 = "This message is a test right?" message.message_send(user1["token"], test_channel["channel_id"], test_message) message.message_send(user2["token"], test_channel["channel_id"], test_message2) find_message = other.search(user1["token"], "is a test")['messages'] message_key = list(find_message)[0] assert message_key["message_id"] == 1 assert message_key["u_id"] == 1 assert message_key["message"] == "This is a test message." message_key = list(find_message)[1] assert message_key["message_id"] == 2 assert message_key["u_id"] == 3 assert message_key["message"] == "This message is a test right?" other.clear()
def test_hangmne_normal(): """ Test if hangman can start with correct input """ user_infor = auth_register("*****@*****.**", "ccc337992611", "Min", "Li") channel_infor = channels_create(user_infor['token'], 'test_one', True) time_create_date = datetime.datetime.now().replace(microsecond=0) time_create = time_create_date.timestamp() message_id = message_send(user_infor['token'], channel_infor['channel_id'], "/hangman") assert search(BOT_TOKEN, "A game of hangman has been started in this channel") == { "messages": [{ 'message_id': message_id["message_id"] + 1, 'u_id': -1, 'message': "A game of hangman has been started in this channel", 'time_created': time_create, 'reacts': [set_reacts()], 'is_pinned': False }] } auth_logout(BOT_TOKEN) message_id = message_send(user_infor['token'], channel_infor['channel_id'], "/guess e") assert database.get_current_user(BOT_TOKEN) == -1
def test_search_user_not_in_channel(): """ Test that given a valid token and query string, the search function doesn't return a message if its in a channel that the caller is not a member of """ clear() # Register user and create channels user1 = auth_register("*****@*****.**", "password", "firstName", "lastName") user2 = auth_register("*****@*****.**", "password", "firstName2", "lastName2") channel1 = channels_create(user1['token'], "Public_channel", True) channel2 = channels_create(user2['token'], "Public_channel1", True) message_send(user1['token'], channel1['channel_id'], 'Here') message_send(user2['token'], channel2['channel_id'], 'But not here') result = search(user2['token'], "not") del result['messages'][0]['time_created'] assert result['messages'] == [{ 'message_id': 1, 'u_id': user2['u_id'], 'message': "But not here", 'reacts': [], 'is_pinned': False }]