Esempio n. 1
0
def login():
    """
    """
    auth_form = LoginForm()
    search_form = utils.MovieSearchForm()
    username_not_recognised = None
    password_doesnt_match = None
    if auth_form.validate_on_submit():
        try:
            user = services.get_user(auth_form.username.data,
                                     repo.repo_instance)
            services.authenticate_user(user['username'],
                                       auth_form.password.data,
                                       repo.repo_instance)

            session.clear()
            session['username'] = user['username']
            return redirect(url_for('home_bp.home'))
        except services.UnknownUserException:
            username_not_recognised = "Username not recognised- please try another"
        except services.AuthenticationException:
            password_doesnt_match = 'Password does not match username. Please examine your credentials and try again'

    return render_template(
        'authentication/credentials.html',
        title='Login',
        username_error_message=username_not_recognised,
        password_error_message=password_doesnt_match,
        auth_form=auth_form,
        search_form=search_form,
    )
Esempio n. 2
0
def login():
    form = LoginForm()
    username_not_recognised = None
    password_does_not_match_username = None

    if form.validate_on_submit():
        # If form is successful
        try:
            user = services.get_user(form.username.data)

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

            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'

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

    # If form not successful
    return render_template(
        'authentication/credentials.html',
        title='Login',
        username_error_message=username_not_recognised,
        password_error_message=password_does_not_match_username,
        form=form)
Esempio n. 3
0
def test_authentication_with_invalid_credentials(in_memory_repo):
    new_username = '******'
    new_password = '******'

    auth_services.add_user(new_username, new_password, in_memory_repo)

    with pytest.raises(auth_services.AuthenticationException):
        auth_services.authenticate_user(new_username, '0987654321', in_memory_repo)
Esempio n. 4
0
def test_authentication_with_valid_credentials(in_memory_repo):
    new_username = '******'
    new_password = '******'

    auth_services.add_user(new_username, new_password, in_memory_repo)

    try:
        auth_services.authenticate_user(new_username, new_password, in_memory_repo)
    except AuthenticationException:
        assert False
Esempio n. 5
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:
            # Usernames are converted to lowercase, so we need to use .lower()
            user = services.get_user(form.username.data.lower(),
                                     repo.repo_instance)

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

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

        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,
        tag_urls=utilities.get_tags_and_urls())
Esempio n. 6
0
def test_authenticate_user_password_does_not_match(in_mem_repo):
    with pytest.raises(AuthenticationException):
        authenticate_user("shaunp", "pw654321", in_mem_repo)
Esempio n. 7
0
def test_authenticate_user(in_mem_repo):
    assert authenticate_user("shaunp", "pw123456", in_mem_repo) is True