-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from tamu-edu-students/oauth_tests
Test login logout and cleanup
- Loading branch information
Showing
9 changed files
with
176 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,3 +33,5 @@ | |
|
||
# Ignore master key for decrypting credentials and more. | ||
/config/master.key | ||
|
||
/coverage |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Mock the OAuth authentication, since we don't want to use an actual email/password | ||
|
||
OmniAuth.config.test_mode = true | ||
|
||
OmniAuth.config.mock_auth[:google_oauth2] = OmniAuth::AuthHash.new({ | ||
provider: 'google_oauth2', | ||
uid: '12345', | ||
info: { | ||
name: 'John Doe', | ||
email: '[email protected]' | ||
}, | ||
credentials: { | ||
token: 'mock_token', | ||
refresh_token: 'mock_refresh_token', | ||
expires_at: Time.now + 1.week | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# frozen_string_literal: true | ||
|
||
# spec/controllers/application_controller_spec.rb | ||
require 'rails_helper' | ||
|
||
RSpec.describe ApplicationController, type: :controller do | ||
describe '#current_user' do | ||
context 'when session contains user_id' do | ||
before do | ||
@user = User.create!(uid: '12345', provider: 'google_oauth2', email: '[email protected]', first_name: 'John', | ||
last_name: 'Doe') | ||
session[:user_id] = @user.id | ||
puts "session[:user_id] = #{session[:user_id]}" | ||
end | ||
|
||
it 'returns the current user' do | ||
expect(controller.send(:current_user)).to eq(@user) | ||
end | ||
end | ||
|
||
context 'when session does not contain user_id' do | ||
before do | ||
session[:user_id] = nil | ||
end | ||
|
||
it 'returns nil' do | ||
expect(controller.send(:current_user)).to be_nil | ||
end | ||
end | ||
end | ||
|
||
describe '#logged_in?' do | ||
context 'when user is logged in' do | ||
before do | ||
@user = User.create!(uid: '12345', provider: 'google_oauth2', email: '[email protected]', first_name: 'John', | ||
last_name: 'Doe') | ||
session[:user_id] = @user.id | ||
end | ||
|
||
it 'returns the logged in user' do | ||
expect(controller.send(:logged_in?)).to eq(@user) | ||
end | ||
end | ||
|
||
context 'when user is not logged in' do | ||
before do | ||
session[:user_id] = nil | ||
end | ||
|
||
it 'returns nil' do | ||
expect(controller.send(:logged_in?)).to be nil | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# frozen_string_literal: true | ||
|
||
# spec/controllers/sessions_controller_spec.rb | ||
require 'rails_helper' | ||
|
||
RSpec.describe SessionsController, type: :controller do | ||
# before(:all) do | ||
# @user = User.create!( | ||
# uid: '12345', | ||
# provider: 'google_oauth2', | ||
# email: '[email protected]', | ||
# first_name: 'John', last_name: 'Doe' | ||
# ) | ||
# end | ||
# after(:all) do | ||
# User.destroy_all | ||
# end | ||
describe 'GET #logout' do | ||
before do | ||
@user = User.create!(uid: '12345', provider: 'google_oauth2', email: '[email protected]', first_name: 'John', | ||
last_name: 'Doe') | ||
session[:user_id] = @user.id | ||
end | ||
it 'resets the session' do | ||
get :logout | ||
expect(session[:user_id]).to be_nil | ||
end | ||
after do | ||
User.destroy_all | ||
end | ||
|
||
it 'redirects to the welcome path with a notice' do | ||
get :logout | ||
expect(response).to redirect_to(welcome_path) | ||
expect(flash[:notice]).to eq('You are logged out') | ||
end | ||
end | ||
describe 'GET #omniauth' do | ||
let(:auth_hash) do | ||
{ | ||
'uid' => '12345', 'provider' => 'google_oauth2', | ||
'info' => { 'email' => '[email protected]', 'name' => 'John Doe' } | ||
} | ||
end | ||
before do | ||
request.env['omniauth.auth'] = OmniAuth::AuthHash.new(auth_hash) | ||
end | ||
it 'finds or creates a user and sets the session user_id' do | ||
expect { get :omniauth }.to change(User, :count).by(1) | ||
user = User.last | ||
expect(user.uid).to eq('12345') | ||
expect(user.email).to eq('[email protected]') | ||
expect(user.first_name).to eq('John') | ||
expect(user.last_name).to eq('Doe') | ||
end | ||
|
||
it 'redirects to the root path' do | ||
get :omniauth | ||
expect(response).to redirect_to(user_path(User.last)) | ||
end | ||
|
||
it 'sets a flash notice' do | ||
get :omniauth | ||
expect(flash[:notice]).to eq('You are logged in') | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
# spec/controllers/welcome_controller_spec.rb | ||
require 'rails_helper' | ||
|
||
RSpec.describe WelcomeController, type: :controller do | ||
describe 'GET #index' do | ||
context 'when user is logged in' do | ||
before do | ||
@user = User.create!(uid: '12345', provider: 'google_oauth2', email: '[email protected]', first_name: 'John', | ||
last_name: 'Doe') | ||
allow(controller).to receive(:logged_in?).and_return(true) | ||
controller.instance_variable_set(:@current_user, @user) | ||
end | ||
|
||
it 'redirects to the user path with a welcome notice' do | ||
get :index | ||
expect(response).to redirect_to(user_path(@user)) | ||
expect(flash[:notice]).to eq('Welcome back, John!') | ||
end | ||
end | ||
|
||
context 'when user is not logged in' do | ||
before do | ||
allow(controller).to receive(:logged_in?).and_return(false) | ||
end | ||
|
||
it 'renders the index template' do | ||
get :index | ||
expect(response).to render_template(:index) | ||
end | ||
end | ||
end | ||
end |