Skip to content

Commit

Permalink
Exclude email providers from body home pages
Browse files Browse the repository at this point in the history
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 #6434
  • Loading branch information
garethrees committed Jul 24, 2023
1 parent 864bc90 commit 1cff459
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
29 changes: 29 additions & 0 deletions app/models/public_body/calculated_home_page.rb
Original file line number Diff line number Diff line change
@@ -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.
#
Expand All @@ -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
16 changes: 16 additions & 0 deletions spec/models/public_body_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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: '[email protected]')
expect(public_body.calculated_home_page).to be_nil
end

it 'ignores case sensitivity for excluded domains' do
public_body = PublicBody.new(request_email: '[email protected]')
expect(public_body.calculated_home_page).to be_nil
end
end

describe '#site_administration?' do
Expand Down

0 comments on commit 1cff459

Please sign in to comment.