Created
April 10, 2019 23:08
-
-
Save localhostdotdev/f8e4b0d32e5836f768eb8cba972a99e1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # frozen_string_literal: true | |
| require 'test_helper' | |
| class SignUpTest < ActionDispatch::IntegrationTest | |
| test 'can user sign up' do | |
| get users_path | |
| assert_response :success | |
| assert_difference 'User.count' do | |
| post users_path, | |
| params: { user: { email: 'am@example.com', password: 'password' } } | |
| end | |
| assert_redirected_to root_path | |
| assert_select 'h1', 'Welcome' | |
| end | |
| test "user can't sign up without email and password" do | |
| get users_path | |
| assert_response :success | |
| assert_no_difference 'User.count' do | |
| post users_path, | |
| params: { user: { email: 'am@example.com', password: 'password' } } | |
| end | |
| assert_response :error | |
| assert_select 'h2', 'error' | |
| end | |
| end | |
| # and here's the error: | |
| # "User.count" didn't change by 1. | |
| # Expected: 1 | |
| # Actual: 0 | |
| # Here's the user's controller: | |
| class UsersController < ApplicationController | |
| before_action :set_user, only: %i[edit update show] | |
| before_action :require_same_user, only: %i[edit update destroy] | |
| before_action :require_admin, only: %i[destroy] | |
| def index | |
| @users = User.paginate(page: params[:page], per_page: 5) | |
| end | |
| def new | |
| @user = User.new | |
| end | |
| def create | |
| @user = User.new(user_params) | |
| if @user.save | |
| session[:user_id] = @user.id | |
| flash[:success] = "Welcome to the Alpha Blog #{@user.username}" | |
| redirect_to user_path(@user) | |
| else | |
| render 'new' | |
| end | |
| end | |
| def edit; end | |
| def update | |
| if @user.update(user_params) | |
| flash[:success] = 'Your account was update successfully!' | |
| redirect_to articles_path | |
| else | |
| render 'edit' | |
| end | |
| end | |
| def show | |
| @user_articles = @user.articles.paginate(page: params[:page], per_page: 5) | |
| end | |
| def destroy | |
| @user = User.find(params[:id]) | |
| @user.destroy | |
| flash[:danger] = 'User and all articles created by user have been deleted.' | |
| redirect_to users_path | |
| end | |
| private | |
| def user_params | |
| params.require(:user).permit(:username, :email, :password) | |
| end | |
| def set_user | |
| @user = User.find(params[:id]) | |
| end | |
| def require_same_user | |
| if (current_user != @user) && !current_user.admin? | |
| flash[:danger] = 'You can only edit your own account.' | |
| redirect_to root_path | |
| end | |
| end | |
| def require_admin | |
| if logged_in? && !current_user.admin? | |
| flash[:danger] = 'Only admin users can perform that action.' | |
| redirect_to root_path | |
| end | |
| end | |
| end | |
| # Here's the sessions controller: | |
| class SessionsController < ApplicationController | |
| def new; end | |
| def create | |
| user = User.find_by(email: params[:session][:email].downcase) | |
| if user&.authenticate(params[:session][:password]) | |
| session[:user_id] = user.id | |
| flash[:success] = 'You have successfully logged in.' | |
| redirect_to user_path(user) | |
| else | |
| flash.now[:danger] = | |
| 'There was something wrong with your login information.' | |
| render 'new' | |
| end | |
| end | |
| def destroy | |
| session[:user_id] = nil | |
| flash[:success] = 'You have logged out!' | |
| redirect_to root_path | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment