Enable CSRF protection in Javascript tests

Posted . Visible to the public.

You might not know that Rails disables CSRF protection in tests Show archive.org snapshot . This means that if you accidentally forget to send the CSRF token for non-GET requests, your tests will be green even though your application is completely broken (a failed CSRF check usually logs out the user). Rails probably does this because CSRF protection sort of requires Javascript.

You want to enable CSRF protection in Cucumber scenarios that can speak Javascript. To do so, copy the attached file to features/support. For this to work you also need the following things:

  1. RSpec stubs and mocks in Cucumber
  2. rspec_candy Show archive.org snapshot
  3. Cucumber: Detect if the current Capybara driver supports Javascript
Henning Koch
Last edit
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2013-10-08 12:58)