summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Andrew <mail@timothyandrew.net>2017-06-14 04:30:07 +0000
committerTimothy Andrew <mail@timothyandrew.net>2017-07-03 16:25:10 +0000
commitdd9264011bf554567b3e7f860c2acbf53dfa3f77 (patch)
treeb07c14d363bbbf175bc48518268f73a220afc0f1
parent5b649ac64dc2a987e2bede544dd13c1fab2d55a4 (diff)
downloadgitlab-ce-dd9264011bf554567b3e7f860c2acbf53dfa3f77.tar.gz
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
-rw-r--r--app/views/devise/shared/_omniauth_box.html.haml2
-rw-r--r--spec/features/oauth_login_spec.rb58
-rw-r--r--spec/support/login_helpers.rb7
3 files changed, 66 insertions, 1 deletions
diff --git a/app/views/devise/shared/_omniauth_box.html.haml b/app/views/devise/shared/_omniauth_box.html.haml
index acb38c300b9..e06b804e349 100644
--- a/app/views/devise/shared/_omniauth_box.html.haml
+++ b/app/views/devise/shared/_omniauth_box.html.haml
@@ -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"
diff --git a/spec/features/oauth_login_spec.rb b/spec/features/oauth_login_spec.rb
new file mode 100644
index 00000000000..f960dacdcac
--- /dev/null
+++ b/spec/features/oauth_login_spec.rb
@@ -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
diff --git a/spec/support/login_helpers.rb b/spec/support/login_helpers.rb
index 4c88958264b..27f12cacc62 100644
--- a/spec/support/login_helpers.rb
+++ b/spec/support/login_helpers.rb
@@ -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.