2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe Auth::TwitterAuthenticator do
|
2017-12-14 12:35:27 +08:00
|
|
|
it "takes over account if email is supplied" do
|
|
|
|
auth = Auth::TwitterAuthenticator.new
|
|
|
|
|
|
|
|
user = Fabricate(:user)
|
|
|
|
|
|
|
|
auth_token = {
|
|
|
|
info: {
|
2018-12-07 23:39:06 +08:00
|
|
|
email: user.email,
|
|
|
|
username: "test",
|
|
|
|
name: "test",
|
|
|
|
nickname: "minion",
|
2017-12-14 12:35:27 +08:00
|
|
|
},
|
2018-12-07 23:39:06 +08:00
|
|
|
uid: "123",
|
|
|
|
provider: "twitter",
|
2017-12-14 12:35:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
result = auth.after_authenticate(auth_token)
|
|
|
|
|
|
|
|
expect(result.user.id).to eq(user.id)
|
|
|
|
|
2018-12-07 23:39:06 +08:00
|
|
|
info = UserAssociatedAccount.find_by(provider_name: "twitter", user_id: user.id)
|
|
|
|
expect(info.info["email"]).to eq(user.email)
|
2017-12-14 12:35:27 +08:00
|
|
|
end
|
|
|
|
|
2018-07-27 19:28:51 +08:00
|
|
|
it "can connect to a different existing user account" do
|
|
|
|
authenticator = Auth::TwitterAuthenticator.new
|
|
|
|
user1 = Fabricate(:user)
|
|
|
|
user2 = Fabricate(:user)
|
|
|
|
|
2018-12-07 23:39:06 +08:00
|
|
|
UserAssociatedAccount.create!(provider_name: "twitter", user_id: user1.id, provider_uid: 100)
|
2018-07-27 19:28:51 +08:00
|
|
|
|
|
|
|
hash = {
|
|
|
|
info: {
|
2018-12-07 23:39:06 +08:00
|
|
|
email: user1.email,
|
|
|
|
username: "test",
|
|
|
|
name: "test",
|
|
|
|
nickname: "minion",
|
2018-07-27 19:28:51 +08:00
|
|
|
},
|
2018-12-07 23:39:06 +08:00
|
|
|
uid: "100",
|
|
|
|
provider: "twitter",
|
2018-07-27 19:28:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
result = authenticator.after_authenticate(hash, existing_account: user2)
|
|
|
|
|
|
|
|
expect(result.user.id).to eq(user2.id)
|
2018-12-07 23:39:06 +08:00
|
|
|
expect(UserAssociatedAccount.exists?(provider_name: "twitter", user_id: user1.id)).to eq(false)
|
|
|
|
expect(UserAssociatedAccount.exists?(provider_name: "twitter", user_id: user2.id)).to eq(true)
|
2018-07-27 19:28:51 +08:00
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
describe "revoke" do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2018-07-27 19:28:51 +08:00
|
|
|
let(:authenticator) { Auth::TwitterAuthenticator.new }
|
|
|
|
|
|
|
|
it "raises exception if no entry for user" do
|
|
|
|
expect { authenticator.revoke(user) }.to raise_error(Discourse::NotFound)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "revokes correctly" do
|
2018-12-07 23:39:06 +08:00
|
|
|
UserAssociatedAccount.create!(provider_name: "twitter", user_id: user.id, provider_uid: 100)
|
2018-07-27 19:28:51 +08: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
|
2017-12-14 12:35:27 +08:00
|
|
|
end
|