2016-04-01 05:33:25 +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
|
|
|
|
|
|
|
|
context 'after_authenticate' do
|
|
|
|
it 'can authenticate and create a user record for already existing users' do
|
|
|
|
authenticator = described_class.new
|
|
|
|
user = Fabricate(:user)
|
|
|
|
|
|
|
|
hash = {
|
|
|
|
:uid => "123456789",
|
|
|
|
:info => {
|
|
|
|
:name => "John Doe",
|
|
|
|
:email => user.email
|
|
|
|
},
|
|
|
|
:extra => {
|
|
|
|
:raw_info => {
|
|
|
|
:email => "user@domain.example.com",
|
|
|
|
:email_verified => true,
|
|
|
|
:name => "John Doe"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 = {
|
|
|
|
:uid => "123456789",
|
|
|
|
:info => {
|
|
|
|
:name => "Jane Doe",
|
|
|
|
:email => "jane.doe@the.google.com"
|
|
|
|
},
|
|
|
|
:extra => {
|
|
|
|
:raw_info => {
|
|
|
|
:email => "jane.doe@the.google.com",
|
|
|
|
:email_verified => true,
|
|
|
|
:name => "Jane Doe"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
end
|