login_helpers.rb 4.9 KB
Newer Older
1 2
require_relative 'devise_helpers'

3
module LoginHelpers
4 5
  include DeviseHelpers

6 7 8 9 10 11 12 13
  # Overriding Devise::Test::IntegrationHelpers#sign_in to store @current_user
  # since we may need it in LiveDebugger#live_debug.
  def sign_in(resource, scope: nil)
    super

    @current_user = resource
  end

R
Rémy Coutable 已提交
14 15 16 17 18 19 20
  # Overriding Devise::Test::IntegrationHelpers#sign_out to clear @current_user.
  def sign_out(resource_or_scope)
    super

    @current_user = nil
  end

21
  # Internal: Log in as a specific user or a new user of a specific role
22
  #
23 24 25 26 27
  # user_or_role - User object, or a role to create (e.g., :admin, :user)
  #
  # Examples:
  #
  #   # Create a user automatically
28
  #   gitlab_sign_in(:user)
29 30
  #
  #   # Create an admin automatically
31
  #   gitlab_sign_in(:admin)
32 33 34
  #
  #   # Provide an existing User record
  #   user = create(:user)
35
  #   gitlab_sign_in(user)
36
  def gitlab_sign_in(user_or_role, **kwargs)
37
    user =
D
Douwe Maan 已提交
38
      if user_or_role.is_a?(User)
D
Douwe Maan 已提交
39 40 41 42
        user_or_role
      else
        create(user_or_role)
      end
43

44 45
    gitlab_sign_in_with(user, **kwargs)

46
    @current_user = user
47 48
  end

49 50 51 52 53 54 55 56 57 58
  def gitlab_sign_in_via(provider, user, uid)
    mock_auth_hash(provider, uid, user.email)
    visit new_user_session_path
    click_link provider
  end

  # Requires Javascript driver.
  def gitlab_sign_out
    find(".header-user-dropdown-toggle").click
    click_link "Sign out"
R
Rémy Coutable 已提交
59
    @current_user = nil
60

61
    expect(page).to have_button('Sign in')
62 63 64 65 66
  end

  private

  # Private: Login as the specified user
67
  #
68 69
  # user     - User instance to login with
  # remember - Whether or not to check "Remember me" (default: false)
70
  def gitlab_sign_in_with(user, remember: false)
71
    visit new_user_session_path
72

73
    fill_in "user_login", with: user.email
74
    fill_in "user_password", with: "12345678"
75
    check 'user_remember_me' if remember
76

B
Bryce Johnson 已提交
77
    click_button "Sign in"
78 79
  end

80
  def login_via(provider, user, uid, remember_me: false)
81 82 83
    mock_auth_hash(provider, uid, user.email)
    visit new_user_session_path
    expect(page).to have_content('Sign in with')
84

85
    check 'remember_me' if remember_me
86

87 88 89
    click_link "oauth-login-#{provider}"
  end

90 91 92 93 94 95 96 97 98 99 100 101 102 103
  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.
    OmniAuth.config.mock_auth[provider.to_sym] = OmniAuth::AuthHash.new({
      provider: provider,
      uid: uid,
      info: {
        name: 'mockuser',
        email: email,
        image: 'mock_user_thumbnail_url'
      },
      credentials: {
        token: 'mock_token',
        secret: 'mock_secret'
104 105 106 107 108 109 110 111 112
      },
      extra: {
        raw_info: {
          info: {
            name: 'mockuser',
            email: email,
            image: 'mock_user_thumbnail_url'
          }
        }
113 114
      }
    })
115
    Rails.application.env_config['omniauth.auth'] = OmniAuth.config.mock_auth[provider.to_sym]
116
  end
117 118 119 120 121 122 123 124 125 126 127

  def mock_saml_config
    OpenStruct.new(name: 'saml', label: 'saml', args: {
      assertion_consumer_service_url: 'https://localhost:3443/users/auth/saml/callback',
      idp_cert_fingerprint: '26:43:2C:47:AF:F0:6B:D0:07:9C:AD:A3:74:FE:5D:94:5F:4E:9E:52',
      idp_sso_target_url: 'https://idp.example.com/sso/saml',
      issuer: 'https://localhost:3443/',
      name_identifier_format: 'urn:oasis:names:tc:SAML:2.0:nameid-format:transient'
    })
  end

128 129 130 131
  def stub_omniauth_provider(provider, context: Rails.application)
    env = env_from_context(context)

    set_devise_mapping(context: context)
132
    env['omniauth.auth'] = OmniAuth.config.mock_auth[provider.to_sym]
133 134
  end

135 136 137 138 139 140 141 142
  def stub_omniauth_failure(strategy, message_key, exception = nil)
    env = @request.env

    env['omniauth.error'] = exception
    env['omniauth.error.type'] = message_key.to_sym
    env['omniauth.error.strategy'] = strategy
  end

143
  def stub_omniauth_saml_config(messages)
144
    set_devise_mapping(context: Rails.application)
145 146 147 148
    Rails.application.routes.disable_clear_and_finalize = true
    Rails.application.routes.draw do
      post '/users/auth/saml' => 'omniauth_callbacks#saml'
    end
149
    allow(Gitlab::Auth::OAuth::Provider).to receive_messages(providers: [:saml], config_for: mock_saml_config)
150
    stub_omniauth_setting(messages)
151 152 153 154
    stub_saml_authorize_path_helpers
  end

  def stub_saml_authorize_path_helpers
T
Timothy Andrew 已提交
155 156
    allow_any_instance_of(Object).to receive(:user_saml_omniauth_authorize_path).and_return('/users/auth/saml')
    allow_any_instance_of(Object).to receive(:omniauth_authorize_path).with(:user, "saml").and_return('/users/auth/saml')
157
  end
158 159 160 161 162 163

  def stub_omniauth_config(messages)
    allow(Gitlab.config.omniauth).to receive_messages(messages)
  end

  def stub_basic_saml_config
164
    allow(Gitlab::Auth::Saml::Config).to receive_messages({ options: { name: 'saml', args: {} } })
165 166 167
  end

  def stub_saml_group_config(groups)
168
    allow(Gitlab::Auth::Saml::Config).to receive_messages({ options: { name: 'saml', groups_attribute: 'groups', external_groups: groups, args: {} } })
169
  end
170
end