chore: cross domain banner between APP_HOST_LEGACY and APP_HOST

This commit is contained in:
Colin Darie 2024-02-23 14:05:12 +01:00
parent 61a9bedfff
commit 4dd24f0925
No known key found for this signature in database
GPG key ID: 8C76CADD40253590
10 changed files with 137 additions and 11 deletions

1
.env.test Normal file
View file

@ -0,0 +1 @@
APP_HOST="test.host" # must match host defined in spec/rails_helper.rb

View file

@ -10,7 +10,6 @@ ol.fr-ol-content--override {
}
}
// with Marianne font, weight of font is less bolder, so bold it up
.button.primary {
font-weight: bold;
@ -20,10 +19,17 @@ trix-editor.fr-input {
max-height: none;
}
.fr-label + .fr-ds-combobox { // same as .fr-label + .fr-input
margin-top: 0.5rem;
.fr-header {
.fr-notice {
// get back link underlined in notices, because they are usually hidden in headers
--underline-img: linear-gradient(0deg, currentColor, currentColor);
}
}
.fr-label + .fr-ds-combobox {
// same as .fr-label + .fr-input
margin-top: 0.5rem;
}
.fr-ds-combobox {
.fr-menu {

View file

@ -0,0 +1,37 @@
# frozen_string_literal: true
class SwitchDomainBannerComponent < ApplicationComponent
attr_reader :user
def initialize(user:)
@user = user
end
def render?
return false unless helpers.switch_domain_enabled?(request)
# TODO if preferred hosts
true
end
def auto_switch?
helpers.auto_switch_domain?(request, user.present?)
end
def manual_switch?
helpers.app_host_legacy?(request) && user.present?
end
def new_host_url
helpers.url_for(url_options)
end
end
private
def url_options
request.params.except(:switch_domain).merge(host: ApplicationHelper::APP_HOST)
end
end

View file

@ -0,0 +1,5 @@
---
en:
message_new_domain: "demarches-simplifiees.fr is now called"
follow_link: Follow this link to connect to the new address.
detected_error: We have detected a network error in your access. Contact your IT department to authorize connection to demarches.gouv.fr.

View file

@ -0,0 +1,5 @@
---
fr:
message_new_domain: "demarches-simplifiees.fr sappelle maintenant"
follow_link: Suivez ce lien pour vous connecter à la nouvelle adresse.
detected_error: Nous avons détecté une erreur réseau pour vous y accéder. Contactez votre service informatique pour autoriser la connexion sur demarches.gouv.fr.

View file

@ -0,0 +1,19 @@
- if auto_switch?
:javascript
const hintUrl = "http://#{ApplicationHelper::APP_HOST}/favicon.ico"
fetch(hintUrl, { mode: 'cors' }).then(() => {
window.location = '#{new_host_url}';
}).catch((e) => {
// TODO: notify us in order to monitor connectivity issues volume
})
= render Dsfr::NoticeComponent.new(closable: true) do |c|
- c.with_title do
= t(".message_new_domain")
= "#{helpers.link_to APPLICATION_NAME, new_host_url}."
- if manual_switch?
= t(".follow_link")
- elsif auto_switch? || true
= t(".detected_error")

View file

@ -70,6 +70,8 @@
- if is_expert_context
= render partial: 'layouts/search_dossiers_form'
= render SwitchDomainBannerComponent.new(user: current_user)
#modal-header__menu.fr-header__menu.fr-modal{ "aria-labelledby": "navbar-burger-button" }
.fr-container
%button.fr-btn--close.fr-btn{ "aria-controls" => "modal-header__menu", title: t('close_modal', scope: [:layouts, :header]) }= t('close_modal', scope: [:layouts, :header])

View file

@ -1,7 +0,0 @@
- 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,7 +36,6 @@
= 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

@ -0,0 +1,59 @@
# frozen_string_literal: true
require "rails_helper"
RSpec.describe SwitchDomainBannerComponent, type: :component do
let(:app_host_legacy) { "demarches-simplifiees.fr" }
let(:app_host) { "demarches.gouv.fr" }
let(:user) { create(:user) }
let(:request_host) { app_host_legacy }
let(:path) { "/" }
before do
allow(Current).to receive(:host).and_return(app_host)
stub_const("ApplicationHelper::APP_HOST_LEGACY", app_host_legacy)
stub_const("ApplicationHelper::APP_HOST", app_host)
Flipper.enable(:switch_domain)
end
after do
Flipper.disable(:switch_domain)
end
subject(:rendered) do
with_request_url path, host: request_host, format: nil do
render_inline(described_class.new(user: user))
end
end
context "when request is already on APP_HOST" do
let(:request_host) { app_host }
it "notify about names change" do
expect(rendered.to_html).to include("demarches-simplifiees.fr")
expect(rendered.to_html).to include(app_host)
expect(rendered.to_html).not_to include("window.location")
expect(rendered.to_html).not_to include("Suivez ce lien")
end
context "when user has already set preferred domain" do
let(:user) { create(:user, preferred_domain: :demarches_gouv_fr) }
it "does not render the banner" do
expect(rendered.to_html).to be_empty
end
end
end
describe "URL generation" do
let(:path) { "/admin/procedures" }
it "generate an url to the new domain" do
expect(rendered.to_html).to have_link(APPLICATION_NAME, href: "http://demarches.gouv.fr/admin/procedures")
expect(rendered.to_html).not_to include("window.location")
expect(rendered.to_html).to include("Suivez ce lien")
end
end
end