2017-02-28 22:58:24 -05:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe "OmniAuth Callbacks" do
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|