def channel_removeowner(token, channel_id, u_id): # check validity of inputs check_token(token) check_channel_id(channel_id) check_u_id(u_id) # check that the target is an owner of the specified channel if is_owner(u_id, channel_id) is False: raise ValueError("User is not an owner of the channel") # check that the user is authorised to remove the owner auth_user = get_user_from_token(token)["u_id"] if is_owner(auth_user, channel_id) is False: raise AccessError # Check that the target is not a owner or admin if get_user_from_id(u_id)["permission_id"] != 3: raise AccessError( "Cannot remove admin/slackr owner from channel owners") # since all values are correct, remove the owner of the channel for channel in data["channels"]: if channel["channel_id"] == channel_id: for owner in channel["owner_members"]: if owner["u_id"] == u_id: channel["owner_members"].remove(owner) break save() return {}
def channel_addowner(token, channel_id, u_id): # Check the validity of the inputs check_token(token) check_channel_id(channel_id) check_u_id(u_id) # Check that the user is not an owner of the specified channel if is_owner(u_id, channel_id) is True: raise ValueError("User is already an owner of the channel") # Check that the user is authorised to add the owner auth_user = get_user_from_token(token)["u_id"] if is_owner(auth_user, channel_id) is False: raise AccessError( "Authorised user is not an owner of slackr, or an owner of the channel" ) # Get user info user = get_user_from_id(u_id) # Create a dict of type member member_details = { info: user[info] for info in ["u_id", "name_first", "name_last"] } # Add user to channel get_channel(channel_id)["owner_members"].append(member_details) save() return {}
def test_channel_removeowner(): helper.reset_data() # SETUP START user1 = helper.token_admin() user2 = helper.token_account_1() new_channel_id = helper.channelid_public(user1["token"]) # SETUP END # Check that error is raised if an invalid token is passed in with pytest.raises(validation_helper.AccessError): stub.channel_removeowner("fake token", new_channel_id, user1["u_id"]) with pytest.raises(validation_helper.ValueError): # check that validation_helper.ValueError is raised if channel does not exist # Assumption: channel_id cannot be negative stub.channel_removeowner(user1["token"], -1, user1["u_id"]) with pytest.raises(validation_helper.ValueError): # Check ValueError raised if target user is not an owner stub.channel_removeowner(user1["token"], new_channel_id, user2["u_id"]) with pytest.raises(validation_helper.AccessError): # Check accessError raised if user not an owner stub.channel_removeowner(user2["token"], new_channel_id, user1["u_id"]) with pytest.raises(validation_helper.AccessError): # Check accessError raised if target is an owner stub.channel_removeowner(user1["token"], new_channel_id, user1["u_id"]) # Check that call is successful if all inputs are valid stub.channel_addowner(user1["token"], new_channel_id, user2["u_id"]) stub.channel_removeowner(user1["token"], new_channel_id, user2["u_id"]) assert validation_helper.is_owner(user2["u_id"], new_channel_id) is not True
def message_remove(token, message_id): # check that token is valid check_token(token) # get info about user and message channel_id = get_channel_id_from_message(message_id) message_info = get_info_about_message(message_id) u_id = get_user_from_token(token)["u_id"] # check that message_id is valid if channel_id is None: raise ValueError("Invalid message") # check that the authorised user is the one who sent the message # or is an admin/owner of the channel or slackr if message_info["u_id"] != u_id: if is_owner(u_id, channel_id) is False or get_user_from_id( u_id)["permission_id"] == 3: raise AccessError("User is not authorised to remove this message") # since all values are correct, remove the message for channel in data["channels"]: if channel["channel_id"] == channel_id: for message in channel["messages"]: if message["message_id"] == message_info["message_id"]: channel["messages"].remove(message) break save() return {}
def message_edit(token, message_id, message): # check that token is valid check_token(token) # check that message is valid if len(message) > 1000: raise ValueError("Message can't be more than 1000 characters") # get info about user and message channel_id = get_channel_id_from_message(message_id) message_info = get_info_about_message(message_id) u_id = get_user_from_token(token)["u_id"] # check that message_id is valid if channel_id is None: raise ValueError("Invalid message") # check that the authorised user is the one who sent the message # or is an admin/owner of the channel or slackr if message_info["u_id"] != u_id: if is_owner(u_id, channel_id) is False or get_user_from_id( u_id)["permission_id"] == 3: raise AccessError("User is not authorised to remove this message") # check if the message is empty string remove the message if message == "": message_remove(token, message_id) # since all values are correct, edit the message for channel in data["channels"]: if channel["channel_id"] == channel_id: for msg in channel["messages"]: if msg["message_id"] == message_info["message_id"]: msg["message"] = message break save() return {}