2019-07-25 05:27:42 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-09 08:51:59 +00:00
|
|
|
module DeviseHelpers
|
|
|
|
# explicitly tells Devise which mapping to use
|
|
|
|
# this is needed when we are testing a Devise controller bypassing the router
|
|
|
|
def set_devise_mapping(context:)
|
2018-01-09 16:47:31 +00:00
|
|
|
env = env_from_context(context)
|
2017-07-09 08:51:59 +00:00
|
|
|
|
|
|
|
env['devise.mapping'] = Devise.mappings[:user] if env
|
|
|
|
end
|
2018-01-09 16:47:31 +00:00
|
|
|
|
|
|
|
def env_from_context(context)
|
2018-11-20 06:33:45 +00:00
|
|
|
# When we modify env_config, that is on the global
|
|
|
|
# Rails.application, and we need to stub it and allow it to be
|
|
|
|
# modified in-place, without polluting later tests.
|
2018-01-09 16:47:31 +00:00
|
|
|
if context.respond_to?(:env_config)
|
2018-11-20 06:33:45 +00:00
|
|
|
context.env_config.deep_dup.tap do |env|
|
|
|
|
allow(context).to receive(:env_config).and_return(env)
|
|
|
|
end
|
|
|
|
# When we modify env, then the context is a request, or something
|
|
|
|
# else that only lives for a single spec.
|
2018-01-09 16:47:31 +00:00
|
|
|
elsif context.respond_to?(:env)
|
|
|
|
context.env
|
|
|
|
end
|
|
|
|
end
|
2019-07-04 08:53:31 +00:00
|
|
|
|
|
|
|
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
|
2017-07-09 08:51:59 +00:00
|
|
|
end
|