From 5f7cf70ac6fb37d2a63ce9a8e9a3bf9d4e1a244c Mon Sep 17 00:00:00 2001 From: kmeyerhofer Date: Tue, 14 Aug 2018 20:44:33 -0700 Subject: [PATCH 1/4] Adds custom routes for signing up, logging in and logging out. --- app/controllers/sessions_controller.rb | 1 - app/views/welcome/index.html.erb | 6 +++--- config/routes.rb | 5 +++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index b262515..3cb0383 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -6,7 +6,6 @@ def create user = User.find_by(email: params[:email]) if user && user.authenticate(params[:password]) session[:user_id] = user.id - # flash[:notice] = "Login successful." redirect_to root_url else flash[:warning] = 'Email or password is invalid' diff --git a/app/views/welcome/index.html.erb b/app/views/welcome/index.html.erb index 4c15c1c..466ca89 100644 --- a/app/views/welcome/index.html.erb +++ b/app/views/welcome/index.html.erb @@ -2,8 +2,8 @@ <% if current_user%> <%= "Welcome, #{current_user.first_name || current_user.email}." %> - <%= link_to "Sign Out", "/sessions/#{session[:user_id]}", method: :delete %> + <%= link_to "Sign Out", logout_path, method: :delete %> <% else %> - <%= link_to 'Log In', new_session_path %> or - <%= link_to 'Sign Up', new_user_path %> + <%= link_to 'Log In', login_path %> or + <%= link_to 'Sign Up', signup_path %> <% end %> diff --git a/config/routes.rb b/config/routes.rb index a5327c8..e6e80a9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,8 +7,9 @@ # note Rails has a helper for the above "root" path: root to: 'welcome#index' # to see all routes and connections to controllers, run `rails routes` Rake task - get '/new', to: 'users#new', as: 'signup' - get "logout", to: "sessions#destroy", as: 'logout' + get '/signup' => 'users#new', as: 'signup' + get '/login' => 'sessions#new', as: 'login' + delete '/logout' => 'sessions#destroy', as: 'logout' resources :users, only: [:new, :create, :show] resources :sessions, except: [:edit, :update] From 805c99795b66157a85f5286fc82a5c06ca948909 Mon Sep 17 00:00:00 2001 From: kmeyerhofer Date: Tue, 14 Aug 2018 21:30:55 -0700 Subject: [PATCH 2/4] User can access profile if logged in, unable to if logged out. --- app/controllers/users_controller.rb | 9 +++++++-- app/controllers/welcome_controller.rb | 1 - app/views/users/show.html.erb | 3 +++ app/views/welcome/index.html.erb | 4 +++- config/routes.rb | 1 + 5 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 app/views/users/show.html.erb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index ada5628..9240180 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -13,8 +13,13 @@ def create end def show - byebug - @user = User.find(params[:id]); + if !session[:user_id] + flash[:warning] = 'You must be logged in first.' + redirect_to root_url + else + @user = User.find(session[:user_id]) + render 'show' + end end private diff --git a/app/controllers/welcome_controller.rb b/app/controllers/welcome_controller.rb index ca9019d..f9b859b 100644 --- a/app/controllers/welcome_controller.rb +++ b/app/controllers/welcome_controller.rb @@ -1,5 +1,4 @@ class WelcomeController < ApplicationController def index - #@users = User.all end end diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb new file mode 100644 index 0000000..f8c1bb1 --- /dev/null +++ b/app/views/users/show.html.erb @@ -0,0 +1,3 @@ +Name: <%= "#{@user.first_name} #{@user.last_name}" %> +
+Email: <%= @user.email %> \ No newline at end of file diff --git a/app/views/welcome/index.html.erb b/app/views/welcome/index.html.erb index 466ca89..7f0ca59 100644 --- a/app/views/welcome/index.html.erb +++ b/app/views/welcome/index.html.erb @@ -2,7 +2,9 @@ <% if current_user%> <%= "Welcome, #{current_user.first_name || current_user.email}." %> - <%= link_to "Sign Out", logout_path, method: :delete %> + <%= link_to 'Sign Out', logout_path, method: :delete %> +
+ <%= link_to 'Profile', profile_path %> <% else %> <%= link_to 'Log In', login_path %> or <%= link_to 'Sign Up', signup_path %> diff --git a/config/routes.rb b/config/routes.rb index e6e80a9..4db1003 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,6 +8,7 @@ # to see all routes and connections to controllers, run `rails routes` Rake task get '/signup' => 'users#new', as: 'signup' + get '/profile' => 'users#show', as: 'profile' get '/login' => 'sessions#new', as: 'login' delete '/logout' => 'sessions#destroy', as: 'logout' From e29d994cef6286fe878286bbe4912f1e0411ade7 Mon Sep 17 00:00:00 2001 From: Kurt Date: Fri, 17 Aug 2018 19:29:50 -0700 Subject: [PATCH 3/4] Adds user model validation tests. --- spec/models/user_spec.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index e7e3e8d..e47d62c 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -7,4 +7,27 @@ expect(u.authenticate('notthepassword')).to be_falsey expect(u.authenticate('password123')).to eq u end + + describe 'validations' do + it 'validates email is present' do + u = User.new(first_name: 'Raz', last_name: 'Z', email: '', password: 'passpass') + expect(u.valid?).to eq false + end + + it 'validates first name is present' do + u = User.new(first_name: nil, last_name: 'Z', email: 'email@email.email', password: 'passpass') + expect(u.valid?).to be_falsey + end + + it 'validates last name is present' do + u = User.new(first_name: 'Razz', last_name: nil, email: 'email@email.email', password: 'passpass') + expect(u.valid?).to be_falsey + end + + it 'validates email is unique' do + User.create(first_name: 'Raz', last_name: 'Z', email: 'email@email.email', password: 'passpass') + v = User.new(first_name: 'Raz', last_name: 'Z', email: 'email@email.email', password: 'passpass') + expect(v.valid?).to eq false + end + end end From 8ea6dd88e3a1fc0f4f6d53cc1eab3499e23344ad Mon Sep 17 00:00:00 2001 From: Kurt Date: Fri, 17 Aug 2018 20:27:45 -0700 Subject: [PATCH 4/4] Adds user signup testing. Improved user flow with signup. Removed redundant routes. --- .gitignore | 3 ++ app/controllers/sessions_controller.rb | 4 +-- app/controllers/users_controller.rb | 17 +++++---- config/routes.rb | 2 +- spec/features/users/user_can_signup_spec.rb | 38 +++++++++++++++++++++ 5 files changed, 55 insertions(+), 9 deletions(-) create mode 100644 spec/features/users/user_can_signup_spec.rb diff --git a/.gitignore b/.gitignore index 211a27a..4dcdf9e 100644 --- a/.gitignore +++ b/.gitignore @@ -24,3 +24,6 @@ # Ignore master key for decrypting credentials and more. /config/master.key + +# Ignore rbenv version +.ruby-version diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 3cb0383..92e34cc 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -6,7 +6,7 @@ def create user = User.find_by(email: params[:email]) if user && user.authenticate(params[:password]) session[:user_id] = user.id - redirect_to root_url + redirect_to root_path else flash[:warning] = 'Email or password is invalid' render 'new' @@ -16,6 +16,6 @@ def create def destroy session[:user_id] = nil flash[:notice] = "Log out successful." - redirect_to root_url + redirect_to root_path end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 9240180..34eed65 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,24 +1,29 @@ class UsersController < ApplicationController def new @user = User.new + if current_user + redirect_to root_path + else + render 'new' + end end def create @user = User.new(user_params) if @user.save - redirect_to root_url + redirect_to login_path else render 'new' end end - + def show - if !session[:user_id] - flash[:warning] = 'You must be logged in first.' - redirect_to root_url - else + if current_user @user = User.find(session[:user_id]) render 'show' + else + redirect_to root_url + flash[:warning] = 'You must be logged in first.' end end diff --git a/config/routes.rb b/config/routes.rb index 4db1003..92725e8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -12,6 +12,6 @@ get '/login' => 'sessions#new', as: 'login' delete '/logout' => 'sessions#destroy', as: 'logout' - resources :users, only: [:new, :create, :show] + resources :users, only: [:create] resources :sessions, except: [:edit, :update] end diff --git a/spec/features/users/user_can_signup_spec.rb b/spec/features/users/user_can_signup_spec.rb new file mode 100644 index 0000000..29a6883 --- /dev/null +++ b/spec/features/users/user_can_signup_spec.rb @@ -0,0 +1,38 @@ +require 'rails_helper' + +describe 'user signup' do + context 'with a new user' do + it 'they can create an account' do + visit root_path + click_on 'Sign Up' + expect(current_path).to eq signup_path + fill_in('user[first_name]', with: 'Ra') + fill_in('user[last_name]', with: 'Zz') + fill_in('user[email]', with: 'email@email.email') + fill_in('user[password]', with: 'passpass') + fill_in('user[password_confirmation]', with: 'passpass') + click_on 'Create User' + + expect(current_path).to eq login_path + fill_in('email', with: 'email@email.email') + fill_in('password', with: 'passpass') + click_on 'Log In' + + expect(current_path).to eq root_path + expect(page).to have_content 'Welcome, Ra.' + expect(page).to have_link 'Sign Out' + expect(page).to have_link 'Profile' + expect(page).not_to have_link 'Log In' + expect(page).not_to have_link 'Sign Up' + end + end + + context 'as an existing, logged in user' do + it 'redirects from /signup to root' do + u = User.create(first_name: 'Raa', last_name: 'Zzz', email: 'email@raa.zzz', password: 'passpass') + allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(u) + visit signup_path + expect(current_path).to eq root_path + end + end +end