2011-03-11 68 views
5

我正在編寫一個也是OAuth提供者的API。有沒有推薦的方法來編寫你的rspec測試?oauth提供者的Rspec測試

啓用oauth以保護所有端點後,如何編寫rspec測試以通過身份驗證步驟?

回答

6

如果您正在使用的OAuth和OAuth的插件寶石,這篇文章可以幫助你: http://peachshake.com/2010/11/11/oauth-capybara-rspec-headers-and-a-lot-of-pain/


然而,OAuth的插件寶石產生一定的規格,其中包括一個幫手幫你模擬認證過程。

看看這個文件: 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 

我建議你把這個文件拷貝作爲幫手您的規格和修改根據您的需求。