0

omni_auth_helper.rb

module OmniAuthHelper
  def set_omniauth
    OmniAuth.config.test_mode = true
    OmniAuth.config.mock_auth[:google_oauth2] = OmniAuth::AuthHash.new({
        :provider => "google_oauth2",
        :uid => "123456789",
        :info => {
          :name => "Tony Stark",
          :email => "tony@stark.com"
        },
        :credentials => {
          :token => "token",
          :refresh_token => "refresh token"
        }
      }
    )
  end
end

user_controller.rb

class UsersController < ApplicationController
  before_action :check_logged_in

  def edit
    @user = User.find_by(session[:user_id])
  end

  def update
    @user = User.find_by(session[:user_id])
    if @user.update(user_params)
      redirect_to root_path
    else
      render :edit
    end
  end

  private

  def user_params
    params.require(:user).permit(:name, :email)
  end

  def check_logged_in
    return if current_user
    redirect_to root_path
  end
end

omniauth.rb

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :google_oauth2, ENV['GOOGLE_CLIENT_ID'], ENV['GOOGLE_CLIENT_SECRET']
end
OmniAuth.config.allowed_request_methods = [:get, :post]

users_spec.rb

require 'rails_helper'

RSpec.describe 'Users', type: :request do
  let!(:user) { FactoryBot.create(:user) } # name(string), email(string)

  before(:each) do
    OmniAuth.config.mock_auth[:google_oauth2] = nil
    Rails.application.env_config['omniauth.auth'] = set_omniauth
    # post '/auth/google_oauth2'
    # get '/auth/google_oauth2'
    post sessions_path
  end

  describe '#edit' do
    it 'returns view' do
      get user_edit_path
      expect(response).to have_http_status(:success)
# THIS FAILS BECAUSE OF session[:user_id] IS NOT EXISTS ON RSPEC TEST
    end
  end
end

routes.rb

Rails.application.routes.draw do
  root 'home#index'

  get 'auth/:provider/callback', to: 'sessions#create'
  get 'auth/failure', to: redirect('/'), as: 'auth_failure'
  get 'log_out', to: 'sessions#destroy', as: 'log_out'

  resources :sessions, only: [:create, :destroy]
  get 'user/edit', to: 'users#edit'
  patch 'user/edit', to: 'users#update'
end

link on html

      <%= link_to "log in", "/auth/google_oauth2", method: :post %>

sessions_helper

module SessionsHelper
  def current_user
    return unless (user_id = session[:user_id])
    @current_user ||= User.find_by(id: user_id)
  end

  def log_in(user)
    session[:user_id] = user.id
  end

  def log_out
    session.delete(:user_id)
    @current_user = nil
  end
end

sessions_controller

class SessionsController < ApplicationController
  def create
    if (user = User.find_or_create_from_auth_hash(auth_hash))
      log_in user
    end
    redirect_to root_path
  end

  def destroy
    log_out
    redirect_to root_path
  end

  private

  def auth_hash
    request.env['omniauth.auth']
  end
end

I implemented Google login using omniuth-google-oauth2 in Rails.

I also want to write the test with rspec (request), but I can't access user#edit or user#update on rspec test because I don't have a session[:user_id] at the moment.

I think I need the session[:user_id] information to access edit, update, what should I do in rspec?

=================

I've tried these three things,->

post '/auth/google_oauth2'

get '/auth/google_oauth2'

post sessions_path

but all of them make this error

 ArgumentError:
   wrong number of arguments (given 3, expected 2)

==================

I also tried to click_link 'login' with capybara, but I got an error that I couldn't find the login link.

When using the google login feature, how do I get session[:user_id] information on rspec (not using devise) so I can access user#edit or user#update without getting caught in the rspec test check_logged_in?

If anyone knows the answer, can you help me with this?

Full error message :

Failures:

  1) Users #edit returns view
     Failure/Error: post sessions_path
     
     ArgumentError:
       wrong number of arguments (given 3, expected 2)
     # /usr/local/bundle/gems/mock_redis-0.36.0/lib/mock_redis/string_methods.rb:215:in `set'
     # /usr/local/bundle/gems/mock_redis-0.36.0/lib/mock_redis/multi_db_wrapper.rb:21:in `method_missing'
     # /usr/local/bundle/gems/mock_redis-0.36.0/lib/mock_redis/expire_wrapper.rb:17:in `method_missing'
     # /usr/local/bundle/gems/mock_redis-0.36.0/lib/mock_redis/transaction_wrapper.rb:30:in `method_missing'
     # /usr/local/bundle/gems/mock_redis-0.36.0/lib/mock_redis/pipelined_wrapper.rb:27:in `method_missing'
     # /usr/local/bundle/gems/mock_redis-0.36.0/lib/mock_redis.rb:99:in `block in method_missing'
     # /usr/local/bundle/gems/mock_redis-0.36.0/lib/mock_redis.rb:155:in `logging'
     # /usr/local/bundle/gems/mock_redis-0.36.0/lib/mock_redis.rb:98:in `method_missing'
     # /usr/local/bundle/gems/redis-rack-2.1.4/lib/rack/session/redis.rb:49:in `block (2 levels) in write_session'
     # /usr/local/bundle/gems/redis-rack-2.1.4/lib/redis/rack/connection.rb:24:in `with'
     # /usr/local/bundle/gems/redis-rack-2.1.4/lib/rack/session/redis.rb:82:in `with'
     # /usr/local/bundle/gems/redis-rack-2.1.4/lib/rack/session/redis.rb:49:in `block in write_session'
     # /usr/local/bundle/gems/redis-rack-2.1.4/lib/rack/session/redis.rb:70:in `with_lock'
     # /usr/local/bundle/gems/redis-rack-2.1.4/lib/rack/session/redis.rb:48:in `write_session'
     # /usr/local/bundle/gems/rack-2.2.7/lib/rack/session/abstract/id.rb:388:in `commit_session'
     # /usr/local/bundle/gems/rack-2.2.7/lib/rack/session/abstract/id.rb:268:in `context'
     # /usr/local/bundle/gems/rack-2.2.7/lib/rack/session/abstract/id.rb:260:in `call'
     # /usr/local/bundle/gems/rack-dev-mark-0.7.10/lib/rack/dev-mark/middleware.rb:19:in `call'
     # /usr/local/bundle/gems/railties-7.0.4.3/lib/rails/rack/logger.rb:40:in `call_app'
     # /usr/local/bundle/gems/railties-7.0.4.3/lib/rails/rack/logger.rb:25:in `block in call'
     # /usr/local/bundle/gems/railties-7.0.4.3/lib/rails/rack/logger.rb:25:in `call'
     # /usr/local/bundle/gems/request_store-1.5.1/lib/request_store/middleware.rb:19:in `call'
     # /usr/local/bundle/gems/rack-2.2.7/lib/rack/method_override.rb:24:in `call'
     # /usr/local/bundle/gems/rack-2.2.7/lib/rack/runtime.rb:22:in `call'
     # /usr/local/bundle/gems/rack-2.2.7/lib/rack/sendfile.rb:110:in `call'
     # /usr/local/bundle/gems/railties-7.0.4.3/lib/rails/engine.rb:530:in `call'
     # /usr/local/bundle/gems/rack-test-2.1.0/lib/rack/test.rb:360:in `process_request'
     # /usr/local/bundle/gems/rack-test-2.1.0/lib/rack/test.rb:153:in `request'
     # ./spec/requests/users_spec.rb:11:in `block (2 levels) in <main>'

  2) Users #update success with valid information
     Failure/Error: post sessions_path
     
     ArgumentError:
       wrong number of arguments (given 3, expected 2)
     # /usr/local/bundle/gems/mock_redis-0.36.0/lib/mock_redis/string_methods.rb:215:in `set'
     # /usr/local/bundle/gems/mock_redis-0.36.0/lib/mock_redis/multi_db_wrapper.rb:21:in `method_missing'
     # /usr/local/bundle/gems/mock_redis-0.36.0/lib/mock_redis/expire_wrapper.rb:17:in `method_missing'
     # /usr/local/bundle/gems/mock_redis-0.36.0/lib/mock_redis/transaction_wrapper.rb:30:in `method_missing'
     # /usr/local/bundle/gems/mock_redis-0.36.0/lib/mock_redis/pipelined_wrapper.rb:27:in `method_missing'
     # /usr/local/bundle/gems/mock_redis-0.36.0/lib/mock_redis.rb:99:in `block in method_missing'
     # /usr/local/bundle/gems/mock_redis-0.36.0/lib/mock_redis.rb:155:in `logging'
     # /usr/local/bundle/gems/mock_redis-0.36.0/lib/mock_redis.rb:98:in `method_missing'
     # /usr/local/bundle/gems/redis-rack-2.1.4/lib/rack/session/redis.rb:49:in `block (2 levels) in write_session'
     # /usr/local/bundle/gems/redis-rack-2.1.4/lib/redis/rack/connection.rb:24:in `with'
     # /usr/local/bundle/gems/redis-rack-2.1.4/lib/rack/session/redis.rb:82:in `with'
     # /usr/local/bundle/gems/redis-rack-2.1.4/lib/rack/session/redis.rb:49:in `block in write_session'
     # /usr/local/bundle/gems/redis-rack-2.1.4/lib/rack/session/redis.rb:70:in `with_lock'
     # /usr/local/bundle/gems/redis-rack-2.1.4/lib/rack/session/redis.rb:48:in `write_session'
     # /usr/local/bundle/gems/rack-2.2.7/lib/rack/session/abstract/id.rb:388:in `commit_session'
     # /usr/local/bundle/gems/rack-2.2.7/lib/rack/session/abstract/id.rb:268:in `context'
     # /usr/local/bundle/gems/rack-2.2.7/lib/rack/session/abstract/id.rb:260:in `call'
     # /usr/local/bundle/gems/rack-dev-mark-0.7.10/lib/rack/dev-mark/middleware.rb:19:in `call'
     # /usr/local/bundle/gems/railties-7.0.4.3/lib/rails/rack/logger.rb:40:in `call_app'
     # /usr/local/bundle/gems/railties-7.0.4.3/lib/rails/rack/logger.rb:25:in `block in call'
     # /usr/local/bundle/gems/railties-7.0.4.3/lib/rails/rack/logger.rb:25:in `call'
     # /usr/local/bundle/gems/request_store-1.5.1/lib/request_store/middleware.rb:19:in `call'
     # /usr/local/bundle/gems/rack-2.2.7/lib/rack/method_override.rb:24:in `call'
     # /usr/local/bundle/gems/rack-2.2.7/lib/rack/runtime.rb:22:in `call'
     # /usr/local/bundle/gems/rack-2.2.7/lib/rack/sendfile.rb:110:in `call'
     # /usr/local/bundle/gems/railties-7.0.4.3/lib/rails/engine.rb:530:in `call'
     # /usr/local/bundle/gems/rack-test-2.1.0/lib/rack/test.rb:360:in `process_request'
     # /usr/local/bundle/gems/rack-test-2.1.0/lib/rack/test.rb:153:in `request'
     # ./spec/requests/users_spec.rb:11:in `block (2 levels) in <main>'

  3) Users #update success with valid information
     Failure/Error: post sessions_path
     
     ArgumentError:
       wrong number of arguments (given 3, expected 2)
     # /usr/local/bundle/gems/mock_redis-0.36.0/lib/mock_redis/string_methods.rb:215:in `set'
     # /usr/local/bundle/gems/mock_redis-0.36.0/lib/mock_redis/multi_db_wrapper.rb:21:in `method_missing'
     # /usr/local/bundle/gems/mock_redis-0.36.0/lib/mock_redis/expire_wrapper.rb:17:in `method_missing'
     # /usr/local/bundle/gems/mock_redis-0.36.0/lib/mock_redis/transaction_wrapper.rb:30:in `method_missing'
     # /usr/local/bundle/gems/mock_redis-0.36.0/lib/mock_redis/pipelined_wrapper.rb:27:in `method_missing'
     # /usr/local/bundle/gems/mock_redis-0.36.0/lib/mock_redis.rb:99:in `block in method_missing'
     # /usr/local/bundle/gems/mock_redis-0.36.0/lib/mock_redis.rb:155:in `logging'
     # /usr/local/bundle/gems/mock_redis-0.36.0/lib/mock_redis.rb:98:in `method_missing'
     # /usr/local/bundle/gems/redis-rack-2.1.4/lib/rack/session/redis.rb:49:in `block (2 levels) in write_session'
     # /usr/local/bundle/gems/redis-rack-2.1.4/lib/redis/rack/connection.rb:24:in `with'
     # /usr/local/bundle/gems/redis-rack-2.1.4/lib/rack/session/redis.rb:82:in `with'
     # /usr/local/bundle/gems/redis-rack-2.1.4/lib/rack/session/redis.rb:49:in `block in write_session'
     # /usr/local/bundle/gems/redis-rack-2.1.4/lib/rack/session/redis.rb:70:in `with_lock'
     # /usr/local/bundle/gems/redis-rack-2.1.4/lib/rack/session/redis.rb:48:in `write_session'
     # /usr/local/bundle/gems/rack-2.2.7/lib/rack/session/abstract/id.rb:388:in `commit_session'
     # /usr/local/bundle/gems/rack-2.2.7/lib/rack/session/abstract/id.rb:268:in `context'
     # /usr/local/bundle/gems/rack-2.2.7/lib/rack/session/abstract/id.rb:260:in `call'
     # /usr/local/bundle/gems/rack-dev-mark-0.7.10/lib/rack/dev-mark/middleware.rb:19:in `call'
     # /usr/local/bundle/gems/railties-7.0.4.3/lib/rails/rack/logger.rb:40:in `call_app'
     # /usr/local/bundle/gems/railties-7.0.4.3/lib/rails/rack/logger.rb:25:in `block in call'
     # /usr/local/bundle/gems/railties-7.0.4.3/lib/rails/rack/logger.rb:25:in `call'
     # /usr/local/bundle/gems/request_store-1.5.1/lib/request_store/middleware.rb:19:in `call'
     # /usr/local/bundle/gems/rack-2.2.7/lib/rack/method_override.rb:24:in `call'
     # /usr/local/bundle/gems/rack-2.2.7/lib/rack/runtime.rb:22:in `call'
     # /usr/local/bundle/gems/rack-2.2.7/lib/rack/sendfile.rb:110:in `call'
     # /usr/local/bundle/gems/railties-7.0.4.3/lib/rails/engine.rb:530:in `call'
     # /usr/local/bundle/gems/rack-test-2.1.0/lib/rack/test.rb:360:in `process_request'
     # /usr/local/bundle/gems/rack-test-2.1.0/lib/rack/test.rb:153:in `request'
     # ./spec/requests/users_spec.rb:11:in `block (2 levels) in <main>'

Finished in 0.08161 seconds (files took 1.49 seconds to load)
10 examples, 3 failures

Failed examples:

rspec ./spec/requests/users_spec.rb:15 # Users #edit returns view
rspec ./spec/requests/users_spec.rb:22 # Users #update success with valid information
rspec ./spec/requests/users_spec.rb:37 # Users #update success with valid information
sksmsWKd
  • 155
  • 2
  • 12

1 Answers1

0

There seems to be an incompatibility of the redis-mock gem and Ruby 3.2.

There is an open issue on GitHub about it, that describes failing on the same line with the same error message. When you really depend on that gem and cannot replace it, then you might want to consider downgrading to Ruby 3.1 temporarily and wait for an updated and fixed version of the gem.

spickermann
  • 100,941
  • 9
  • 101
  • 131