From 1cff459c392aad903bf90cbcc323a3649e9973e9 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 c98a180b47e..27461f5fe83 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 : "https://#{home_page}" elsif request_email_domain + return if excluded_calculated_home_page_domain?(request_email_domain) "https://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 a60211d8615..35d32705ef8 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