2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe Auth::FacebookAuthenticator do
|
2018-11-28 10:49:24 -05:00
|
|
|
let(:hash) do
|
|
|
|
{
|
|
|
|
provider: "facebook",
|
|
|
|
extra: {
|
2022-10-25 21:05:15 -04:00
|
|
|
raw_info: {
|
2023-01-09 06:18:21 -05:00
|
|
|
},
|
2018-11-28 10:49:24 -05:00
|
|
|
},
|
|
|
|
info: {
|
|
|
|
email: "bob@bob.com",
|
|
|
|
first_name: "Bob",
|
|
|
|
last_name: "Smith",
|
|
|
|
},
|
|
|
|
uid: "100",
|
|
|
|
}
|
2023-01-09 06:18:21 -05:00
|
|
|
end
|
2018-11-28 10:49:24 -05:00
|
|
|
|
|
|
|
let(:authenticator) { Auth::FacebookAuthenticator.new }
|
2013-08-26 03:36:20 -04:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
describe "after_authenticate" do
|
2013-08-26 03:36:20 -04:00
|
|
|
it "can authenticate and create a user record for already existing users" do
|
|
|
|
user = Fabricate(:user)
|
2018-11-28 10:49:24 -05:00
|
|
|
result = authenticator.after_authenticate(hash.deep_merge(info: { email: user.email }))
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(result.user.id).to eq(user.id)
|
2013-08-26 03:36:20 -04:00
|
|
|
end
|
|
|
|
|
2018-07-23 11:51:57 -04:00
|
|
|
it "can connect to a different existing user account" do
|
|
|
|
user1 = Fabricate(:user)
|
|
|
|
user2 = Fabricate(:user)
|
|
|
|
|
2018-11-28 10:49:24 -05:00
|
|
|
UserAssociatedAccount.create!(provider_name: "facebook", user_id: user1.id, provider_uid: 100)
|
2018-07-23 11:51:57 -04:00
|
|
|
|
|
|
|
result = authenticator.after_authenticate(hash, existing_account: user2)
|
|
|
|
|
|
|
|
expect(result.user.id).to eq(user2.id)
|
2018-11-28 10:49:24 -05:00
|
|
|
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,
|
|
|
|
)
|
2018-07-23 11:51:57 -04:00
|
|
|
end
|
|
|
|
|
2013-08-26 03:36:20 -04:00
|
|
|
it "can create a proper result for non existing users" do
|
|
|
|
result = authenticator.after_authenticate(hash)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(result.user).to eq(nil)
|
2018-11-28 10:49:24 -05:00
|
|
|
expect(result.name).to eq("Bob Smith")
|
2013-08-26 03:36:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
describe "description_for_user" do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2018-07-23 11:51:57 -04:00
|
|
|
|
|
|
|
it "returns empty string if no entry for user" do
|
|
|
|
expect(authenticator.description_for_user(user)).to eq("")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns correct information" do
|
2018-11-28 10:49:24 -05:00
|
|
|
UserAssociatedAccount.create!(
|
|
|
|
provider_name: "facebook",
|
|
|
|
user_id: user.id,
|
|
|
|
provider_uid: 100,
|
|
|
|
info: {
|
|
|
|
email: "someuser@somedomain.tld",
|
|
|
|
},
|
|
|
|
)
|
2018-07-23 11:51:57 -04:00
|
|
|
expect(authenticator.description_for_user(user)).to eq("someuser@somedomain.tld")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
describe "revoke" do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2018-07-23 11:51:57 -04:00
|
|
|
let(:authenticator) { Auth::FacebookAuthenticator.new }
|
|
|
|
|
|
|
|
it "raises exception if no entry for user" do
|
|
|
|
expect { authenticator.revoke(user) }.to raise_error(Discourse::NotFound)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with valid record" do
|
|
|
|
before do
|
|
|
|
SiteSetting.facebook_app_id = "123"
|
|
|
|
SiteSetting.facebook_app_secret = "abcde"
|
2018-11-28 10:49:24 -05:00
|
|
|
UserAssociatedAccount.create!(
|
|
|
|
provider_name: "facebook",
|
|
|
|
user_id: user.id,
|
|
|
|
provider_uid: 100,
|
|
|
|
info: {
|
|
|
|
email: "someuser@somedomain.tld",
|
|
|
|
},
|
|
|
|
)
|
2018-07-23 11:51:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "revokes correctly" do
|
2018-11-28 10:49:24 -05:00
|
|
|
expect(authenticator.description_for_user(user)).to eq("someuser@somedomain.tld")
|
2018-07-23 11:51:57 -04:00
|
|
|
expect(authenticator.can_revoke?).to eq(true)
|
|
|
|
expect(authenticator.revoke(user)).to eq(true)
|
|
|
|
expect(authenticator.description_for_user(user)).to eq("")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-08-26 03:36:20 -04:00
|
|
|
end
|