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']).status_code == SUCCESS assert channel_invite_v2(create_input[0][1]['token'], channel_1['channel_id'], create_input[0][2]['auth_user_id']).status_code == SUCCESS # 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']).json()['owner_members']) == 1 assert len(channel_details_v2(create_input[0][1]['token'], channel_1['channel_id']).json()['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']).status_code == SUCCESS
def test_success_owner_leave(create_input): # Add another owner to First Channel assert channel_addowner_v1( create_input[0][0]["token"], create_input[1][0]["channel_id"], create_input[0][4]["auth_user_id"]).json() == {} # length of *_members for First Channel before leave assert len( channel_details_v2( create_input[0][0]["token"], create_input[1][0]["channel_id"]).json()["owner_members"]) == 2 assert len( channel_details_v2( create_input[0][0]["token"], create_input[1][0]["channel_id"]).json()["all_members"]) == 4 # One owner leaves assert channel_leave_v1(create_input[0][0]["token"], create_input[1][0]["channel_id"]).json() == {} # length of *_members for First Channel after leave; testing for all involved users assert len( channel_details_v2( create_input[0][1]["token"], create_input[1][0]["channel_id"]).json()["owner_members"]) == 1 assert len( channel_details_v2( create_input[0][1]["token"], create_input[1][0]["channel_id"]).json()["all_members"]) == 3 assert len( channel_details_v2( create_input[0][2]["token"], create_input[1][0]["channel_id"]).json()["owner_members"]) == 1 assert len( channel_details_v2( create_input[0][2]["token"], create_input[1][0]["channel_id"]).json()["all_members"]) == 3 assert len( channel_details_v2( create_input[0][4]["token"], create_input[1][0]["channel_id"]).json()["owner_members"]) == 1 assert len( channel_details_v2( create_input[0][4]["token"], create_input[1][0]["channel_id"]).json()["all_members"]) == 3 assert channel_details_v2( create_input[0][0]["token"], create_input[1][0]["channel_id"]).status_code == ACCESS_ERROR # number of channels each user is a part of assert len( channels_list_v2(create_input[0][0]["token"]).json()["channels"]) == 0 assert len( channels_list_v2(create_input[0][1]["token"]).json()["channels"]) == 2 assert len( channels_list_v2(create_input[0][2]["token"]).json()["channels"]) == 2 assert len( channels_list_v2(create_input[0][4]["token"]).json()["channels"]) == 1
def test_success_user_is_member(create_input): # Add member to First Channel before making them owner assert channel_join_v2(create_input[0][1]["token"], create_input[1][0]["channel_id"]).json() == {} assert channel_join_v2(create_input[0][2]["token"], create_input[1][0]["channel_id"]).json() == {} # length of *_members for First Channel before add assert len( channel_details_v2( create_input[0][0]["token"], create_input[1][0]["channel_id"]).json()["owner_members"]) == 1 assert len( channel_details_v2( create_input[0][0]["token"], create_input[1][0]["channel_id"]).json()["all_members"]) == 3 assert channel_addowner_v1( create_input[0][0]["token"], create_input[1][0]["channel_id"], create_input[0][1]["auth_user_id"]).json() == {} # length of *_members for First Channel after add; testing for all users assert len( channel_details_v2( create_input[0][0]["token"], create_input[1][0]["channel_id"]).json()["owner_members"]) == 2 assert len( channel_details_v2( create_input[0][0]["token"], create_input[1][0]["channel_id"]).json()["all_members"]) == 3 assert len( channel_details_v2( create_input[0][1]["token"], create_input[1][0]["channel_id"]).json()["owner_members"]) == 2 assert len( channel_details_v2( create_input[0][1]["token"], create_input[1][0]["channel_id"]).json()["all_members"]) == 3 assert len( channel_details_v2( create_input[0][2]["token"], create_input[1][0]["channel_id"]).json()["owner_members"]) == 2 assert len( channel_details_v2( create_input[0][2]["token"], create_input[1][0]["channel_id"]).json()["all_members"]) == 3 # number of channels each user is a part of assert len( channels_list_v2(create_input[0][0]["token"]).json()["channels"]) == 1 assert len( channels_list_v2(create_input[0][1]["token"]).json()["channels"]) == 2 assert len( channels_list_v2(create_input[0][2]["token"]).json()["channels"]) == 2 assert len( channels_list_v2(create_input[0][3]["token"]).json()["channels"]) == 1 assert len( channels_list_v2(create_input[0][4]["token"]).json()["channels"]) == 0
def test_mixed_harder(): clear_v1() result1 = auth_register_v2('*****@*****.**', 'password', 'comp', 'student').json() result2 = auth_register_v2('*****@*****.**', 'password', 'comp', 'student').json() channel1 = channels_create_v2(result1['token'], "The party channel 1", True).json()['channel_id'] message_send_v2(result1['token'], channel1, 'hello how are you') dm_create_v1(result1['token'], [result2['auth_user_id']]) output = user_stats(result1['token']).json() 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'] == 1 assert output['user_stats']['messages_sent'][-1]['num_messages_sent'] == 1 assert output['user_stats']['involvement_rate'] == 1 channels_create_v2(result1['token'], "The party channel 2", True) channel3 = channels_create_v2(result2['token'], "The party channel 3", True).json()['channel_id'] channel_addowner_v1(result2['token'], channel3, result1['auth_user_id']) message_send_v2(result2['token'], channel3, 'hello how are you') output1 = user_stats(result1['token']).json() output2 = user_stats(result2['token']).json() assert len(output1['user_stats']) == 4 assert output1['user_stats']['channels_joined'][-1][ 'num_channels_joined'] == 3 assert output1['user_stats']['dms_joined'][-1]['num_dms_joined'] == 1 assert output1['user_stats']['messages_sent'][-1]['num_messages_sent'] == 1 assert output1['user_stats']['involvement_rate'] == 5 / 6 assert len(output2['user_stats']) == 4 assert output2['user_stats']['channels_joined'][-1][ 'num_channels_joined'] == 1 assert output2['user_stats']['dms_joined'][-1]['num_dms_joined'] == 1 assert output2['user_stats']['messages_sent'][-1]['num_messages_sent'] == 1 assert output2['user_stats']['involvement_rate'] == 3 / 6 dreams = users_stats(result1['token']).json() assert len(dreams['dreams_stats']) == 4 assert dreams['dreams_stats']['channels_exist'][-1][ 'num_channels_exist'] == 3 assert dreams['dreams_stats']['dms_exist'][-1]['num_dms_exist'] == 1 assert dreams['dreams_stats']['messages_exist'][-1][ 'num_messages_exist'] == 2 assert dreams['dreams_stats']['utilization_rate'] == 1
def test_fail_user_already_owner(create_input): # length of *_members for Second Channel before add assert len( channel_details_v2( create_input[0][1]["token"], create_input[1][1]["channel_id"]).json()["owner_members"]) == 1 assert len( channel_details_v2( create_input[0][1]["token"], create_input[1][1]["channel_id"]).json()["all_members"]) == 1 assert channel_addowner_v1( create_input[0][1]["token"], create_input[1][1]["channel_id"], create_input[0][2]["auth_user_id"]).json() == {} assert channel_addowner_v1( create_input[0][1]["token"], create_input[1][1]["channel_id"], create_input[0][2]["auth_user_id"]).status_code == INPUT_ERROR
def test_success_remove_multiple(create_input): # Add extra owner to First Channel assert channel_addowner_v1( create_input[0][0]["token"], create_input[1][0]["channel_id"], create_input[0][2]["auth_user_id"]).json() == {} # length of *_members for First Channel before remove assert len( channel_details_v2( create_input[0][0]["token"], create_input[1][0]["channel_id"]).json()["owner_members"]) == 3 assert len( channel_details_v2( create_input[0][0]["token"], create_input[1][0]["channel_id"]).json()["all_members"]) == 4 assert channel_removeowner_v1( create_input[0][0]["token"], create_input[1][0]["channel_id"], create_input[0][1]["auth_user_id"]).json() == {} assert channel_removeowner_v1( create_input[0][0]["token"], create_input[1][0]["channel_id"], create_input[0][2]["auth_user_id"]).json() == {} # length of *_members for First Channel after remove; testing for all users assert len( channel_details_v2( create_input[0][0]["token"], create_input[1][0]["channel_id"]).json()["owner_members"]) == 1 assert len( channel_details_v2( create_input[0][0]["token"], create_input[1][0]["channel_id"]).json()["all_members"]) == 4 assert len( channel_details_v2( create_input[0][1]["token"], create_input[1][0]["channel_id"]).json()["owner_members"]) == 1 assert len( channel_details_v2( create_input[0][1]["token"], create_input[1][0]["channel_id"]).json()["all_members"]) == 4 assert len( channel_details_v2( create_input[0][2]["token"], create_input[1][0]["channel_id"]).json()["owner_members"]) == 1 assert len( channel_details_v2( create_input[0][2]["token"], create_input[1][0]["channel_id"]).json()["all_members"]) == 4 assert len( channel_details_v2( create_input[0][4]["token"], create_input[1][0]["channel_id"]).json()["owner_members"]) == 1 assert len( channel_details_v2( create_input[0][4]["token"], create_input[1][0]["channel_id"]).json()["all_members"]) == 4
def test_channel_removeowner_stat(): clear_v1() result1 = auth_register_v2('*****@*****.**', 'password', 'comp', 'student').json() result2 = auth_register_v2('*****@*****.**', 'password', 'comp', 'student').json() channel1 = channels_create_v2(result2['token'], "The party channel 1", True).json() channel_addowner_v1(result2['token'], channel1['channel_id'], result1['auth_user_id']) channel_removeowner_v1(result1['token'], channel1['channel_id'], result2['auth_user_id']) output = user_stats(result1['token']).json() output1 = user_stats(result2['token']).json() dreams = users_stats(result1['token']).json() 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_success_owner_one_add(create_input): # length of *_members for First Channel before add assert len( channel_details_v2( create_input[0][0]["token"], create_input[1][0]["channel_id"]).json()["owner_members"]) == 1 assert len( channel_details_v2( create_input[0][0]["token"], create_input[1][0]["channel_id"]).json()["all_members"]) == 1 assert channel_addowner_v1( create_input[0][0]["token"], create_input[1][0]["channel_id"], create_input[0][1]["auth_user_id"]).json() == {} # length of *_members for First Channel after join; testing for both users assert len( channel_details_v2( create_input[0][0]["token"], create_input[1][0]["channel_id"]).json()["owner_members"]) == 2 assert len( channel_details_v2( create_input[0][0]["token"], create_input[1][0]["channel_id"]).json()["all_members"]) == 2 assert len( channel_details_v2( create_input[0][1]["token"], create_input[1][0]["channel_id"]).json()["owner_members"]) == 2 assert len( channel_details_v2( create_input[0][1]["token"], create_input[1][0]["channel_id"]).json()["all_members"]) == 2 # number of channels each user is a part of assert len( channels_list_v2(create_input[0][0]["token"]).json()["channels"]) == 1 assert len( channels_list_v2(create_input[0][1]["token"]).json()["channels"]) == 2
def create_input(): clear_v1() data_test_users = [ auth_register_v2("*****@*****.**", "validpassword", "Josh", "Hatton").json(), auth_register_v2("*****@*****.**", "anotherpassword", "Bunny", "Dong").json(), auth_register_v2("*****@*****.**", "password3", "Jordan", "Milch").json(), auth_register_v2("*****@*****.**", "4thpassword", "Dean", "Zworestine").json(), auth_register_v2("*****@*****.**", "finalpassword", "Eric", "Zheng").json(), ] data_test_channels = [ channels_create_v2(data_test_users[0]["token"], "First Channel", True).json(), channels_create_v2(data_test_users[1]["token"], "Second Channel", True).json(), channels_create_v2(data_test_users[2]["token"], "Third Channel", True).json(), channels_create_v2(data_test_users[3]["token"], "Fourth Channel", False).json(), ] channel_addowner_v1(data_test_users[0]["token"], data_test_channels[0]["channel_id"], data_test_users[1]["auth_user_id"]) channel_addowner_v1(data_test_users[1]["token"], data_test_channels[1]["channel_id"], data_test_users[0]["auth_user_id"]) channel_addowner_v1(data_test_users[2]["token"], data_test_channels[2]["channel_id"], data_test_users[3]["auth_user_id"]) channel_join_v2(data_test_users[4]["token"], data_test_channels[0]["channel_id"]) return [data_test_users, data_test_channels]
def test_success_owner_multiple_add(create_input): # length of *_members for Second Channel before add assert len( channel_details_v2( create_input[0][1]["token"], create_input[1][1]["channel_id"]).json()["owner_members"]) == 1 assert len( channel_details_v2( create_input[0][1]["token"], create_input[1][1]["channel_id"]).json()["all_members"]) == 1 assert channel_addowner_v1( create_input[0][1]["token"], create_input[1][1]["channel_id"], create_input[0][0]["auth_user_id"]).json() == {} assert channel_addowner_v1( create_input[0][1]["token"], create_input[1][1]["channel_id"], create_input[0][2]["auth_user_id"]).json() == {} assert channel_addowner_v1( create_input[0][1]["token"], create_input[1][1]["channel_id"], create_input[0][3]["auth_user_id"]).json() == {} # length of *_members for Second Channel after add; testing for all users assert len( channel_details_v2( create_input[0][0]["token"], create_input[1][1]["channel_id"]).json()["owner_members"]) == 4 assert len( channel_details_v2( create_input[0][0]["token"], create_input[1][1]["channel_id"]).json()["all_members"]) == 4 assert len( channel_details_v2( create_input[0][1]["token"], create_input[1][1]["channel_id"]).json()["owner_members"]) == 4 assert len( channel_details_v2( create_input[0][1]["token"], create_input[1][1]["channel_id"]).json()["all_members"]) == 4 assert len( channel_details_v2( create_input[0][2]["token"], create_input[1][1]["channel_id"]).json()["owner_members"]) == 4 assert len( channel_details_v2( create_input[0][2]["token"], create_input[1][1]["channel_id"]).json()["all_members"]) == 4 assert len( channel_details_v2( create_input[0][3]["token"], create_input[1][1]["channel_id"]).json()["owner_members"]) == 4 assert len( channel_details_v2( create_input[0][3]["token"], create_input[1][1]["channel_id"]).json()["all_members"]) == 4 assert channel_details_v2( create_input[0][4]["token"], create_input[1][1]["channel_id"]).status_code == ACCESS_ERROR assert channel_details_v2( create_input[0][4]["token"], create_input[1][1]["channel_id"]).status_code == ACCESS_ERROR # number of channels each user is a part of assert len( channels_list_v2(create_input[0][0]["token"]).json()["channels"]) == 2 assert len( channels_list_v2(create_input[0][1]["token"]).json()["channels"]) == 1 assert len( channels_list_v2(create_input[0][2]["token"]).json()["channels"]) == 2 assert len( channels_list_v2(create_input[0][3]["token"]).json()["channels"]) == 2 assert len( channels_list_v2(create_input[0][4]["token"]).json()["channels"]) == 0
def test_all_invalid(create_input): # token, channel_id, u_id do not exist. # ACCESS_ERROR is prioritised assert channel_addowner_v1("anotherinvalidtoken", 321, 42).status_code == ACCESS_ERROR
def test_uid_invalid(create_input): # u_id 10 does not exist assert channel_addowner_v1(create_input[0][0]["token"], create_input[1][0]["channel_id"], 10).status_code == INPUT_ERROR
def test_token_invalid(create_input): # token does not exist assert channel_addowner_v1( "invalidtoken", create_input[1][0]["channel_id"], create_input[0][0]["auth_user_id"]).status_code == ACCESS_ERROR
def test_channel_id_invalid(create_input): # channel_id 5 has not been created assert channel_addowner_v1( create_input[0][0]["token"], 5, create_input[0][0]["auth_user_id"]).status_code == INPUT_ERROR
def test_success_global_owner_add(create_input): # length of *_members for Third Channel before add assert len( channel_details_v2( create_input[0][2]["token"], create_input[1][2]["channel_id"]).json()["owner_members"]) == 1 assert len( channel_details_v2( create_input[0][2]["token"], create_input[1][2]["channel_id"]).json()["all_members"]) == 1 # create_input[0][0]["token"] is a global owner; can add owners even if they aren't members of the channel assert channel_addowner_v1( create_input[0][0]["token"], create_input[1][2]["channel_id"], create_input[0][1]["auth_user_id"]).json() == {} assert channel_addowner_v1( create_input[0][0]["token"], create_input[1][2]["channel_id"], create_input[0][3]["auth_user_id"]).json() == {} assert channel_addowner_v1( create_input[0][0]["token"], create_input[1][2]["channel_id"], create_input[0][0]["auth_user_id"]).json() == {} # length of *_members for Third Channel after add; testing for all users assert len( channel_details_v2( create_input[0][0]["token"], create_input[1][2]["channel_id"]).json()["owner_members"]) == 4 assert len( channel_details_v2( create_input[0][0]["token"], create_input[1][2]["channel_id"]).json()["all_members"]) == 4 assert len( channel_details_v2( create_input[0][1]["token"], create_input[1][2]["channel_id"]).json()["owner_members"]) == 4 assert len( channel_details_v2( create_input[0][1]["token"], create_input[1][2]["channel_id"]).json()["all_members"]) == 4 assert len( channel_details_v2( create_input[0][2]["token"], create_input[1][2]["channel_id"]).json()["owner_members"]) == 4 assert len( channel_details_v2( create_input[0][2]["token"], create_input[1][2]["channel_id"]).json()["all_members"]) == 4 assert len( channel_details_v2( create_input[0][3]["token"], create_input[1][2]["channel_id"]).json()["owner_members"]) == 4 assert len( channel_details_v2( create_input[0][3]["token"], create_input[1][2]["channel_id"]).json()["all_members"]) == 4 assert channel_details_v2( create_input[0][4]["token"], create_input[1][2]["channel_id"]).status_code == ACCESS_ERROR assert channel_details_v2( create_input[0][4]["token"], create_input[1][2]["channel_id"]).status_code == ACCESS_ERROR # number of channels each user is a part of assert len( channels_list_v2(create_input[0][0]["token"]).json()["channels"]) == 2 assert len( channels_list_v2(create_input[0][1]["token"]).json()["channels"]) == 2 assert len( channels_list_v2(create_input[0][2]["token"]).json()["channels"]) == 1 assert len( channels_list_v2(create_input[0][3]["token"]).json()["channels"]) == 2 assert len( channels_list_v2(create_input[0][4]["token"]).json()["channels"]) == 0
def test_added_to_channel(create_input): """ Tests the expected return value when a user is added to a channel """ user_1, user_2, user_3, user_4, user_5 = create_input[0] user_1_handle, _, user_3_handle, _, user_5_handle = create_input[1] channel_1 = create_input[2][0] dm_1, dm_2, dm_3 = create_input[3] 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']) channel_invite_v2(user_1['token'], channel_1['channel_id'], user_4['auth_user_id']) # Also test for channel_addowner channel_addowner_v1(user_1['token'], channel_1['channel_id'], user_5['auth_user_id']) assert notifications_get_v1(user_2['token']).json() == { 'notifications': [{ 'channel_id': channel_1['channel_id'], 'dm_id': -1, 'notification_message': f"{user_1_handle['user']['handle_str']} added you to Channel 1" }, { 'channel_id': -1, 'dm_id': dm_3['dm_id'], 'notification_message': f"{user_5_handle['user']['handle_str']} added you to {dm_3['dm_name']}" }, { 'channel_id': -1, 'dm_id': dm_1['dm_id'], 'notification_message': f"{user_1_handle['user']['handle_str']} added you to {dm_1['dm_name']}" }] } assert notifications_get_v1(user_3['token']).json() == { 'notifications': [{ 'channel_id': channel_1['channel_id'], 'dm_id': -1, 'notification_message': f"{user_1_handle['user']['handle_str']} added you to Channel 1" }] } assert notifications_get_v1(user_4['token']).json() == { 'notifications': [{ 'channel_id': channel_1['channel_id'], 'dm_id': -1, 'notification_message': f"{user_1_handle['user']['handle_str']} added you to Channel 1" }, { 'channel_id': -1, 'dm_id': dm_2['dm_id'], 'notification_message': f"{user_3_handle['user']['handle_str']} added you to {dm_2['dm_name']}" }] } assert notifications_get_v1(user_5['token']).json() == { 'notifications': [ { 'channel_id': channel_1['channel_id'], 'dm_id': -1, 'notification_message': f"{user_1_handle['user']['handle_str']} added you to Channel 1" }, ] }