Created
June 18, 2013 17:37
-
-
Save caike/5807561 to your computer and use it in GitHub Desktop.
Testing forgery protection. TIL Rails 3 changed the behavior of protect_from_forgery to just issue a WARNING.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationController < ActionController::Base | |
protect_from_forgery | |
protected | |
def handle_unverified_request | |
super | |
raise ActionController::InvalidAuthenticityToken | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe JobsController do | |
it 'raises error with invalid authenticity token' do | |
with_forgery_protection do | |
expect { post :create }.to raise_error | |
end | |
end | |
private | |
def with_forgery_protection | |
_old_value = @controller.allow_forgery_protection | |
@controller.allow_forgery_protection = true | |
yield | |
ensure | |
@controller.allow_forgery_protection = _old_value | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I made this change to get Rails 5 working since ControllerTests now inherit from
ActionDispatch::IntegrationTest
source: https://codingbunny.wordpress.com/2016/10/18/testing-csrf-and-custom-headers/