def message_unpin(token, message_id): # pinner = the user who is requesting the message to be pinned unpinner = token_validator(token)['u_id'] # Locate the given message_id and verify it valid_message = False for channel in data['channels']: for message in channel['messages']: if message_id == message['message_id']: valid_message = True break if valid_message: if message['is_pinned']: if unpinner in channel['all_members'] or is_flockr_owner( token, unpinner): if unpinner in channel['owner_members'] or is_flockr_owner( token, unpinner): message['is_pinned'] = False else: raise AccessError("Authorised user is not an owner") else: raise AccessError( "Authorised user is not a member of the channel \ that the message is within") else: raise InputError("Message with ID message_id is already unpinned") else: raise InputError("message_id is not a valid message") return {}
def test_valid_flockr_owner_changing_to_same_permission(flockr_owner): owner = flockr_owner admin_userpermission_change(owner['token'], owner['u_id'], 1) # Make sure the owner is still an owner with no errors raised assert is_flockr_owner(owner['token'], owner['u_id'])
def test_valid_flockr_owner_target_themselves(flockr_owner): owner = flockr_owner admin_userpermission_change(owner['token'], owner['u_id'], 2) # Make sure the owner is now a member assert not is_flockr_owner(owner['token'], owner['u_id'])
def message_remove(token, message_id): ''' Either an authorised user (i.e. channel owner) or the sender of a message removes a message. Specified by message_id ''' # Verify that the remover has a valid token remover = token_validator(token) # Locate the relevant message by looping through the channels and messages for channel in data['channels']: # Find the message to be removed for message_find in channel['messages']: if message_find['message_id'] == message_id: # If message has been found, check authorisation to remove # Remover is authorised if they are either the sender of the message or they are the owner of the channel if remover['u_id'] == message_find['u_id'] or remover[ 'u_id'] in channel['owner_members'] or is_flockr_owner( token, remover['u_id']): del channel['messages'][message_id] return {} else: raise AccessError( "Sorry, you are neither the owner of the channel or creator of the message" ) # If the message was not found, raise Input Error raise InputError("The message you are trying to remove was not found")
def test_valid_flockr_owner_making_flockr_owner(flockr_owner, register_user): owner = flockr_owner user = register_user admin_userpermission_change(owner['token'], user['u_id'], 1) # Make sure the user is now a Flockr owner assert is_flockr_owner(user['token'], user['u_id'])
def admin_userpermission_change(token, u_id, permission_id): """ admin_userpermission_change Helper Modules: user_validator: validates if user exists Args: token: authorises user u_id: the targeted user to change permission_id permission_id: can only be 1 (owner) and 2 (member) Returns: Empty dictionary but changes a user's permissions Raises: InputError when u_id does not refer to a valid user within the channel InputError when permission_id is not valid (1 or 2) AccessError when the authorised user (token) is not an owner """ # Check if the token is valid user = token_validator(token) # Check if the u_id is valid u_id_validator(u_id) # Checks if permission_id is valid if permission_id not in (1, 2): raise InputError("Permission value has to be 1 or 2") # Checks if authorised user is a Flockr owner if not is_flockr_owner(token, user['u_id']): raise AccessError("The authorised user is not a Flockr owner") # Change target u_id's permission_id for user in data['users']: if user['u_id'] == u_id: user['permission_id'] = permission_id
def message_edit(token, message_id, message): # Validate the authorised user editor = token_validator(token) new_message = message # Locate the message to edit by looping through channels then messages # If we fix channel_messages and call it, we won't have to loop through. message_found = False for channel in data["channels"]: for curr_message in channel["messages"]: # Find the message the user wants to edit if curr_message['message_id'] == message_id: # Check if user is flockr owner # Verify that the user is authorised to edit if editor['u_id'] == curr_message['u_id'] or editor[ "u_id"] in channel["owner_members"] or is_flockr_owner( token, editor["u_id"]): # Safe to edit the message message_found = True break else: raise AccessError( "Sorry, you are neither the owner of the channel or \ creator of the message, you cannot edit the message" ) if message_found: if len(new_message) == 0: # The entire message including its details is deleted del channel['messages'][message_id] else: # The message in data is replaced with the new message curr_message['message'] = new_message else: raise AccessError( "The message_id does not match the message you are trying to edit." ) return {}