Search code examples
ruby-on-railsrspeccapybarapoltergeist

How to perform non-fresh logins using Devise/Rspec/Capybara/Poltergeist?


While performing integration/system tests with rspec/capybara/poltergeist, I am trying to test non-fresh logins using Devise.

before(:each) do
      login_as(user, :scope => :spree_user)
      user.current_sign_in_at = 1.day.ago
      user.save!
    end

    it "should prompt me for my password" do
      visit "/"
      expect(page).to have_content("Welcome back, #{user.email}. Enter your password to continue.")
    end

I can verify that the user object successfully updates the current_sign_in_at date, however when the root page is rendered, the user's current_sign_in_at timestamp gets updated to today's date.

How can I keep the value consistent after a next request?

I am expecting the user's current_sign_in_at to be Today's date minus 1 day, however I am consistently receiving today's date.


Solution

  • Assuming login_as is from Warden::Test::Helpers, it works by setting up a hook for the next request which logs the user in. That means the actual 'logging in' doesn't actually occur until the visit "/" call which will overwrite the value you've saved. I haven't tried this, but it should be possible to set devise.skip_trackable in the request environment by registering your own request hook rather than calling login_as and thereby stop the login dates from being updated

    Warden.on_next_request do |proxy|
      opts[:event] ||= :authentication
      proxy.env['devise.skip_trackable'] = true
      proxy.set_user(user, opts)
    end