oauth 提供程序的 Rspec 测试 [英] Rspec testing for oauth provider

查看:58
本文介绍了oauth 提供程序的 Rspec 测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 API,它也是一个 OAuth 提供程序.有没有推荐的方法来编写 rspec 测试?

I'm writing a API that's also an OAuth provider. Is there any recommended way to write your rspec tests?

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

After you enable oauth to protect all of your endpoints, how do you write rspec tests that will pass the authentication step?

推荐答案

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

If you are using the oauth and oauth-plugin gems, this post might help you: http://peachshake.com/2010/11/11/oauth-capybara-rspec-headers-and-a-lot-of-pain/

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

Yet, the oauth-plugin gem generates some specs which include a helper to help you simulate the authentication process.

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

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

You can use the following methods to sign your requests:

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

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

I suggest you to copy this file as a helper for your specs and modify accordingly to your needs.

这篇关于oauth 提供程序的 Rspec 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆