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/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 cd246b99..00000000 Binary files a/app/controllers/dashboard/.items_controller.rb.swp and /dev/null differ diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb new file mode 100644 index 00000000..80835d3b --- /dev/null +++ b/app/controllers/sessions_controller.rb @@ -0,0 +1,24 @@ +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 + 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 + redirect_to login_path + flash[:message] = "The email or password you entered was incorrect." + 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/app/views/admin/admin/dashboard.html.erb b/app/views/admin/admin/dashboard.html.erb deleted file mode 100644 index e69de29b..00000000 diff --git a/app/views/admin/admins/dashboard.html.erb b/app/views/admin/admins/dashboard.html.erb new file mode 100644 index 00000000..891a9d91 --- /dev/null +++ b/app/views/admin/admins/dashboard.html.erb @@ -0,0 +1 @@ +<%= link_to "Admin Dashboard", admin_dashboard_path %> 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/config/routes.rb b/config/routes.rb index 6d15f812..80c47875 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,9 @@ get '/dashboard', to: 'merchants#dashboard', as: 'dashboard' # 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] @@ -20,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 6a1cc4b0..ebce198b 100644 --- a/spec/features/navigation/admin_sees_nav_bar_spec.rb +++ b/spec/features/navigation/admin_sees_nav_bar_spec.rb @@ -31,7 +31,8 @@ visit root_path expect(page).to have_link("Admin Dashboard") - click_on "Admin Dashboard" + + click_link "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 new file mode 100644 index 00000000..aab8087d --- /dev/null +++ b/spec/features/users/user_can_login_spec.rb @@ -0,0 +1,94 @@ +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") + 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 + + 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 + + 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 + +# 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