def test_can_add_user(memory_repo):
    username = '******'
    password = '******'
    auth_services.add_user(username, password, memory_repo)
    user_dict = auth_services.get_user(username, memory_repo)
    assert user_dict['username'] == 'TestUser'
    assert user_dict['password'].startswith('pbkdf2:sha256:')

    user_dict = auth_services.get_user(username.lower(), memory_repo)
    assert user_dict['username'] == 'TestUser'
    assert user_dict['password'].startswith('pbkdf2:sha256:')
def login():
    form = LoginForm()
    username_not_recognised = None
    password_does_not_match_username = None

    if form.validate_on_submit():
        # POST
        try:
            user = services.get_user(form.username.data, repo.repository_instance)

            # Authenticate user
            services.authenticate_user(user.user_name, form.password.data, repo.repository_instance)

            # Initialize session
            session.clear()
            session['username'] = user.user_name
            return redirect(url_for('home_bp.home'))
        except services.UnknownUserException:
            username_not_recognised = 'Unknown User'
        except services.AuthenticationException:
            # Authentication failed, set a suitable error message.
            password_does_not_match_username = '******'

    return render_template(
        'authentication/credentials.html',
        title='Login',
        username_error_message=username_not_recognised,
        password_error_message=password_does_not_match_username,
        form=form
    )
Exemple #3
0
def login():
    form = LoginForm()
    username_not_recognised = None
    password_does_not_match_username = None

    if form.validate_on_submit():
        # Successful POST, i.e. the username and password have passed validation checking.
        # Use the service layer to lookup the user.
        try:
            user = services.get_user(form.username.data, repo.repo_instance)

            # Authenticate user.
            services.authenticate_user(user['username'], form.password.data,
                                       repo.repo_instance)

            # Initialise session and redirect the user to the home page.
            session.clear()
            session['username'] = user['username']
            return redirect(url_for('home_bp.home'))

        except services.UnknownUserException:
            # Username not known to the system, set a suitable error message.
            username_not_recognised = 'Username not recognised - please supply another'

        except services.AuthenticationException:
            # Authentication failed, set a suitable error message.
            password_does_not_match_username = '******'

    # For a GET or a failed POST, return the Login Web page.
    return render_template(
        'authentication/credentials.html',
        title='Login',
        username_error_message=username_not_recognised,
        password_error_message=password_does_not_match_username,
        form=form)
Exemple #4
0
def test_can_add_user(in_memory_repo):
    new_username = '******'
    new_password = '******'

    auth_services.add_user(new_username, new_password, in_memory_repo)

    user_as_dict = auth_services.get_user(new_username, in_memory_repo)
    assert user_as_dict['username'] == new_username

    # Check that password has been encrypted.
    assert user_as_dict['password'].startswith('pbkdf2:sha256:')
Exemple #5
0
def login():
    form = LoginForm()
    username_error_msg = None
    password_error_msg = None

    if form.validate_on_submit():
        try:
            user = services.get_user(form.username.data, repo.repo_instance)
            services.authenticate_user(form.username.data, form.password.data,
                                       repo.repo_instance)

            session.clear()
            session['username'] = user['username']
            return redirect(url_for(HOME_BP + '.' + 'home'))
        except services.UnknownUserException:
            username_error_msg = 'User cannot be recognized, please check your username'
        except services.AuthenticationException:
            password_error_msg = 'Password cannot be authenticated, please check your password'

    return render_template('credentials.html',
                           title='Movie Login',
                           form=form,
                           username_error_msg=username_error_msg,
                           password_error_msg=password_error_msg)