Skip to content

Commit

Permalink
Merge pull request #18 from tamu-edu-students/oauth_tests
Browse files Browse the repository at this point in the history
Test login logout and cleanup
  • Loading branch information
navyapriyanandi-tamu authored Oct 4, 2024
2 parents 4b04f9b + 697daa9 commit ada74b0
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@

# Ignore master key for decrypting credentials and more.
/config/master.key

/coverage
4 changes: 0 additions & 4 deletions app/channels/application_cable/channel.rb

This file was deleted.

4 changes: 0 additions & 4 deletions app/channels/application_cable/connection.rb

This file was deleted.

7 changes: 0 additions & 7 deletions app/jobs/application_job.rb

This file was deleted.

4 changes: 0 additions & 4 deletions app/mailers/application_mailer.rb

This file was deleted.

17 changes: 17 additions & 0 deletions features/support/omniauth.rb
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
}
})
55 changes: 55 additions & 0 deletions spec/controllers/application_controller_spec.rb
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
68 changes: 68 additions & 0 deletions spec/controllers/sessions_controller_spec.rb
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
34 changes: 34 additions & 0 deletions spec/controllers/welcome_controller_spec.rb
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

0 comments on commit ada74b0

Please sign in to comment.