Ejemplo 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,
    )
Ejemplo n.º 2
0
def profile():
    movie_id = request.args.get('watched')
    username = session['user_name']
    user = services.get_user(username, repo.repo_instance)
    watched = services.get_watched(username, repo.repo_instance)

    if movie_id is not None:
        services.add_watched(username, int(movie_id), repo.repo_instance)

    return render_template('authentication/profile.html',
                           title=username + "'s Profile",
                           user=services.get_user(username,
                                                  repo.repo_instance),
                           watched=watched,
                           handler_url=url_for('authentication_bp.profile'),
                           tag_urls=utilities.get_tags_and_urls())
Ejemplo n.º 3
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)
Ejemplo n.º 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
    assert user_as_dict['password'].startswith('pbkdf2:sha256:')
Ejemplo n.º 5
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['user_name'] == new_username

    # Check that password has been encrypted.
    assert user_as_dict['password'].startswith('pbkdf2:sha256:')
Ejemplo n.º 6
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())
Ejemplo n.º 7
0
def test_get_user_does_not_exist(in_mem_repo):
    with pytest.raises(UnknownUserException):
        get_user("fakeuser", in_mem_repo)
Ejemplo n.º 8
0
def test_get_user(in_mem_repo):
    user = get_user("shaunp", in_mem_repo)
    assert user['username'] == "shaunp"
    assert check_password_hash(user['password'], "pw123456")