Merge pull request #10042 from mfo/US/vers-demarches-gouv-et-lau-dela

tech(redirect): ETQ usager non connecté, je suis automatiquement redirigé de demarches-simplifiees vers le nouvel host
This commit is contained in:
mfo 2024-02-28 10:30:42 +00:00 committed by GitHub
commit c0d120545e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 94 additions and 0 deletions

View file

@ -1,4 +1,21 @@
module ApplicationHelper
APP_HOST = ENV['APP_HOST']
APP_HOST_LEGACY = ENV['APP_HOST_LEGACY']
def app_host_legacy?(request)
return false if APP_HOST_LEGACY.blank?
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

@ -1,4 +1,73 @@
describe ApplicationHelper do
describe 'app_host_legacy?' do
let(:request) { instance_double(ActionDispatch::Request, base_url: request_base_url) }
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
subject { app_host_legacy?(request) }
context 'when request on ENV[APP_HOST_LEGACY]' do
let(:request_base_url) { app_host_legacy }
it { is_expected.to be_truthy }
end
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' }