oauth_login_spec.rb 3.66 KB
Newer Older
1 2
require 'spec_helper'

3
describe 'OAuth Login', :js, :allow_forgery_protection do
4 5
  include DeviseHelpers

6 7 8 9 10 11
  def enter_code(code)
    fill_in 'user_otp_attempt', with: code
    click_button 'Verify code'
  end

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

16
  providers = [:github, :twitter, :bitbucket, :gitlab, :google_oauth2,
17
               :facebook, :cas3, :auth0, :authentiq]
18

19
  before(:all) do
20 21 22 23
    # 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
24
    OmniAuth.config.full_host = ->(request) { request['REQUEST_URI'].sub(/#{request['REQUEST_PATH']}.*/, '') }
25 26
  end

27 28 29 30
  after(:all) do
    OmniAuth.config.full_host = @omniauth_config_full_host
  end

31 32 33 34 35
  def login_with_provider(provider, enter_two_factor: false)
    login_via(provider.to_s, user, uid, remember_me: remember_me)
    enter_code(user.current_otp) if enter_two_factor
  end

36 37
  providers.each do |provider|
    context "when the user logs in using the #{provider} provider" do
38 39 40 41 42 43 44 45 46
      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) }

      before do
        stub_omniauth_config(provider)
      end

47
      context 'when two-factor authentication is disabled' do
48
        it 'logs the user in' do
49
          login_with_provider(provider)
50 51 52 53 54

          expect(current_path).to eq root_path
        end
      end

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

58
        it 'logs the user in' do
59
          login_with_provider(provider, enter_two_factor: true)
60 61 62 63

          expect(current_path).to eq root_path
        end
      end
64 65

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

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

72
            clear_browser_session
73 74 75 76 77 78

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

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

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

85
            clear_browser_session
86 87 88 89 90 91 92 93

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

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

98
            clear_browser_session
99 100 101 102 103 104

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

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

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

111
            clear_browser_session
112 113 114 115 116 117

            visit(root_path)
            expect(current_path).to eq new_user_session_path
          end
        end
      end
118 119 120
    end
  end
end