Reputation: 150
I'm using OmniAuth through Devise in my Rails app. I'm trying to test that my callback method is being called properly and functions correctly. I am currently receiving an error when running my spec.
The error:
Failure/Error: get user_omniauth_authorize_path(:facebook)
ActionController::UrlGenerationError:
No route matches {:action=>"/users/auth/facebook", :controller=>"users/omniauth_callbacks"} missing required keys: [:action]
My spec:
#spec/controllers/users/omniauth_callbacks_controller_spec.rb
require 'rails_helper'
RSpec.describe Users::OmniauthCallbacksController, :type => :controller do
context 'get facebook' do
before do
request.env["devise.mapping"] = Devise.mappings[:user] # If using Devise
request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook]
end
it 'should create user, redirect to homepage, and create session' do
get user_omniauth_authorize_path(:facebook)
expect(response).to redirect_to(user_omniauth_callback_path)
end
end
end
Support file:
#spec/support/omniauth.rb
OmniAuth.config.test_mode = true
OmniAuth.config.mock_auth[:facebook] = OmniAuth::AuthHash.new({
:provider => 'facebook',
:uid => '123545',
:email => '[email protected]'
})
Controller:
#app/controllers/users/omniauth_callbacks_controller.rb
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
@user = User.from_omniauth(request.env['omniauth.auth'])
if @user.persisted?
sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
set_flash_message(:notice, :success, :kind => 'Facebook') if is_navigational_format? #todo what is this doing
else
session['devise.facebook_data'] = request.env['omniauth.auth']
redirect_to new_user_registration_url
end
end
end
Routes:
devise_for :users, :controllers => { :omniauth_callbacks => 'users/omniauth_callbacks' }
I think the issue is in how it's getting routed. I think action should just be 'facebook' not '/users/auth/facebook', but I don't know the right way to resolve this.
Upvotes: 4
Views: 435
Reputation: 2792
In case anyone stumbles on this looking for answers like I did. I had this problem when adding a second Omniauth strategy. Turns out the problem was I'd forgotton to include the strategy in my model declaration
eg I was already authorising with google
# app/models/user.rb
devise :rememberable, :trackable, :omniauthable, :omniauth_providers => [:google]
but then wanted to add a second provider (eg facebook). I forgot to add facebook to the list of omniauth providers and so was getting that error when my spec ran. I fixed it by changing to
# app/models/user.rb
devise :rememberable, :trackable, :omniauthable, :omniauth_providers => [:google,:facebook]
Upvotes: 1
Reputation: 43
I got the same error missing required keys: [:action]
as you. After I read RSpec document, I found that the argument of get
should be something like :index
(the action name). Because I defined:
# app/controllers/users/omniauth_callbacks_controller.rb
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
# ...
end
end
So I changed get user_omniauth_authorize_path(:facebook)
to get :facebook
and added some omniauth mocks. Now it pass!
Upvotes: 0