Skip to content

Commit

Permalink
feat(cross-domain-redirect): redirect to APP_HOST when user is on APP…
Browse files Browse the repository at this point in the history
…_HOST_LEGACY
  • Loading branch information
Martin committed Feb 28, 2024
1 parent 8ca853c commit a135947
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
8 changes: 8 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions app/views/layouts/_switch_domain_banner.html.haml
Original file line number Diff line number Diff line change
@@ -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}")
})
1 change: 1 addition & 0 deletions app/views/layouts/application.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
50 changes: 48 additions & 2 deletions spec/helpers/application_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand Down

0 comments on commit a135947

Please sign in to comment.