3

我正在为 oauth2 身份验证的 get_token 部分创建控制器规范。此时用户已授权我的应用程序,我需要生成并保存令牌和其他信息。Rspec 失败并出现一个有点神秘的错误。

Failure/Error: get :auth, { code: "auth_code", scope: "read_write" }
     OAuth2::Error:
       {:token_type=>"bearer", 
        :stripe_publishable_key=>"PUBLISHABLE_KEY", 
        :scope=>"read_write", 
        :livemode=>"false", 
        :stripe_user_id=>"USER_ID",  
        :refresh_token=>"REFRESH_TOKEN", 
        :access_token=>"ACCESS_TOKEN"}

这是控制器代码。Rspec 说它在 get_token 上失败。

require 'oauth2'

def auth
  code = params[:code]
  client = oauth_client
  token_response = client.auth_code.get_token(code, params: { scope: 'read_write' })
  token = token_response.token

这是测试。webmock 应该拦截 get_token。它是 rspec 建议的自动生成的 webmock,我用适当的请求和响应正文填充了正文。

before do
  stub_request(:post, "https://connect.stripe.com/oauth/token").
    with(:body => {"client_id"=>"CLIENT_ID",
                   "client_secret"=>"SOME_SECRET",
                   "code"=>"auth_code",
                   "grant_type"=>"authorization_code",
                   "params"=>{"scope"=>"read_write"}},
         :headers => {'Accept'=>'*/*',
                      'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
                      'Content-Type'=>'application/x-www-form-urlencoded',
                      'User-Agent'=>'Faraday v0.9.0'}).
    to_return(:status => 200,
              :body => {token_type: "bearer",
                        stripe_publishable_key: "PUBLISHABLE_KEY",
                        scope: "read_write",
                        livemode: "false",
                        stripe_user_id: "USER_ID",
                        refresh_token: "REFRESH_TOKEN",
                        access_token: "ACCESS_TOKEN"},
              :headers => {})
end

describe "#auth" do
  it "creates a payment gateway" do
    get :auth, { code: "auth_code", scope: "read_write" 
  end
end

这个过程已经在实践中起作用,所以至少控制器代码不应该受到责备。我究竟做错了什么?

4

2 回答 2

2

努力工作后遇到同样的问题,我有一个解决方案。是存根请求中返回的标头,您将其留空。oauth2 gem 使用 Content-Type 来定义如何解析正文。尝试以这种方式放置标头的存根:

stub_request(:post, "https://connect.stripe.com/oauth/token").
    with(:body => {"client_id"=>"CLIENT_ID",
                   "client_secret"=>"SOME_SECRET",
                   "code"=>"auth_code",
                   "grant_type"=>"authorization_code",
                   "params"=>{"scope"=>"read_write"}},
         :headers => {'Accept'=>'*/*',
                      'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
                      'Content-Type'=>'application/x-www-form-urlencoded',
                      'User-Agent'=>'Faraday v0.9.0'}).
    to_return(:status => 200,
              :body => {token_type: "bearer",
                        stripe_publishable_key: "PUBLISHABLE_KEY",
                        scope: "read_write",
                        livemode: "false",
                        stripe_user_id: "USER_ID",
                        refresh_token: "REFRESH_TOKEN",
                        access_token: "ACCESS_TOKEN"},
              :headers => { 'Content-Type'=> 'application/json;charset=UTF-8'})
于 2018-10-26T11:44:32.997 回答
0

我认为你收到了这个错误,因为你只存根了 oauth 会话的一部分,即你试图发送一个由 webmock 提供的过时的令牌(或类似的东西)。

我建议不要对这些请求进行手动存根,而是使用特殊工具:stripe-ruby-mock gem,它专为测试 Stripe API 而设计。

作为替代方案,您可以使用VCRgem(这里是docs),它允许在磁盘上写入您的所有 http 会话并像现场一样播放它。很棒的工具,强烈推荐。

于 2015-09-21T21:10:56.253 回答