Skip to content

Commit

Permalink
Add setting to enable cookies consent test mode
Browse files Browse the repository at this point in the history
This way we allow administrators to test the provided cookies
consent configuration before showing the feature to common
users.
  • Loading branch information
Senen committed Mar 8, 2024
1 parent 0c6e483 commit cf8b6e6
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<%= render Admin::Settings::TableComponent.new(setting_name: "feature") do %>
<%= render Admin::Settings::RowComponent.new("feature.cookies_consent", type: :feature, tab: tab) %>
<%= render Admin::Settings::RowComponent.new("cookies_consent.admin_test_mode", type: :feature, tab: tab) %>
<%= render Admin::Settings::RowComponent.new("cookies_consent.more_info_link", type: :text, tab: tab) %>
<%= render Admin::Settings::RowComponent.new("cookies_consent.version_name", type: :text, tab: tab) %>
<% end %>
Expand Down
1 change: 1 addition & 0 deletions app/components/layout/cookies_consent/banner_component.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Layout::CookiesConsent::BannerComponent < Layout::CookiesConsent::BaseComponent
delegate :cookies, to: :helpers
delegate :current_user, to: :helpers

def render?
super && missing_cookies_setup?
Expand Down
12 changes: 11 additions & 1 deletion app/components/layout/cookies_consent/base_component.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
class Layout::CookiesConsent::BaseComponent < ApplicationComponent
delegate :current_user, to: :helpers

def render?
feature?(:cookies_consent)
feature?(:cookies_consent) && (public_setup? || testing_setup?)
end

private

def version_name
Setting["cookies_consent.version_name"]
end

def public_setup?
!feature?("cookies_consent.admin_test_mode")
end

def testing_setup?
current_user&.administrator? && !public_setup?
end
end
3 changes: 2 additions & 1 deletion app/models/setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ def defaults
"sdg.process.legislation": true,
"cookies_consent.more_info_link": "",
"cookies_consent.setup_page": false,
"cookies_consent.version_name": "v1"
"cookies_consent.version_name": "v1",
"cookies_consent.admin_test_mode": false
}
end

Expand Down
2 changes: 2 additions & 0 deletions config/locales/en/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ en:
legislation: Related SDG in collaborative legislation
legislation_description: Allow collaborative legislation to be linked to Sustainable Development Goals
cookies_consent:
admin_test_mode: Enable testing mode
admin_test_mode_description: This feature will hide the cookie consent interface for all users except administrators when enabled. It allows you to test your settings before publishing. Turn off this feature once you have successfully tested it.
more_info_link: Link to cookies policy
more_info_link_description: Link shown in the consent banner to the cookie policy page.
setup_page: Enable third party cookies setup page
Expand Down
2 changes: 2 additions & 0 deletions config/locales/es/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ es:
legislation: Alineamiento ODS en legislación colaborativa
legislation_description: Permitir alineamiento de los Objetivos de Desarrollo Sostenible en legislación colaborativa
cookies_consent:
admin_test_mode: Activar el modo de pruebas
admin_test_mode_description: Esta función ocultará la interfaz de consentimiento de cookies para todos los usuarios excepto los administradores cuando esté activada. Le permite probar su configuración antes de publicarla. Desactive esta función una vez que la haya probado correctamente.
more_info_link: Enlace a la política de cookies
more_info_link_description: Enlace que se muestra en el banner de consentimiento a la página de política de cookies
setup_page: Activar la página de configuración de consentimiento de cookies de terceros
Expand Down
26 changes: 26 additions & 0 deletions spec/components/layout/cookies_consent/banner_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,30 @@

expect(page).not_to have_button("Reject")
end

context "when admin testing mode is enabled" do
before { Setting["cookies_consent.admin_test_mode"] = true }

it "shows the cookie banner to administrators" do
sign_in(create(:administrator).user)

render_inline Layout::CookiesConsent::BannerComponent.new

expect(page).to have_css(".cookies-consent-banner")
end

it "does not show the cookie banner to common users" do
sign_in(create(:user))

render_inline Layout::CookiesConsent::BannerComponent.new

expect(page).not_to have_css(".cookies-consent-banner")
end

it "does not show the cookie banner to anonymous users" do
render_inline Layout::CookiesConsent::BannerComponent.new

expect(page).not_to have_css(".cookies-consent-banner")
end
end
end
48 changes: 48 additions & 0 deletions spec/components/layout/cookies_consent/setup_component_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require "rails_helper"

describe Layout::CookiesConsent::SetupComponent do
before do
Setting["feature.cookies_consent"] = true
Setting["cookies_consent.setup_page"] = true
end

it "is rendered when the cookies consent and the cookies setup features are enabled" do
render_inline Layout::CookiesConsent::SetupComponent.new

expect(page).to have_css(".cookies-consent-setup")
end

it "is not rendered when the cookies consent feature is disabled" do
Setting["feature.cookies_consent"] = false

render_inline Layout::CookiesConsent::SetupComponent.new

expect(page).not_to have_css(".cookies-consent-setup")
end

it "is not rendered when the cookies setup feature is disabled" do
Setting["cookies_consent.setup_page"] = false

render_inline Layout::CookiesConsent::SetupComponent.new

expect(page).not_to have_css(".cookies-consent-setup")
end

it "is rendered when the admin test mode is enabled and the current_user is an administrator" do
Setting["cookies_consent.admin_test_mode"] = true
sign_in(create(:administrator).user)

render_inline Layout::CookiesConsent::SetupComponent.new

expect(page).to have_css(".cookies-consent-setup")
end

it "is not rendered when the admin testing mode is enabled and the current_user is not an administrator" do
Setting["cookies_consent.admin_test_mode"] = true
sign_in(create(:user))

render_inline Layout::CookiesConsent::SetupComponent.new

expect(page).not_to have_css(".cookies-consent-setup")
end
end
24 changes: 24 additions & 0 deletions spec/system/cookies_consent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,28 @@
end
end
end

describe "when admin test mode is enabled" do
before { Setting["cookies_consent.admin_test_mode"] = true }

scenario "shows the cookie banner when an administrator user is logged in" do
login_as(create(:administrator).user)

visit root_path

expect(page).to have_css(".cookies-consent-banner")
end

scenario "does not show the cookie banner for when the user is not an administrator" do
visit root_path

expect(page).not_to have_css(".cookies-consent-banner")

login_as(create(:user))

visit root_path

expect(page).not_to have_css(".cookies-consent-banner")
end
end
end

0 comments on commit cf8b6e6

Please sign in to comment.