Skip to content

Commit

Permalink
Test login and logout
Browse files Browse the repository at this point in the history
  • Loading branch information
tallenaz committed Apr 5, 2016
1 parent 774e610 commit 4a0af0a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 3 deletions.
14 changes: 14 additions & 0 deletions app/controllers/authentication_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
###
# Controller for handling login/logout and redirecting with flash messages.
###
class AuthenticationController < ApplicationController
def login
flash[:success] = 'You have been successfully logged in.'
redirect_to params[:referrer] || :back
end

def logout
flash[:notice] = 'You have been successfully logged out.'
redirect_to params[:referrer] || :back
end
end
1 change: 1 addition & 0 deletions app/views/batch_record_updates/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<div class="home-page-section">
<h1>Batch Record Updates</h1>
<%= request.env.inspect %>
<% if current_user %>
<h3>What would you like to do?</h3>
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
Expand Down
2 changes: 1 addition & 1 deletion app/views/home/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@
<%= main_menu_button %>
</div>
<% else %>
<%= link_to "Login here", root_path %>
<%= link_to "Login here", login_path(referrer: request.original_url) %>
<% end %>
</div>
4 changes: 2 additions & 2 deletions app/views/shared/_top_navbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

<div class="header-links">
<% if current_user %>
<%= link_to "#{current_user}: Logout", referrer: root_path %>
<%= link_to "#{current_user}: Logout", logout_path(referrer: root_path) %>
<% else %>
<%= link_to "Login here", root_path %>
<%= link_to "Login here", login_path(referrer: request.original_url) %>
<% end %>
</div>

Expand Down
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

get 'home/index'

get 'webauth/login' => 'authentication#login', as: :login
get 'webauth/logout' => 'authentication#logout', as: :logout

get 'batch_record_updates' => 'batch_record_updates#index'

get 'transfer_request_form' => 'batch_record_updates#transfer_request_form'
Expand Down
35 changes: 35 additions & 0 deletions spec/controllers/authentication_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'rails_helper'

describe AuthenticationController do
before do
request.env['HTTP_REFERER'] = 'https://example.com'
end
describe 'login' do
it 'should redirect back to the provided referrer' do
get :login, referrer: '/'
expect(response).to redirect_to('/')
end
it 'should redirect back when there is no provided referrer' do
get :login
expect(response).to redirect_to('https://example.com')
end
it 'should have a flash success message informing the user they logged in' do
get :login
expect(flash[:success]).to eq 'You have been successfully logged in.'
end
end
describe 'logout' do
it 'should redirect back to the provided referrer' do
get :logout, referrer: '/'
expect(response).to redirect_to('/')
end
it 'should redirect back when there is no provided referrer' do
get :logout
expect(response).to redirect_to('https://example.com')
end
it 'should have a flash notice message informing the user they logged out' do
get :logout
expect(flash[:notice]).to eq 'You have been successfully logged out.'
end
end
end

0 comments on commit 4a0af0a

Please sign in to comment.