gem Warden, why do I need scope: in login_as helper?

2.9k Views Asked by At

After looking at somebody else's code I have noticed the following:

login_as user, scope: :user

I have always used simply

login_as user

So I went out to look for an explanation and found this article How to: Test with Capybara that says use scope: :user however without any explanation. All my tests are working fine without it.

Another strange thing is Warden.test_mode! which I am not using either. Why would I need it?

Any explanation?

2

There are 2 best solutions below

0
On BEST ANSWER

1.
As you can see here, login_as calls set_user with the same set of options.

Here's the source code of set_user (click "View source"). On line 165, you'll see that if the :scope option is empty, the default scope will be used. In your Rails application, open config/initializers/devise.rb, you'll find something as follows

# Configure the default scope given to Warden. By default it's the first
# devise role declared in your routes (usually :user).
# config.default_scope = :user

It means your default scope is :user which is used when you call login_as without passing a scope.

2.
Here's the documentation of Warden.test_mode!

.test_mode! ⇒ Object

Provides helper methods to warden for testing.

To setup warden in test mode call the test_mode! method on warden

This will provide a number of methods. Warden.on_next_request(&blk) - captures a block which is yielded the warden proxy on the next request Warden.test_reset! - removes any captured blocks that would have been executed on the next request

Warden.test_reset! should be called in after blocks for rspec, or teardown methods for Test::Unit

It means if you're sure you won't need/use any of the helper methods provided by warden as listed, not calling this method won't break your tests.

0
On

This might be helpfull https://github.com/wardencommunity/warden/wiki/authenticated-session-data . scope is used to separate session data for logged_in entities (eg. :user and :admin). Warden allows You to have one session with more entities logged_in at same time.