From a52b0eaba82129727949c5ca4632a76d27ebd0b1 Mon Sep 17 00:00:00 2001 From: Brian Plantico Date: Thu, 23 May 2019 15:57:18 -0600 Subject: [PATCH 1/6] IN PROGRESS working on user ability to log in --- .../admin/{admin => }/dashboard.html.erb | 0 app/views/users/login.html.erb | 0 config/routes.rb | 3 +- spec/features/users/user_can_login_spec.rb | 41 +++++++++++++++++++ .../users/visitor_sees_nav_bar_spec.rb | 22 ++++++++++ 5 files changed, 65 insertions(+), 1 deletion(-) rename app/views/admin/{admin => }/dashboard.html.erb (100%) create mode 100644 app/views/users/login.html.erb create mode 100644 spec/features/users/user_can_login_spec.rb create mode 100644 spec/features/users/visitor_sees_nav_bar_spec.rb diff --git a/app/views/admin/admin/dashboard.html.erb b/app/views/admin/dashboard.html.erb similarity index 100% rename from app/views/admin/admin/dashboard.html.erb rename to app/views/admin/dashboard.html.erb diff --git a/app/views/users/login.html.erb b/app/views/users/login.html.erb new file mode 100644 index 00000000..e69de29b diff --git a/config/routes.rb b/config/routes.rb index 6d15f812..7d0498a9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,7 +2,7 @@ # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html root to: 'welcome#index' - resources :merchants, only: [:index] + resources :merchants, only: [:index] namespace :dashboard do resources :items, only: [:index] @@ -11,6 +11,7 @@ get '/dashboard', to: 'merchants#dashboard', as: 'dashboard' # get '/merchants', to: 'merchants#index' + get '/login', to: 'sessions#new' get '/logout', to: 'application#logout' resources :items, only: [:index, :show] diff --git a/spec/features/users/user_can_login_spec.rb b/spec/features/users/user_can_login_spec.rb new file mode 100644 index 00000000..faf10a61 --- /dev/null +++ b/spec/features/users/user_can_login_spec.rb @@ -0,0 +1,41 @@ +require 'rails_helper' + +RSpec.describe "As a visitor" do + + before :each do + user = User.create!(email: "user@email.com", password: "password", role: 0, active: true, name: "Yu Xer", address: "123 street", city: "News Userville", state:"US", zip: "80211") + merchant = User.create!(email: "merchant@email.com", password: "password", role: 1, active: true, name: "Murr Chante", address: "123 street", city: "Merchantston", state:"MR", zip: "80211") + admin = User.create!(email: "admin@email.com", password: "password", role: 2, active: true, name: "Addie Munn", address: "123 street", city: "West Adminster", state:"AD", zip: "80211") + end + + describe "when I visit the login path" do + it "I see a field to enter my email address and password" do + + visit login_path + + expect(page).to have_field("Email") + expect(page).to have_field("Password") + end + + end +end + +# As a visitor +# When I visit the login path +# I see a field to enter my email address and password +# When I submit valid information +# If I am a regular user, I am redirected to my profile page +# If I am a merchant user, I am redirected to my merchant dashboard page +# If I am an admin user, I am redirected to the home page of the site +# And I see a flash message that I am logged in + +# describe "when I click on the 'login' link in the nav bar" do +# describe "I am taken to a login page" do +# it "and as a User can login and go to my profile page" do +# end +# end +# end +# +# fill_in 'Email', with: 'user@email.com' +# fill_in 'Password', with: 'password' +# click_link("Submit") diff --git a/spec/features/users/visitor_sees_nav_bar_spec.rb b/spec/features/users/visitor_sees_nav_bar_spec.rb new file mode 100644 index 00000000..ff86d9a9 --- /dev/null +++ b/spec/features/users/visitor_sees_nav_bar_spec.rb @@ -0,0 +1,22 @@ +require 'rails_helper' + +RSpec.describe "as a visitor on the site" do + describe "a navigation bar is seen" do + it "has working links that redirect" do + visit root_path + + click_link("Items") + expect(current_path).to eq(items_path) + + click_link("Merchants") + expect(current_path).to eq(merchants_path) + + click_link("Home") + expect(current_path).to eq(root_path) + + expect(page).to have_link("Cart") + expect(page).to have_link("Login") + expect(page).to have_link("Register") + end + end +end From e180dd2dcbb62c404fe4de58c5b4f5a2e9a1a557 Mon Sep 17 00:00:00 2001 From: Brian Plantico Date: Thu, 23 May 2019 16:22:23 -0600 Subject: [PATCH 2/6] Adds email and password fields to new login/session view Co-Authored-By: Glynnis O'Connell <44204866+GlynnisOC@users.noreply.github.com> --- app/controllers/sessions_controller.rb | 6 ++++++ app/views/sessions/new.html.erb | 8 ++++++++ app/views/users/login.html.erb | 0 3 files changed, 14 insertions(+) create mode 100644 app/controllers/sessions_controller.rb create mode 100644 app/views/sessions/new.html.erb delete mode 100644 app/views/users/login.html.erb diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb new file mode 100644 index 00000000..8b044822 --- /dev/null +++ b/app/controllers/sessions_controller.rb @@ -0,0 +1,6 @@ +class SessionsController < ApplicationController + + def new + end + +end diff --git a/app/views/sessions/new.html.erb b/app/views/sessions/new.html.erb new file mode 100644 index 00000000..21e044c9 --- /dev/null +++ b/app/views/sessions/new.html.erb @@ -0,0 +1,8 @@ +<%= form_tag login_path do %> + <%= label_tag :email %> + <%= text_field_tag :email %> + + <%= label_tag :password %> + <%= password_field_tag :password %> + <%= submit_tag "Login" %> +<% end %> diff --git a/app/views/users/login.html.erb b/app/views/users/login.html.erb deleted file mode 100644 index e69de29b..00000000 From 396c8187f9aa71a745a8f64685dc3c1852d9d869 Mon Sep 17 00:00:00 2001 From: Brian Plantico Date: Thu, 23 May 2019 17:20:53 -0600 Subject: [PATCH 3/6] Adds authentication and correct path to login as a registered user. Co-Authored-By: Glynnis O'Connell <44204866+GlynnisOC@users.noreply.github.com> --- app/controllers/sessions_controller.rb | 12 +++++++++++ app/controllers/users_controller.rb | 1 + config/routes.rb | 2 ++ spec/features/users/user_can_login_spec.rb | 24 +++++++++++++++------- 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 8b044822..a6c54f7b 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -3,4 +3,16 @@ class SessionsController < ApplicationController def new end + def create + user = User.find_by_email(params[:email]) + if user && user.authenticate(params[:password]) + session[:user_id] = user.id + redirect_to profile_path + flash[:message] = "Logged in as #{user.name}" + else + render :new + end + end + + end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 6038a083..1663d032 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -25,6 +25,7 @@ def show end def profile + if params[:new_id] != nil @user = User.find(params[:new_id]) else diff --git a/config/routes.rb b/config/routes.rb index 7d0498a9..50325657 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -12,6 +12,8 @@ # get '/merchants', to: 'merchants#index' get '/login', to: 'sessions#new' + post '/login', to: 'sessions#create' + get '/logout', to: 'application#logout' resources :items, only: [:index, :show] diff --git a/spec/features/users/user_can_login_spec.rb b/spec/features/users/user_can_login_spec.rb index faf10a61..55fd4e99 100644 --- a/spec/features/users/user_can_login_spec.rb +++ b/spec/features/users/user_can_login_spec.rb @@ -3,26 +3,36 @@ RSpec.describe "As a visitor" do before :each do - user = User.create!(email: "user@email.com", password: "password", role: 0, active: true, name: "Yu Xer", address: "123 street", city: "News Userville", state:"US", zip: "80211") - merchant = User.create!(email: "merchant@email.com", password: "password", role: 1, active: true, name: "Murr Chante", address: "123 street", city: "Merchantston", state:"MR", zip: "80211") - admin = User.create!(email: "admin@email.com", password: "password", role: 2, active: true, name: "Addie Munn", address: "123 street", city: "West Adminster", state:"AD", zip: "80211") + @user = User.create!(email: "user@email.com", password: "password", role: 0, active: true, name: "Yu Xer", address: "123 street", city: "News Userville", state:"US", zip: "80211") + @merchant = User.create!(email: "merchant@email.com", password: "password", role: 1, active: true, name: "Murr Chante", address: "123 street", city: "Merchantston", state:"MR", zip: "80211") + @admin = User.create!(email: "admin@email.com", password: "password", role: 2, active: true, name: "Addie Munn", address: "123 street", city: "West Adminster", state:"AD", zip: "80211") end describe "when I visit the login path" do it "I see a field to enter my email address and password" do visit login_path - + expect(page).to have_field("Email") expect(page).to have_field("Password") + expect(page).to have_button("Login") end + describe "when I submit valid information" do + it "if regular user, I am redirected to my profile page" do + visit login_path + + fill_in "Email", with: "user@email.com" + fill_in "Password", with: "password" + click_button("Login") + + expect(current_path).to eq(profile_path) + expect(page).to have_content("Logged in as #{@user.name}") + end + end end end -# As a visitor -# When I visit the login path -# I see a field to enter my email address and password # When I submit valid information # If I am a regular user, I am redirected to my profile page # If I am a merchant user, I am redirected to my merchant dashboard page From 8613cd650a9f446aebe9c86073bfbe3e48366b6a Mon Sep 17 00:00:00 2001 From: Brian Plantico Date: Thu, 23 May 2019 17:38:50 -0600 Subject: [PATCH 4/6] Adds authentication and correct routing for merchant and admin users when they login. Co-Authored-By: Glynnis O'Connell <44204866+GlynnisOC@users.noreply.github.com> --- app/controllers/sessions_controller.rb | 9 +++++++-- spec/features/users/user_can_login_spec.rb | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index a6c54f7b..9af48c39 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -7,12 +7,17 @@ def create user = User.find_by_email(params[:email]) if user && user.authenticate(params[:password]) session[:user_id] = user.id - redirect_to profile_path + if user.default? + redirect_to profile_path + elsif user.merchant? + redirect_to dashboard_path + elsif user.admin? + redirect_to root_path + end flash[:message] = "Logged in as #{user.name}" else render :new end end - end diff --git a/spec/features/users/user_can_login_spec.rb b/spec/features/users/user_can_login_spec.rb index 55fd4e99..70436c4d 100644 --- a/spec/features/users/user_can_login_spec.rb +++ b/spec/features/users/user_can_login_spec.rb @@ -29,6 +29,28 @@ expect(current_path).to eq(profile_path) expect(page).to have_content("Logged in as #{@user.name}") end + + it "if merchant user, I am redirected to my merchant dashboard page" do + visit login_path + + fill_in "Email", with: "merchant@email.com" + fill_in "Password", with: "password" + click_button("Login") + + expect(current_path).to eq(dashboard_path) + expect(page).to have_content("Logged in as #{@merchant.name}") + end + + it "if admin user, I am redirected to the root page" do + visit login_path + + fill_in "Email", with: "admin@email.com" + fill_in "Password", with: "password" + click_button("Login") + + expect(current_path).to eq(root_path) + expect(page).to have_content("Logged in as #{@admin.name}") + end end end end From 2aef26c70b9ba7304f085795f2c333792ff5d1c7 Mon Sep 17 00:00:00 2001 From: Brian Plantico Date: Thu, 23 May 2019 17:54:55 -0600 Subject: [PATCH 5/6] Adds case for invalid credentials supplied at login. Co-Authored-By: Glynnis O'Connell <44204866+GlynnisOC@users.noreply.github.com> --- app/controllers/admin/admin_controller.rb | 6 ------ app/controllers/sessions_controller.rb | 3 ++- .../navigation/admin_sees_nav_bar_spec.rb | 1 + spec/features/users/user_can_login_spec.rb | 21 +++++++++++++++++++ 4 files changed, 24 insertions(+), 7 deletions(-) delete mode 100644 app/controllers/admin/admin_controller.rb diff --git a/app/controllers/admin/admin_controller.rb b/app/controllers/admin/admin_controller.rb deleted file mode 100644 index 1c161592..00000000 --- a/app/controllers/admin/admin_controller.rb +++ /dev/null @@ -1,6 +0,0 @@ -class Admin::AdminController < ActionController::Base - - def dashboard - end - -end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 9af48c39..80835d3b 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -16,7 +16,8 @@ def create end flash[:message] = "Logged in as #{user.name}" else - render :new + redirect_to login_path + flash[:message] = "The email or password you entered was incorrect." end end diff --git a/spec/features/navigation/admin_sees_nav_bar_spec.rb b/spec/features/navigation/admin_sees_nav_bar_spec.rb index 6a1cc4b0..54dac62e 100644 --- a/spec/features/navigation/admin_sees_nav_bar_spec.rb +++ b/spec/features/navigation/admin_sees_nav_bar_spec.rb @@ -31,6 +31,7 @@ visit root_path expect(page).to have_link("Admin Dashboard") + sav click_on "Admin Dashboard" expect(current_path).to eq(admin_dashboard_path) end diff --git a/spec/features/users/user_can_login_spec.rb b/spec/features/users/user_can_login_spec.rb index 70436c4d..aab8087d 100644 --- a/spec/features/users/user_can_login_spec.rb +++ b/spec/features/users/user_can_login_spec.rb @@ -51,6 +51,27 @@ expect(current_path).to eq(root_path) expect(page).to have_content("Logged in as #{@admin.name}") end + + describe "if invalid info given" do + it "redirects to login and tells me credentials were incorrect" do + visit login_path + + fill_in "Email", with: "admin@email.com" + fill_in "Password", with: "forgot" + click_button("Login") + + expect(current_path).to eq(login_path) + expect(page).to have_content("The email or password you entered was incorrect.") + end + end + + +# As a visitor +# When I visit the login page ("/login") +# And I submit invalid information +# Then I am redirected to the login page +# And I see a flash message that tells me that my credentials were incorrect +# I am NOT told whether it was my email or password that was incorrect end end end From 6f6a12dd4c88420a82dca575c8a729ac744093df Mon Sep 17 00:00:00 2001 From: Brian Plantico Date: Thu, 23 May 2019 18:13:23 -0600 Subject: [PATCH 6/6] Adds completed login functionality if valid/invalid credentials. All tests passing, 100% coverage. Co-Authored-By: Glynnis O'Connell <44204866+GlynnisOC@users.noreply.github.com> --- app/controllers/admin/admins_controller.rb | 7 +++++++ .../dashboard/.items_controller.rb.swp | Bin 12288 -> 0 bytes app/views/admin/admins/dashboard.html.erb | 1 + app/views/admin/dashboard.html.erb | 0 config/routes.rb | 4 ++-- .../navigation/admin_sees_nav_bar_spec.rb | 4 ++-- 6 files changed, 12 insertions(+), 4 deletions(-) create mode 100644 app/controllers/admin/admins_controller.rb delete mode 100644 app/controllers/dashboard/.items_controller.rb.swp create mode 100644 app/views/admin/admins/dashboard.html.erb delete mode 100644 app/views/admin/dashboard.html.erb diff --git a/app/controllers/admin/admins_controller.rb b/app/controllers/admin/admins_controller.rb new file mode 100644 index 00000000..c1530672 --- /dev/null +++ b/app/controllers/admin/admins_controller.rb @@ -0,0 +1,7 @@ +class Admin::AdminsController < ActionController::Base + + def dashboard + + end + +end diff --git a/app/controllers/dashboard/.items_controller.rb.swp b/app/controllers/dashboard/.items_controller.rb.swp deleted file mode 100644 index cd246b99ad7b1308fe37ccf99931a695abd678da..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI&O-jTt6bJA(B7zH1^a54aos4xe3~v0`%wj+U-6)i{$=FJ!2}wp=>Cyu!xY4!X z1-yY5@B+5A)>Lq%I5N*CVFUf=KO<&?;%SQuxW z$?|hy%7WSL=(Elr>>c{S?2>Bs`ul>+jmwo;5RVieOTn~J7b0<%$;>$^LYpcbUIr}E znk6cCMoB4*WjwNJtRll%=ETT`(_GuczwD0`1R5(ac0<3rMBBku^{Q`ftb1##$BpH{ z!65(v2tWV=5P$##Akahsr(1MW9DJ@m{Cpi-KVm&ZfB*y_009U<00Izz00bZa0SG|g z9|}aA=ysXtuIT0e|H=3Nb1{A_`a_+2D)VR%fB*y_009U<00Izz00bZa0SNq+fXF#5 sRu3+QUY2ulMTv~8^$sQ%<9oe+c{6_h>(acp diff --git a/app/views/admin/dashboard.html.erb b/app/views/admin/dashboard.html.erb deleted file mode 100644 index e69de29b..00000000 diff --git a/config/routes.rb b/config/routes.rb index 50325657..80c47875 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -13,7 +13,7 @@ get '/login', to: 'sessions#new' post '/login', to: 'sessions#create' - + get '/logout', to: 'application#logout' resources :items, only: [:index, :show] @@ -23,7 +23,7 @@ resources :users, only: [:index, :new, :create, :show, :edit] namespace :admin do - get '/dashboard', to: "admin#dashboard" + get '/dashboard', to: "admins#dashboard" end # resources :carts, only: [:create] diff --git a/spec/features/navigation/admin_sees_nav_bar_spec.rb b/spec/features/navigation/admin_sees_nav_bar_spec.rb index 54dac62e..ebce198b 100644 --- a/spec/features/navigation/admin_sees_nav_bar_spec.rb +++ b/spec/features/navigation/admin_sees_nav_bar_spec.rb @@ -31,8 +31,8 @@ visit root_path expect(page).to have_link("Admin Dashboard") - sav - click_on "Admin Dashboard" + + click_link "Admin Dashboard" expect(current_path).to eq(admin_dashboard_path) end