def test_dm_remove_v1_success(user_setup): dm_create_v1(make_token(0), [1]) dm_create_v1(make_token(0), [2]) # removing this dm dm_remove_v1(make_token(0), 1) assert dm_list_v1(make_token(0)) == { 'dms': [{ "dm_id": 0, "name": "gungeetsingh, petertran", "members": [ { 'u_id': 0, 'email': "*****@*****.**", 'name_first': 'Gungeet', 'name_last': 'Singh', 'handle': 'gungeetsingh' }, { 'u_id': 1, 'email': "*****@*****.**", 'name_first': 'Peter', 'name_last': 'Tran', 'handle': 'petertran' }, ], "owner_members": [{ 'u_id': 0, 'email': "*****@*****.**", 'name_first': 'Gungeet', 'name_last': 'Singh', 'handle': 'gungeetsingh' }] }] }
def test_notifications(register_users): channels_create_v2(make_token(1), "PublicChannel1", True) channel_invite_v2(make_token(1), 0, 0) assert notifications_get_v1(make_token(0)) == { "notifications": [{ "channel_id": 0, "dm_id": -1, "notification_message": "petertran invited you to PublicChannel1" }] } dm_create_v1(make_token(0), [1]) # dm_id 0 assert notifications_get_v1(make_token(1)) == { "notifications": [{ "channel_id": -1, "dm_id": 0, "notification_message": "gungeetsingh added you to dm gungeetsingh, petertran" }] } clear_v1()
def test_dm_create_v1(): clear_v1() auth_user_1_token = auth_register_v2("*****@*****.**", "123456", "Andy", "Dandy")["token"] auth_user_2_id = auth_register_v2("*****@*****.**", "123456", "Bob", "Hobs")["auth_user_id"] auth_user_3_id = auth_register_v2("*****@*****.**", "123456", "Chris", "Wiz")["auth_user_id"] dm_data_1 = dm_create_v1(auth_user_1_token, [auth_user_2_id, auth_user_3_id]) dm_id_1 = dm_data_1["dm_id"] dm_name_1 = dm_data_1["dm_name"] dm_list = dm_list_v1(auth_user_1_token) assert dm_list["dms"][0]["dm_id"] == dm_id_1 assert dm_list["dms"][0]["name"] == dm_name_1 auth_user_4_id = auth_register_v2("*****@*****.**", "123456", "Joshua", "King")["auth_user_id"] dm_data_2 = dm_create_v1(auth_user_1_token, [auth_user_3_id, auth_user_4_id]) dm_id_2 = dm_data_2["dm_id"] dm_name_2 = dm_data_2["dm_name"] dm_list = dm_list_v1(auth_user_1_token) assert dm_list["dms"][1]["dm_id"] == dm_id_2 assert dm_list["dms"][1]["name"] == dm_name_2
def test_commom_more(reg_user): clear_v2() user = reg_user(0) token = user['token'] user_2 = reg_user(1) user_2_id = user_2['auth_user_id'] # more dm id dm1 = dm_create_v1(token, [user_2_id]) dm_id1 = dm1['dm_id'] dm2 = dm_create_v1(token, [user_2_id]) dm_id2 = dm2['dm_id'] dm3 = dm_create_v1(token, [user_2_id]) dm_id3 = dm3['dm_id'] dm_remove_v1(token, dm_id1) dm_remove_v1(token, dm_id2) dm_remove_v1(token, dm_id3) dm_list = dm_list_v1(token)['dms'] assert not len(dm_list) # dm become invaild with pytest.raises(InputError): dm_details_v1(token, dm_id1) with pytest.raises(InputError): dm_details_v1(token, dm_id2) with pytest.raises(InputError): dm_details_v1(token, dm_id3)
def test_message_senddm_invalid_dm_id(): # InputError if dm_id given is invalid clear_v1() DM_owner = auth_register_v2('*****@*****.**', 'password123', 'Jordy', 'Eli') user_receiver = auth_register_v2('*****@*****.**', 'password123', 'John', 'Lam') dm_create_v1(DM_owner['token'], [user_receiver['auth_user_id']]) with pytest.raises(InputError): message_senddm_v1(DM_owner['token'], -1, 'sup')
def test_dm_list_v1_token(reg_user): clear_v2() token_sender = reg_user(0)['token'] auth_user_id_receiver = reg_user(1)['auth_user_id'] dm_create_v1(token_sender, [auth_user_id_receiver]) fake_token = 'garbage values' with pytest.raises(AccessError): dm_list_v1(fake_token)
def test_dm_details_v1_dm_id_is_non_valid_dm(reg_user): clear_v2() token_sender = reg_user(0)['token'] auth_user_id_receiver = reg_user(1)['auth_user_id'] dm_create_v1(token_sender, [auth_user_id_receiver]) fake_dm_id = 'garbage values' with pytest.raises(InputError): dm_details_v1(token_sender, fake_dm_id)
def test_search_no_match(register_users): assert search_v2(make_token(0), "Ciao") == {"messages": []} channels_create_v2(make_token(0), "PublicChannel1", True) dm_create_v1(make_token(0), [1, 2]) # dm_id 0 message_send_v2(make_token(0), 0, "Hello") message_senddm_v1(make_token(0), 0, "Goodbye") assert search_v2(make_token(0), "Ciao") == {"messages": []}
def test_dm_list_v1_basic(reg_user): clear_v2() token_sender = reg_user(0)['token'] auth_user_id_receiver = reg_user(1)['auth_user_id'] dm_id_create = dm_create_v1(token_sender, [auth_user_id_receiver])['dm_id'] dm_list_checker = [dm_id_create] for i in range(2, 11): auth_user_id_extra = reg_user(i)['auth_user_id'] dm_id_extra = dm_create_v1(token_sender, [auth_user_id_extra])['dm_id'] dm_list_checker.append(dm_id_extra) dms = dm_list_v1(token_sender)['dms'] for dm in dms: #inside this whole list of dictonarys get this dictonary then check if the dm_id is in the list assert dm['dm_id'] in dm_list_checker
def test_message_sendlaterdm_invalid_dm_id(): # Test if InputError is raised when given invalid dm id clear_v1() user = auth_register_v2('*****@*****.**', 'password123', 'Terry', 'Wills') user2 = auth_register_v2('*****@*****.**', 'password1234', 'Terry', 'Lurr') dm_create_v1(user['token'], [user2['auth_user_id']]) time = datetime.now().replace(microsecond=0) + timedelta(0, 3) time_sent = time.timestamp() with pytest.raises(InputError): message_sendlaterdm_v1(user['token'], -1, 'anyone here?', time_sent)
def test_share_pin(test_data): a_data, b_data, _ = test_data a_ch_id = channels_create_v2(a_data["token"], "A channel", True)["channel_id"] channel_join_v2(b_data["token"], a_ch_id) b_dm_id = dm_create_v1(b_data["token"], [a_data["auth_user_id"]])["dm_id"] a_msg_id = message_send_v2(a_data["token"], a_ch_id, "Message to be shared to dm")["message_id"] b_msgdm_id = message_senddm_v1( b_data["token"], b_dm_id, "Message to be shared to channel")["message_id"] a_share_id = message_share_v1(a_data["token"], a_msg_id, "Shared a msg", -1, b_dm_id)["shared_message_id"] b_share_id = message_share_v1(b_data["token"], b_msgdm_id, "Shared a msg", a_ch_id, -1)["shared_message_id"] message_pin_v1(a_data["token"], b_share_id) messages = channel_messages_v2(a_data["token"], a_ch_id, 0)["messages"] assert messages[0]["message_id"] == b_share_id assert messages[0]["is_pinned"] == True message_pin_v1(b_data["token"], a_share_id) messages = dm_messages_v1(b_data["token"], b_dm_id, 0)["messages"] assert messages[0]["message_id"] == a_share_id assert messages[0]["is_pinned"] == True
def dm_create(): info = request.get_json() token = info["token"] u_ids = info["u_ids"] dm_create = dm_create_v1(token, u_ids) return dumps(dm_create)
def test_50_messages(reg_user): clear_v2() sender = reg_user(0) token_sender = sender['token'] id_sender = sender['auth_user_id'] auth_user_id_receiver = reg_user(1)['auth_user_id'] dm_id = dm_create_v1(token_sender, [auth_user_id_receiver])['dm_id'] start = 0 message_ids = [] message_count = 50 for i in range(message_count): message = f"Test{i}" message_id = message_senddm_v1(token_sender, dm_id, message)['message_id'] message_ids.append(message_id) message_dict = dm_messages_v1(token_sender, dm_id, start) messages = message_dict['messages'] messages.reverse() # Most recent message should now be last # Reverse for purpose of comparison assert len(messages) == message_count assert message_dict['start'] == start assert message_dict['end'] == -1 for i in range(message_count): message_details = messages[i] assert message_details['message_id'] == message_ids[i] assert message_details['u_id'] == id_sender assert message_details['message'] == f"Test{i}"
def test_common_case(reg_user, create_channel): clear_v2() # new user new_user = reg_user(0) user_token = new_user['token'] user_id = new_user['auth_user_id'] # new channel channel_id = create_channel(user_token)['channel_id'] # new dm new_dm = dm_create_v1(user_token, [user_id]) dm_id = new_dm['dm_id'] # new message og_message_text = 'Hi !' og_message_id = message_send_v2(user_token, channel_id, og_message_text)['message_id'] shared_message_text = "Hello World !" shared_message_id = message_share_v1(user_token, og_message_id, shared_message_text, channel_id, dm_id)['shared_message_id'] channel_messages = channel_messages_v2(user_token, channel_id, 0)['messages'] for msg in channel_messages: if msg['message_id'] == shared_message_id: assert msg[ 'message'] == f'{shared_message_text}\n\n{og_message_text}'
def test_dm_messages_v1_less_50(): clear_v1() auth_user_1_data = auth_register_v2("*****@*****.**", "123456", "Andy", "Dandy") auth_user_1_token = auth_user_1_data["token"] auth_user_1_id = auth_user_1_data["auth_user_id"] auth_user_2_data = auth_register_v2("*****@*****.**", "123456", "Bob", "Hobs") auth_user_2_token = auth_user_2_data["token"] auth_user_2_id = auth_user_2_data["auth_user_id"] auth_user_3_data = auth_register_v2("*****@*****.**", "123456", "Chris", "Wiz") auth_user_3_id = auth_user_3_data["auth_user_id"] dm_data_1 = dm_create_v1(auth_user_1_token, [auth_user_2_id, auth_user_3_id]) dm_id_1 = dm_data_1["dm_id"] message_id_1 = message_senddm_v1(auth_user_1_token, dm_id_1, "Hello")["message_id"] message_id_2 = message_senddm_v1(auth_user_2_token, dm_id_1, "World")["message_id"] dm_messages_data = dm_messages_v1(auth_user_1_token, dm_id_1, 0) assert dm_messages_data["messages"][0]["message_id"] == message_id_2 assert dm_messages_data["messages"][0]["u_id"] == auth_user_2_id assert dm_messages_data["messages"][0]["message"] == "World" assert dm_messages_data["messages"][1]["message_id"] == message_id_1 assert dm_messages_data["messages"][1]["u_id"] == auth_user_1_id assert dm_messages_data["messages"][1]["message"] == "Hello" assert dm_messages_data["end"] == -1
def test_dm_messages_v1_more_50(): clear_v1() auth_user_1_data = auth_register_v2("*****@*****.**", "123456", "Andy", "Dandy") auth_user_1_token = auth_user_1_data["token"] auth_user_1_id = auth_user_1_data["auth_user_id"] auth_user_2_data = auth_register_v2("*****@*****.**", "123456", "Bob", "Hobs") auth_user_2_id = auth_user_2_data["auth_user_id"] auth_user_3_data = auth_register_v2("*****@*****.**", "123456", "Chris", "Wiz") auth_user_3_id = auth_user_3_data["auth_user_id"] dm_data_1 = dm_create_v1(auth_user_1_token, [auth_user_2_id, auth_user_3_id]) dm_id_1 = dm_data_1["dm_id"] latest_msg = None for num in range(99): latest_msg = message_senddm_v1(auth_user_1_token, dm_id_1, "Test") dm_messages_data = dm_messages_v1(auth_user_1_token, dm_id_1, 0) for num in range(50): assert dm_messages_data["messages"][num]["u_id"] == auth_user_1_id assert dm_messages_data["messages"][num]["message"] == "Test" assert dm_messages_data["messages"][0]["message_id"] == latest_msg["message_id"] assert len(dm_messages_data["messages"]) == 50 assert dm_messages_data["start"] == 0 assert dm_messages_data["end"] == 50
def test_dm(test_data): a_data, b_data, c_data, a_ch_id = test_data channel_join_v2(b_data["token"], a_ch_id) channel_join_v2(c_data["token"], a_ch_id) # Create the unscramble user message_send_v2(a_data["token"], a_ch_id, "/u start") us_id = None for user in users_all_v1(a_data["token"])["users"]: if user["name_first"] == "Sinister" and user[ "name_last"] == "Scrambler": us_id = user["u_id"] a_dm_id = dm_create_v1( a_data["token"], [b_data["auth_user_id"], c_data["auth_user_id"]])["dm_id"] dm_invite_v1(a_data["token"], a_dm_id, us_id) message_senddm_v1(a_data["token"], a_dm_id, "/u start") # latest message should be the one above. # scrambler shouldnt send any messages assert dm_messages_v1(a_data["token"], a_dm_id, 0)["messages"][0]["message"] == "/u start" message_senddm_v1(a_data["token"], a_dm_id, "/u leaderboard") assert dm_messages_v1(a_data["token"], a_dm_id, 0)["messages"][0]["message"] == "/u leaderboard" message_senddm_v1(a_data["token"], a_dm_id, "/u simple") assert dm_messages_v1(a_data["token"], a_dm_id, 0)["messages"][0]["message"] == "/u simple"
def test_auth_user_not_owner(test_data): a_data, b_data, c_data = test_data # channel a_ch_id = channels_create_v2(a_data["token"], "A channel", True)["channel_id"] channel_join_v2(b_data["token"], a_ch_id) channel_join_v2(c_data["token"], a_ch_id) a_msg_id = message_send_v2(a_data["token"], a_ch_id, "A message")["message_id"] with pytest.raises(AccessError): message_pin_v1(b_data["token"], a_msg_id) message_pin_v1(c_data["token"], a_msg_id) # dm a_dm_id = dm_create_v1( a_data["token"], [b_data["auth_user_id"], c_data["auth_user_id"]])["dm_id"] a_msgdm_id = message_senddm_v1(a_data["token"], a_dm_id, "A message")["message_id"] with pytest.raises(AccessError): message_pin_v1(b_data["token"], a_msgdm_id) message_pin_v1(c_data["token"], a_msgdm_id)
def test_message_senddm_msg_too_long(): # InputError when message is more than 1000 characters clear_v1() DM_owner = auth_register_v2('*****@*****.**', 'password123', 'Roger', 'Rogerson') user_receiver = auth_register_v2('*****@*****.**', 'password123', 'Billy', 'Batson') DM = dm_create_v1(DM_owner['token'], [user_receiver['auth_user_id']]) with pytest.raises(InputError): message_senddm_v1(DM_owner['token'], DM['dm_id'], "E"*1001)
def test_message_senddm_invalid_token(): # AccessError should arise if passing invalid token clear_v1() DM_owner = auth_register_v2('*****@*****.**', 'password123', 'Jerry', 'Tin') user_receiver = auth_register_v2('*****@*****.**', 'password123', 'Shelly', 'Tin') DM = dm_create_v1(DM_owner['token'], [user_receiver['auth_user_id']]) with pytest.raises(AccessError): message_senddm_v1(-1, DM['dm_id'], 'yo what up')
def test_invite_from_non_member(reg_user): clear_v2() sender_token = reg_user(0)['token'] receiver_id = reg_user(1)['auth_user_id'] dm = dm_create_v1(sender_token, [receiver_id]) invitee = reg_user(2) with pytest.raises(AccessError): dm_invite_v1(invitee['token'], dm['dm_id'], invitee['auth_user_id'])
def test_invalid_u_id(reg_user): clear_v2() sender_token = reg_user(0)['token'] receiver_id = reg_user(1)['auth_user_id'] dm = dm_create_v1(sender_token, [receiver_id]) invitee_id = reg_user(2)['auth_user_id'] with pytest.raises(InputError): dm_invite_v1(sender_token, dm['dm_id'], invitee_id + 1)
def test_single_dm_to_single_user(reg_user, u_handle, dm_name): clear_v2() sender_token = reg_user(0)['token'] sender_handle = u_handle(0) receiver_id = reg_user(1)['auth_user_id'] receiver_handle = u_handle(1) dm = dm_create_v1(sender_token, [receiver_id]) assert dm['dm_id'] == 0 assert dm['dm_name'] == dm_name([receiver_handle, sender_handle])
def test_message_senddm_user_not_in_dm_group(): # AccessError should arise if user is not a member of the DM group clear_v1() DM_owner = auth_register_v2('*****@*****.**', 'password123', 'Sheldon', 'Andie') user_receiver = auth_register_v2('*****@*****.**', 'password123', 'Roger', 'Allie') user_random = auth_register_v2('*****@*****.**', 'password123', 'Billy', 'Bobby') DM = dm_create_v1(DM_owner['token'], [user_receiver['auth_user_id']]) with pytest.raises(AccessError): message_senddm_v1(user_random['token'], DM['dm_id'], 'can i join plz')
def test_message_pin_v1_dm_invalid_message_id(reg_user, basic_message): clear_v2() token_1 = reg_user(0)['token'] auth_user_id_receiver = reg_user(1)['auth_user_id'] dm_id = dm_create_v1(token_1, [auth_user_id_receiver])['dm_id'] message = basic_message message_id = message_senddm_v1(token_1, dm_id, message)['message_id'] with pytest.raises(InputError): message_pin_v1(token_1, message_id + 1)
def test_dm_message_senddm_v1_auth_user_not_member(reg_user, basic_message): clear_v2() token_sender = reg_user(0)['token'] auth_user_id_receiver = reg_user(1)['auth_user_id'] non_member_of_dm_token = reg_user(2)['token'] dm_id = dm_create_v1(token_sender, [auth_user_id_receiver])['dm_id'] message = basic_message with pytest.raises(AccessError): message_senddm_v1(non_member_of_dm_token, dm_id, message) #cant post when the user is not part of the dm
def test_dm_message_senddm_v1_message_more_than_1000_characters(reg_user): clear_v2() token_sender = reg_user(0)['token'] auth_user_id_receiver = reg_user(1)['auth_user_id'] dm_id = dm_create_v1(token_sender, [auth_user_id_receiver])['dm_id'] yuge_string = 'a'*1001 #check 1000 and 1001 with pytest.raises(InputError): message_senddm_v1(token_sender, dm_id, yuge_string) #yuge strings over 1000 characters so will raise error
def test_dm_message_senddm_v1_token(reg_user, basic_message): clear_v2() token_sender = reg_user(0)['token'] auth_user_id_receiver = reg_user(1)['auth_user_id'] dm_id = dm_create_v1(token_sender, [auth_user_id_receiver])['dm_id'] fake_token = 'garbage values' message = basic_message with pytest.raises(AccessError): message_senddm_v1(fake_token, dm_id, message) #check token
def test_dm_details_v1_auth_user_not_a_member_of_dm(reg_user): clear_v2() token_sender = reg_user(0)['token'] auth_user_id_receiver = reg_user(1)['auth_user_id'] token_non_auth_user = reg_user(2)['token'] dm_id = dm_create_v1(token_sender, [auth_user_id_receiver])['dm_id'] with pytest.raises(AccessError): dm_details_v1( token_non_auth_user, dm_id ) #since this user was never appart of the dm he cant access info to it
def test_message_pin_v1_token_dm(reg_user, basic_message): clear_v2() token_1 = reg_user(0)['token'] auth_user_id_receiver = reg_user(1)['auth_user_id'] dm_id = dm_create_v1(token_1, [auth_user_id_receiver])['dm_id'] message = basic_message message_id = message_senddm_v1(token_1, dm_id, message)['message_id'] fake_token = 'random values r32fecaswd' with pytest.raises(AccessError): message_pin_v1(fake_token, message_id)