使用rspec测试oauth2 get_token

时间:2014-08-04 08:33:53

标签: ruby-on-rails ruby-on-rails-4 rspec oauth-2.0 webmock

我正在为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

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

2 个答案:

答案 0 :(得分:1)

经过艰苦的工作,我遇到了同样的问题。存根请求中是否返回了标题,您将其保留为空白。 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'})

答案 1 :(得分:0)

我认为你得到了这个错误,因为你已经只删除了oauth会话的一部分,即你试图发送一个陈旧的令牌(或类似的东西) webmock。

我建议使用专门用于测试Stripe API的专用工具stripe-ruby-mock gem,而不是手动存根这些请求。

作为替代方案,您可以使用VCR gem(此处为the docs),它允许在磁盘上写入您的所有http会话并在其处于活动状态时播放。伟大的工具,强烈推荐。