summaryrefslogtreecommitdiff
path: root/spec/features/oauth_login_spec.rb
blob: 4b8d15ec8dc80e9f3529b04c1b0926d950207a67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
require 'spec_helper'

feature 'OAuth Login', :js, :allow_forgery_protection do
  include DeviseHelpers

  def enter_code(code)
    fill_in 'user_otp_attempt', with: code
    click_button 'Verify code'
  end

  def stub_omniauth_config(provider)
    OmniAuth.config.add_mock(provider, OmniAuth::AuthHash.new(provider: provider.to_s, uid: "12345"))
    stub_omniauth_provider(provider)
  end

  before(:all) do
    # The OmniAuth `full_host` parameter doesn't get set correctly (it gets set to something like `http://localhost`
    # here), and causes integration tests to fail with 404s. We set the `full_host` by removing the request path (and
    # anything after it) from the request URI.
    @omniauth_config_full_host = OmniAuth.config.full_host
    OmniAuth.config.full_host = ->(request) { request['REQUEST_URI'].sub(/#{request['REQUEST_PATH']}.*/, '') }
  end

  after(:all) do
    OmniAuth.config.full_host = @omniauth_config_full_host
  end

  def login_with_provider(provider, enter_two_factor: false)
    if provider.to_s.starts_with?('ldap')
      gitlab_ldap_sign_with(user, remember: remember_me)
    else
      login_via(provider.to_s, user, uid, remember_me: remember_me)
    end

    enter_code(user.current_otp) if enter_two_factor
  end

    shared_context 'login_context' do
      let(:uid) { 'my-uid' }
      let(:remember_me) { false }
      let(:user) { create(:omniauth_user, extern_uid: uid, provider: provider.to_s) }
      let(:two_factor_user) { create(:omniauth_user, :two_factor, extern_uid: uid, provider: provider.to_s) }


      context 'when two-factor authentication is disabled' do
        it 'logs the user in' do
          login_with_provider(provider)

          expect(current_path).to eq root_path
        end
      end

      context 'when two-factor authentication is enabled' do
        let(:user) { two_factor_user }

        it 'logs the user in' do
          login_with_provider(provider, enter_two_factor: true)

          expect(current_path).to eq root_path
        end
      end

      context 'when "remember me" is checked' do
        let(:remember_me) { true }

        context 'when two-factor authentication is disabled' do
          it 'remembers the user after a browser restart' do
            login_with_provider(provider)

            clear_browser_session

            visit(root_path)
            expect(current_path).to eq root_path
          end
        end

        context 'when two-factor authentication is enabled' do
          let(:user) { two_factor_user }

          it 'remembers the user after a browser restart' do
            login_with_provider(provider, enter_two_factor: true)

            clear_browser_session

            visit(root_path)
            expect(current_path).to eq root_path
          end
        end
      end

      context 'when "remember me" is not checked' do
        context 'when two-factor authentication is disabled' do
          it 'does not remember the user after a browser restart' do
            login_with_provider(provider)

            clear_browser_session

            visit(root_path)
            expect(current_path).to eq new_user_session_path
          end
        end

        context 'when two-factor authentication is enabled' do
          let(:user) { two_factor_user }

          it 'does not remember the user after a browser restart' do
            login_with_provider(provider, enter_two_factor: true)

            clear_browser_session

            visit(root_path)
            expect(current_path).to eq new_user_session_path
          end
        end
      end
    end

  providers = [:github, :twitter, :bitbucket, :gitlab, :google_oauth2,
               :facebook, :cas3, :auth0, :authentiq]
  providers.each do |provider|
    context "when the user logs in using the #{provider} provider" do
      let(:provider) { provider }

      before do
        stub_omniauth_config(provider)
      end

      include_context 'login_context'
    end
  end

  context 'via ldap' do
    before do
      providers = Gitlab::OAuth::Provider.providers << :ldapmain
      allow(Gitlab::OAuth::Provider).to receive(:providers).and_return(providers)
      allow(User).to receive(:omniauth_providers).and_return(providers)
    end

    #TODO: deduplicate with controller spec
    let(:provider) { 'ldapmain' }
    let(:valid_login?) { true }
    let(:ldap_server_config) do
      { main: ldap_config_defaults(:main) }
    end
    def ldap_config_defaults(key, hash = {})
      {
        provider_name: "ldap#{key}",
        attributes: {},
        encryption: 'plain'
      }.merge(hash)
    end
    #TODO: De-duplicate this with controller spec
    before do
      stub_ldap_setting(enabled: true, servers: ldap_server_config)
      Ldap::OmniauthCallbacksController.define_providers!
      Rails.application.reload_routes!

      mock_auth_hash(provider.to_s, uid, user.email)
      stub_omniauth_provider(provider)

      allow(Gitlab::LDAP::Access).to receive(:allowed?).and_return(valid_login?)
    end

    include_context 'login_context'
  end
end