2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe Auth::TwitterAuthenticator do
|
2017-12-13 23:35:27 -05:00
|
|
|
it "takes over account if email is supplied" do
|
|
|
|
auth = Auth::TwitterAuthenticator.new
|
|
|
|
|
|
|
|
user = Fabricate(:user)
|
|
|
|
|
|
|
|
auth_token = {
|
|
|
|
info: {
|
2018-12-07 10:39:06 -05:00
|
|
|
email: user.email,
|
|
|
|
username: "test",
|
|
|
|
name: "test",
|
|
|
|
nickname: "minion",
|
2017-12-13 23:35:27 -05:00
|
|
|
},
|
2018-12-07 10:39:06 -05:00
|
|
|
uid: "123",
|
|
|
|
provider: "twitter"
|
2017-12-13 23:35:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
result = auth.after_authenticate(auth_token)
|
|
|
|
|
|
|
|
expect(result.user.id).to eq(user.id)
|
|
|
|
|
2018-12-07 10:39:06 -05:00
|
|
|
info = UserAssociatedAccount.find_by(provider_name: "twitter", user_id: user.id)
|
|
|
|
expect(info.info["email"]).to eq(user.email)
|
2017-12-13 23:35:27 -05:00
|
|
|
end
|
|
|
|
|
2018-07-27 07:28:51 -04:00
|
|
|
it 'can connect to a different existing user account' do
|
|
|
|
authenticator = Auth::TwitterAuthenticator.new
|
|
|
|
user1 = Fabricate(:user)
|
|
|
|
user2 = Fabricate(:user)
|
|
|
|
|
2018-12-07 10:39:06 -05:00
|
|
|
UserAssociatedAccount.create!(provider_name: "twitter", user_id: user1.id, provider_uid: 100)
|
2018-07-27 07:28:51 -04:00
|
|
|
|
|
|
|
hash = {
|
|
|
|
info: {
|
2018-12-07 10:39:06 -05:00
|
|
|
email: user1.email,
|
|
|
|
username: "test",
|
|
|
|
name: "test",
|
|
|
|
nickname: "minion",
|
2018-07-27 07:28:51 -04:00
|
|
|
},
|
2018-12-07 10:39:06 -05:00
|
|
|
uid: "100",
|
|
|
|
provider: "twitter"
|
2018-07-27 07:28:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
result = authenticator.after_authenticate(hash, existing_account: user2)
|
|
|
|
|
|
|
|
expect(result.user.id).to eq(user2.id)
|
2018-12-07 10:39:06 -05: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 07:28:51 -04:00
|
|
|
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-27 07:28:51 -04: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 10:39:06 -05:00
|
|
|
UserAssociatedAccount.create!(provider_name: "twitter", user_id: user.id, provider_uid: 100)
|
2018-07-27 07:28:51 -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
|
2017-12-13 23:35:27 -05:00
|
|
|
end
|