OmniAuth full_host spec helper

Allows us to correctly set omniauth's full_host
so redirects take the port into account.

Needed when running selenium tests on a different port
This commit is contained in:
James Edwards-Jones 2019-07-04 08:53:31 +00:00 committed by James Lopez
parent ac6cebce12
commit efde6f7db9
4 changed files with 29 additions and 11 deletions

View file

@ -16,16 +16,8 @@ describe 'OAuth Login', :js, :allow_forgery_protection do
providers = [:github, :twitter, :bitbucket, :gitlab, :google_oauth2,
:facebook, :cas3, :auth0, :authentiq, :salesforce]
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
around(:all) do |example|
with_omniauth_full_host { example.run }
end
def login_with_provider(provider, enter_two_factor: false)

View file

@ -21,4 +21,16 @@ module DeviseHelpers
context.env
end
end
def with_omniauth_full_host(&block)
# 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) { ActionDispatch::Request.new(request).base_url }
yield
OmniAuth.config.full_host = omniauth_config_full_host
end
end

View file

@ -32,6 +32,10 @@ class FakeU2fDevice
")
end
def fake_u2f_authentication
@page.execute_script("window.gl.u2fAuthenticate.renderAuthenticated('abc');")
end
private
def u2f_device(app_id)

View file

@ -87,12 +87,17 @@ module LoginHelpers
click_link "oauth-login-#{provider}"
end
def fake_successful_u2f_authentication
allow(U2fRegistration).to receive(:authenticate).and_return(true)
FakeU2fDevice.new(page, nil).fake_u2f_authentication
end
def mock_auth_hash_with_saml_xml(provider, uid, email, saml_response)
response_object = { document: saml_xml(saml_response) }
mock_auth_hash(provider, uid, email, response_object: response_object)
end
def mock_auth_hash(provider, uid, email, response_object: nil)
def configure_mock_auth(provider, uid, email, response_object: nil)
# 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({
@ -118,6 +123,11 @@ module LoginHelpers
response_object: response_object
}
})
end
def mock_auth_hash(provider, uid, email, response_object: nil)
configure_mock_auth(provider, uid, email, response_object: response_object)
original_env_config_omniauth_auth = Rails.application.env_config['omniauth.auth']
Rails.application.env_config['omniauth.auth'] = OmniAuth.config.mock_auth[provider.to_sym]