简体   繁体   中英

Request spec with devise_token_auth + omniauth

I'm currently writing an API with Rails 5 using devise_token_auth and omniauth as authentication strategy.

To check if the omniauth authentication works, I wrote a request spec:

OmniAuth.config.test_mode = true
OmniAuth.config.mock_auth[:facebook] = OmniAuth::AuthHash.new(
  {
      provider: 'facebook',
      uid: '123456'
  })
Rails.application.env_config["devise.mapping"] = Devise.mappings[:user]
Rails.application.env_config["omniauth.auth"]  = OmniAuth.config.mock_auth[:facebook]
get '/omniauth/facebook/callback'
expect(response).to have_http_status(:ok)

When I run the test I have to following error:

NoMethodError: undefined method `[]' for nil:NilClass
/Users/gaetan/.rvm/gems/ruby-2.3.1/gems/devise_token_auth-0.1.39/app/controllers/devise_token_auth/omniauth_callbacks_controller.rb:14:in `redirect_callbacks'

Here is the line 14:

devise_mapping = [request.env['omniauth.params']['namespace_name'],

So I guess that's because omniauth.params is nil . Does it mean that I also have to mock the params? How?

Controller

(ex: Users::OmniauthCallbacksController)

def omniauth_params
  request.env['omniauth.params']
end

Spec

allow_any_instance_of(Users::OmniauthCallbacksController).to(receive(:omniauth_params).and_return('namespace_name' => 'namespace_name'))

or

allow_any_instance_of(Users::OmniauthCallbacksController).to(receive(:omniauth_params).and_return(OmniAuth::AuthHash.new({:namespace_name => 'namespace_name'})))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM