How to Test Facebook Login Action with Rails, Omniauth and Rspec

freddyrangel

I asked a similar question before but I think I've gotten past my original error. Anyway I have a new fun failure that I'm having a blast trying to figure out (note the sarcasm). Here's my failure:

1) SessionsController#facebook_login should be valid
   Failure/Error: get :facebook_login
   NoMethodError:
     undefined method `slice' for nil:NilClass
   # ./app/models/user.rb:19:in `from_omniauth'
   # ./app/controllers/sessions_controller.rb:22:in `facebook_login'
   # ./spec/controllers/sessions_controller_spec.rb:96:in `block (3 levels) in <top (required)>'

sessions_controller_spec.rb

describe '#facebook_login' do

  before(:each) do
    valid_facebook_login_setup
    request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook]
    get :facebook_login
  end

  it "should be valid" do
    expect(response).to be_success
  end

  it "should set user_id" do
    expect(session[:user_id]).to be_true
  end
end

sessions_controller.rb

def facebook_login
  if request.env['omniauth.auth']
    user = User.from_omniauth(env['omniauth.auth'])
    session[:user_id] = user.id
    redirect_back_or root_path
  else
    redirect_to root_path
  end
end

omniauth_test_helper.rb

module OmniAuthTestHelper
  def valid_facebook_login_setup
    if Rails.env.test?
      OmniAuth.config.test_mode = true
      OmniAuth.config.mock_auth[:facebook] = OmniAuth::AuthHash.new({
        provider: 'facebook',
        uid: '123545',
        info: {
          first_name: "Andrea",
          last_name:  "Del Rio",
          email:      "[email protected]"
        },
        credentials: {
          token: "123456",
          expires_at: Time.now + 1.week
        }
      })
    end
  end

  def facebook_login_failure
    OmniAuth.config.mock_auth[:facebook] = :invalid_credentials
  end
end

spec_helper.rb

RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods
  config.include Capybara::DSL
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true
  config.infer_base_class_for_anonymous_controllers = false
  config.order = "random"
  config.include SessionTestHelper, type: :controller
  config.include OmniAuthTestHelper, type: :controller
end

user.rb

class User < ActiveRecord::Base

  def self.from_omniauth(auth)
    where(auth.slice(:provider, :uid)).first_or_create do |user|
      user.provider = auth.provider
      user.uid = auth.uid
      user.first_name = auth.info.first_name
      user.last_name = auth.info.last_name
      user.email = auth.info.email
      user.password = auth.credentials.token
      user.password_confirmation = auth.credentials.token
      user.oauth_token = auth.credentials.token
      user.oauth_expires_at = Time.at(auth.credentials.expires_at)
    end
  end
end

Any help would be really cool. Thanks guys!

freddyrangel

Ok I left these tests pending but I finally came around to figuring this out. First, because it's a callback, it shouldn't be a controller test. It should be a request spec. So we're going to test that "/auth/facebook/callback" when given a mock will login a user.

spec/requests/user_sessions_request_spec.rb

require 'spec_helper'

describe "GET '/auth/facebook/callback'" do

  before(:each) do
    valid_facebook_login_setup
    get "auth/facebook/callback"
    request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook]
  end

  it "should set user_id" do
    expect(session[:user_id]).to eq(User.last.id)
  end

  it "should redirect to root" do
    expect(response).to redirect_to root_path
  end
end

describe "GET '/auth/failure'" do

  it "should redirect to root" do
    get "/auth/failure"
    expect(response).to redirect_to root_path
  end
end

Here's the rspec helper

spec/support/omni_auth_test_helper

module OmniAuthTestHelper
  def valid_facebook_login_setup
    if Rails.env.test?
      OmniAuth.config.test_mode = true
      OmniAuth.config.mock_auth[:facebook] = OmniAuth::AuthHash.new({
        provider: 'facebook',
        uid: '123545',
        info: {
          first_name: "Gaius",
          last_name:  "Baltar",
          email:      "[email protected]"
        },
        credentials: {
          token: "123456",
          expires_at: Time.now + 1.week
        },
        extra: {
          raw_info: {
            gender: 'male'
          }
        }
      })
    end
  end
end

Don't forget to include the module in your spec_helper

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何使用Rails,Omniauth和Rspec测试Facebook登录操作

来自分类Dev

从omniauth保存用户的Facebook数据

来自分类Dev

Omniauth Facebook-获取朋友

来自分类Dev

从Omniauth Facebook访问信息

来自分类Dev

RSpec输出中的Rails omniauth错误

来自分类Dev

How to run a single test in guard for rspec?

来自分类Dev

Rails 4 and Rspec - generate controller test

来自分类Dev

Rails 4.1.5 omniauth strong parameters

来自分类Dev

Rails omniauth facebook SSL握手失败

来自分类Dev

Rails设计了omniauth-facebook .persisted吗?返回假

来自分类Dev

设计Omniauth-facebook旁路Rails验证

来自分类Dev

Rails-Omniauth-facebook App_id必需错误

来自分类Dev

Rails ActiveModel :: ForbiddenAttributesError设计,Omniauth

来自分类Dev

Rails中的omniauth-facebook验证错误

来自分类Dev

Rails 5,Devise,Omniauth,Twitter

来自分类Dev

在Ruby on Rails应用程序中具有omniauth-facebook的omniauth_callbacks_controller的未初始化常量用户

来自分类Dev

Rails OmniAuth Facebook“重定向URL必须是绝对的”

来自分类Dev

使用OmniAuth Facebook设计

来自分类Dev

How to test if else condition in rspec

来自分类Dev

尝试将omniauth facebook与rails集成并部署到heroku时无法登录

来自分类Dev

Rails omniauth-facebook生成随机电子邮件

来自分类Dev

获得(omniauth-facebook)和(omniauth-twitter)工作

来自分类Dev

在rails_env = test时Rspec无法正常工作

来自分类Dev

Rails设计专家omniauth

来自分类Dev

Rails omniauth facebook SSL握手失败

来自分类Dev

Rails 4 OmniAuth Facebook NoAuthorizationErrorCode

来自分类Dev

如何在Rails 4中使用Omniauth-facebook对用户进行身份验证

来自分类Dev

Facebook omniauth登录上的多个Rails应用程序路由出现问题

来自分类Dev

如何在Rails 4中保存Facebook omniauth的request.referrer

Related 相关文章

  1. 1

    如何使用Rails,Omniauth和Rspec测试Facebook登录操作

  2. 2

    从omniauth保存用户的Facebook数据

  3. 3

    Omniauth Facebook-获取朋友

  4. 4

    从Omniauth Facebook访问信息

  5. 5

    RSpec输出中的Rails omniauth错误

  6. 6

    How to run a single test in guard for rspec?

  7. 7

    Rails 4 and Rspec - generate controller test

  8. 8

    Rails 4.1.5 omniauth strong parameters

  9. 9

    Rails omniauth facebook SSL握手失败

  10. 10

    Rails设计了omniauth-facebook .persisted吗?返回假

  11. 11

    设计Omniauth-facebook旁路Rails验证

  12. 12

    Rails-Omniauth-facebook App_id必需错误

  13. 13

    Rails ActiveModel :: ForbiddenAttributesError设计,Omniauth

  14. 14

    Rails中的omniauth-facebook验证错误

  15. 15

    Rails 5,Devise,Omniauth,Twitter

  16. 16

    在Ruby on Rails应用程序中具有omniauth-facebook的omniauth_callbacks_controller的未初始化常量用户

  17. 17

    Rails OmniAuth Facebook“重定向URL必须是绝对的”

  18. 18

    使用OmniAuth Facebook设计

  19. 19

    How to test if else condition in rspec

  20. 20

    尝试将omniauth facebook与rails集成并部署到heroku时无法登录

  21. 21

    Rails omniauth-facebook生成随机电子邮件

  22. 22

    获得(omniauth-facebook)和(omniauth-twitter)工作

  23. 23

    在rails_env = test时Rspec无法正常工作

  24. 24

    Rails设计专家omniauth

  25. 25

    Rails omniauth facebook SSL握手失败

  26. 26

    Rails 4 OmniAuth Facebook NoAuthorizationErrorCode

  27. 27

    如何在Rails 4中使用Omniauth-facebook对用户进行身份验证

  28. 28

    Facebook omniauth登录上的多个Rails应用程序路由出现问题

  29. 29

    如何在Rails 4中保存Facebook omniauth的request.referrer

热门标签

归档