views:

321

answers:

3

I'd like to have a Cucumber feature testing the rememberable functionality of devise (a remember me cookie).

It is easy to check the remember me check box using capybara but how should I simulate a user returning to the site after closing their window?

+1  A: 

I used email-spec to accomplish this. My Scenario looks like the following:

@allow-rescue
Scenario: Create New Account (Everything cool)
Given I am not authenticated
When I go to register
And I fill in "Name" with "bill"
And I fill in "Email" with "[email protected]"
And I fill in "Password" with "please"
And I fill in "Password Confirmation" with "please"
And I press "Sign up"
Then "[email protected]" should receive an email
And I open the email
And I should see "Confirm my account" in the email body
When I follow "Confirm my account" in the email
Then I should see "Your account was successfully confirmed. You are now signed in."

Note the @allow-rescue decoration above the scenario that is necessary when using Devise.

Hope this helps.

Jonathan
That's handy stuff, particularly email-spec, however I wanted to know about testing Devise's rememberable functionality - that is the cookie storage ability so users don't need to constantly login everytime they visit the site.
srboisvert
A: 

I guess you could log out with capybara, and then log back in something like

Given I am on the login screen
And I select 'Remember Me'
And I click 'login'
Then I should be 'logged in'
When I click 'log out'
Then I should be 'logged out' #=> potentially destroy a session here?
When I click log in
Then I should be logged in
And I should not be directed to the login form.

This should use cabybara's current cookie state to model this flow.

Jed Schneider
+1  A: 

Hi Stephen

I came up with the following rack-test hack, and slightly cleaner selenium api use, to test Devise remember-me functionality in cucumber/capybara. It just tells the driver to manually erase the session cookie. Not all drivers are supported, I only implemented the two I've used:

http://gist.github.com/484787

This assumes cookie storage of the session. Remove @announce tag from the scenario to get rid of the verbosity.

Another option, suggested by Matt Wynne in the mailing list discussion, may be looking at other cookie stores, and deleting them by query or file deletion:

lifted from agile rails book:

config.action_controller.session_store = CGI::Session::PStore (or just :p_store)
config.action_controller.session_options[:tmpdir] = "/Users/dave/tmp" 
config.action_controller.session_options[:prefix] = "myapp_session_"

or

rake db:sessions:create
config.action_controller.session_store = :active_record_store

Rails also has a reset session method, but I believe we don't have access to this because we can't hook into the rails session when testing with capybara.

Hope this helps,

Nick

nruth