简体   繁体   English

测试Auth0在Rails中使用RSpec登录

[英]Test Auth0 Login with RSpec in Rails

I'm using Auth0 for authentication in my rails app. 我在我的rails应用程序中使用Auth0进行身份验证。 I need to write some feature tests for login and signup. 我需要为登录和注册编写一些功能测试。 I can't seem to find something concrete on how to do this with rspec and capybara. 我似乎无法找到关于如何使用rspec和capybara进行具体操作的具体内容。

Tried doing something along the lines explained in this gist but it still doesn't work. 尝试按照这个要点中解释的方式做一些事情,但它仍然无效。 If someone has had experience with rspec feature tests with Auth0 I'd appreciate if you would guide me in the right direction. 如果有人有使用Auth0进行rspec功能测试的经验,我会很感激,如果你能指导我正确的方向。

Thanks! 谢谢!

My configuration 我的配置

# in spec/support/omniauth_macros.rb
module OmniauthMacros
  def mock_auth_hash
    # The mock_auth configuration allows you to set per-provider (or default)
    # authentication hashes to return during integration testing.
    OmniAuth.config.mock_auth[:auth0] = {
      'provider' => 'auth0',
      'uid' => '123545',
      'user_info' => {
        'name' => 'mockuser',
        'image' => 'mock_user_thumbnail_url'
      },
      'credentials' => {
        'token' => 'mock_token',
        'secret' => 'mock_secret'
      }
    }
  end
end

# in spec/requests/spec_helper.rb
RSpec.configure do |config|
  # ...
  # include our macro
  config.include(OmniauthMacros)
end

OmniAuth.config.test_mode = true

Then in my spec I have 然后在我的规格中我有

   scenario 'Should successfully login user' do
       visit login_path

       mock_auth_hash
       click_link "Sign in"

      expect(page).to have_content('Signed in successfully')
      expect(page).to have_link("Logout", href: logout_path)
    end

This is how I solved it 这就是我解决它的方式

# in spec/support/omniauth_macros.rb
module OmniauthMacros
  def mock_valid_auth_hash(user)
    # The mock_auth configuration allows you to set per-provider (or default)
    # authentication hashes to return during integration testing.
    opts = {
        "provider": user.provider,
        "uid": user.uid,
        "info": {
            "email": user.email,
            "first_name": user.first_name,
            "last_name": user.last_name,
        },
        "credentials": {
            "token": "XKLjnkKJj7hkHKJkk",
            "expires": true,
            "id_token": "eyJ0eXAiOiJK1VveHkwaTFBNXdTek41dXAiL.Wz8bwniRJLQ4Fqx_omnGDCX1vrhHjzw",
            "token_type": "Bearer"
        }
    }
    OmniAuth.config.mock_auth[:auth0] =  OmniAuth::AuthHash.new(opts)
  end

  def mock_invalid_auth_hash
    OmniAuth.config.mock_auth[:auth0] = :invalid_credentials
  end
end

# in spec/support/features/session_helpers.rb
module Features
  module SessionHelpers

    def log_in(user, invalid=false, strategy = :auth0)
      invalid ?  mock_invalid_auth_hash : mock_valid_auth_hash(user)
      Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[strategy.to_sym]
      visit "/auth/#{strategy.to_s}/callback?code=vihipkGaumc5IVgs"
    end
  end
end

# in spec/support/rails_helper.rb
RSpec.configure do |config|
  # ...
  # include our macro
  config.include(OmniauthMacros)

  # include Session helpers
  config.include Features::SessionHelpers, type: :feature
end

And finally my feature test to simulate login looks like 最后我的模拟登录的功能测试看起来像

# user signin feature test

require 'rails_helper'

feature 'Login with Omniauth' do
  given(:user) { create(:user) }

  context "With valid credentials" do
    scenario 'Should successfully login user' do
      log_in(user)

      expect(page).to have_content("successfully signed in")
      expect(page).to have_link("Logout", href: logout_path)
    end
  end

  context "With invalid credentials" do
    scenario "Should not log in user" do
      log_in(user, true)
      expect(page).to have_content('invalid_credentials')
    end
  end
end

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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