Skip to content

Commit

Permalink
#507 Accept invites through google oauth. (#1122)
Browse files Browse the repository at this point in the history
  • Loading branch information
dipil-saud authored and julianguyen committed Oct 10, 2018
1 parent 5d23513 commit 41c9d53
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 30 deletions.
9 changes: 9 additions & 0 deletions app/controllers/omniauth_callbacks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def google_oauth2
if user.present?
user.accept_invitation! if invitation_token

flash[:notice] = I18n.t('devise.omniauth_callbacks.success',
kind: t('omniauth.google'))
sign_in_and_redirect @user, event: :authentication
Expand All @@ -11,10 +13,17 @@ def google_oauth2
end
end

private

def user
@user ||= User.find_for_google_oauth2(
request.env['omniauth.auth'],
current_user
)
end

# returns the invitation token passed in with the callback url
def invitation_token
request.env.dig('omniauth.params', 'invitation_token')
end
end
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ def available_groups(order)
def self.find_for_google_oauth2(access_token, _signed_in_resource = nil)
data = access_token.info
user = find_or_initialize_by(email: data.email) do |u|
u.name = data.name
u.password = Devise.friendly_token[0, 20]
end
user.name ||= data.name

user.update!(
provider: access_token.provider,
Expand Down
8 changes: 8 additions & 0 deletions app/views/users/invitations/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,11 @@
<%= f.submit t("devise.invitations.edit.submit_button"), class: 'buttonGhostM smallMarginTop' %>
<% end %>
<div class="center">
<div class="smallMarginTop">
<%= button_to t('devise.shared.sign_up_google'),
omniauth_authorize_path(resource_name, :google_oauth2, invitation_token: resource.invitation_token),
class: 'buttonGhostM'
%>
</div>
</div>
80 changes: 51 additions & 29 deletions spec/controllers/omniauth_callbacks_controller_spec.rb
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
Expand All @@ -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

0 comments on commit 41c9d53

Please sign in to comment.