-
Notifications
You must be signed in to change notification settings - Fork 229
Implement #login! helper #332
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
Spone
wants to merge
6
commits into
Sorcery:master
Choose a base branch
from
Spone:login-bang-method
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
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1f5ce59
Implement #login! helper
Spone 9ebaf31
Add CHANGELOG entry
Spone b644d62
Improve code style
Spone 0592c84
Refactor with Sorcery::Errors
Spone b46de4f
Add support & spec for #login! with block
Spone dcb8141
Merge branch 'master' into login-bang-method
Spone 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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
module Sorcery | ||
## | ||
# Custom error class for rescuing from all Sorcery errors. | ||
# | ||
class Error < StandardError; end | ||
|
||
module Errors | ||
class InvalidCredentials < Sorcery::Error; end | ||
end | ||
end |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
require 'sorcery/version' | ||
require 'errors' | ||
|
||
module Sorcery | ||
require 'sorcery/model' | ||
|
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 |
---|---|---|
|
@@ -85,6 +85,72 @@ | |
end | ||
end | ||
|
||
describe '#login!' do | ||
context 'when succeeds' do | ||
before do | ||
expect(User).to receive(:authenticate).with('[email protected]', 'secret') { |&block| block.call(user, nil) } | ||
get :test_login_bang, params: { email: '[email protected]', password: 'secret' } | ||
end | ||
|
||
it 'assigns user to @user variable' do | ||
expect(assigns[:user]).to eq user | ||
end | ||
|
||
it 'writes user id in session' do | ||
expect(session[:user_id]).to eq user.id.to_s | ||
end | ||
|
||
it 'sets csrf token in session' do | ||
expect(session[:_csrf_token]).not_to be_nil | ||
end | ||
end | ||
|
||
context 'when fails' do | ||
before do | ||
expect(User).to receive(:authenticate).with('[email protected]', 'opensesame!').and_return(nil) | ||
end | ||
|
||
it 'raises InvalidCredentials exception' do | ||
expect do | ||
get :test_login_bang, params: { email: '[email protected]', password: 'opensesame!' } | ||
end.to raise_error(Sorcery::Errors::InvalidCredentials) | ||
end | ||
end | ||
end | ||
|
||
describe '#login! with block' do | ||
context 'when succeeds' do | ||
before do | ||
expect(User).to receive(:authenticate).with('[email protected]', 'secret') { |&block| block.call(user, nil) } | ||
get :test_login_bang_with_block, params: { email: '[email protected]', password: 'secret' } | ||
end | ||
|
||
it 'writes user id in session' do | ||
expect(session[:user_id]).to eq user.id.to_s | ||
end | ||
|
||
it 'sets csrf token in session' do | ||
expect(session[:_csrf_token]).not_to be_nil | ||
end | ||
|
||
it 'redirects to root' do | ||
expect(response).to redirect_to(root_url) | ||
end | ||
end | ||
|
||
context 'when fails' do | ||
before do | ||
expect(User).to receive(:authenticate).with('[email protected]', 'opensesame!').and_return(nil) | ||
end | ||
|
||
it 'raises InvalidCredentials exception' do | ||
expect do | ||
get :test_login_bang_with_block, params: { email: '[email protected]', password: 'opensesame!' } | ||
end.to raise_error(Sorcery::Errors::InvalidCredentials) | ||
end | ||
end | ||
end | ||
|
||
describe '#logout' do | ||
it 'clears the session' do | ||
cookies[:remember_me_token] = nil | ||
|
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
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.
Uh oh!
There was an error while loading. Please reload this page.