def test_invite_global_owner(create_input): """ Tests that when a global owner is invited to a channel, they will have owner permissions in that channel. """ channel_1 = create_input[1][1] assert channel_invite_v2(create_input[0][1]['token'], channel_1['channel_id'], create_input[0][0]['auth_user_id']) == {} assert channel_invite_v2(create_input[0][1]['token'], channel_1['channel_id'], create_input[0][2]['auth_user_id']) == {} # create_input[0][0] is a global owner, so when invited, they should join # as a member but have owner permissions # create_input[0][2] is a member assert len( channel_details_v2(create_input[0][1]['token'], channel_1['channel_id'])['owner_members']) == 1 assert len( channel_details_v2(create_input[0][1]['token'], channel_1['channel_id'])['all_members']) == 3 # create_input[0][0] should be able to promote create_input[0][2] to a # channel owner assert channel_addowner_v1(create_input[0][0]['token'], channel_1['channel_id'], create_input[0][2]['auth_user_id']) == {}
def test_channel_invite_stat(): clear_v1() result1 = auth_register_v2('*****@*****.**', 'password', 'comp', 'student') result2 = auth_register_v2('*****@*****.**', 'password', 'comp', 'student') channel1 = channels_create_v2(result1['token'], "The party channel 1", True) channel_invite_v2(result1['token'], channel1['channel_id'], result2['auth_user_id']) output = user_stats(result1['token']) output1 = user_stats(result2['token']) dreams = users_stats(result1['token']) assert len(output['user_stats']) == 4 assert output['user_stats']['channels_joined'][-1]['num_channels_joined'] == 1 assert output['user_stats']['dms_joined'][-1]['num_dms_joined'] == 0 assert output['user_stats']['messages_sent'][-1]['num_messages_sent'] == 0 assert output['user_stats']['involvement_rate'] == 1 assert len(output1['user_stats']) == 4 assert output1['user_stats']['channels_joined'][-1]['num_channels_joined'] == 1 assert output1['user_stats']['dms_joined'][-1]['num_dms_joined'] == 0 assert output1['user_stats']['messages_sent'][-1]['num_messages_sent'] == 0 assert output1['user_stats']['involvement_rate'] == 1 assert len(dreams['dreams_stats']) == 4 assert dreams['dreams_stats']['channels_exist'][-1]['num_channels_exist'] == 1 assert dreams['dreams_stats']['dms_exist'][-1]['num_dms_exist'] == 0 assert dreams['dreams_stats']['messages_exist'][-1]['num_messages_exist'] == 0 assert dreams['dreams_stats']['utilization_rate'] == 2/2
def test_add_channel(test_data): a_data, b_data, c_data, a_ch_id = test_data b_ch_id = channels_create_v2(b_data['token'], 'Channel 2', True)['channel_id'] # ADD TO CHANNEL channel_invite_v2(a_data["token"], a_ch_id, c_data["auth_user_id"]) notifs = notifications_get_v1(c_data["token"])["notifications"] assert len(notifs) == 1 assert notifs[0] == { "channel_id": a_ch_id, "dm_id": -1, "notification_message": "andyanderson added you to Channel 1" } sleep(1) channel_invite_v2(b_data["token"], b_ch_id, c_data["auth_user_id"]) notifs = notifications_get_v1(c_data["token"])["notifications"] assert len(notifs) == 2 assert notifs[0] == { "channel_id": b_ch_id, "dm_id": -1, "notification_message": "bobbobinson added you to Channel 2" } assert notifs[1] == { "channel_id": a_ch_id, "dm_id": -1, "notification_message": "andyanderson added you to Channel 1" } sleep(1)
def test_different_permissions_edit(): 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') channel_1 = channels_create_v2(user_1['token'], 'Channel 1', True) channel_invite_v2(user_1['token'], channel_1['channel_id'], user_2['auth_user_id']) channel_invite_v2(user_1['token'], channel_1['channel_id'], user_3['auth_user_id']) msg1 = message_send_v2(user_2['token'], channel_1['channel_id'], 'Hello World!') assert channel_messages_v2(user_1['token'], channel_1['channel_id'], 0)['messages'][0]['message'] == "Hello World!" assert message_edit_v2(user_1['token'], msg1['message_id'], 'Goodbye World!') == {} assert channel_messages_v2(user_1['token'], channel_1['channel_id'], 0)['messages'][0]['message'] == "Goodbye World!" assert message_edit_v2(user_2['token'], msg1['message_id'], 'Hello Again World!') == {} assert channel_messages_v2( user_1['token'], channel_1['channel_id'], 0)['messages'][0]['message'] == "Hello Again World!" assert len( channel_messages_v2(user_1['token'], channel_1['channel_id'], 0)['messages']) == 1 with pytest.raises(AccessError): message_edit_v2(user_3['token'], msg1['message_id'], 'Goodbye Again!') assert channel_messages_v2( user_1['token'], channel_1['channel_id'], 0)['messages'][0]['message'] == "Hello Again World!"
def test_different_permissions_edit_remove(): 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') channel_1 = channels_create_v2(user_1['token'], 'Channel 1', True) channel_invite_v2(user_1['token'], channel_1['channel_id'], user_2['auth_user_id']) channel_invite_v2(user_1['token'], channel_1['channel_id'], user_3['auth_user_id']) msg1 = message_send_v2(user_2['token'], channel_1['channel_id'], 'First Message') msg2 = message_send_v2(user_2['token'], channel_1['channel_id'], 'Second Message') assert len( channel_messages_v2(user_1['token'], channel_1['channel_id'], 0)['messages']) == 2 assert message_edit_v2(user_1['token'], msg1['message_id'], '') == {} assert len( channel_messages_v2(user_1['token'], channel_1['channel_id'], 0)['messages']) == 1 assert channel_messages_v2(user_1['token'], channel_1['channel_id'], 0)['messages'][0]['message'] == "Second Message" assert message_edit_v2(user_2['token'], msg2['message_id'], '') == {} msg3 = message_send_v2(user_2['token'], channel_1['channel_id'], 'Third Message') assert channel_messages_v2(user_1['token'], channel_1['channel_id'], 0)['messages'][0]['message'] == "Third Message" with pytest.raises(AccessError): message_edit_v2(user_3['token'], msg3['message_id'], '') with pytest.raises(AccessError): message_remove_v1(user_3['token'], msg3['message_id']) assert channel_messages_v2(user_1['token'], channel_1['channel_id'], 0)['messages'][0]['message'] == "Third Message"
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_channel_invite_v2_channel_not_exist_non_negative(reg_user): clear_v2() token_1 = reg_user(0)['token'] # this maker of the channel u_id = reg_user(1)['auth_user_id'] # this is the invitee of the channel channel_id = 'garbage values' with pytest.raises(InputError): channel_invite_v2(token_1, channel_id, u_id) # channel_id does not exist
def test_assorted_notifications(): clear_v1() user1 = auth_register_v2("*****@*****.**", "finalpassword", "Eric", "Zheng") user2 = auth_register_v2("*****@*****.**", "validpassword", "Josh", "Hatton") channel1 = channels_create_v2(user1['token'], 'Channel 1', True) channel_invite_v2(user1['token'], channel1['channel_id'], user2['auth_user_id']) msg1str = "Welcome to the channel @joshhatton" msg1 = message_send_v2(user1['token'], channel1['channel_id'], msg1str) message_react_v1(user2['token'], msg1['message_id'], 1) msg2str = "Thanks for having me @ericzheng" msg2 = message_send_v2(user2['token'], channel1['channel_id'], msg2str) message_react_v1(user1['token'], msg2['message_id'], 1) assert notifications_get_v1(user1['token']) == { 'notifications': [{ 'channel_id': channel1['channel_id'], 'dm_id': -1, 'notification_message': f"joshhatton tagged you in Channel 1: {msg2str[0:20]}" }, { 'channel_id': channel1['channel_id'], 'dm_id': -1, 'notification_message': "joshhatton reacted to your message in Channel 1" }] } assert notifications_get_v1(user2['token']) == { 'notifications': [ { 'channel_id': channel1['channel_id'], 'dm_id': -1, 'notification_message': "ericzheng reacted to your message in Channel 1" }, { 'channel_id': channel1['channel_id'], 'dm_id': -1, 'notification_message': f"ericzheng tagged you in Channel 1: {msg1str[0:20]}" }, { 'channel_id': channel1['channel_id'], 'dm_id': -1, 'notification_message': "ericzheng added you to Channel 1" }, ] }
def test_channel_invite_v2_token(reg_user, basic_channel_name): clear_v2() token_1 = reg_user(0)['token'] u_id = reg_user(1)['auth_user_id'] channel_name = basic_channel_name channel_id = channels_create_v2(token_1, channel_name, True)['channel_id'] fake_token = 'random values r32fecaswd' with pytest.raises(AccessError): channel_invite_v2(fake_token, channel_id, u_id)
def test_channel_invite_v2_u_id_not_valid_user(reg_user, basic_channel_name): clear_v2() token_1 = reg_user(0)['token'] # this maker of the channel u_id = -1 # this is the invitee of the channel channel_name = basic_channel_name is_public = False channel_id = channels_create_v2(token_1, channel_name, is_public)['channel_id'] with pytest.raises(InputError): channel_invite_v2(token_1, channel_id, u_id) # u_id does not exist
def test_single_channel_add(reg_user, u_handle, crt_channel, ch_add_str): clear_v2() user_1_token = reg_user(0)['token'] user_2 = reg_user(1) channel_id = crt_channel(user_1_token, 0)['channel_id'] channel_invite_v2(user_1_token, channel_id, user_2['auth_user_id']) notifs = notifications_get_v1(user_2['token'])['notifications'] assert len(notifs) == 1 notif = notifs[0] assert notif['channel_id'] == channel_id assert notif['dm_id'] == -1 assert notif['notification_message'] == ch_add_str(u_handle(0), 0)
def test_invalid_channel_id_exception(): """ Tests when channel_id does not refer to a valid channel. """ clear_v1() user_1 = auth_register_v2("*****@*****.**", "password34", "Eric", "Zheng") user_2 = auth_register_v2("*****@*****.**", "password3", "Jordan", "Milch") with pytest.raises(InputError): channel_invite_v2(user_1['token'], 0, user_2['auth_user_id']) channel_invite_v2(user_2['token'], 1, user_1['auth_user_id'])
def test_channels_list_v2_invite(user_setup, channel_setup): ''' list channels after inviting another user (Non-owner) ''' channel_invite_v2(make_token(1), 1, 2) assert channels_list_v2(make_token(1)) == { 'channels': [ { 'channel_id': 1, 'name': 'Pchannel', }, ], }
def test_edit_other_users_message(): clear_v1() user_1 = auth_register_v2('*****@*****.**', 'aaaaaa', 'a', 'a') channel_1 = channels_create_v2(user_1['token'], 'Channel 1', True) user_2 = auth_register_v2('*****@*****.**', 'aaaaaa', 'a', 'a') channel_invite_v2(user_1['token'], channel_1['channel_id'], user_2['auth_user_id']) msg1 = message_send_v2(user_1['token'], channel_1['channel_id'], 'Hey!') msg2 = message_send_v2(user_2['token'], channel_1['channel_id'], 'Hello!') with pytest.raises(AccessError): message_edit_v2(user_1['token'], msg2['message_id'], 'He is stealing') message_edit_v2(user_2['token'], msg1['message_id'], 'I did not say this')
def test_single_channel_tag(reg_user, u_handle, crt_channel, tag_message): clear_v2() user_1_token = reg_user(0)['token'] user_2 = reg_user(1) channel_id = crt_channel(user_1_token, 0)['channel_id'] channel_invite_v2(user_1_token, channel_id, user_2['auth_user_id']) message_text = tag_message(user_1_token, channel_id, 0, 0, 1, 0) notifs = notifications_get_v1(user_2['token'])['notifications'] assert len(notifs) == 2 notif = notifs[0] assert notif['channel_id'] == channel_id assert notif['dm_id'] == -1 assert notif['notification_message'] == message_text
def test_channel_invite_v2_authorised_user_is_not_part_of_channel( reg_user, basic_channel_name): clear_v2() token_1 = reg_user(0)['token'] # this maker of the channel token_2 = reg_user(1)['token'] #user not inside channel u_id_3 = reg_user(2)['auth_user_id'] # this is the invitee of the channel channel_name = basic_channel_name is_public = False channel_id = channels_create_v2(token_1, channel_name, is_public)['channel_id'] with pytest.raises(AccessError): channel_invite_v2( token_2, channel_id, u_id_3 ) # this user is not part of the channel therefor they should not be able to invite another user
def test_other_menber_case(reg_user, crt_channel): clear_v2() token = reg_user(0)['token'] user1 = reg_user(1) token1 = user1['token'] u_id = user1['auth_user_id'] channel_id = crt_channel(token)['channel_id'] channel_invite_v2(token, channel_id, u_id) standup_length = 1 standup_start_v1(token, channel_id, standup_length) assert standup_send_v1(token1, channel_id, 'HELLOWORLD') == {} assert standup_send_v1(token1, channel_id, 'PYTHON') == {}
def test_channel_invite_v2_u_id_currently_in_channel(reg_user, basic_channel_name): clear_v2() token_1 = reg_user(0)['token'] # this maker of the channel new_user = reg_user(1) # this is the invitee of the channel channel_name = basic_channel_name is_public = True channel_id = channels_create_v2(token_1, channel_name, is_public)['channel_id'] channel_join_v2(new_user['token'], channel_id) # invitee has joined channel with pytest.raises(AccessError): channel_invite_v2( token_1, channel_id, new_user['auth_user_id'] ) # the u_id is already inside 'all_members' does not need to be invited
def channel_invite(): data = request.get_json() token = data['token'] channel_id = data['channel_id'] u_id = data['u_id'] return dumps(channel_invite_v2(token, channel_id, u_id))
def channel_invite(): parameters = request.get_json() token = parameters['token'] channel_id = parameters['channel_id'] u_id = parameters['u_id'] output = channel_invite_v2(token, channel_id, u_id) return dumps(output)
def test_channel_invite_v2_multiple_private(user_setup, channel_setup): assert channel_invite_v2(make_token(1), 3, 2) == {} assert channel_details_v2(make_token(1), 3) == { 'name': 'PrivateChannel1', 'is_public': False, 'owner_members': [ { 'u_id': 1, 'email': "*****@*****.**", 'name_first': "Peter", 'name_last': "Tran", 'handle': "petertran" }, ], 'all_members': [ { 'u_id': 1, 'email': "*****@*****.**", 'name_first': "Peter", 'name_last': "Tran", 'handle': "petertran" }, { 'u_id': 2, 'email': "*****@*****.**", 'name_first': "Christopher", 'name_last': "Luong", 'handle': "christopherluong" } ] }
def test_twenty_channel_tag(reg_user, u_handle, crt_channel, tag_message): clear_v2() user_1_token = reg_user(0)['token'] user_2 = reg_user(1) channel_id = crt_channel(user_1_token, 0)['channel_id'] channel_invite_v2(user_1_token, channel_id, user_2['auth_user_id']) message_count = 20 messages_texts = [] for i in range(message_count): messages_texts.append(tag_message(user_1_token, channel_id, 0, 0, 1, i)) notifs = notifications_get_v1(user_2['token'])['notifications'] assert len(notifs) == message_count for i in reversed(range(message_count)): assert notifs[i]['channel_id'] == channel_id assert notifs[i]['dm_id'] == -1 assert notifs[i]['notification_message'] == messages_texts[i]
def channel_invite(): info = request.get_json() token = info["token"] channel_id = info["channel_id"] u_id = info["u_id"] channel_invite = channel_invite_v2(token, channel_id, u_id) return dumps(channel_invite)
def test_channel_invite_v2_basic( reg_user, basic_channel_name): #check if channel_invite works clear_v2() token_1 = reg_user(0)['token'] # this maker of the channel u_id = reg_user(1)['auth_user_id'] # this is the invitee of the channel channel_name = basic_channel_name is_public = False channel_id = channels_create_v2(token_1, channel_name, is_public)['channel_id'] channel_invite_v2(token_1, channel_id, u_id) details = channel_details_v2( token_1, channel_id) #changed token_1 from u_id second user user_found = False for user in details['all_members']: if u_id == user['u_id']: user_found = True assert user_found
def test_channel_invite_v2_dreams_owner(reg_user, basic_channel_name): clear_v2() user_1 = reg_user(0) u_id = user_1['auth_user_id'] # this is the invitee of the channel token_1 = user_1['token'] token_2 = reg_user(1)['token'] # this maker of the channel channel_name = basic_channel_name is_public = False channel_id = channels_create_v2(token_2, channel_name, is_public)['channel_id'] channel_invite_v2(token_2, channel_id, u_id) details = channel_details_v2( token_1, channel_id) #changed token_1 from u_id second user user_found = False for user in details['owner_members']: if u_id == user['u_id']: user_found = True assert user_found
def test_invite(test_data): a_data, b_data, c_data, a_ch_id, b_ch_id = test_data # public channel assert channel_invite_v2(a_data["token"], a_ch_id, c_data["auth_user_id"]) == {} # a member of a channel should be able to get its details # they should also be within all_members of channel_details return details = channel_details_v2(c_data["token"], a_ch_id) is_member_exist = False for member in details["all_members"]: if member["u_id"] == c_data["auth_user_id"]: is_member_exist = True assert is_member_exist == True # private channel assert channel_invite_v2(b_data["token"], b_ch_id, c_data["auth_user_id"]) == {} details = channel_details_v2(c_data["token"], b_ch_id) is_member_exist = False for member in details["all_members"]: if member["u_id"] == c_data["auth_user_id"]: is_member_exist = True assert is_member_exist == True # inviting an existing user (no change) assert channel_invite_v2(b_data["token"], b_ch_id, c_data["auth_user_id"]) == {} details = channel_details_v2(c_data["token"], b_ch_id) is_member_exist = False for member in details["all_members"]: if member["u_id"] == c_data["auth_user_id"]: is_member_exist = True assert is_member_exist == True
def test_more_than_twenty_channel_adds(reg_user, u_handle, crt_channel, ch_add_str): clear_v2() user_1_token = reg_user(0)['token'] user_2 = reg_user(1) add_count = 21 channel_ids = [] for i in range(add_count): channel_id = crt_channel(user_1_token, i)['channel_id'] channel_invite_v2(user_1_token, channel_id, user_2['auth_user_id']) channel_ids.append(channel_id) channel_ids.reverse() notifs = notifications_get_v1(user_2['token'])['notifications'] assert len(notifs) == add_count - 1 for i in range(len(notifs)): assert notifs[i]['channel_id'] == channel_ids[i] assert notifs[i]['dm_id'] == -1 assert notifs[i]['notification_message'] == ch_add_str( u_handle(0), 20 - i)
def test_remove_admin(): channels_create_v2(make_token(1), "PublicChannel1", True) channel_invite_v2(make_token(1), 0, 0) message_send_v2(make_token(1), 0, "Hello") assert admin_user_remove_v1(make_token(0), 1) == {} assert user_profile_v1(make_token(0), 1) == { 'user': { 'u_id': 1, 'email': '*****@*****.**', 'name_first': 'Removed user', 'name_last': 'Removed user', 'handle_str': 'petertran', } } assert channel_messages_v2(make_token(0), 0, 0) == { 'messages': [{ "message_id": 0, "u_id": 1, "message": "Removed user", "time_created": str(create_timestamp()), "channel_id": 0, "dm_id": -1, "reacts": [{ "react_id": 1, "u_ids": [], "is_this_user_reacted": False }], "is_pinned": False }], 'start': 0, 'end': -1, }
def test_invalid_token_exception(): """ Tests when the authorised user is an invalid id """ clear_v1() # Create a valid channel with user as owner user = auth_register_v2("*****@*****.**", "mangofruit1", "Adam", "Apple") channel = channels_create_v2(user['token'], "Valid channel", True) with pytest.raises(AccessError): channel_invite_v2(user['token'] + 'bug', channel['channel_id'], user['auth_user_id']) channel_invite_v2(user['token'] + 'pipswe23', channel['channel_id'], user['auth_user_id']) channel_invite_v2(user['token'] + '5000', channel['channel_id'], user['auth_user_id']) channel_invite_v2(user['token'] + '689d', channel['channel_id'], user['auth_user_id']) channel_invite_v2(user['token'] + 'd', channel['channel_id'], user['auth_user_id'])
def test_channel_invite_v2_errors(user_setup, channel_setup): # invites an invalid user to a private channel with pytest.raises(InputError): channel_invite_v2(make_token(1), 5, 10) # invites an invalid user to a public channel with pytest.raises(InputError): channel_invite_v2(make_token(1), 2, 10) # invites a user to an invalid channel with pytest.raises(InputError): channel_invite_v2(make_token(1), 10, 2) # invitation to a channel from an invalid user with pytest.raises(AccessError): channel_invite_v2(make_token(10), 4, 2) # the authorised user sending an invite is not a member of the channel with pytest.raises(AccessError): channel_invite_v2(make_token(0), 4, 3)