rails 1.2.3.7116でcsrf-killer pluginを使う
csrf-pluginをインストールする
$ ruby script/plugin install http://svn.techno-weenie.net/projects/plugins/csrf_killer/
コントローラーにverify_tokenを書く
class AccountController < ApplicationController verify_token :only => :index
READMEに従いtest/test_helper.rbでTestSessionを拡張する
ActionController::TestSession.class_eval do def dbman @dbman ||= CGI::Session::CookieStore.new(nil, 'secret' =>'csrf-killer') end end
これでテストをしてみる
test_should_require_password_on_signup(AccountControllerTest):
ActionView::TemplateError: A session_key is required to write a cookie containin
g the session data. Use config.action_controller.session = { :session_key => "_m
yapp_session", :secret => "some secret phrase" } in config/environment.rb
On line #2 of app/views/account/signup.rhtml
こんな感じでエラーになる。
actionpack-1.13.3.7116/lib/action_controller/session/cookie_store.rb
を見てみると
def initialize(session, options = {}) # The session_key option is required. if options['session_key'].blank? raise ArgumentError, 'A session_key is required to write a cookie contain\ ing the session data. Use config.action_controller.session = { :session_key => \ "_myapp_session", :secret => "some secret phrase" } in config/environment.rb' end
こうなってる。つまりsession_keyってのが第二引数のハッシュに必要らしい。
ActionController::TestSession.class_eval do def dbman @dbman ||= CGI::Session::CookieStore.new(nil, 'session_key' => 'key', 'secret' =>'csrf-killer') end end
これでテストが通った。