5

OAuth プロバイダーでもある API を作成しています。rspec テストを作成するための推奨される方法はありますか?

oauth を有効にしてすべてのエンドポイントを保護した後、認証ステップを通過する rspec テストをどのように作成しますか?

4

1 に答える 1

6

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

このファイルを仕様のヘルパーとしてコピーし、必要に応じて変更することをお勧めします。

于 2011-03-22T14:01:03.493 に答える