gpt4 book ai didi

ruby-on-rails - 在 Rails 中使用 RSpec 测试 Auth0 登录

转载 作者:太空宇宙 更新时间:2023-11-03 18:08:28 24 4
gpt4 key购买 nike

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

尝试按照此 gist 中解释的方式做一些事情但它仍然不起作用。如果有人有使用 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

然后在我的规范中我有

   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

最佳答案

我是这样解决的

# 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

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

# 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

希望这对您有所帮助!

关于ruby-on-rails - 在 Rails 中使用 RSpec 测试 Auth0 登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39229594/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com