def register(): form = RegistrationForm() username_not_unique = 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 attempt to add the new user. try: services.add_user(form.username.data, form.password.data, repo.repo_instance) # All is well, redirect the user to the login page. return redirect(url_for('authentication_bp.login')) except services.NameNotUniqueException: username_not_unique = 'Your username is already taken - please supply another' # For a GET or a failed POST request, return the Registration Web page. return render_template( 'authentication/credentials.html', title='Register', form=form, username_error_message=username_not_unique, password_error_message=password_does_not_match_username, handler_url=url_for('authentication_bp.register'), )
def register(): """ """ auth_form = RegistrationForm() search_form = utils.MovieSearchForm() username_unique = None if auth_form.validate_on_submit(): try: services.add_user(auth_form.username.data, auth_form.password.data, repo.repo_instance) return redirect(url_for('authentication_bp.login')) except services.NameNotUnqiueException: username_unique = "Username is already used, please try another one" return render_template( 'authentication/credentials.html', title='Register', auth_form=auth_form, username_error_message=username_unique, handler_url=url_for('authentication_bp.register'), search_form=search_form) return render_template('authentication/credentials.html', title='Register', auth_form=auth_form, username_error_message=username_unique, handler_url=url_for('authentication_bp.register'), search_form=search_form)
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)
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:')
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
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:')
def test_cannot_add_user_with_existing_name(in_memory_repo): new_username = '******' new_password = '******' auth_services.add_user(new_username, new_password, in_memory_repo) # Add already existing username. username = '******' password = '******' with pytest.raises(auth_services.NameNotUniqueException): auth_services.add_user(username, password, in_memory_repo)
def test_cannot_add_review_for_non_existent_movie(in_memory_repo): # Add user to repository first. new_username = '******' new_password = '******' auth_services.add_user(new_username, new_password, in_memory_repo) # Create review review_text = "Great movie!" movie_rank = 1001 rating = 9 # Call the service layer to attempt to add the review. with pytest.raises(movies_services.NonExistentMovieException): movies_services.add_review(movie_rank, review_text, new_username, rating, in_memory_repo)
def register(): form = RegistrationForm() username_not_unique = None if form.validate_on_submit(): # If form is successful try: services.add_user(form.username.data, form.password.data) return redirect(url_for('authentication_bp.login')) except services.NameNotUniqueException: username_not_unique = 'Your username is already taken - please supply another' # If form is not successful, return register page return render_template( 'authentication/credentials.html', title='Register', form=form, username_error_message=username_not_unique, handler_url=url_for('authentication_bp.register'), )
def test_can_add_review(in_memory_repo): # Add user to repository first. new_username = '******' new_password = '******' auth_services.add_user(new_username, new_password, in_memory_repo) # Create review review_text = "Great movie!" movie_rank = 114 rating = 9 # Call the service layer to add the review movies_services.add_review(movie_rank, review_text, new_username, rating, in_memory_repo) # Retrieve the reviews for the movie from the repository. reviews_as_dict = movies_services.get_reviews_for_movie( movie_rank, in_memory_repo) # Check that the reviews include a review with the new review text. assert next((dictionary['review_text'] for dictionary in reviews_as_dict if dictionary['review_text'] == review_text), None) is not None
def test_cannot_add_user_with_existing_name(in_memory_repo): username = '******' password = '******' with pytest.raises(auth_services.NameNotUniqueException): auth_services.add_user(username, password, in_memory_repo)
def test_add_user_name_not_unique(in_mem_repo): with pytest.raises(NameNotUniqueException): add_user("shaunp", "password", in_mem_repo)
def test_add_user(in_mem_repo): add_user("testuser", "password", in_mem_repo) assert in_mem_repo.get_user("testuser").username == "testuser"