mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-05-23 03:41:54 +08:00
Faraday [updated their authentication/authorization middleware in version 2](https://github.com/lostisland/faraday/pull/1306), which was a breaking change for the Twitter auth health check. The relevant test was manually mocking and stubbing the individual Faraday objects, so the relevant code path wasn't exercised by our tests. This commit: - Updates the test to use `stub_request` instead. (This correctly catches the deprecated method error.) - Updates the health check to use the new middleware.
98 lines
2.7 KiB
Ruby
Vendored
98 lines
2.7 KiB
Ruby
Vendored
# frozen_string_literal: true
|
|
|
|
RSpec.describe Auth::TwitterAuthenticator do
|
|
it "takes over account if email is supplied" do
|
|
auth = Auth::TwitterAuthenticator.new
|
|
|
|
user = Fabricate(:user)
|
|
|
|
auth_token = {
|
|
info: {
|
|
email: user.email,
|
|
username: "test",
|
|
name: "test",
|
|
nickname: "minion",
|
|
},
|
|
uid: "123",
|
|
provider: "twitter",
|
|
}
|
|
|
|
result = auth.after_authenticate(auth_token)
|
|
|
|
expect(result.user.id).to eq(user.id)
|
|
|
|
info = UserAssociatedAccount.find_by(provider_name: "twitter", user_id: user.id)
|
|
expect(info.info["email"]).to eq(user.email)
|
|
end
|
|
|
|
it "can connect to a different existing user account" do
|
|
authenticator = Auth::TwitterAuthenticator.new
|
|
user1 = Fabricate(:user)
|
|
user2 = Fabricate(:user)
|
|
|
|
UserAssociatedAccount.create!(provider_name: "twitter", user_id: user1.id, provider_uid: 100)
|
|
|
|
hash = {
|
|
info: {
|
|
email: user1.email,
|
|
username: "test",
|
|
name: "test",
|
|
nickname: "minion",
|
|
},
|
|
uid: "100",
|
|
provider: "twitter",
|
|
}
|
|
|
|
result = authenticator.after_authenticate(hash, existing_account: user2)
|
|
|
|
expect(result.user.id).to eq(user2.id)
|
|
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)
|
|
end
|
|
|
|
describe "revoke" do
|
|
fab!(:user)
|
|
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
|
|
UserAssociatedAccount.create!(provider_name: "twitter", user_id: user.id, provider_uid: 100)
|
|
expect(authenticator.can_revoke?).to eq(true)
|
|
expect(authenticator.revoke(user)).to eq(true)
|
|
expect(authenticator.description_for_user(user)).to eq("")
|
|
end
|
|
end
|
|
|
|
describe "#healthy?" do
|
|
let(:authenticator) { described_class.new }
|
|
|
|
before do
|
|
stub_request(:post, "https://api.twitter.com/oauth2/token").with(
|
|
basic_auth: [SiteSetting.twitter_consumer_key, SiteSetting.twitter_consumer_secret],
|
|
).to_return(status:)
|
|
end
|
|
|
|
context "when endpoint is reachable" do
|
|
let(:status) { 200 }
|
|
|
|
it { expect(authenticator).to be_healthy }
|
|
end
|
|
|
|
context "when credentials aren't recognized" do
|
|
let(:status) { 403 }
|
|
|
|
it { expect(authenticator).not_to be_healthy }
|
|
end
|
|
|
|
context "when an unexpected error happens" do
|
|
let(:status) { anything }
|
|
|
|
before { Faraday::Connection.any_instance.stubs(:post).raises(Faraday::ServerError) }
|
|
|
|
it { expect(authenticator).not_to be_healthy }
|
|
end
|
|
end
|
|
end
|