Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
helper_method :current_user
def authenticate
redirect_to :login unless user_signed_in?
end
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
# @current_user || @current_user = User.find(session[:user_id]) if session[:user_id]
end
def user_signed_in?
# converts current_user to a boolean by negating the negation
!!current_user
end
end
class GroupsController < ApplicationController
before_action :authenticate
# clipped for brevity
end
require 'rails_helper'
RSpec.describe GroupsController, type: :controller do
describe 'GET #show' do
context 'when user logged in' do
before do
Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:google_oauth2]
end
it "responds with HTTP 200 and a group object" do
get :show, params: {id: 1}, session: {user_id: 1} # ADDING SESSION HASH WAS THE FIX!
expect(response).to have_http_status(200)
end
end
context 'when user logged out' do
end
end
end
Rails.application.routes.draw do
get 'login', to: redirect('/auth/google_oauth2'), as: 'login'
get 'logout', to: 'sessions#destroy', as: 'logout'
get 'auth/:provider/callback', to: 'sessions#create'
get 'auth/failure', to: redirect('/')
get '/users/:id/groups', to: 'users#groups'
resources :groups, only: [:show, :create]
end
# config/environments/test.rb
# Mock user account for OmniAuth integration testing
OmniAuth.config.test_mode = true
OmniAuth.config.mock_auth[:google_oauth2] = OmniAuth::AuthHash.new({
'provider': 'google_oauth2',
'uid': '1',
# etc.
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment