In a Feature spec, set the user with Warden for redirects in actions in a Ruby on Rails application

Viewed 78

I'm able to get faked authenticated feature specs (via rspec) to work with my application using login_as which covers the next request to occur. However, for actions that contain redirects with the Rails controller's action, it does not appear possible to set the user proxy in place for that request (the redirection). Is there a way to tell the application to use login_as for all requests in a feature spec?

1 Answers

So, this old GitHub thread comment appears to have a solution that still works from in my initial testing.

module Warden
  module Test
    module Helpers

      def login_permanently_as(user, opts = {})
        Warden::Manager.on_request do |proxy|
          opts[:event] || :authentication
          proxy.set_user(user, opts)
        end
      end

    end
  end
end

Monkey patch Warden::Test::Helpers to add a method which makes use of on_request. Now in my Capybara feature specs I no longer need to constantly set login_as for every click to a new page and it is set for those actions which have redirects as part of the execution.

Related