-
Notifications
You must be signed in to change notification settings - Fork 749
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
5d23513
commit 41c9d53
Showing
4 changed files
with
69 additions
and
30 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,49 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe OmniauthCallbacksController, type: :controller do | ||
shared_examples 'successful sign in with oauth details' do | ||
it 'should sign in and redirect to root' do | ||
expect(subject.current_user).to eq user | ||
expect(response).to redirect_to('/') | ||
end | ||
|
||
it 'should create authentication with google_oauth2' do | ||
expect(user.reload.google_oauth2_enabled?).to eq true | ||
end | ||
end | ||
|
||
describe 'GET #google_oauth2' do | ||
before { stub_env_for_omniauth } | ||
let(:oauth_email) { request.env['omniauth.auth']['info']['email'] } | ||
let(:oauth_user) { User.find_by(email: oauth_email) } | ||
|
||
context 'when google_oauth2 email doesnt exist in the system' do | ||
let(:user) { User.find_by(email: '[email protected]') } | ||
|
||
before do | ||
stub_env_for_omniauth | ||
get :google_oauth2 | ||
end | ||
let(:user) { oauth_user } | ||
|
||
before { get :google_oauth2 } | ||
|
||
it 'creates user with info in google_oauth2' do | ||
expect(user.name).to eq 'Test User' | ||
end | ||
|
||
it 'should create authentication with google_oauth2' do | ||
expect(user.google_oauth2_enabled?).to eq true | ||
end | ||
|
||
it 'should sign in the user and redirect_to root' do | ||
expect(subject.current_user).to eq user | ||
expect(response).to redirect_to('/') | ||
end | ||
include_examples 'successful sign in with oauth details' | ||
end | ||
|
||
context 'when google_oauth2 email already exist in the system' do | ||
let!(:user) { create(:user, email: '[email protected]') } | ||
|
||
before do | ||
stub_env_for_omniauth | ||
get :google_oauth2 | ||
end | ||
|
||
before { get :google_oauth2 } | ||
|
||
it 'updates the user with google_oauth2 credentials' do | ||
expect(user.reload.token).to eq 'abcdefg12345' | ||
end | ||
|
||
it 'should create authentication with google_oauth2' do | ||
expect(user.reload.google_oauth2_enabled?).to eq true | ||
end | ||
|
||
it 'should sign in the user and redirect_to root' do | ||
expect(subject.current_user).to eq user | ||
expect(response).to redirect_to('/') | ||
end | ||
include_examples 'successful sign in with oauth details' | ||
end | ||
|
||
context 'when google_oauth2 enabling fails' do | ||
|
@@ -57,6 +52,33 @@ | |
it 'redirects to sign in path' do | ||
expect(response).to redirect_to(new_user_session_path) | ||
end | ||
end | ||
end | ||
|
||
context 'when an invitation_token is passed in' do | ||
let(:inviter) { create(:user) } | ||
let(:invitee_email) { oauth_email } | ||
let!(:invitee) { User.invite!({ email: invitee_email }, inviter) } | ||
|
||
before do | ||
request.env['omniauth.params'] = { 'invitation_token' => invitee.invitation_token } | ||
get :google_oauth2 | ||
end | ||
|
||
context 'when the user logs in with the same email as the invitation' do | ||
let(:user) { invitee } | ||
|
||
include_examples 'successful sign in with oauth details' | ||
it { expect(user.reload.invitation_accepted_at).to be_present } | ||
end | ||
|
||
context 'when the user logs in with a different email from the invitation' do | ||
let(:invitee_email) { '[email protected]' } | ||
let(:user) { oauth_user } | ||
|
||
include_examples 'successful sign in with oauth details' | ||
|
||
it { expect(user.reload.invitation_accepted_at).to be_blank } | ||
end | ||
end | ||
end | ||
end |