Rspec:让Omniauth/oauth0处理法拉第请求

Rspec:让Omniauth/oauth0处理法拉第请求,rspec,graphql,faraday,faraday-oauth,Rspec,Graphql,Faraday,Faraday Oauth,我正在尝试使用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.con

我正在尝试使用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

如何通过法拉第将环境设置为在连接到GraphQL之前进行身份验证?还是有更好的方法来测试GraphQL?

我通过不使用graphlient和faraday来解决这个问题(虽然我在发现问题后没有尝试过,所以它可能仍然有效,但这里有一个替代方案)

将“登录”方法更改为“获取请求”而不是“访问”:

spec/support/features/session\u helpers.rb 在我的rspec请求测试中,我喜欢这样做(我将包括一个完整的测试示例,以防它对任何人都有帮助):

spec/support/graphql/client.rb spec/graphql/querys/current\u user\u querys\u spec.rb
require'rails\u helper'
RSpec.description'GraphQL::querys::CurrentUser',键入'request'do
包括上下文“GraphQL客户端”
让(:查询)去做
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.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
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