mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
REFACTOR: Migrate FacebookAuthenticator to use ManagedAuthenticator
Changes to functionality
- Removed syncing of user metadata including gender, location etc.
These are no longer available to standard Facebook applications.
- Removed the remote 'revoke' functionality. No other providers have
it, and it does not appear to be standard practice in other apps.
- The 'facebook_no_email' event is no longer logged. The system can
cope fine with a missing email address.
Data is migrated to the new user_associated_accounts table.
facebook_user_infos can be dropped once we are confident the data has
been migrated successfully.
This commit is contained in:
@@ -1,101 +1,60 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe Auth::FacebookAuthenticator do
|
||||
let(:hash) {
|
||||
{
|
||||
provider: "facebook",
|
||||
extra: {
|
||||
raw_info: {
|
||||
}
|
||||
},
|
||||
info: {
|
||||
email: "bob@bob.com",
|
||||
first_name: "Bob",
|
||||
last_name: "Smith"
|
||||
},
|
||||
uid: "100"
|
||||
}
|
||||
}
|
||||
|
||||
let(:authenticator) { Auth::FacebookAuthenticator.new }
|
||||
|
||||
context 'after_authenticate' do
|
||||
it 'can authenticate and create a user record for already existing users' do
|
||||
authenticator = Auth::FacebookAuthenticator.new
|
||||
user = Fabricate(:user)
|
||||
|
||||
hash = {
|
||||
"extra" => {
|
||||
"raw_info" => {
|
||||
"username" => "bob"
|
||||
}
|
||||
},
|
||||
"info" => {
|
||||
:email => user.email,
|
||||
"location" => "America",
|
||||
"description" => "bio",
|
||||
"urls" => {
|
||||
"Website" => "https://awesome.com"
|
||||
}
|
||||
},
|
||||
"uid" => "100"
|
||||
}
|
||||
|
||||
result = authenticator.after_authenticate(hash)
|
||||
|
||||
result = authenticator.after_authenticate(hash.deep_merge(info: { email: user.email }))
|
||||
expect(result.user.id).to eq(user.id)
|
||||
expect(result.user.user_profile.website).to eq("https://awesome.com")
|
||||
expect(result.user.user_profile.bio_raw).to eq("bio")
|
||||
expect(result.user.user_profile.location).to eq("America")
|
||||
end
|
||||
|
||||
it 'can connect to a different existing user account' do
|
||||
authenticator = Auth::FacebookAuthenticator.new
|
||||
user1 = Fabricate(:user)
|
||||
user2 = Fabricate(:user)
|
||||
|
||||
FacebookUserInfo.create!(user_id: user1.id, facebook_user_id: 100)
|
||||
|
||||
hash = {
|
||||
"extra" => {
|
||||
"raw_info" => {
|
||||
"username" => "bob"
|
||||
}
|
||||
},
|
||||
"info" => {
|
||||
"location" => "America",
|
||||
"description" => "bio",
|
||||
"urls" => {
|
||||
"Website" => "https://awesome.com"
|
||||
}
|
||||
},
|
||||
"uid" => "100"
|
||||
}
|
||||
UserAssociatedAccount.create!(provider_name: "facebook", user_id: user1.id, provider_uid: 100)
|
||||
|
||||
result = authenticator.after_authenticate(hash, existing_account: user2)
|
||||
|
||||
expect(result.user.id).to eq(user2.id)
|
||||
expect(FacebookUserInfo.exists?(user_id: user1.id)).to eq(false)
|
||||
expect(FacebookUserInfo.exists?(user_id: user2.id)).to eq(true)
|
||||
expect(UserAssociatedAccount.exists?(provider_name: "facebook", user_id: user1.id)).to eq(false)
|
||||
expect(UserAssociatedAccount.exists?(provider_name: "facebook", user_id: user2.id)).to eq(true)
|
||||
end
|
||||
|
||||
it 'can create a proper result for non existing users' do
|
||||
|
||||
hash = {
|
||||
"extra" => {
|
||||
"raw_info" => {
|
||||
"username" => "bob",
|
||||
"name" => "bob bob"
|
||||
}
|
||||
},
|
||||
"info" => {
|
||||
email: "bob@bob.com"
|
||||
},
|
||||
"uid" => "100"
|
||||
}
|
||||
|
||||
authenticator = Auth::FacebookAuthenticator.new
|
||||
|
||||
result = authenticator.after_authenticate(hash)
|
||||
|
||||
expect(result.user).to eq(nil)
|
||||
expect(result.extra_data[:name]).to eq("bob bob")
|
||||
expect(result.name).to eq("Bob Smith")
|
||||
end
|
||||
end
|
||||
|
||||
context 'description_for_user' do
|
||||
let(:user) { Fabricate(:user) }
|
||||
let(:authenticator) { Auth::FacebookAuthenticator.new }
|
||||
|
||||
it 'returns empty string if no entry for user' do
|
||||
expect(authenticator.description_for_user(user)).to eq("")
|
||||
end
|
||||
|
||||
it 'returns correct information' do
|
||||
FacebookUserInfo.create!(user_id: user.id, facebook_user_id: 12345, email: 'someuser@somedomain.tld')
|
||||
UserAssociatedAccount.create!(provider_name: "facebook", user_id: user.id, provider_uid: 100, info: { email: "someuser@somedomain.tld" })
|
||||
expect(authenticator.description_for_user(user)).to eq('someuser@somedomain.tld')
|
||||
end
|
||||
end
|
||||
@@ -112,31 +71,15 @@ describe Auth::FacebookAuthenticator do
|
||||
before do
|
||||
SiteSetting.facebook_app_id = '123'
|
||||
SiteSetting.facebook_app_secret = 'abcde'
|
||||
FacebookUserInfo.create!(user_id: user.id, facebook_user_id: 12345, email: 'someuser@somedomain.tld')
|
||||
UserAssociatedAccount.create!(provider_name: "facebook", user_id: user.id, provider_uid: 100, info: { email: "someuser@somedomain.tld" })
|
||||
end
|
||||
|
||||
it 'revokes correctly' do
|
||||
stub = stub_request(:delete, authenticator.revoke_url(12345)).to_return(body: "true")
|
||||
|
||||
expect(authenticator.description_for_user(user)).to eq("someuser@somedomain.tld")
|
||||
expect(authenticator.can_revoke?).to eq(true)
|
||||
expect(authenticator.revoke(user)).to eq(true)
|
||||
|
||||
expect(stub).to have_been_requested.once
|
||||
expect(authenticator.description_for_user(user)).to eq("")
|
||||
end
|
||||
|
||||
it 'handles errors correctly' do
|
||||
stub = stub_request(:delete, authenticator.revoke_url(12345)).to_return(status: 404)
|
||||
|
||||
expect(authenticator.revoke(user)).to eq(false)
|
||||
expect(stub).to have_been_requested.once
|
||||
expect(authenticator.description_for_user(user)).to eq('someuser@somedomain.tld')
|
||||
|
||||
expect(authenticator.revoke(user, skip_remote: true)).to eq(true)
|
||||
expect(stub).to have_been_requested.once
|
||||
expect(authenticator.description_for_user(user)).to eq("")
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user