Пример #1
0
def test_valid_email_invalid_email():
    clear()
    with pytest.raises(InputError):
        valid_email("obviously not")

    with pytest.raises(InputError):
        valid_email("12345")
Пример #2
0
def user_profile_setemail(token, email):
    """
    Changes and sets user's email.
    Parameters:
        token(str): A string that validates the users actions while
    				 They are logged in
        email(str): The new email of the user
    Returns: None
    """
    # raise InputError if any of the inputs is empty
    if token == '':
        raise InputError("token input left empty")
    if email == '':
        raise InputError("email input left empty")

    # raise InputError if any of the inputs is an invalid type
    if not isinstance(token, str):
        raise AccessError("token of wrong data type.")
    if not isinstance(email, str):
        raise InputError("email of wrong data type.")

    # Checks that email is in valid format
    valid_email(email)
    # Checks that email is not being used by another user
    existing_email(email)

    # check that token is authorised
    tok = authenticate_token(token)

    user = data.users[tok]
    user.email = email
    return {}
Пример #3
0
def auth_passwordreset_request(email):
    """
    Sends user an secret code to their email.
    Parameters:
    	email (str): an email address to identify the user
   	Returns:
   		{}
    """

    # checks email is for a valid user
    valid_email(email)
    # Check email exists - If reached here, then email is valid
    email_exists = False

    for user in data.users:
        if user.email == email:
            current_user = user
            email_exists = True

    if not email_exists:
        raise InputError("No registered user with that email")

    # generates a secret key
    letdig = string.ascii_letters + string.digits
    secret_key = ''.join((random.choice(letdig) for i in range(5)))

    # stores the key in the user dictionary
    current_user.secret_key = secret_key

    # sends email with secret key
    system_email = '*****@*****.**'
    system_password = '******'

    msg = MIMEMultipart()
    msg["Subject"] = "flockr Changing your Password"
    msg["From"] = system_email
    msg["To"] = email

    text = f"""\
    Hi {current_user.name_first},

    The code to reset your password is {secret_key}.

    This is an Automated message from Flockr. Do not reply.
    """
    msgtext = MIMEText(text, "plain")
    msg.attach(msgtext)
    context = ssl.create_default_context()

    with smtplib.SMTP_SSL('smtp.gmail.com', 465,
                          context=context) as server_ssl:
        server_ssl.login(system_email, system_password)
        server_ssl.sendmail(system_email, email, msg.as_string())
        server_ssl.quit()

    return {}
Пример #4
0
def auth_register(email, password, name_first, name_last):
    """
    Given a user's details, create a new account for them

    Parameters:
        email (str): user input of their email
        password (str): user input of their password
        name_first (str): user input of their first name
        name_last (str): user input of their last name

    Returns:
        {
        	u_id (int):  A unique id for the user
            token (str): A string that validates the users actions while
        				 They are logged in
        }
    """
    ### Errors ###
    # Check for Invalid emails
    # Checks if email follows the above regex if not raises an error,
    # NOTE: added capitals, / and - to be allowed
    valid_email(email)

    # Email already belongs to another user
    if data.users != []:
        existing_email(email)

    # Password is less than 6 characters
    if len(password) < 6:
        raise InputError("Password too short")

    # name_first or name_last not is between 1 and 50 characters in length
    if len(name_first) > 50 or len(name_last) > 50:
        raise InputError("Name too long")

    if len(name_first) < 1 or len(name_last) < 1:
        raise InputError("No name entered")

    ### Creates the user ###
    new_user = user(email, password, name_first, name_last)

    # add the new user to the data
    data.new_user(new_user)

    ### Token generation ###
    token = generate_token(new_user.u_id)
    data.tokens.append(token)

    return {
        'u_id': new_user.u_id,
        'token': token,
    }
Пример #5
0
def auth_login(email, password):
    """
    Logs in a user, generates a token for their session

    Parameters:
    	email (str): an email address to identify the user
    	password (str): A security feature that needs to be checked with
    					the password entered on registry

   	Returns:
   		{
   			u_id (int): A unique ID for each user
   			token (str): A new generated token for the user until they log out
   		}
    """
    # Check email format - Used Siennas method + test if email is string
    valid_email(email)

    # Check email exists - If reached here, then email is valid
    email_exists = False

    for item in data.users:
        if item.email == email:
            current_user = item.u_id
            email_exists = True

    if not email_exists:
        raise InputError("No registered user with that email")

    if not isinstance(password, str):
        raise InputError("Invalid password")

    # Check hashed password - If reached here, then user exists
    hashed_password = hashlib.sha256(password.encode()).hexdigest()
    if not hashed_password == data.users[current_user].password:
        print(data.users[current_user].password)
        print(hashed_password)
        raise InputError("Incorrect Password")

    # Generate Token - Used Siennas method - If reached here, login successful
    token = generate_token(current_user)
    data.tokens[current_user] = token

    return {
        'u_id': current_user,
        'token': token,
    }
Пример #6
0
def test_valid_email_valid_email():
    clear()
    k = 2
    valid_email("*****@*****.**")
    k = 3
    assert k == 3