Add integration tests around OAuth login.
- There was previously a test for `saml` login in `login_spec`, but this didn't seem to be passing. A lot of things didn't seem right here, and I suspect that this test hasn't been running. I'll investigate this further. - It took almost a whole working day to figure out this line: OmniAuth.config.full_host = ->(request) { request['REQUEST_URI'].sub(request['REQUEST_PATH'], '') } As always, it's obvious in retrospect, but it took some digging to figure out tests were failing and returning 404s during the callback phase. - Test all OAuth providers - github, twitter, bitbucket, gitlab, google, and facebook
This commit is contained in:
parent
633793cf47
commit
e936db963e
3 changed files with 66 additions and 1 deletions
|
@ -6,7 +6,7 @@
|
|||
- providers.each do |provider|
|
||||
%span.light
|
||||
- has_icon = provider_has_icon?(provider)
|
||||
= link_to provider_image_tag(provider), omniauth_authorize_path(:user, provider), method: :post, class: 'oauth-login' + (has_icon ? ' oauth-image-link' : ' btn')
|
||||
= link_to provider_image_tag(provider), omniauth_authorize_path(:user, provider), method: :post, class: 'oauth-login' + (has_icon ? ' oauth-image-link' : ' btn'), id: "oauth-login-#{provider}"
|
||||
%fieldset
|
||||
= check_box_tag :remember_me
|
||||
= label_tag :remember_me, "Remember Me"
|
||||
|
|
58
spec/features/oauth_login_spec.rb
Normal file
58
spec/features/oauth_login_spec.rb
Normal file
|
@ -0,0 +1,58 @@
|
|||
require 'spec_helper'
|
||||
|
||||
feature 'OAuth Login', feature: true, js: true do
|
||||
def enter_code(code)
|
||||
fill_in 'user_otp_attempt', with: code
|
||||
click_button 'Verify code'
|
||||
end
|
||||
|
||||
def provider_config(provider)
|
||||
OpenStruct.new(name: provider.to_s, app_id: 'app_id', app_secret: 'app_secret')
|
||||
end
|
||||
|
||||
def stub_omniauth_config(provider)
|
||||
OmniAuth.config.add_mock(provider, OmniAuth::AuthHash.new({ provider: provider.to_s, uid: "12345" }))
|
||||
Rails.application.env_config['devise.mapping'] = Devise.mappings[:user]
|
||||
Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[provider]
|
||||
end
|
||||
|
||||
providers = [:github, :twitter, :bitbucket, :gitlab, :google_oauth2, :facebook]
|
||||
|
||||
before do
|
||||
OmniAuth.config.full_host = ->(request) { request['REQUEST_URI'].sub(request['REQUEST_PATH'], '') }
|
||||
|
||||
messages = {
|
||||
enabled: true,
|
||||
allow_single_sign_on: providers.map(&:to_s),
|
||||
providers: providers.map { |provider| provider_config(provider) }
|
||||
}
|
||||
|
||||
allow(Gitlab.config.omniauth).to receive_messages(messages)
|
||||
end
|
||||
|
||||
providers.each do |provider|
|
||||
context "when the user logs in using the #{provider} provider" do
|
||||
context "when two-factor authentication is disabled" do
|
||||
it 'logs the user in' do
|
||||
stub_omniauth_config(provider)
|
||||
user = create(:omniauth_user, extern_uid: 'my-uid', provider: provider.to_s)
|
||||
login_via(provider.to_s, user, 'my-uid')
|
||||
|
||||
expect(current_path).to eq root_path
|
||||
save_screenshot
|
||||
end
|
||||
end
|
||||
|
||||
context "when two-factor authentication is enabled" do
|
||||
it 'logs the user in' do
|
||||
stub_omniauth_config(provider)
|
||||
user = create(:omniauth_user, :two_factor, extern_uid: 'my-uid', provider: provider.to_s)
|
||||
login_via(provider.to_s, user, 'my-uid')
|
||||
|
||||
enter_code(user.current_otp)
|
||||
expect(current_path).to eq root_path
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -62,6 +62,13 @@ module LoginHelpers
|
|||
Thread.current[:current_user] = user
|
||||
end
|
||||
|
||||
def login_via(provider, user, uid)
|
||||
mock_auth_hash(provider, uid, user.email)
|
||||
visit new_user_session_path
|
||||
expect(page).to have_content('Sign in with')
|
||||
click_link "oauth-login-#{provider}"
|
||||
end
|
||||
|
||||
def mock_auth_hash(provider, uid, email)
|
||||
# The mock_auth configuration allows you to set per-provider (or default)
|
||||
# authentication hashes to return during integration testing.
|
||||
|
|
Loading…
Reference in a new issue