From 04821b1701a420185cbd672e4f4727eb402dbbce Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Mon, 24 Jul 2023 10:06:13 +0100 Subject: [PATCH] Exclude email providers from body home pages Many smaller authorities use a request email address from a general purpose public email provider. We don't want e.g. gmail.com being set as the home page for the authority. This is configurable in the theme by setting the class attribute: # Add to the defaults PublicBody.excluded_calculated_home_page_domains << %w[ example.net ] # Clear the defaults and set your own list PublicBody.excluded_calculated_home_page_domains = %w[ example.org example.net ] Fixes https://github.com/mysociety/alaveteli/issues/6434 --- .../public_body/calculated_home_page.rb | 29 +++++++++++++++++++ spec/models/public_body_spec.rb | 16 ++++++++++ 2 files changed, 45 insertions(+) diff --git a/app/models/public_body/calculated_home_page.rb b/app/models/public_body/calculated_home_page.rb index 87495b1ab6..7efbc73b29 100644 --- a/app/models/public_body/calculated_home_page.rb +++ b/app/models/public_body/calculated_home_page.rb @@ -1,5 +1,27 @@ # Guess the home page based on the request email domain. module PublicBody::CalculatedHomePage + extend ActiveSupport::Concern + + included do + cattr_accessor :excluded_calculated_home_page_domains, default: %w[ + aol.com + btinternet.com + gmail.com + gmx.com + hotmail.com + icloud.com + mail.com + mail.ru + outlook.com + protonmail.com + qq.com + yahoo.com + yandex.com + ymail.com + zoho.com + ] + end + # Guess home page from the request email, or use explicit override, or nil # if not known. # @@ -9,7 +31,14 @@ def calculated_home_page if home_page && !home_page.empty? home_page[URI.regexp(%w(http https))] ? home_page : "http://#{home_page}" elsif request_email_domain + return if excluded_calculated_home_page_domain?(request_email_domain) "http://www.#{request_email_domain}" end end + + private + + def excluded_calculated_home_page_domain?(domain) + excluded_calculated_home_page_domains.include?(domain) + end end diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb index 5c6a0f8fd6..39190aac61 100644 --- a/spec/models/public_body_spec.rb +++ b/spec/models/public_body_spec.rb @@ -1905,6 +1905,12 @@ def set_default_attributes(public_body) end RSpec.describe PublicBody do + around do |example| + previous = PublicBody.excluded_calculated_home_page_domains + PublicBody.excluded_calculated_home_page_domains = %w[example.net] + example.run + PublicBody.excluded_calculated_home_page_domains = previous + end describe 'calculated home page' do it "returns the home page verbatim if it's present" do @@ -1943,6 +1949,16 @@ def set_default_attributes(public_body) public_body = PublicBody.new(home_page: 'https://example.com') expect(public_body.calculated_home_page).to eq('https://example.com') end + + it 'does not calculate the homepage for excluded domains' do + public_body = PublicBody.new(request_email: 'x@example.net') + expect(public_body.calculated_home_page).to be_nil + end + + it 'ignores case sensitivity for excluded domains' do + public_body = PublicBody.new(request_email: 'x@EXAMPLE.net') + expect(public_body.calculated_home_page).to be_nil + end end describe '#site_administration?' do