【问题标题】:Rspec testing for oauth provideroauth 提供者的 Rspec 测试
【发布时间】:2011-07-13 05:23:55
【问题描述】:

我正在编写一个同时也是 OAuth 提供程序的 API。有什么推荐的方法来编写你的 rspec 测试吗?

启用 oauth 以保护所有端点后,如何编写将通过身份验证步骤的 rspec 测试?

【问题讨论】:

    标签: ruby-on-rails oauth rspec oauth-provider


    【解决方案1】:

    如果您使用的是 oauth 和 oauth-plugin gem,这篇文章可能会对您有所帮助: http://peachshake.com/2010/11/11/oauth-capybara-rspec-headers-and-a-lot-of-pain/


    然而,oauth-plugin gem 会生成一些规范,其中包括帮助您模拟身份验证过程的助手。

    看看这个文件: https://github.com/pelle/oauth-plugin/blob/v0.4.0.pre4/lib/generators/rspec/templates/controller_spec_helper.rb

    您可以使用以下方法来签署您的请求:

    def sign_request_with_oauth(token=nil,options={})
      ActionController::TestRequest.use_oauth=true
      @request.configure_oauth(current_consumer,token,options)
    end
    
    def two_legged_sign_request_with_oauth(consumer=nil,options={})
      ActionController::TestRequest.use_oauth=true
      @request.configure_oauth(consumer,nil,options)
    end
    
    def add_oauth2_token_header(token,options={})
      request.env['HTTP_AUTHORIZATION'] = "OAuth #{token.token}"
    end
    

    我建议您复制此文件作为您的规范的助手,并根据您的需要进行相应的修改。

    【讨论】:

      猜你喜欢
      • 2013-09-20
      • 2014-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-06
      • 2012-04-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多