Example #1
0
def test_get_user_with(user1):
    """
    Returns user when given correct attribute, otherwise returns none
    """    
    assert data.get_user_with({"email":"*****@*****.**"})
    assert data.get_user_with({"email":"*****@*****.**"}) == None
    
    other.clear()
Example #2
0
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()
Example #3
0
def check_correct_email(email):
    """
    Determine whether email exists when logging in

    Parameters:
        email(string): Email in question

    Returns:
        Raises an error if email doesn't exist
        Returns nothing if email exists
    """
    if data.get_user_with({ "email" : email }) is not None:
        return
    raise InputError(description = "User does not exist")
Example #4
0
def check_existing_email(email):
    """
    Determine whether email is used with an account.

    Parameters:
        email(string): Email in question

    Returns:
        Raises an error if email already exists
        Returns nothing if email doesn't exist
    """
    if data.get_user_with({ "email" : email }) is not None:
        raise InputError("Email already in use")
    return
Example #5
0
def check_existing_handle(handle_str):
    """
    Determine whether supplied handle already exists

    Parameters:
        handle_str(string): New handle

    Returns:
        Raises an error if handle already exists
        Returns nothing if handle doesn't exist
    """

    if data.get_user_with({ "handle_str" : handle_str }) is not None:
        raise InputError(description = "Handle already in use")
    return
Example #6
0
def check_valid_reset_code(reset_code):
    """
    Determines who reset code belongs to

    Parameters:
        reset_code(str): A hash used for resetting a user's password

    Returns:
        User dictionary of whoever has the reset_code
        Raises an error if reset_code doesn't belong to a user
    """
    user = data.get_user_with({'reset_code' : reset_code})
    if user:
        return user
    raise InputError(description='Reset code is not valid')
Example #7
0
def check_correct_password(email, password):
    """
    Determines whether password matches account created with given email

    Parameters:
        email(string): Email used for account being logged into
        password(string): Password given

    Returns:
        Raises an error if password doesn't match email
        Returns nothing if password and email match
    """
    password_hash = hashlib.sha256(password.encode()).hexdigest()
    original = data.get_user_with({ "email" : email })
    if original is None:
        raise InputError(description = "Email does not exist")
    if original["password"] != password_hash:
        raise InputError(description = "Password is incorrect")
Example #8
0
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]
    }
Example #9
0
def generate_handle(first, last):
    """
    Generates a unique handle based on first and last name

    Parameters:
        first(string): User"s given girst name
        last(string): User"s given last name

    Returns:
        handle(string) concatenated from first and last line
    """
    names = first + last
    # Handle is 20 letters max.
    handle = names[:20]
    # Check if handle already exists
    if data.get_user_with({ "handle_str" : handle}) is not None:
        length = str(data.get_num_users())
        handle = handle[:len(length) * -1] + length
    return handle
Example #10
0
def check_valid_email(email):
    """
    Determine whether email is valid.

    Parameters:
        email(string): Email in question

    Returns:
        Raises an error if email is invalid (Doesn't match regex or already in use)
        Returns nothing is valid email
    """
    # If email already taken.
    if data.get_user_with({ "email" : email }) is not None:
        raise InputError(description="Email already in use")
    # Must be standard email (may change to custom later).
    # Regex mostly taken from geeksforgeeks site (linked in spec (6.2)).
    regex = r"^[a-z0-9]+[._]?[a-z0-9]+[@]\w+[.]\w{2,3}(\.\w{2})?$"
    # If email doesn"t match regex, it"s not valid.
    if not re.search(regex, email):
        raise InputError(description = "Email is invalid")
    return
Example #11
0
def auth_passwordreset_request(email):
    """
    Sends an email to specified email adress with code 
    to reset password if email is valid

    Parameters:
        email(str): A user's email
    
    Returns:
        An empty dictionary
    """
    # Check email is valid
    validation.check_correct_email(email)

    user = data.get_user_with({'email' : email})

    # Check user is logged out
    validation.check_logged_out(user['u_id'])

    code = generate_reset_code(user)
    
    send_email(email, code)
    return {}