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_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_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_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_dm_list_v1_success(user_setup, dm_setup): # list out a particular users dms assert dm_list_v1(make_token(3)) == { 'dms': [{ "dm_id": 3, "name": "gungeetsingh, talavrahami", "members": [ { 'u_id': 3, 'email': "*****@*****.**", 'name_first': 'Tal', 'name_last': 'Avrahami', 'handle': 'talavrahami' }, { 'u_id': 0, 'email': "*****@*****.**", 'name_first': 'Gungeet', 'name_last': 'Singh', 'handle': 'gungeetsingh' }, ], "owner_members": [{ 'u_id': 3, 'email': "*****@*****.**", 'name_first': 'Tal', 'name_last': 'Avrahami', 'handle': 'talavrahami' }] }] }
def test_dm_leave_v1(): clear_v1() auth_user_1_data = auth_register_v2("*****@*****.**", "123456", "Andy", "Dandy") auth_user_1_token = auth_user_1_data["token"] 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"] dm_name_1 = dm_data_1["dm_name"] assert dm_list_v1(auth_user_1_token) == { "dms": [ { "dm_id": dm_id_1, "name": dm_name_1 } ] } dm_leave_v1(auth_user_1_token, dm_id_1) assert dm_list_v1(auth_user_1_token) == { "dms": [ ] } assert dm_list_v1(auth_user_2_token) == { "dms": [ { "dm_id": dm_id_1, "name": dm_name_1 } ] } dm_leave_v1(auth_user_2_token, dm_id_1) assert dm_list_v1(auth_user_2_token) == { "dms": [ ] }
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_one_commom(reg_user): clear_v2() user = reg_user(0) token = user['token'] second_user = reg_user(1) second_id = second_user['auth_user_id'] # one dm id dm = dm_create_v1(token, [second_id]) dm_id = dm['dm_id'] dm_leave_v1(token, dm_id) dm_list = dm_list_v1(token)['dms'] assert not len(dm_list)
def test_commom_one(reg_user): clear_v2() user = reg_user(0) token = user['token'] u_id = user['auth_user_id'] # one dm id dm = dm_create_v1(token, [u_id]) dm_id = dm['dm_id'] dm_remove_v1(token, dm_id) dm_list = dm_list_v1(token)['dms'] assert not len(dm_list) with pytest.raises(InputError): dm_details_v1(token, dm_id)
def test_more_commom(reg_user): clear_v2() user = reg_user(0) token = user['token'] second_user = reg_user(1) second_id = second_user['auth_user_id'] # more dm id dm1 = dm_create_v1(token, [second_id]) dm_id1 = dm1['dm_id'] dm2 = dm_create_v1(token, [second_id]) dm_id2 = dm2['dm_id'] dm3 = dm_create_v1(token, [second_id]) dm_id3 = dm3['dm_id'] dm_leave_v1(token, dm_id1) dm_leave_v1(token, dm_id2) dm_leave_v1(token, dm_id3) dm_list = dm_list_v1(token)['dms'] assert not len(dm_list)
def dm_list(): parameters = request.args token = parameters['token'] output = dm_list_v1(token) return dumps(output)
def test_dm_invite_v1(): 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"] dm_name_1 = dm_data_1["dm_name"] auth_user_4_data = auth_register_v2("*****@*****.**", "123456", "Joshua", "King") auth_user_4_token = auth_user_4_data["token"] auth_user_4_id = auth_user_4_data["auth_user_id"] assert dm_list_v1(auth_user_4_token) == { "dms": [ ] } dm_invite_v1(auth_user_1_token, dm_id_1, auth_user_4_id) assert dm_list_v1(auth_user_4_token) == { "dms": [ { "dm_id": dm_id_1, "name": dm_name_1 } ] } dm_data_2 = dm_create_v1(auth_user_4_token, [auth_user_1_id, auth_user_2_id, auth_user_3_id]) dm_id_2 = dm_data_2["dm_id"] dm_name_2 = dm_data_2["dm_name"] auth_user_5_data = auth_register_v2("*****@*****.**", "123456", "John", "Smith") auth_user_5_token = auth_user_5_data["token"] auth_user_5_id = auth_user_5_data["auth_user_id"] assert dm_list_v1(auth_user_5_token) == { "dms": [ ] } dm_invite_v1(auth_user_1_token, dm_id_1, auth_user_5_id) dm_invite_v1(auth_user_1_token, dm_id_2, auth_user_5_id) assert dm_list_v1(auth_user_5_token) == { "dms": [ { "dm_id": dm_id_1, "name": dm_name_1 }, { "dm_id": dm_id_2, "name": dm_name_2 } ] }
def notifications_get_v1(token): """ Return the user's most recent 20 notifications Parameters: token (string) - string that stores user data of caller Exceptions: AccessError - Occurs when token is invalid Returns: notifications - List of dictionaries, where each dictionary contains types { channel_id, dm_id, notification_message } """ with open("data.json", "r") as json_file: data = load(json_file) data_structure = token_check(token) user_handle = user_profile_v2(token, data_structure["u_id"])["user"]["handle_str"] raw_notifs = [] # CHANNEL MESSAGES channel_list = channels_list_v2(token)["channels"] channel_id_list = [] for channel in channel_list: channel_id_list.append(channel["channel_id"]) # go through every single channel for their messages for channel in channel_list: for message in channel_messages_v2(token, channel["channel_id"], 0)["messages"]: # for each of these messages look for the tag if f"@{user_handle}" in message["message"]: # add them to a list of tagged messages # use string range [:] to get the latest 20 of them raw_notifs.append({ "notif": [ CHANNEL, TAG, message["u_id"], channel["channel_id"], channel["name"], message["message"][:20] ], "time_stamp": message["time_created"] }) # for each of these messages look at the reacts if message["u_id"] == data_structure["u_id"] and len( message["react_history"]) > 0: for react in message["react_history"]: raw_notifs.append({ "notif": [ CHANNEL, REACT, react["u_id"], channel["channel_id"], channel["name"] ], "time_stamp": react["time_stamp"] }) # CHANNEL INVITES for channel in data["channels"]: if channel["channel_id"] in channel_id_list: for invite in channel["invites"]: if invite["u_id"] == data_structure["u_id"]: raw_notifs.append({ "notif": [ CHANNEL, INVITE, invite["auth_user_id"], channel["channel_id"], channel["name"] ], "time_stamp": invite["time_invited"] }) # DM MESSAGES dm_list = dm_list_v1(token)["dms"] dm_id_list = [] for dm in dm_list: dm_id_list.append(dm["dm_id"]) for dm in dm_list: for message in dm_messages_v1(token, dm["dm_id"], 0)["messages"]: if f"@{user_handle}" in message["message"]: raw_notifs.append({ "notif": [ DM, TAG, message["u_id"], dm["dm_id"], dm["name"], message["message"][:20] ], "time_stamp": message["time_created"] }) if message["u_id"] == data_structure["u_id"] and len( message["react_history"]) > 0: for react in message["react_history"]: raw_notifs.append({ "notif": [DM, REACT, react["u_id"], dm["dm_id"], dm["name"]], "time_stamp": react["time_stamp"] }) # DM INVITES for dm in data["dms"]: if dm["dm_id"] in dm_id_list: for invite in dm["invites"]: if invite["u_id"] == data_structure["u_id"]: raw_notifs.append({ "notif": [ DM, INVITE, invite["auth_user_id"], dm["dm_id"], dm["name"] ], "time_stamp": invite["time_invited"] }) # sort them by time raw_notifs.sort(reverse=True, key=sort_time_stamp) raw_notifs = raw_notifs[:20] # format the notifications notifs = [] """ tagged: "{User’s handle} tagged you in {channel/DM name}: {first 20 characters of the message}" added to a channel/DM: "{User’s handle} added you to {channel/DM name} """ for notif in raw_notifs: # find user handle notif = notif["notif"] for user in data["users"]: if user["u_id"] == notif[AUTH_U_ID]: auth_user_handle = user["handle_str"] if notif[CHAT_TYPE] == CHANNEL: channel_id = notif[CHAT_ID] dm_id = -1 else: dm_id = notif[CHAT_ID] channel_id = -1 if notif[NOTIF_TYPE] == TAG: notifs.append({ "channel_id": channel_id, "dm_id": dm_id, "notification_message": f"{auth_user_handle} tagged you in {notif[NAME]}: {notif[MESSAGE]}" }) elif notif[NOTIF_TYPE] == INVITE: notifs.append({ "channel_id": channel_id, "dm_id": dm_id, "notification_message": f"{auth_user_handle} added you to {notif[NAME]}" }) else: # notif[NOTIF_TYPE] == REACT: notifs.append({ "channel_id": channel_id, "dm_id": dm_id, "notification_message": f"{auth_user_handle} reacted to your message in {notif[NAME]}" }) return {"notifications": notifs}
def test_dm_list_v1_errors(user_setup, dm_setup): # invalid token with pytest.raises(AccessError): dm_list_v1(123456)
def dm_list(): token = request.args.get('token') dm_list = dm_list_v1(token) return dumps(dm_list)
def test_dm_list_v1_non_join(): clear_v1() auth_user_1_token = auth_register_v2("*****@*****.**", "123456", "Andy", "Dandy")["token"] assert dm_list_v1(auth_user_1_token) == {"dms": []}