require 'rails_helper'
describe 'authentication' do
before do
set_omniauth
Rails.application.env_config["devise.mapping"] = Devise.mappings[:user]
Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:twitterfacebook]
visit '/users/auth/facebook?user_type=host'
end
it 'Facebookから承認OK' do
expect(page).to have_content 'Facebook から承認されました。'
end
end
追記
バージョン情報
rails (4.1.6)
rspec (3.3.0)
omniauth (1.2.2)
omniauth-facebook (2.0.0)
omniauth-oauth2 (1.2.0)
devise (3.4.1)
app/controllers/users/omniauth_callbacks_controller.rb
class Users::OmniauthCallbacksController <
Devise::OmniauthCallbacksController
def facebook
user_type = request.env["omniauth.params"]["user_type"]
auth_info = request.env["omniauth.auth"]
@user = User.find_for_facebook_oauth(auth_info, user_type)
# 略
end
end
app/models/user.rb
def self.find_for_facebook_oauth(auth, user_type)
# 略
User.new(provider: auth.provider, uid: auth.uid,
token: auth.credentials.token, authable_type: authable_type)
end