Rspec:让 Omniauth/oauth0 使用法拉第请求
Rspec: Getting Omniauth/oauth0 working with a Faraday Request
我正在尝试使用 Faraday 测试 graphql 请求,但我首先需要使用 omniauth/auth0 进行身份验证。
我通过设置
来验证它
def sign_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"
end
但是当 graphql 被测试时,它完全失去了那个 cookie,因为这是一个全新的请求:
require 'graphlient'
RSpec.shared_context "GraphQL Client", shared_context: :metadata do
let(:client) do
Graphlient::Client.new('https://www.example.org/graphql') do |client|
client.http do |h|
h.connection do |c|
c.use Faraday::Adapter::Rack, app
end
end
end
end
end
如何通过 Faraday 将环境设置为在连接到 GraphQL 之前进行身份验证?或者有没有更好的方法来测试 GraphQL?
我是通过不使用 graphlient 和 faraday 来解决这个问题的(虽然我在弄清楚问题所在后就没有尝试过,所以它可能仍然有效,但这里有一个替代方案)。
将 sign_in
方法更改为获取请求而不是访问:
spec/support/features/session_helpers.rb
def sign_in(user, invalid=false, strategy = :auth0)
invalid ? mock_invalid_auth_hash : mock_valid_auth_hash(user)
get "/auth/#{strategy.to_s}"
Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[strategy.to_sym]
get "/auth/#{strategy.to_s}/callback"
end
在我的 rspec 请求测试中,我确实喜欢这样(我将包含一个完整的测试示例,以防它对任何人有帮助):
spec/support/graphql/client.rb
RSpec.shared_context "GraphQL Client", shared_context: :metadata do
let(:user) { create(:user) }
let(:post_query_as_user) do
sign_in(user)
params = { query: query }
post "/graphql", params: params
end
def data
JSON.parse(response.body)["data"]
end
end
spec/graphql/queries/current_user_queries_spec.rb
require 'rails_helper'
RSpec.describe 'GraphQL::Queries::CurrentUser', type: 'request' do
include_context 'GraphQL Client'
let(:query) do
<<-GRAPHQL
{
current_user {
id
first_name
}
}
GRAPHQL
end
it 'returns the current user with all user attributes' do
post_query_as_user
current_user = data["current_user"]
expect(current_user["first_name"]).to eq(user.first_name)
end
end
我正在尝试使用 Faraday 测试 graphql 请求,但我首先需要使用 omniauth/auth0 进行身份验证。
我通过设置
来验证它 def sign_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"
end
但是当 graphql 被测试时,它完全失去了那个 cookie,因为这是一个全新的请求:
require 'graphlient'
RSpec.shared_context "GraphQL Client", shared_context: :metadata do
let(:client) do
Graphlient::Client.new('https://www.example.org/graphql') do |client|
client.http do |h|
h.connection do |c|
c.use Faraday::Adapter::Rack, app
end
end
end
end
end
如何通过 Faraday 将环境设置为在连接到 GraphQL 之前进行身份验证?或者有没有更好的方法来测试 GraphQL?
我是通过不使用 graphlient 和 faraday 来解决这个问题的(虽然我在弄清楚问题所在后就没有尝试过,所以它可能仍然有效,但这里有一个替代方案)。
将 sign_in
方法更改为获取请求而不是访问:
spec/support/features/session_helpers.rb
def sign_in(user, invalid=false, strategy = :auth0)
invalid ? mock_invalid_auth_hash : mock_valid_auth_hash(user)
get "/auth/#{strategy.to_s}"
Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[strategy.to_sym]
get "/auth/#{strategy.to_s}/callback"
end
在我的 rspec 请求测试中,我确实喜欢这样(我将包含一个完整的测试示例,以防它对任何人有帮助):
spec/support/graphql/client.rb
RSpec.shared_context "GraphQL Client", shared_context: :metadata do
let(:user) { create(:user) }
let(:post_query_as_user) do
sign_in(user)
params = { query: query }
post "/graphql", params: params
end
def data
JSON.parse(response.body)["data"]
end
end
spec/graphql/queries/current_user_queries_spec.rb
require 'rails_helper'
RSpec.describe 'GraphQL::Queries::CurrentUser', type: 'request' do
include_context 'GraphQL Client'
let(:query) do
<<-GRAPHQL
{
current_user {
id
first_name
}
}
GRAPHQL
end
it 'returns the current user with all user attributes' do
post_query_as_user
current_user = data["current_user"]
expect(current_user["first_name"]).to eq(user.first_name)
end
end