2019-11-25 06:31:57 -06:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
RSpec.describe Users::AssociateAccountsController do
|
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
fab!(:user2) { Fabricate(:user) }
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
before { OmniAuth.config.test_mode = true }
|
2019-11-25 06:31:57 -06:00
|
|
|
|
|
|
|
after do
|
|
|
|
Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:google_oauth2] = nil
|
|
|
|
OmniAuth.config.test_mode = false
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
context "when attempting reconnect" do
|
2019-11-25 06:31:57 -06:00
|
|
|
before do
|
|
|
|
SiteSetting.enable_google_oauth2_logins = true
|
|
|
|
OmniAuth.config.mock_auth[:google_oauth2] = OmniAuth::AuthHash.new(
|
2023-01-09 05:18:21 -06:00
|
|
|
provider: "google_oauth2",
|
|
|
|
uid: "12345",
|
2019-11-25 06:31:57 -06:00
|
|
|
info: {
|
2023-01-09 05:18:21 -06:00
|
|
|
email: "someemail@test.com",
|
2019-11-25 06:31:57 -06:00
|
|
|
},
|
|
|
|
extra: {
|
|
|
|
raw_info: {
|
|
|
|
email_verified: true,
|
2023-01-09 05:18:21 -06:00
|
|
|
},
|
2019-11-25 06:31:57 -06:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:google_oauth2]
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "should work correctly" do
|
2019-11-25 06:31:57 -06:00
|
|
|
sign_in(user)
|
|
|
|
|
|
|
|
# Reconnect flow:
|
|
|
|
post "/auth/google_oauth2?reconnect=true"
|
|
|
|
expect(response.status).to eq(302)
|
|
|
|
expect(session[:auth_reconnect]).to eq(true)
|
|
|
|
|
|
|
|
OmniAuth.config.mock_auth[:google_oauth2].uid = "123456"
|
|
|
|
get "/auth/google_oauth2/callback.json"
|
|
|
|
expect(response.status).to eq(302)
|
|
|
|
|
|
|
|
expect(session[:current_user_id]).to eq(user.id) # Still logged in
|
|
|
|
expect(UserAssociatedAccount.count).to eq(0) # Reconnect has not yet happened
|
|
|
|
|
|
|
|
# Request associate info
|
|
|
|
uri = URI.parse(response.redirect_url)
|
|
|
|
get "#{uri.path}.json"
|
2020-05-07 10:04:12 -05:00
|
|
|
data = response.parsed_body
|
2019-11-25 06:31:57 -06:00
|
|
|
expect(data["provider_name"]).to eq("google_oauth2")
|
|
|
|
expect(data["account_description"]).to eq("someemail@test.com")
|
|
|
|
|
|
|
|
# Make the connection
|
|
|
|
events = DiscourseEvent.track_events { post "#{uri.path}.json" }
|
2020-11-13 08:41:54 -06:00
|
|
|
expect(events.any? { |e| e[:event_name] == :before_auth }).to eq(true)
|
2023-01-09 05:18:21 -06:00
|
|
|
expect(
|
|
|
|
events.any? do |e|
|
|
|
|
e[:event_name] === :after_auth && Auth::GoogleOAuth2Authenticator === e[:params][0] &&
|
|
|
|
!e[:params][1].failed?
|
|
|
|
end,
|
|
|
|
).to eq(true)
|
2019-11-25 06:31:57 -06:00
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(UserAssociatedAccount.count).to eq(1)
|
|
|
|
|
|
|
|
# Token cannot be reused
|
|
|
|
get "#{uri.path}.json"
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "should only work within the current session" do
|
2021-08-10 08:09:30 -05:00
|
|
|
sign_in(user)
|
|
|
|
|
|
|
|
post "/auth/google_oauth2?reconnect=true"
|
|
|
|
expect(response.status).to eq(302)
|
|
|
|
expect(session[:auth_reconnect]).to eq(true)
|
|
|
|
|
|
|
|
OmniAuth.config.mock_auth[:google_oauth2].uid = "123456"
|
|
|
|
get "/auth/google_oauth2/callback.json"
|
|
|
|
expect(response.status).to eq(302)
|
|
|
|
|
|
|
|
expect(session[:current_user_id]).to eq(user.id) # Still logged in
|
|
|
|
expect(UserAssociatedAccount.count).to eq(0) # Reconnect has not yet happened
|
|
|
|
|
|
|
|
uri = URI.parse(response.redirect_url)
|
|
|
|
get "#{uri.path}.json"
|
|
|
|
data = response.parsed_body
|
|
|
|
expect(data["provider_name"]).to eq("google_oauth2")
|
|
|
|
expect(data["account_description"]).to eq("someemail@test.com")
|
|
|
|
|
|
|
|
cookies.delete "_forum_session"
|
|
|
|
|
|
|
|
get "#{uri.path}.json"
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
end
|
|
|
|
|
2021-06-04 00:30:29 -05:00
|
|
|
it "returns the correct response for non-existent tokens" do
|
2021-08-05 11:36:34 -05:00
|
|
|
sign_in(user)
|
|
|
|
|
2019-11-25 06:31:57 -06:00
|
|
|
get "/associate/12345678901234567890123456789012.json"
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
|
|
|
|
get "/associate/shorttoken.json"
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
end
|
2021-08-05 11:36:34 -05:00
|
|
|
|
|
|
|
it "requires login" do
|
|
|
|
# XHR should 403
|
|
|
|
get "/associate/#{SecureRandom.hex}.json"
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
2019-11-25 06:31:57 -06:00
|
|
|
end
|
|
|
|
end
|