Rspec testing for oauth provider

不羁的心 提交于 2019-12-07 04:41:02

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/5268672/rspec-testing-for-oauth-provider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!