在 Rails 中使用 RSpec 测试 Auth0 登录

Jos*_* N. 5 ruby authentication rspec ruby-on-rails auth0

我在 Rails 应用程序中使用Auth0进行身份验证。我需要编写一些用于登录和注册的功能测试。我似乎找不到关于如何使用 rspec 和 capybara 执行此操作的具体内容。

尝试按照本要点中解释的方式做一些事情,但它仍然不起作用。如果有人有使用 Auth0 进行 rspec 功能测试的经验,如果您能指导我正确的方向,我将不胜感激。

谢谢!

我的配置

# 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
Run Code Online (Sandbox Code Playgroud)

然后在我的规范中我有

   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
Run Code Online (Sandbox Code Playgroud)

Jos*_* N. 6

这就是我解决它的方法

# 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
Run Code Online (Sandbox Code Playgroud)

最后我模拟登录的功能测试看起来像

# 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
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!