2017-02-28 22:58:24 -05:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2017-08-23 23:01:11 -04:00
|
|
|
RSpec.describe Users::OmniauthCallbacksController do
|
2017-02-28 22:58:24 -05:00
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
OmniAuth.config.test_mode = true
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
2017-05-08 23:26:24 -04:00
|
|
|
Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:google_oauth2] = nil
|
2017-02-28 22:58:24 -05:00
|
|
|
OmniAuth.config.test_mode = false
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'Google Oauth2' do
|
|
|
|
before do
|
|
|
|
SiteSetting.enable_google_oauth2_logins = true
|
|
|
|
end
|
|
|
|
|
2017-05-04 15:35:03 -04:00
|
|
|
context "without an `omniauth.auth` env" do
|
|
|
|
it "should return a 404" do
|
|
|
|
get "/auth/eviltrout/callback"
|
2017-05-08 16:33:43 -04:00
|
|
|
expect(response.code).to eq("404")
|
2017-05-04 15:35:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-28 22:58:24 -05:00
|
|
|
describe 'when user has been verified' do
|
|
|
|
before do
|
|
|
|
OmniAuth.config.mock_auth[:google_oauth2] = OmniAuth::AuthHash.new(
|
|
|
|
provider: 'google_oauth2',
|
|
|
|
uid: '123545',
|
|
|
|
info: OmniAuth::AuthHash::InfoHash.new(
|
|
|
|
email: user.email,
|
|
|
|
name: 'Some name'
|
|
|
|
),
|
|
|
|
extra: {
|
|
|
|
raw_info: OmniAuth::AuthHash.new(
|
|
|
|
email_verified: true,
|
|
|
|
email: user.email,
|
|
|
|
family_name: 'Huh',
|
|
|
|
given_name: user.name,
|
|
|
|
gender: 'male',
|
|
|
|
name: "#{user.name} Huh",
|
|
|
|
)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:google_oauth2]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return the right response' do
|
2017-06-07 19:05:33 -04:00
|
|
|
expect(user.email_confirmed?).to eq(false)
|
|
|
|
|
2017-06-01 04:19:42 -04:00
|
|
|
events = DiscourseEvent.track_events do
|
|
|
|
get "/auth/google_oauth2/callback.json"
|
|
|
|
end
|
|
|
|
|
2017-06-07 19:05:33 -04:00
|
|
|
expect(events.map { |event| event[:event_name] }).to include(:user_logged_in, :user_first_logged_in)
|
2017-02-28 22:58:24 -05:00
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
|
|
|
|
response_body = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(response_body["authenticated"]).to eq(true)
|
|
|
|
expect(response_body["awaiting_activation"]).to eq(false)
|
|
|
|
expect(response_body["awaiting_approval"]).to eq(false)
|
|
|
|
expect(response_body["not_allowed_from_ip_address"]).to eq(false)
|
|
|
|
expect(response_body["admin_not_allowed_from_ip_address"]).to eq(false)
|
2017-06-07 19:05:33 -04:00
|
|
|
|
|
|
|
user.reload
|
|
|
|
expect(user.email_confirmed?).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should confirm email even when the tokens are expired" do
|
|
|
|
user.email_tokens.update_all(confirmed: false, expired: true)
|
|
|
|
|
|
|
|
user.reload
|
|
|
|
expect(user.email_confirmed?).to eq(false)
|
|
|
|
|
|
|
|
events = DiscourseEvent.track_events do
|
|
|
|
get "/auth/google_oauth2/callback.json"
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(events.map { |event| event[:event_name] }).to include(:user_logged_in, :user_first_logged_in)
|
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
|
|
|
|
user.reload
|
|
|
|
expect(user.email_confirmed?).to eq(true)
|
2017-02-28 22:58:24 -05:00
|
|
|
end
|
|
|
|
|
2017-12-12 07:08:57 -05:00
|
|
|
it "should activate/unstage staged user" do
|
|
|
|
user.update!(staged: true, registration_ip_address: nil)
|
|
|
|
|
|
|
|
user.reload
|
|
|
|
expect(user.staged).to eq(true)
|
|
|
|
expect(user.registration_ip_address).to eq(nil)
|
|
|
|
|
|
|
|
events = DiscourseEvent.track_events do
|
|
|
|
get "/auth/google_oauth2/callback.json"
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(events.map { |event| event[:event_name] }).to include(:user_logged_in, :user_first_logged_in)
|
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
|
|
|
|
user.reload
|
|
|
|
expect(user.staged).to eq(false)
|
|
|
|
expect(user.registration_ip_address).to be_present
|
|
|
|
end
|
|
|
|
|
2017-02-28 22:58:24 -05:00
|
|
|
context 'when user has not verified his email' do
|
|
|
|
before do
|
|
|
|
GoogleUserInfo.create!(google_user_id: '12345', user: user)
|
|
|
|
user.update!(active: false)
|
|
|
|
|
|
|
|
OmniAuth.config.mock_auth[:google_oauth2] = OmniAuth::AuthHash.new(
|
|
|
|
provider: 'google_oauth2',
|
|
|
|
uid: '12345',
|
|
|
|
info: OmniAuth::AuthHash::InfoHash.new(
|
|
|
|
email: 'someother_email@test.com',
|
|
|
|
name: 'Some name'
|
|
|
|
),
|
|
|
|
extra: {
|
|
|
|
raw_info: OmniAuth::AuthHash.new(
|
|
|
|
email_verified: true,
|
|
|
|
email: 'someother_email@test.com',
|
|
|
|
family_name: 'Huh',
|
|
|
|
given_name: user.name,
|
|
|
|
gender: 'male',
|
|
|
|
name: "#{user.name} Huh",
|
|
|
|
)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:google_oauth2]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return the right response' do
|
|
|
|
get "/auth/google_oauth2/callback.json"
|
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
|
|
|
|
response_body = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(user.reload.active).to eq(false)
|
|
|
|
expect(response_body["authenticated"]).to eq(false)
|
|
|
|
expect(response_body["awaiting_activation"]).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-09-12 11:36:17 -04:00
|
|
|
|
|
|
|
context 'after changing email' do
|
|
|
|
require_dependency 'email_updater'
|
|
|
|
|
|
|
|
def login(identity)
|
|
|
|
OmniAuth.config.mock_auth[:google_oauth2] = OmniAuth::AuthHash.new(
|
|
|
|
provider: 'google_oauth2',
|
|
|
|
uid: "123545#{identity[:username]}",
|
|
|
|
info: OmniAuth::AuthHash::InfoHash.new(
|
|
|
|
email: identity[:email],
|
|
|
|
name: 'Some name'
|
|
|
|
),
|
|
|
|
extra: {
|
|
|
|
raw_info: OmniAuth::AuthHash.new(
|
|
|
|
email_verified: true,
|
|
|
|
email: identity[:email],
|
|
|
|
family_name: 'Huh',
|
|
|
|
given_name: identity[:name],
|
|
|
|
gender: 'male',
|
|
|
|
name: "#{identity[:name]} Huh",
|
|
|
|
)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:google_oauth2]
|
|
|
|
|
|
|
|
get "/auth/google_oauth2/callback.json"
|
|
|
|
JSON.parse(response.body)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'activates the correct email' do
|
|
|
|
old_email = 'old@email.com'
|
|
|
|
old_identity = { name: 'Bob',
|
|
|
|
username: 'bob',
|
|
|
|
email: old_email }
|
|
|
|
user = Fabricate(:user, email: old_email)
|
|
|
|
new_email = 'new@email.com'
|
|
|
|
new_identity = { name: 'Bob',
|
|
|
|
username: 'boguslaw',
|
|
|
|
email: new_email }
|
|
|
|
|
|
|
|
updater = EmailUpdater.new(user.guardian, user)
|
|
|
|
updater.change_to(new_email)
|
|
|
|
|
|
|
|
user.reload
|
|
|
|
expect(user.email).to eq(old_email)
|
|
|
|
|
|
|
|
response = login(old_identity)
|
|
|
|
expect(response['authenticated']).to eq(true)
|
|
|
|
|
|
|
|
user.reload
|
|
|
|
expect(user.email).to eq(old_email)
|
|
|
|
|
|
|
|
response = login(new_identity)
|
|
|
|
expect(response['authenticated']).to eq(nil)
|
|
|
|
expect(response['email']).to eq(new_email)
|
|
|
|
end
|
|
|
|
end
|
2017-02-28 22:58:24 -05:00
|
|
|
end
|
|
|
|
end
|