-
Notifications
You must be signed in to change notification settings - Fork 230
Make the remember_me_token
cookie name configurable
#179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
robinvdvleuten
wants to merge
1
commit into
Sorcery:master
Choose a base branch
from
robinvdvleuten:cookie-name
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,20 @@ | |
expect(cookies.signed['remember_me_token']).to eq assigns[:current_user].remember_me_token | ||
end | ||
|
||
it 'sets cookie on remember_me! with custom cookie name' do | ||
sorcery_controller_property_set(:remember_me_cookie_name, :custom) | ||
|
||
expect(User).to receive(:authenticate).with('[email protected]', 'secret') { |&block| block.call(user, nil) } | ||
expect(user).to receive(:remember_me!) | ||
|
||
post :test_login_with_remember, params: { email: '[email protected]', password: 'secret' } | ||
|
||
expect(cookies.signed['custom']).to eq assigns[:current_user].remember_me_token | ||
|
||
# Reset property back to default so it won't interfere with other tests. | ||
sorcery_controller_property_set(:remember_me_cookie_name, :remember_me_token) | ||
end | ||
|
||
it 'clears cookie on forget_me!' do | ||
cookies['remember_me_token'] = { value: 'asd54234dsfsd43534', expires: 3600 } | ||
get :test_logout | ||
|
@@ -58,6 +72,22 @@ | |
expect(cookies.signed['remember_me_token']).to eq assigns[:user].remember_me_token | ||
end | ||
|
||
it 'login(email,password,remember_me) logs user in and remembers with custom cookie name' do | ||
sorcery_controller_property_set(:remember_me_cookie_name, :custom) | ||
|
||
expect(User).to receive(:authenticate).with('[email protected]', 'secret', '1') { |&block| block.call(user, nil) } | ||
expect(user).to receive(:remember_me!) | ||
expect(user).to receive(:remember_me_token).and_return('abracadabra').twice | ||
|
||
post :test_login_with_remember_in_login, params: { email: '[email protected]', password: 'secret', remember: '1' } | ||
|
||
expect(cookies.signed['custom']).not_to be_nil | ||
expect(cookies.signed['custom']).to eq assigns[:user].remember_me_token | ||
|
||
# Reset property back to default so it won't interfere with other tests. | ||
sorcery_controller_property_set(:remember_me_cookie_name, :remember_me_token) | ||
end | ||
|
||
it 'logout also calls forget_me!' do | ||
session[:user_id] = user.id.to_s | ||
expect(User.sorcery_adapter).to receive(:find_by_id).with(user.id.to_s).and_return(user) | ||
|
@@ -68,6 +98,21 @@ | |
expect(cookies['remember_me_token']).to be_nil | ||
end | ||
|
||
it 'logout also calls forget_me! with custom cookie name' do | ||
sorcery_controller_property_set(:remember_me_cookie_name, 'custom') | ||
|
||
session[:user_id] = user.id.to_s | ||
expect(User.sorcery_adapter).to receive(:find_by_id).with(user.id.to_s).and_return(user) | ||
expect(user).to receive(:remember_me!) | ||
expect(user).to receive(:forget_me!) | ||
get :test_logout_with_remember | ||
|
||
expect(cookies['custom']).to be_nil | ||
|
||
# Reset property back to default so it won't interfere with other tests. | ||
sorcery_controller_property_set(:remember_me_cookie_name, :remember_me_token) | ||
end | ||
|
||
it 'logs user in from cookie' do | ||
session[:user_id] = user.id.to_s | ||
expect(User.sorcery_adapter).to receive(:find_by_id).with(user.id.to_s).and_return(user) | ||
|
@@ -90,6 +135,33 @@ | |
expect(assigns[:current_user]).to eq user | ||
end | ||
|
||
it 'logs user in from cookie with custom cookie name' do | ||
sorcery_controller_property_set(:remember_me_cookie_name, :custom) | ||
|
||
session[:user_id] = user.id.to_s | ||
expect(User.sorcery_adapter).to receive(:find_by_id).with(user.id.to_s).and_return(user) | ||
expect(user).to receive(:remember_me!) | ||
expect(user).to receive(:remember_me_token).and_return('token') | ||
expect(user).to receive(:has_remember_me_token?) { true } | ||
|
||
subject.remember_me! | ||
subject.instance_eval do | ||
remove_instance_variable :@current_user | ||
end | ||
session[:user_id] = nil | ||
|
||
expect(User.sorcery_adapter).to receive(:find_by_remember_me_token).with('token').and_return(user) | ||
|
||
expect(subject).to receive(:after_remember_me!).with(user) | ||
|
||
get :test_login_from_cookie | ||
|
||
expect(assigns[:current_user]).to eq user | ||
|
||
# Reset property back to default so it won't interfere with other tests. | ||
sorcery_controller_property_set(:remember_me_cookie_name, :remember_me_token) | ||
end | ||
|
||
it 'doest not remember_me! when not asked to, even if third parameter is used' do | ||
post :test_login_with_remember_in_login, params: { email: '[email protected]', password: 'secret', remember: '0' } | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does this receive the token twice? I'm not very familiar with the callback system, but this looks like it might be a side-effect of the double callback bug in
0.13.0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@athix to be honest, I copied it over from the testcase above this one. So it could be part of the double callback bug. When I remove the
twice
method though, it gives an error. Could it be that the double callback bug is still relevant?