def admin_userpermission_change(token, u_id, permission_id): """ Sets a user's permissions described by permission_id Parameters: token(string): A user authorisation hash u_id(int): Indentifier for User permission_id(int): A value describing a user"s permissions - permission_id for owners: 1 - permission_id for members: 2 Returns: Nothing """ # Check that token is valid auth_u_id = validation.check_valid_token(token) # Dictionary of all valid permission values: valid_permission_id = {1, 2} # Checks if authorised user is an owner of Flockr # Returns AccessError if not an owner of Flockr if data.get_user_info(auth_u_id)["permission_id"] != 1: raise AccessError(description="User is not owner of Flockr") # Finds target user and sets "permission_id" value to permission_id target_user = data.get_user_info(u_id) if target_user is None: raise InputError("Target user does not exist") if permission_id in valid_permission_id: data.change_permission(u_id, permission_id) return {} raise InputError(description="Permission id is not a valid value")
def test_get_user_info(user1): """ Returns user's info when given correct u_id, otherwise returns none """ assert data.get_user_info(user1["u_id"]) assert data.get_user_info(2) == None other.clear()
def user_profile(token, u_id): """ Check whether user is valid and for a valid user, returns information about their user_id, email, first name, last name, and handle Parameters: token(string): An authorisation hash u_id(int): Identifier for user Returns: Dictionary with information about user """ #Check for valid token validation.check_valid_token(token) #Check for valid u_id validation.check_valid_u_id(u_id) #Everything valid, proceed with getting profile details user = data.get_user_info(u_id) profile = { "u_id": user["u_id"], "email": user["email"], "name_first": user["name_first"], "name_last": user["name_last"], "handle_str": user["handle_str"], "profile_img_url": user["profile_img_url"] } return {"user": profile}
def user1(): """ Pytest fixture that automatically registers a user and returns their info """ user = auth.auth_register("*****@*****.**", "kh12345", "Kevin", "Huang") user1 = data.get_user_info(user["u_id"]) return user1
def channel_details(token, channel_id): """ Checks that given information (token, channel_id, u_id) is valid then given a Channel with ID channel_id that the authorised user is part of, provide basic details about the channel Parameters: token(string): an authorisation hash channel_id(int): identifier for channel Returns: (dict): { name, owner_members, all_members } """ # Check if token is valid u_id = validation.check_valid_token(token) # Check if given valid channel_id validation.check_valid_channel_id(channel_id) # Check if user is authorised(member of channel) validation.check_user_in_channel(u_id, channel_id) # Everything valid, Proceed with getting details channel_info = {"name": "", "owner_members": [], "all_members": []} # Find channel and copy infomation into channel_details channel = data.get_channel_info(channel_id) channel_info["name"] = channel["name"] for u_id in channel["members"]: user = data.get_user_info(u_id) member = {} member["u_id"] = user["u_id"] member["name_first"] = user["name_first"] member["name_last"] = user["name_last"] member["name_last"] = user["name_last"] member["profile_img_url"] = user["profile_img_url"] channel_info["all_members"].append(member) for u_id in channel["owners"]: user = data.get_user_info(u_id) owner = {} owner["u_id"] = user["u_id"] owner["name_first"] = user["name_first"] owner["name_last"] = user["name_last"] owner["profile_img_url"] = user["profile_img_url"] channel_info["owner_members"].append(owner) return channel_info
def loss_message(info): """ Returns loss message """ user = data.get_user_info(info['u_id']) name = user['name_first'] info['failures'] = 9 return f"""
def user1(): """ Registers and returns a user dictionary which contains all the information about the user (name, last name, email, handle, etc) """ user = auth.auth_register("*****@*****.**", "Testing123", "Hello", "There") return data.get_user_info(user["u_id"])
def test_update_user(user1): """ Updates attribute of user. Returns nothing """ user_info = data.get_user_info(user1["u_id"]) data.update_user(user_info,{"name_last":"Hang"}) assert data.get_user_with({"name_last": "Hang"}) == user_info #data.users[1] other.clear()
def test_update_user_channel_list(user1): """ Adds channel id to user's channel list. Returns nothing """ user_info = data.get_user_info(user1["u_id"]) data.update_user_channel_list(user_info, 52) assert data.users[user1["u_id"]]["channel_list"] == {52} other.clear()
def test_channel_remove_owner(channel1): """ Removes member as an owner, returns nothing """ channel_id = channel1["channel_id"] user = data.get_user_info(1) data.channel_remove_owner(channel_id, user["u_id"]) assert data.check_channel_owner(channel_id, user["u_id"]) == False other.clear()
def victory_message(info): """ Returns victory message """ user = data.get_user_info(info['u_id']) name = user['name_first'] plural = 'es' if info['guesses'] == 1: plural = '' return f"""
def check_valid_u_id(u_id): """ Check if u_id of invitee is valid Parameters: u_id(int): Identifier for user Returns: Raises an error if user doesn't exist Returns nothing if user exists """ if data.get_user_info(u_id) is None: raise InputError(description = "User does not exist")
def edit_status(info): """ Changes the status message with updated picture and incorrect letters """ user = data.get_user_info(info['u_id']) msg = f""" {user['name_first']} has started a hangman! Word: {''.join(info['revealed_word'])} Incorrect letters guessed: {', '.join(info['letters'])} {generate_picture(info)} """ message_id = info['status_message'] channel_id = data.find_channel(message_id) data.edit_message(channel_id, message_id, msg)
def test_channel_remove_member(channel1): """ Removes member from channel, returns nothing """ channel_id = channel1["channel_id"] user = data.get_user_info(1) data.channel_remove_member(channel_id, user["u_id"]) assert data.check_user_in_channel(channel_id, user["u_id"]) == False user2 = auth.auth_register("*****@*****.**", "google", "Alphabet", "Gamma") channel.channel_join(user2["token"], channel_id) data.channel_remove_member(channel_id, user2["u_id"]) assert data.check_user_in_channel(channel_id, user2["u_id"]) == False other.clear()
def auth_login(email, password): """ Attempts to log user in by checking whether eamil and password match Parameters: email(string): Email given by user password(string): Password given by user Returns: Dictionary with user"s token and u_id (if successful) """ # Convert email to lowercase new_email = email.lower() # Checks to determine whether email and password are correct validation.check_correct_email(new_email) # Check if supplied password matches the email validation.check_correct_password(new_email, password) # Everything is valid # User has definitely registered. Password is correct # There is at least one user in data.data["users"] user = data.get_user_with({ "email" : new_email }) # Update global state. # Adds user to data["logged_in"]. data.login_user(user["u_id"]) # Gives user a new random number for token validation. data.update_user(data.get_user_info(user["u_id"]), {"session_secret" : random.randrange(100000), "reset_code" : None}) payload = { "u_id" : user["u_id"], "session_secret" : user["session_secret"] } token = str(jwt.encode(payload, data.get_jwt_secret(), algorithm = "HS256")) return { "u_id" : user["u_id"], "token": token[2:-1] }
def auth_logout(token): """ Logs the user out after checking that they are originally logged in Parameters: token(string): An authorisation hash Returns: Dictionary with a boolean that depends on whether user can be successfully logged out """ # Check if token is valid. try: u_id = validation.check_valid_token(token) except AccessError: return {"is_success" : False} # Check if user is active (logged in). data.logout_user(u_id) data.update_user(data.get_user_info(u_id), {"session_secret" : ""}) return {"is_success" : True}
def user_profile_setemail(token, email): """ Check whether use is valid and for a valid user, update their handle Parameters: token(string): An authorisation hash email(string): New email Returns: Nothing """ #Check for valid token u_id = validation.check_valid_token(token) #Check for valid email validation.check_valid_email(email.lower()) #Everything valid, proceed with changing email user = data.get_user_info(u_id) data.update_user(user, {"email": email.lower()}) return {}
def user_profile_setname(token, name_first, name_last): """ Check whether use is valid and for a valid user, update their name Parameters: token(string): An authorisation hash name_first(string): new first name name_last(stirng): new last name Returns: Nothing """ #Check for valid token u_id = validation.check_valid_token(token) #Check for valid name validation.check_valid_name(name_first, name_last) #Everything valid, proceed with changing name user = data.get_user_info(u_id) data.update_user(user, {"name_first": name_first, "name_last": name_last}) return {}
def channels_create(token, name, is_public): """ Checks information given is valid, then creates a new channel Parameters: token(string): A user authorisation hash name(string): Name of channel is_public(boolean): True if channel should be public Returns: Dictionary with information about the created channel """ # Check if token is valid u_id = validation.check_valid_token(token) # Returns InputError if channel name is more than 20 characters if len(name) > 20: raise InputError( description="Name cannot be more than 20 characters long") # Creates a new channel and stores to "channels" in data.py new_channel = { "channel_id": data.get_num_channels() + 1, "name": name, "is_public": is_public, "owners": {u_id}, "members": {u_id}, "messages": {}, "hangman": hangman.init() } data.channel_create(new_channel) # Stores channel as part of the user"s channel list user = data.get_user_info(u_id) data.update_user_channel_list(user, new_channel["channel_id"]) return {"channel_id": new_channel["channel_id"]}
def user_profile_sethandle(token, handle_str): """ Check whether use is valid and for a valid user, update their handle Parameters: token(string): An authorisation hash handle_str(string): New Handle Returns: Nothing """ #Check for valid token u_id = validation.check_valid_token(token) #Check for valid handle validation.check_valid_handle(handle_str) #Check for existing handle validation.check_existing_handle(handle_str) #Everything valid, proceed with changing handle user = data.get_user_info(u_id) data.update_user(user, {"handle_str": handle_str}) return {}
def print_start(info): """ Prints starting message of hangman """ user = data.get_user_info(info['u_id']) return f"""
def handle_get_user(params): user_email = params["user_email"] return data.get_user_info( user_email )