def test_senddm_different_channels(): clear_v1() user_1 = auth_register_v2('*****@*****.**', 'aaaaaa', 'a', 'a') user_2 = auth_register_v2('*****@*****.**', 'bbbbbb', 'b', 'b') user_3 = auth_register_v2('*****@*****.**', 'cccccc', 'c', 'c') dm_1 = dm_create_v1(user_1['token'], [user_2['auth_user_id']]) dm_2 = dm_create_v1(user_2['token'], [user_3['auth_user_id']]) dm_3 = dm_create_v1(user_3['token'], [user_1['auth_user_id']]) msg1 = message_senddm_v1(user_1['token'], dm_1['dm_id'], 'Hello World!') msg2 = message_senddm_v1(user_2['token'], dm_2['dm_id'], 'Hello World!') msg3 = message_senddm_v1(user_3['token'], dm_3['dm_id'], 'Hello World!') assert type(msg1) == dict assert type(msg1['message_id']) == int assert type(msg2) == dict assert type(msg2['message_id']) == int assert type(msg3) == dict assert type(msg3['message_id']) == int assert dm_messages_v1(user_1['token'], dm_1['dm_id'], 0)['messages'][0]['message'] == "Hello World!" assert dm_messages_v1(user_2['token'], dm_2['dm_id'], 0)['messages'][0]['message'] == "Hello World!" assert dm_messages_v1(user_3['token'], dm_3['dm_id'], 0)['messages'][0]['message'] == "Hello World!" assert msg1 != msg2 assert msg1 != msg3 assert msg2 != msg3
def test_multiple_success(create_input): message_senddm_v1(create_input[0][0]["token"], create_input[1][0]["dm_id"], "This is the first normal message") assert len(dm_messages_v1(create_input[0][0]["token"], create_input[1][0]["dm_id"], 0)["messages"]) == 1 current_time = int(time.time()) msg = message_sendlaterdm_v1(create_input[0][1]["token"], create_input[1][0]["dm_id"], "Hey there, I'm from the past!", current_time + 2) assert type(msg) == dict assert type(msg["message_id"]) == int msg = message_sendlaterdm_v1(create_input[0][2]["token"], create_input[1][0]["dm_id"], "Hey, I'm from the distant past!", current_time + 4) assert type(msg) == dict assert type(msg["message_id"]) == int msg = message_sendlaterdm_v1(create_input[0][0]["token"], create_input[1][0]["dm_id"], "Hey there, I'm from the past past!!", current_time + 3) assert type(msg) == dict assert type(msg["message_id"]) == int message_senddm_v1(create_input[0][2]["token"], create_input[1][0]["dm_id"], "This is the second normal message") # Make sure delayed message has not been sent yet assert len(dm_messages_v1(create_input[0][0]["token"], create_input[1][0]["dm_id"], 0)["messages"]) == 2 # Wait 2.5 seconds and check again; first delayed message should be there time.sleep(2.5) msgs = dm_messages_v1(create_input[0][1]["token"], create_input[1][0]["dm_id"], 0)["messages"] assert len(msgs) == 3 assert msgs[0]["message"] == "Hey there, I'm from the past!" assert msgs[0]["time_created"] == current_time + 2 assert msgs[1]["message"] == "This is the second normal message" assert msgs[1]["time_created"] == current_time assert msgs[2]["message"] == "This is the first normal message" assert msgs[2]["time_created"] == current_time # Wait 1 second; second delayed message should be there time.sleep(1) msgs = dm_messages_v1(create_input[0][1]["token"], create_input[1][0]["dm_id"], 0)["messages"] assert len(msgs) == 4 assert msgs[0]["message"] == "Hey there, I'm from the past past!!" assert msgs[0]["time_created"] == current_time + 3 assert msgs[1]["message"] == "Hey there, I'm from the past!" assert msgs[1]["time_created"] == current_time + 2 assert msgs[2]["message"] == "This is the second normal message" assert msgs[2]["time_created"] == current_time assert msgs[3]["message"] == "This is the first normal message" assert msgs[3]["time_created"] == current_time # Wait 1 more second; final delayed message should be there time.sleep(1) msgs = dm_messages_v1(create_input[0][1]["token"], create_input[1][0]["dm_id"], 0)["messages"] assert len(msgs) == 5 assert msgs[0]["message"] == "Hey, I'm from the distant past!" assert msgs[0]["time_created"] == current_time + 4 assert msgs[1]["message"] == "Hey there, I'm from the past past!!" assert msgs[1]["time_created"] == current_time + 3 assert msgs[2]["message"] == "Hey there, I'm from the past!" assert msgs[2]["time_created"] == current_time + 2 assert msgs[3]["message"] == "This is the second normal message" assert msgs[3]["time_created"] == current_time assert msgs[4]["message"] == "This is the first normal message" assert msgs[4]["time_created"] == current_time
def search_v2(token, query_str): """ Given a query string, return a collection of messages in all the channels/DMs that the authorised user has joined that match the query Arguments: token (string) - token of authenticated user query_str (string) - string to be searched for Exceptions: InputError - Occurs when query_str is above 1000 characters - Occurs when query_str is empty AccessError - Occurs when token is invalid Return value: { messages - List of dictionaries, where each dictionary contains types: {message_id, u_id, message, time_created} } """ # Check validity of token findUser(token) # Check query_str is less than 1000 characters or not empty if len(query_str) > 1000: raise InputError(description="query string can't be over 1000 characters") if len(query_str) == 0: raise InputError(description="query string can't be empty") # Define messages return type message_dict = { "messages" : [] } # Go through channels u_channels_list = channels_list_v2(token)["channels"] for channel in u_channels_list: msgs = channel_messages_v2(token, channel["channel_id"], 0) look_for_message(query_str, msgs, message_dict) while msgs["end"] != -1: msgs = channel_messages_v2(token, channel["channel_id"], msgs["end"]) look_for_message(query_str, msgs, message_dict) # Go through dms u_dms_list = dm_list_v1(token)["dms"] for dm in u_dms_list: msgs = dm_messages_v1(token, dm["dm_id"], 0) look_for_message(query_str, msgs, message_dict) while msgs["end"] != -1: msgs = dm_messages_v1(token, dm["dm_id"], msgs["end"]) look_for_message(query_str, msgs, message_dict) return message_dict
def test_success_no_delay(create_input): assert dm_messages_v1(create_input[0][0]["token"], create_input[1][0]["dm_id"], 0)["messages"] == [] current_time = int(time.time()) msg = message_sendlaterdm_v1(create_input[0][0]["token"], create_input[1][0]["dm_id"], "Hey there!", current_time) assert type(msg) == dict assert type(msg["message_id"]) == int # Message should have been sent already msgs = dm_messages_v1(create_input[0][0]["token"], create_input[1][0]["dm_id"], 0)["messages"] assert len(msgs) == 1 assert msgs[0]["message"] == "Hey there!" assert msgs[0]["time_created"] == current_time
def test_dm_message_pass(create_dms): users = create_dms['users'] dm_1 = create_dms['dms'][0] dm_2 = create_dms['dms'][1] message_ids_1 = create_dms['msg_ids'][0] message_ids_2 = create_dms['msg_ids'][1] messages_1 = dm_messages_v1(users[0]['token'], dm_1['dm_id'], 0) end = messages_1['end'] start = messages_1['start'] msgs = messages_1['messages'] assert end == -1 assert start == 0 for i in range(0, 50): assert msgs[i]['message_id'] == message_ids_1[49 - i]['message_id'] messages_2 = dm_messages_v1(users[0]['token'], dm_2['dm_id'], 0) end = messages_2['end'] start = messages_2['start'] msgs = messages_2['messages'] assert end == 50 assert start == 0 for i in range(0, 50): assert msgs[i]['message_id'] == message_ids_2[199 - i]['message_id'] messages_2 = dm_messages_v1(users[0]['token'], dm_2['dm_id'], end) end = messages_2['end'] start = messages_2['start'] msgs = messages_2['messages'] assert end == 100 assert start == 50 for i in range(0, 50): assert msgs[i]['message_id'] == message_ids_2[149 - i]['message_id'] messages_2 = dm_messages_v1(users[0]['token'], dm_2['dm_id'], end) end = messages_2['end'] start = messages_2['start'] msgs = messages_2['messages'] assert end == 150 assert start == 100 for i in range(0, 50): assert msgs[i]['message_id'] == message_ids_2[99 - i]['message_id'] messages_2 = dm_messages_v1(users[0]['token'], dm_2['dm_id'], end) end = messages_2['end'] start = messages_2['start'] msgs = messages_2['messages'] assert end == -1 assert start == 150 for i in range(0, 50): assert msgs[i]['message_id'] == message_ids_2[49 - i]['message_id']
def test_remove_from_dm(): clear_v1() user_1 = auth_register_v2('*****@*****.**', 'aaaaaa', 'a', 'a') user_2 = auth_register_v2('*****@*****.**', 'bbbbbb', 'b', 'b') dm_1 = dm_create_v1(user_1['token'], [user_2['auth_user_id']]) msg_1 = message_senddm_v1(user_1['token'], dm_1['dm_id'], 'Sliding in') message_remove_v1(user_1['token'], msg_1['message_id']) assert len(dm_messages_v1(user_1['token'], dm_1['dm_id'], 0)['messages']) == 0 msg_2 = message_senddm_v1(user_1['token'], dm_1['dm_id'], 'Pls dont block me') message_edit_v2(user_1['token'], msg_2['message_id'], '') assert len(dm_messages_v1(user_1['token'], dm_1['dm_id'], 0)['messages']) == 0
def test_simple_success(create_input): assert dm_messages_v1(create_input[0][0]["token"], create_input[1][0]["dm_id"], 0)["messages"] == [] current_time = int(time.time()) msg = message_sendlaterdm_v1(create_input[0][0]["token"], create_input[1][0]["dm_id"], "Hey there!", current_time + 2) assert type(msg) == dict assert type(msg["message_id"]) == int # Make sure message has not been sent yet assert dm_messages_v1(create_input[0][0]["token"], create_input[1][0]["dm_id"], 0)["messages"] == [] # Wait 2.5 seconds and check again; message should be there time.sleep(2.5) msgs = dm_messages_v1(create_input[0][0]["token"], create_input[1][0]["dm_id"], 0)["messages"] assert len(msgs) == 1 assert msgs[0]["message"] == "Hey there!" assert msgs[0]["time_created"] == current_time + 2
def test_removed_dms(): """Tests that sent dm messages are now 'Removed user' after the user is removed """ clear_v1() user_1 = auth_register_v2("*****@*****.**", "finalpassword", "Eric", "Zheng") user_2 = auth_register_v2("*****@*****.**", "validpassword", "Josh", "Hatton") dm_1 = dm_create_v1(user_1["token"], [user_2["auth_user_id"]]) message_senddm_v1(user_1["token"], dm_1["dm_id"], "Hello user 2!") message_2 = message_senddm_v1(user_2["token"], dm_1["dm_id"], "Nice to meet you user 1") message_senddm_v1(user_2["token"], dm_1["dm_id"], "you are a donkey :)") assert admin_user_remove_v1(user_1["token"], user_2["auth_user_id"]) == {} dm_1_messages = dm_messages_v1(user_1["token"], dm_1["dm_id"], 0) for dm in dm_1_messages["messages"]: if dm["message_id"] == message_2["message_id"]: assert dm["message"] == "Removed user" clear_v1()
def test_edit_from_dm(): clear_v1() user_1 = auth_register_v2('*****@*****.**', 'aaaaaa', 'a', 'a') user_2 = auth_register_v2('*****@*****.**', 'bbbbbb', 'b', 'b') dm_1 = dm_create_v1(user_1['token'], [user_2['auth_user_id']]) msg_1 = message_senddm_v1(user_1['token'], dm_1['dm_id'], 'Sliding in') message_edit_v2(user_1['token'], msg_1['message_id'], 'Beep beep lettuce') assert dm_messages_v1(user_1['token'], dm_1['dm_id'], 0)['messages'][0]['message'] == 'Beep beep lettuce'
def dm_messages_v1_http(): data = request.args token = str(data['token']) dm_id = int(data['dm_id']) start = int(data['start']) return_val = dm_messages_v1(token, dm_id, start) return dumps(return_val)
def test_senddm_basic(): clear_v1() user_1 = auth_register_v2('*****@*****.**', 'aaaaaa', 'a', 'a') user_2 = auth_register_v2('*****@*****.**', 'bbbbbb', 'b', 'b') dm_1 = dm_create_v1(user_1['token'], [user_2['auth_user_id']]) msg_1 = message_senddm_v1(user_1['token'], dm_1['dm_id'], 'Sliding in') assert type(msg_1) == dict assert type(msg_1['message_id']) == int assert dm_messages_v1(user_1['token'], dm_1['dm_id'], 0)['messages'][0]['message'] == "Sliding in"
def test_mixed_success(create_input): current_time = int(time.time()) message_senddm_v1(create_input[0][0]["token"], create_input[1][0]["dm_id"], "This is the first normal message") assert len(dm_messages_v1(create_input[0][0]["token"], create_input[1][0]["dm_id"], 0)["messages"]) == 1 msg = message_sendlaterdm_v1(create_input[0][1]["token"], create_input[1][0]["dm_id"], "Hey there, I'm from the past!", current_time + 2) assert type(msg) == dict assert type(msg["message_id"]) == int message_senddm_v1(create_input[0][2]["token"], create_input[1][0]["dm_id"], "This is the second normal message") # Make sure delayed message has not been sent yet msgs = dm_messages_v1(create_input[0][0]["token"], create_input[1][0]["dm_id"], 0)["messages"] assert len(msgs) == 2 # Wait 2.5 seconds and check again; delayed message should be there time.sleep(2.5) msgs = dm_messages_v1(create_input[0][1]["token"], create_input[1][0]["dm_id"], 0)["messages"] assert len(msgs) == 3 assert msgs[0]["message"] == "Hey there, I'm from the past!" assert msgs[0]["time_created"] == current_time + 2 assert msgs[1]["message"] == "This is the second normal message" assert msgs[1]["time_created"] == current_time assert msgs[2]["message"] == "This is the first normal message" assert msgs[2]["time_created"] == current_time
def test_share_multiple_message_dm(): clear_v1() user_1 = auth_register_v2('*****@*****.**', 'aaaaaa', 'a', 'a') dm_1 = dm_create_v1(user_1['token'], []) dm_2 = dm_create_v1(user_1['token'], []) msg1 = message_senddm_v1(user_1['token'], dm_1['dm_id'], 'Hi DM 1') msg2 = message_senddm_v1(user_1['token'], dm_2['dm_id'], 'Hi DM 2') msg3 = message_share_v1(user_1['token'], msg1['message_id'], '', -1, dm_2['dm_id']) msg4 = message_share_v1(user_1['token'], msg2['message_id'], 'SPAM!', -1, dm_1['dm_id']) assert dm_messages_v1(user_1['token'], dm_1['dm_id'], 0)['messages'][0]['message'] == "Hi DM 2\nSPAM!" assert dm_messages_v1(user_1['token'], dm_2['dm_id'], 0)['messages'][0]['message'] == "Hi DM 1" assert len(dm_messages_v1(user_1['token'], dm_1['dm_id'], 0)['messages']) == 2 assert len(dm_messages_v1(user_1['token'], dm_2['dm_id'], 0)['messages']) == 2 assert msg1['message_id'] != msg4['shared_message_id'] assert msg1['message_id'] != msg3['shared_message_id'] msg5 = message_senddm_v1(user_1['token'], dm_1['dm_id'], 'DM 1 > DM 2') msg6 = message_senddm_v1(user_1['token'], dm_2['dm_id'], 'DM 1 stinkyyyy') msg7 = message_share_v1(user_1['token'], msg5['message_id'], '123', -1, dm_2['dm_id']) msg8 = message_share_v1(user_1['token'], msg6['message_id'], '', -1, dm_1['dm_id']) assert dm_messages_v1(user_1['token'], dm_1['dm_id'], 0)['messages'][0]['message'] == "DM 1 stinkyyyy" assert dm_messages_v1(user_1['token'], dm_2['dm_id'], 0)['messages'][0]['message'] == "DM 1 > DM 2\n123" assert len(dm_messages_v1(user_1['token'], dm_1['dm_id'], 0)['messages']) == 4 assert len(dm_messages_v1(user_1['token'], dm_2['dm_id'], 0)['messages']) == 4 assert msg5['message_id'] != msg8['shared_message_id'] assert msg5['message_id'] != msg7['shared_message_id']
def test_dm_message_errors(create_dms): users = create_dms['users'] dm_1 = create_dms['dms'][0] dm_2 = create_dms['dms'][1] #invalid dm_id with pytest.raises(InputError): dm_messages_v1(users[0]['token'], 1000, 0) with pytest.raises(InputError): dm_messages_v1(users[0]['token'], 32423, 0) #too large start number with pytest.raises(InputError): dm_messages_v1(users[0]['token'], dm_1['dm_id'], 500) with pytest.raises(InputError): dm_messages_v1(users[0]['token'], dm_2['dm_id'], 800) #invalid token with pytest.raises(AccessError): dm_messages_v1("dwaedwqa", dm_1['dm_id'], 0) with pytest.raises(AccessError): dm_messages_v1("I need your time", dm_1['dm_id'], 0) #authorised member not in the dm with pytest.raises(AccessError): dm_messages_v1(users[3]['token'], dm_1['dm_id'], 0) with pytest.raises(AccessError): dm_messages_v1(users[3]['token'], dm_2['dm_id'], 0)