feat(cross-domain-redirect): redirect to APP_HOST when user is on APP_HOST_LEGACY

This commit is contained in:
Martin 2024-02-26 05:41:01 +01:00
parent 8ca853c79c
commit a13594792a
4 changed files with 64 additions and 2 deletions

View file

@ -8,6 +8,14 @@ module ApplicationHelper
Regexp.new(APP_HOST_LEGACY).match?(request.base_url)
end
def auto_switch_domain?(request, user_signed_in)
switch_domain_enabled?(request) && !user_signed_in && app_host_legacy?(request)
end
def switch_domain_enabled?(request)
request.params.key?(:switch_domain) || Flipper.enabled?(:switch_domain)
end
def html_lang
I18n.locale.to_s
end

View file

@ -0,0 +1,7 @@
- if auto_switch_domain?(request, user_signed_in?)
:javascript
const hintUrl = "#{image_url(FAVICONS_SRC["16px"])}"
fetch(hintUrl)
.then(function(){
window.location = window.location.href.replace("#{ApplicationHelper::APP_HOST_LEGACY}", "#{ApplicationHelper::APP_HOST}")
})

View file

@ -36,6 +36,7 @@
= yield(:invisible_captcha_styles)
= render partial: 'layouts/setup_theme'
= render partial: 'layouts/switch_domain_banner'
%body{ { id: content_for(:page_id), class: browser.platform.ios? ? 'ios' : nil, data: { controller: 'turbo number-input' } }.compact }
= render partial: 'layouts/skiplinks'

View file

@ -11,17 +11,63 @@ describe ApplicationHelper do
subject { app_host_legacy?(request) }
context 'request on ENV[APP_HOST_LEGACY]' do
context 'when request on ENV[APP_HOST_LEGACY]' do
let(:request_base_url) { app_host_legacy }
it { is_expected.to be_truthy }
end
context 'request on ENV[APP_HOST]' do
context 'when request on ENV[APP_HOST]' do
let(:request_base_url) { app_host }
it { is_expected.to be_falsey }
end
end
describe 'auto_switch_domain?' do
subject { auto_switch_domain?(request, user_signed_in) }
context 'when user_signed_in? is true' do
let(:user_signed_in) { true }
let(:request) { instance_double(ActionDispatch::Request, base_url: 'osf', params: {}) }
it { is_expected.to be_falsey }
end
context 'when user_signed_in? is false' do
let(:user_signed_in) { false }
let(:params) { {} }
let(:request) { instance_double(ActionDispatch::Request, base_url: request_base_url, params:) }
let(:app_host_legacy) { 'legacy' }
let(:app_host) { 'host' }
before do
stub_const("ApplicationHelper::APP_HOST_LEGACY", app_host_legacy)
stub_const("ApplicationHelper::APP_HOST", app_host)
end
context 'request on ENV[APP_HOST_LEGACY] without feature or url' do
let(:request_base_url) { app_host_legacy }
it { is_expected.to be_falsey }
end
context 'request on ENV[APP_HOST_LEGACY] with switch_domain params' do
let(:params) { { switch_domain: '1' } }
let(:request_base_url) { app_host_legacy }
it { is_expected.to be_truthy }
end
context 'request on ENV[APP_HOST_LEGACY] with switch_domain params' do
before { Flipper.enable :switch_domain }
after { Flipper.disable :switch_domain }
let(:request_base_url) { app_host_legacy }
it { is_expected.to be_truthy }
end
context 'request on ENV[APP_HOST]' do
let(:request_base_url) { app_host }
it { is_expected.to be_falsey }
end
end
end
describe "#flash_class" do
it { expect(flash_class('notice')).to eq 'alert-success' }
it { expect(flash_class('alert', sticky: true, fixed: true)).to eq 'alert-danger sticky alert-fixed' }