From a13594792a77d0185cc4b26fee936defad0e3e9e Mon Sep 17 00:00:00 2001 From: Martin Date: Mon, 26 Feb 2024 05:41:01 +0100 Subject: [PATCH] feat(cross-domain-redirect): redirect to APP_HOST when user is on APP_HOST_LEGACY --- app/helpers/application_helper.rb | 8 +++ .../layouts/_switch_domain_banner.html.haml | 7 +++ app/views/layouts/application.html.haml | 1 + spec/helpers/application_helper_spec.rb | 50 ++++++++++++++++++- 4 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 app/views/layouts/_switch_domain_banner.html.haml diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index bd46cecf189..a93b202f2ac 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -8,6 +8,14 @@ def app_host_legacy?(request) 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 diff --git a/app/views/layouts/_switch_domain_banner.html.haml b/app/views/layouts/_switch_domain_banner.html.haml new file mode 100644 index 00000000000..0c63e32a190 --- /dev/null +++ b/app/views/layouts/_switch_domain_banner.html.haml @@ -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}") + }) diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index e0cc5f86e12..0aa9a9fe2f4 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -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' diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index 0bb9a1471f0..dc3998c9b57 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -11,17 +11,63 @@ 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' }