diff --git a/.gitignore b/.gitignore index 4aaf102..3feae55 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,5 @@ # Ignore master key for decrypting credentials and more. /config/master.key + +/coverage diff --git a/app/channels/application_cable/channel.rb b/app/channels/application_cable/channel.rb deleted file mode 100644 index d672697..0000000 --- a/app/channels/application_cable/channel.rb +++ /dev/null @@ -1,4 +0,0 @@ -module ApplicationCable - class Channel < ActionCable::Channel::Base - end -end diff --git a/app/channels/application_cable/connection.rb b/app/channels/application_cable/connection.rb deleted file mode 100644 index 0ff5442..0000000 --- a/app/channels/application_cable/connection.rb +++ /dev/null @@ -1,4 +0,0 @@ -module ApplicationCable - class Connection < ActionCable::Connection::Base - end -end diff --git a/app/jobs/application_job.rb b/app/jobs/application_job.rb deleted file mode 100644 index d394c3d..0000000 --- a/app/jobs/application_job.rb +++ /dev/null @@ -1,7 +0,0 @@ -class ApplicationJob < ActiveJob::Base - # Automatically retry jobs that encountered a deadlock - # retry_on ActiveRecord::Deadlocked - - # Most jobs are safe to ignore if the underlying records are no longer available - # discard_on ActiveJob::DeserializationError -end diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb deleted file mode 100644 index 3c34c81..0000000 --- a/app/mailers/application_mailer.rb +++ /dev/null @@ -1,4 +0,0 @@ -class ApplicationMailer < ActionMailer::Base - default from: "from@example.com" - layout "mailer" -end diff --git a/features/support/omniauth.rb b/features/support/omniauth.rb new file mode 100644 index 0000000..78f33c2 --- /dev/null +++ b/features/support/omniauth.rb @@ -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: 'johndoe@tamu.edu' + }, + credentials: { + token: 'mock_token', + refresh_token: 'mock_refresh_token', + expires_at: Time.now + 1.week + } +}) \ No newline at end of file diff --git a/spec/controllers/application_controller_spec.rb b/spec/controllers/application_controller_spec.rb new file mode 100644 index 0000000..0ba9eae --- /dev/null +++ b/spec/controllers/application_controller_spec.rb @@ -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: 'test@example.com', 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: 'test@example.com', 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 diff --git a/spec/controllers/sessions_controller_spec.rb b/spec/controllers/sessions_controller_spec.rb new file mode 100644 index 0000000..0e129f1 --- /dev/null +++ b/spec/controllers/sessions_controller_spec.rb @@ -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: 'test@example.com', + # 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: 'test@example.com', 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' => 'test@example.com', '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('test@example.com') + 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 diff --git a/spec/controllers/welcome_controller_spec.rb b/spec/controllers/welcome_controller_spec.rb new file mode 100644 index 0000000..6170480 --- /dev/null +++ b/spec/controllers/welcome_controller_spec.rb @@ -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: 'test@example.com', 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