def test_removed_user(reg_user): clear_v2() token = reg_user(0)['token'] u_id_2 = reg_user(1)['auth_user_id'] admin_user_remove_v1(token, u_id_2) # Remove second user assert len(users_all_v1(token)['users']) == 1
def test_only_owner(): """Raise an input error when the user is currently the only owner""" clear_v1() user_1 = auth_register_v2("*****@*****.**", "finalpassword", "Eric", "Zheng") with pytest.raises(InputError): admin_user_remove_v1(user_1["token"], user_1["auth_user_id"]) clear_v1()
def test_successful_remove(create_input): user_1, user_2, user_3, user_4, user_5 = create_input[0] assert admin_user_remove_v1(user_1["token"], user_2["auth_user_id"]) == {} assert admin_user_remove_v1(user_1["token"], user_3["auth_user_id"]) == {} assert admin_user_remove_v1(user_1["token"], user_4["auth_user_id"]) == {} assert admin_user_remove_v1(user_1["token"], user_5["auth_user_id"]) == {} # check to see that these users are indeed removed # only user_1 should be shown when calling users_all assert users_all_v1(user_1["token"]) == { "users": [user_profile_v2(user_1["token"], user_1["auth_user_id"])["user"]] } # calling channels list should raise an input error as their u_id's are invalid with pytest.raises(AccessError): channels_list_v2(user_2["token"]) channels_list_v2(user_3["token"]) channels_list_v2(user_4["token"]) channels_list_v2(user_5["token"]) # Check to see each removed user's profile is retrievable and their name is # now 'Removed user' for user in [user_2, user_3, user_4, user_5]: user_profile = user_profile_v2(user_1["token"], user["auth_user_id"]) assert type(user_profile) == dict assert ( f"{user_profile['user']['name_first']}{user_profile['user']['name_last']}" == "Removed user" or f"{user_profile['user']['name_first']} {user_profile['user']['name_last']}" == "Removed user") # CHECK THAT MESSAGES SENT ARE 'REMOVED USER' # check channel messages for user 2 channel_join_v2(user_1["token"], create_input[1][1]["channel_id"]) channel_1_messages = channel_messages_v2(user_1["token"], create_input[1][1]["channel_id"], 0) for message in channel_1_messages["messages"]: if message["message_id"] == create_input[2][1]: assert message["message"] == "Removed user" # check channel messages for user 3 channel_join_v2(user_1["token"], create_input[1][0]["channel_id"]) channel_2_messages = channel_messages_v2(user_1["token"], create_input[1][0]["channel_id"], 0) for message in channel_2_messages["messages"]: if message["message_id"] == create_input[2][2]: assert message["message"] == "Removed user" clear_v1()
def test_remove_from_users_all(): """ Tests that a user is removed from users_all list""" clear_v1() user_1 = auth_register_v2("*****@*****.**", "finalpassword", "Eric", "Zheng") user_2 = auth_register_v2("*****@*****.**", "validpassword", "Josh", "Hatton") admin_user_remove_v1(user_1["token"], user_2["auth_user_id"]) users = users_all_v1(user_1["token"])["users"] assert user_2["auth_user_id"] not in [u["u_id"] for u in users]
def test_removed_users(): result1 = auth_register_v2('*****@*****.**', '123abc!@#', 'Comp', 'Student') result2 = auth_register_v2('*****@*****.**', '123abc!@#', 'Comp', 'Student') result3 = auth_register_v2('*****@*****.**', '123abc!@#', 'Comp', 'Student') admin_user_remove_v1(result1['token'], result3['auth_user_id']) result4 = auth_register_v2('*****@*****.**', '123abc!@#', 'Comp', 'Student') assert result4['auth_user_id'] != result3['auth_user_id'] assert result4['auth_user_id'] != result2['auth_user_id'] assert result4['auth_user_id'] != result1['auth_user_id']
def test_removed_user(): clear_v1() result1 = auth_register_v2('*****@*****.**', 'password', 'one', 'aye') result2 = auth_register_v2('*****@*****.**', 'password', 'two', 'bee') admin_user_remove_v1(result1['token'], result2['auth_user_id']) output1 = user_profile_v2(result1['token'], result2['auth_user_id']) assert len(output1) == 1 assert len(output1['user']) == 6 assert output1['user']['email'] == '*****@*****.**' assert output1['user']['name_first'] + output1['user'][ 'name_last'] == 'Removed user' assert output1['user']['handle_str'] == 'twobee' clear_v1()
def test_removed_from_dm(): """Tests that a user is removed from a dm""" 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"], []) dm_invite_v1(user_1["token"], dm_1["dm_id"], user_2["auth_user_id"]) admin_user_remove_v1(user_1["token"], user_2["auth_user_id"]) members = dm_details_v1(user_1["token"], dm_1["dm_id"])["members"] assert user_2["auth_user_id"] not in [m["u_id"] for m in members]
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_auth_not_owner(): """Raise an access error when the authorised user is not an owner""" clear_v1() # Register the Dreams owner first auth_register_v2("*****@*****.**", "finalpassword", "Eric", "Zheng") user_2 = auth_register_v2("*****@*****.**", "validpassword", "Josh", "Hatton") user_3 = auth_register_v2("*****@*****.**", "anotherpassword", "Bunny", "Dong") with pytest.raises(AccessError): admin_user_remove_v1(user_2["token"], user_3["auth_user_id"]) admin_user_remove_v1(user_3["token"], user_2["auth_user_id"]) clear_v1()
def test_removed_from_channel(): """Tests that a user is removed from a channel""" clear_v1() user_1 = auth_register_v2("*****@*****.**", "finalpassword", "Eric", "Zheng") user_2 = auth_register_v2("*****@*****.**", "validpassword", "Josh", "Hatton") channel_1 = channels_create_v2(user_1["token"], "Test", True) channel_join_v2(user_2["token"], channel_1["channel_id"]) admin_user_remove_v1(user_1["token"], user_2["auth_user_id"]) members = channel_details_v2(user_1["token"], channel_1["channel_id"])["all_members"] assert user_2["auth_user_id"] not in [m["u_id"] for m in members]
def test_invalid_u_id(): """Raise an input error when u_id does not refer to a valid user id""" clear_v1() user_1 = auth_register_v2("*****@*****.**", "finalpassword", "Eric", "Zheng") user_2 = auth_register_v2("*****@*****.**", "validpassword", "Josh", "Hatton") with pytest.raises(InputError): admin_user_remove_v1(user_1["token"], user_1["auth_user_id"] + 5) admin_user_remove_v1(user_1["token"], user_1["auth_user_id"] + 50) admin_user_remove_v1(user_1["token"], user_2["auth_user_id"] + 1) admin_user_remove_v1(user_1["token"], user_2["auth_user_id"] + 50) admin_user_remove_v1(user_1["token"], user_2["auth_user_id"] + 500) clear_v1()
def test_invalid_token(): """Raise an access error when the token passed in does not refer to a valid user """ clear_v1() user_1 = auth_register_v2("*****@*****.**", "finalpassword", "Eric", "Zheng") user_2 = auth_register_v2("*****@*****.**", "validpassword", "Josh", "Hatton") with pytest.raises(AccessError): admin_user_remove_v1(user_1["token"] + "bug", user_2["auth_user_id"]) admin_user_remove_v1(user_1["token"] + "yum", user_2["auth_user_id"]) admin_user_remove_v1(user_1["token"] + "discodisco", user_2["auth_user_id"]) admin_user_remove_v1(user_1["token"] + "34897", user_2["auth_user_id"]) admin_user_remove_v1(user_1["token"] + "t", user_2["auth_user_id"]) clear_v1()
def userpermission_remove(): parameters = request.get_json() token = parameters['token'] u_id = parameters['u_id'] output = admin_user_remove_v1(token, u_id) return dumps(output)
def test_remove_already_removed(create_input): """Trying to remove an already removed user should raise an input error.""" user_1, user_2, user_3, user_4, user_5 = create_input[0] assert admin_user_remove_v1(user_1["token"], user_2["auth_user_id"]) == {} assert admin_user_remove_v1(user_1["token"], user_3["auth_user_id"]) == {} assert admin_user_remove_v1(user_1["token"], user_4["auth_user_id"]) == {} assert admin_user_remove_v1(user_1["token"], user_5["auth_user_id"]) == {} with pytest.raises(InputError): admin_user_remove_v1(user_1["token"], user_2["auth_user_id"]) admin_user_remove_v1(user_1["token"], user_3["auth_user_id"]) admin_user_remove_v1(user_1["token"], user_4["auth_user_id"]) admin_user_remove_v1(user_1["token"], user_5["auth_user_id"]) clear_v1()
def admin_user_remove(): data = request.get_json() token = data['token'] u_id = data['u_id'] return dumps(admin_user_remove_v1(token, u_id))