diff --git a/app/controllers/users/omniauth_callbacks_controller.rb b/app/controllers/users/omniauth_callbacks_controller.rb index ed7ce011..1d0400cd 100644 --- a/app/controllers/users/omniauth_callbacks_controller.rb +++ b/app/controllers/users/omniauth_callbacks_controller.rb @@ -11,6 +11,11 @@ module Users def handle(provider) auth_hash = request.env['omniauth.auth'] + unless auth_hash.info.email.present? + flash[:error] = "Email field is missing in your #{provider} account" + redirect_to new_user_registration_path + return + end username = auth_hash.info.email.split('@')[0] openid = Openid.find_for_oauth(auth_hash) # Get or create openid # If openid exists and is associated with a user, sign in with associated user, diff --git a/app/models/openid.rb b/app/models/openid.rb index 2b954f8b..b0f45f0c 100644 --- a/app/models/openid.rb +++ b/app/models/openid.rb @@ -1,6 +1,6 @@ class Openid < ApplicationRecord belongs_to :user - validates :provider, :uid, presence: true + validates :provider, :uid, :email, presence: true # Searches for openid based on provider and uid. # Returns found openid or a new openid. diff --git a/spec/controllers/users/omniauth_callbacks_controller_spec.rb b/spec/controllers/users/omniauth_callbacks_controller_spec.rb new file mode 100644 index 00000000..c67e6ac9 --- /dev/null +++ b/spec/controllers/users/omniauth_callbacks_controller_spec.rb @@ -0,0 +1,31 @@ +require 'spec_helper' + +describe Users::OmniauthCallbacksController do + context 'email is not there in auth hash' do + before do + stub_env_for_omniauth + get :google + end + + it { expect(flash[:error]).to eq('Email field is missing in your google account') } + end +end + +def stub_env_for_omniauth + request.env['devise.mapping'] = Devise.mappings[:user] + env = OmniAuth::AuthHash.new( + provider: 'google', + uid: 'google-test-uid-1', + info: { + name: 'google user', + email: nil, + username: 'user_google' + }, + credentials: { + token: 'google_mock_token', + secret: 'google_mock_secret' + } + ) + request.env['omniauth.auth'] = env + @controller.stub(:env).and_return(env) +end diff --git a/spec/factories/openid.rb b/spec/factories/openid.rb new file mode 100644 index 00000000..6950c718 --- /dev/null +++ b/spec/factories/openid.rb @@ -0,0 +1,10 @@ +# Read about factories at https://github.com/thoughtbot/factory_girl + +FactoryGirl.define do + factory :openid do + provider { Faker::Internet.domain_word } + email { Faker::Internet.email } + uid { SecureRandom.hex } + user + end +end diff --git a/spec/models/openid.rb b/spec/models/openid.rb new file mode 100644 index 00000000..42b6a74f --- /dev/null +++ b/spec/models/openid.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +describe Openid do + subject { create(:openid) } + + describe 'validation' do + it { is_expected.to validate_presence_of(:provider) } + it { is_expected.to validate_presence_of(:uid) } + it { is_expected.to validate_presence_of(:email) } + end + + describe 'association' do + it { is_expected.to belong_to(:user) } + end +end