简体   繁体   English

使用rspec测试oauth2 get_token

[英]Test oauth2 get_token with rspec

I'm creating a controller spec for the get_token part of an oauth2 authentication. 我正在为oauth2身份验证的get_token部分创建一个控制器规范。 At this point the user has authorized my app and I need to generate and save the token and other information. 此时用户已授权我的应用程序,我需要生成并保存令牌和其他信息。 Rspec fails with a somewhat cryptic error. 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"}

Here's the controller code. 这是控制器代码。 Rspec says it fails on get_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

And here's the test. 这是测试。 The webmock should be intercepting get_token. webmock应该拦截get_token。 It is the autogenerated webmock suggeted by rspec that I filled in the body with the appropriate request and response body. 它是由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

This process already works in practice so at least the controller code is not to blame. 这个过程已经在实践中起作用,所以至少控制器代码不应该受到责备。 What am I doing wrong? 我究竟做错了什么?

After work hard having the same problem I have a solution. 努力工作后遇到同样的问题我有一个解决方案。 Is the header returned in the stub request, you have it blank. 是否在存根请求中返回标头 ,您将其留空。 The oauth2 gem use the Content-Type to define how to parse the body. oauth2 gem使用Content-Type来定义如何解析主体。 Try the stub putting the header in this way: 尝试以这种方式放置标题的存根:

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'})

I think you're getting this error, because you've stubbed only part of oauth session, ie you're trying to send a stale token (or something like that) that has been provided by webmock. 我认为你得到了这个错误,因为你只是存在oauth会话的一部分,即你试图发送一个由webmock提供的陈旧令牌(或类似的东西)。

Instead of manual stubbing of these requests, I'd suggest to use special tool: stripe-ruby-mock gem , which has been designed exactly for testing Stripe API. 我建议使用特殊工具: stripe-ruby-mock gem ,而不是手动存根这些请求,它专为测试Stripe API而设计。

As an alternative, you may use VCR gem (here are the docs ), which allows to write on disk all your http session and play it as it was live. 作为替代方案,您可以使用VCR gem (这里是文档 ),它允许在磁盘上写入您的所有http会话并在其实时播放。 Great tool, highly recommended. 伟大的工具,强烈推荐。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM