From 5ffaf7a80ee6ffa2a810aad1b8cdb9b182537645 Mon Sep 17 00:00:00 2001 From: taitus Date: Mon, 9 Dec 2024 16:34:27 +0100 Subject: [PATCH] Add scripts component in order to enable/disable vendor scripts --- .erb-lint.yml | 1 + .../vendors/scripts_component.html.erb | 16 +++++++++ .../vendors/scripts_component.rb | 5 +++ app/views/layouts/application.html.erb | 1 + .../vendors/scripts_component_spec.rb | 32 ++++++++++++++++++ spec/system/cookies_consent_spec.rb | 33 ++++++++++++++++++- 6 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 app/components/layout/cookies_consent/vendors/scripts_component.html.erb create mode 100644 app/components/layout/cookies_consent/vendors/scripts_component.rb create mode 100644 spec/components/layout/cookies_consent/vendors/scripts_component_spec.rb diff --git a/.erb-lint.yml b/.erb-lint.yml index 31bcfa563..aeb957940 100644 --- a/.erb-lint.yml +++ b/.erb-lint.yml @@ -13,6 +13,7 @@ linters: enabled: true exclude: - app/components/budgets/investments/content_blocks_component.html.erb + - app/components/layout/cookies_consent/vendors/scripts_component.html.erb - app/components/layout/footer_component.html.erb - app/components/layout/social_component.html.erb - app/components/layout/subnavigation_component.html.erb diff --git a/app/components/layout/cookies_consent/vendors/scripts_component.html.erb b/app/components/layout/cookies_consent/vendors/scripts_component.html.erb new file mode 100644 index 000000000..a21ef23e1 --- /dev/null +++ b/app/components/layout/cookies_consent/vendors/scripts_component.html.erb @@ -0,0 +1,16 @@ + diff --git a/app/components/layout/cookies_consent/vendors/scripts_component.rb b/app/components/layout/cookies_consent/vendors/scripts_component.rb new file mode 100644 index 000000000..6e0f3b30a --- /dev/null +++ b/app/components/layout/cookies_consent/vendors/scripts_component.rb @@ -0,0 +1,5 @@ +class Layout::CookiesConsent::Vendors::ScriptsComponent < Layout::CookiesConsent::BaseComponent + def render? + super && vendors.any? + end +end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index fdb5289ee..b0daa354b 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -11,6 +11,7 @@ <%= content_for :social_media_meta_tags %> <%= raw setting["html.per_page_code_head"] %> + <%= render Layout::CookiesConsent::Vendors::ScriptsComponent.new %> <%= render Layout::SkipToMainContentComponent.new %> diff --git a/spec/components/layout/cookies_consent/vendors/scripts_component_spec.rb b/spec/components/layout/cookies_consent/vendors/scripts_component_spec.rb new file mode 100644 index 000000000..b88779600 --- /dev/null +++ b/spec/components/layout/cookies_consent/vendors/scripts_component_spec.rb @@ -0,0 +1,32 @@ +require "rails_helper" + +describe Layout::CookiesConsent::Vendors::ScriptsComponent do + before do + Setting["feature.cookies_consent"] = true + end + + it "is not rendered when there are no vendors" do + render_inline Layout::CookiesConsent::Vendors::ScriptsComponent.new + + expect(page).not_to be_rendered + end + + it "is not rendered when cookies consent feature is not enabled" do + Setting["feature.cookies_consent"] = false + create(:cookies_vendor, name: "Third party", cookie: "third_party", script: "alert('Welcome!')") + + render_inline Layout::CookiesConsent::Vendors::ScriptsComponent.new + + expect(page).not_to be_rendered + end + + it "renders script for vendor" do + create(:cookies_vendor, name: "Third party", cookie: "third_party", script: "alert('Welcome!')") + + render_inline Layout::CookiesConsent::Vendors::ScriptsComponent.new + + expect(page).to have_content "function third_party_script()" + expect(page).to have_content "alert('Welcome!')" + expect(page).to have_css "script", visible: :none + end +end diff --git a/spec/system/cookies_consent_spec.rb b/spec/system/cookies_consent_spec.rb index 5d18a240e..e2cfb94d3 100644 --- a/spec/system/cookies_consent_spec.rb +++ b/spec/system/cookies_consent_spec.rb @@ -3,7 +3,8 @@ require "rails_helper" describe "Cookies consent" do before do Setting["feature.cookies_consent"] = true - create(:cookies_vendor, name: "Third party", cookie: "third_party" ) + script = "$('body').append('Running third party script');" + create(:cookies_vendor, name: "Third party", cookie: "third_party", script: script) end context "Banner consent" do @@ -12,6 +13,7 @@ describe "Cookies consent" do expect(cookie_by_name("cookies_consent")).to be nil expect(cookie_by_name("third_party")).to be nil + expect(page).not_to have_content "Running third party script" within ".cookies-consent-banner" do click_button "Accept essential cookies" @@ -20,10 +22,12 @@ describe "Cookies consent" do expect(cookie_by_name("cookies_consent")[:value]).to eq "essential" expect(cookie_by_name("third_party")[:value]).to eq "false" expect(page).not_to have_content "Cookies policy" + expect(page).not_to have_content "Running third party script" refresh expect(page).not_to have_content "Cookies policy" + expect(page).not_to have_content "Running third party script" end scenario "Hides the banner when accepting all cookies and for consecutive visits" do @@ -31,6 +35,7 @@ describe "Cookies consent" do expect(cookie_by_name("cookies_consent")).to be nil expect(cookie_by_name("third_party")).to be nil + expect(page).not_to have_content "Running third party script" within ".cookies-consent-banner" do click_button "Accept all" @@ -39,10 +44,12 @@ describe "Cookies consent" do expect(cookie_by_name("cookies_consent")[:value]).to eq "all" expect(cookie_by_name("third_party")[:value]).to eq "true" expect(page).not_to have_content "Cookies policy" + expect(page).not_to have_content "Running third party script" refresh expect(page).not_to have_content "Cookies policy" + expect(page).to have_content "Running third party script", count: 1 end end @@ -52,6 +59,7 @@ describe "Cookies consent" do expect(cookie_by_name("cookies_consent")).to be nil expect(cookie_by_name("third_party")).to be nil + expect(page).not_to have_content "Running third party script" within ".cookies-consent-banner" do click_button "Manage cookies" @@ -65,10 +73,12 @@ describe "Cookies consent" do expect(cookie_by_name("third_party")[:value]).to eq "false" expect(page).not_to have_content "Cookies policy" expect(page).not_to have_content "Cookies management" + expect(page).not_to have_content "Running third party script" refresh expect(page).not_to have_content "Cookies policy" + expect(page).not_to have_content "Running third party script" end scenario "Allow users to accept all cookies from the cookies management modal" do @@ -76,6 +86,7 @@ describe "Cookies consent" do expect(cookie_by_name("cookies_consent")).to be nil expect(cookie_by_name("third_party")).to be nil + expect(page).not_to have_content "Running third party script" within ".cookies-consent-banner" do click_button "Manage cookies" @@ -89,10 +100,12 @@ describe "Cookies consent" do expect(cookie_by_name("third_party")[:value]).to eq "true" expect(page).not_to have_content "Cookies policy" expect(page).not_to have_content "Cookies management" + expect(page).not_to have_content "Running third party script" refresh expect(page).not_to have_content "Cookies policy" + expect(page).to have_content "Running third party script", count: 1 end scenario "Allow users to accept custom cookies from the cookies management modal" do @@ -117,10 +130,28 @@ describe "Cookies consent" do expect(cookie_by_name("third_party")[:value]).to eq "true" expect(page).not_to have_content "Cookies policy" expect(page).not_to have_content "Cookies management" + expect(page).not_to have_content "Running third party script" refresh + expect(page).to have_content "Running third party script", count: 1 expect(page).not_to have_content "Cookies policy" + + click_link "Manage cookies" + + within ".cookies-consent-management" do + expect(page).to have_checked_field "third_party", visible: :none + find("label[for='third_party']").click + expect(page).to have_unchecked_field "third_party", visible: :none + + click_button "Save preferences" + end + + expect(page).to have_content "Running third party script", count: 1 + + refresh + + expect(page).not_to have_content "Running third party script" end end end