2015-10-11 17:41:23 +08:00
|
|
|
require 'rails_helper'
|
2014-05-22 06:19:40 +08:00
|
|
|
|
|
|
|
# For autospec:
|
|
|
|
Auth.send(:remove_const, :GoogleOAuth2Authenticator)
|
|
|
|
load 'auth/google_oauth2_authenticator.rb'
|
|
|
|
|
|
|
|
describe Auth::GoogleOAuth2Authenticator do
|
|
|
|
|
2016-11-07 09:48:00 +08:00
|
|
|
it 'does not look up user unless email is verified' do
|
|
|
|
# note, emails that come back from google via omniauth are always valid
|
|
|
|
# this protects against future regressions
|
|
|
|
|
|
|
|
authenticator = Auth::GoogleOAuth2Authenticator.new
|
|
|
|
user = Fabricate(:user)
|
|
|
|
|
|
|
|
hash = {
|
2017-07-28 09:20:09 +08:00
|
|
|
uid: "123456789",
|
|
|
|
info: {
|
|
|
|
name: "John Doe",
|
|
|
|
email: user.email
|
2016-11-07 09:48:00 +08:00
|
|
|
},
|
2017-07-28 09:20:09 +08:00
|
|
|
extra: {
|
|
|
|
raw_info: {
|
|
|
|
email: user.email,
|
|
|
|
email_verified: false,
|
|
|
|
name: "John Doe"
|
2016-11-07 09:48:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result = authenticator.after_authenticate(hash)
|
|
|
|
|
|
|
|
expect(result.user).to eq(nil)
|
|
|
|
end
|
|
|
|
|
2014-05-22 06:19:40 +08:00
|
|
|
context 'after_authenticate' do
|
|
|
|
it 'can authenticate and create a user record for already existing users' do
|
2016-11-07 09:48:00 +08:00
|
|
|
authenticator = Auth::GoogleOAuth2Authenticator.new
|
2014-05-22 06:19:40 +08:00
|
|
|
user = Fabricate(:user)
|
|
|
|
|
|
|
|
hash = {
|
2017-07-28 09:20:09 +08:00
|
|
|
uid: "123456789",
|
|
|
|
info: {
|
|
|
|
name: "John Doe",
|
|
|
|
email: user.email
|
2014-05-22 06:19:40 +08:00
|
|
|
},
|
2017-07-28 09:20:09 +08:00
|
|
|
extra: {
|
|
|
|
raw_info: {
|
|
|
|
email: user.email,
|
|
|
|
email_verified: true,
|
|
|
|
name: "John Doe"
|
2014-05-22 06:19:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result = authenticator.after_authenticate(hash)
|
|
|
|
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(result.user.id).to eq(user.id)
|
2014-05-22 06:19:40 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'can create a proper result for non existing users' do
|
|
|
|
hash = {
|
2017-07-28 09:20:09 +08:00
|
|
|
uid: "123456789",
|
|
|
|
info: {
|
|
|
|
name: "Jane Doe",
|
|
|
|
email: "jane.doe@the.google.com"
|
2014-05-22 06:19:40 +08:00
|
|
|
},
|
2017-07-28 09:20:09 +08:00
|
|
|
extra: {
|
|
|
|
raw_info: {
|
|
|
|
email: "jane.doe@the.google.com",
|
|
|
|
email_verified: true,
|
|
|
|
name: "Jane Doe"
|
2014-05-22 06:19:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
authenticator = described_class.new
|
|
|
|
result = authenticator.after_authenticate(hash)
|
|
|
|
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(result.user).to eq(nil)
|
|
|
|
expect(result.extra_data[:name]).to eq("Jane Doe")
|
2014-05-22 06:19:40 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-08 04:04:56 +08:00
|
|
|
context 'after_create_account' do
|
|
|
|
it 'confirms email' do
|
|
|
|
authenticator = Auth::GoogleOAuth2Authenticator.new
|
|
|
|
user = Fabricate(:user)
|
|
|
|
session = {
|
2017-07-28 09:20:09 +08:00
|
|
|
email_valid: "true",
|
|
|
|
extra_data: {
|
|
|
|
google_user_id: 1
|
2017-07-08 04:04:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
authenticator.after_create_account(user, session)
|
|
|
|
expect(user.email_confirmed?).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-22 06:19:40 +08:00
|
|
|
end
|