Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 514
firedev Avatar asked Nov 23 '25 01:11

firedev


2 Answers

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.

like image 77
Hoa Avatar answered Nov 25 '25 14:11

Hoa


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.

like image 24
Foton Avatar answered Nov 25 '25 15:11

Foton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!