oauth 提供商的 Rspec 测试

发布于 2024-10-21 16:33:22 字数 108 浏览 8 评论 0原文

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风尘浪孓 2024-10-28 16:33:22

如果您正在使用 oauth 和 oauth-plugin gems,这篇文章可能会帮助您:
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

我建议您复制此文件作为您的帮助程序规格并根据您的需要进行相应修改。

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/


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

Take a look at this file:
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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文