Skip to content

Commit

Permalink
add and test user login and logout
Browse files Browse the repository at this point in the history
  • Loading branch information
lao9 committed Aug 1, 2017
1 parent 2c7eeef commit bec5433
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 1 deletion.
24 changes: 24 additions & 0 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class SessionsController < ApplicationController

def new
end

def create
user = User.find_by(email: params[:session][:email])
if user && user.authenticate(params[:session][:password])
session[:user_id] = user.id
flash[:notice] = "Welcome!"
redirect_to bracks_path
else
flash[:error] = "Invalid Username or Password!"
redirect_to login_path
end
end

def destroy
session.clear
flash[:notice] = "Goodbye!"
redirect_to login_path
end

end
4 changes: 3 additions & 1 deletion app/views/layouts/_navbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
<li><%= link_to "Bike Rack Map", bracks_path %></li>
<li><a href="#">Add New Bike</a></li>
<% if current_user %>
<li><%= link_to "Log Out", logout_path %></li>
<li><%= link_to logout_path, method: :delete do %>
<span class="glyphicon glyphicon-log-out"></span> Log Out</a>
</li><% end %>
<% end %>
</ul>
</div>
Expand Down
20 changes: 20 additions & 0 deletions app/views/sessions/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<h1>Login</h1>

<div class="well">
<%= form_for :session, url: login_path do |f| %>
<div class="form-group">
<%= f.label :email %>
<%= f.text_field :email, class: 'form-control' %>
</div>

<div class="form-group">
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
</div>

<div class="form-group">
<%= f.submit "Log In", class: 'btn btn-primary' %>
</div>
<% end %>
<h5>Don't have an account? <%= link_to "Sign up by clicking here!", new_user_path %></h5>
</div>
21 changes: 21 additions & 0 deletions spec/features/users/user_logout_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'rails_helper'

feature "user can log out" do
let(:user) {create(:user)}
context "as a logged in user" do
scenario "user is logged in and clicks logout" do
visit login_path

within ".well" do
fill_in 'session[email]', with: user.email
fill_in 'session[password]', with: user.password
click_on "Log In"
end

click_on "Log Out"
expect(page).to have_content("Goodbye!")
expect(page).to have_content("Log In")
expect(page).to_not have_content("Log Out")
end
end
end
94 changes: 94 additions & 0 deletions spec/features/users/user_sign_in_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
require 'rails_helper'

feature "login process" do
let(:user) {create(:user)}
context "as an unauthenticated user" do
scenario "can navigate to login path" do
visit root_path

click_on "Log In"

expect(current_path).to eq(login_path)
end
scenario "can login with email and password" do
visit login_path

within ".well" do
fill_in "session[email]", with: user.email
fill_in "session[password]", with: user.password
click_on "Log In"
end

expect(page).to have_content("Welcome!")
expect(current_path).to eq(bracks_path)
expect(page).to have_content("Log Out")

expect(page).to_not have_content "Sign Up"
expect(page).to_not have_content "Log In"
end
scenario "they provide invalid email" do
visit login_path

within ".well" do
fill_in "session[email]", with: "[email protected]"
fill_in "session[password]", with: user.password
click_button "Log In"
end

expect(current_path).to eq(login_path)
expect(page).to have_content("Invalid Username or Password!")

expect(page).to_not have_content "Log Out"
expect(page).to have_content "Sign Up"
expect(page).to have_content "Log In"
end
scenario "they provide invalid password" do
visit login_path

within ".well" do
fill_in "session[email]", with: user.email
fill_in "session[password]", with: "blah"
click_on "Log In"
end

expect(current_path).to eq(login_path)
expect(page).to have_content("Invalid Username or Password!")

expect(page).to_not have_content "Log Out"
expect(page).to have_content "Sign Up"
expect(page).to have_content "Log In"
end
scenario "they provide blank email" do
visit login_path

within ".well" do
fill_in "session[email]", with: ""
fill_in "session[password]", with: user.password
click_button "Log In"
end

expect(current_path).to eq(login_path)
expect(page).to have_content("Invalid Username or Password!")

expect(page).to_not have_content "Log Out"
expect(page).to have_content "Sign Up"
expect(page).to have_content "Log In"
end
scenario "they provide blank password" do
visit login_path

within ".well" do
fill_in "session[email]", with: user.email
fill_in "session[password]", with: ""
click_button "Log In"
end

expect(current_path).to eq(login_path)
expect(page).to have_content("Invalid Username or Password!")

expect(page).to_not have_content "Log Out"
expect(page).to have_content "Sign Up"
expect(page).to have_content "Log In"
end
end
end

0 comments on commit bec5433

Please sign in to comment.