Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#6434] Exclude email providers from body home pages #7836

Merged
merged 5 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 1 addition & 13 deletions app/models/public_body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
require 'confidence_intervals'

class PublicBody < ApplicationRecord
include CalculatedHomePage
include Taggable
include Notable
include Rails.application.routes.url_helpers
Expand Down Expand Up @@ -402,19 +403,6 @@ def legislation
legislations.first
end

# Guess home page from the request email, or use explicit override, or nil
# if not known.
#
# TODO: PublicBody#calculated_home_page would be a good candidate to cache
# in an instance variable
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
"https://www.#{request_email_domain}"
end
end

# The "internal admin" is a special body for internal use.
def self.internal_admin_body
matching_pbs = AlaveteliLocalization.
Expand Down
55 changes: 55 additions & 0 deletions app/models/public_body/calculated_home_page.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# 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
gmail.com
garethrees marked this conversation as resolved.
Show resolved Hide resolved
googlemail.com
gmx.com
hotmail.com
garethrees marked this conversation as resolved.
Show resolved Hide resolved
icloud.com
garethrees marked this conversation as resolved.
Show resolved Hide resolved
live.com
mac.com
mail.com
mail.ru
me.com
outlook.com
protonmail.com
qq.com
yahoo.com
garethrees marked this conversation as resolved.
Show resolved Hide resolved
yandex.com
ymail.com
zoho.com
]
end

def calculated_home_page
@calculated_home_page ||= calculated_home_page!
end

private

# Ensure known home page has a full URL or guess if not known.
def calculated_home_page!
ensure_home_page_protocol || guess_home_page
end

# Ensure the home page has the HTTP protocol at the start of the URL
def ensure_home_page_protocol
return unless home_page.present?
home_page[URI.regexp(%w(http https))] ? home_page : "https://#{home_page}"
end

# Guess the home page from the request address email domain.
def guess_home_page
return unless request_email_domain
return if excluded_calculated_home_page_domain?(request_email_domain)
"https://www.#{request_email_domain}"
end

def excluded_calculated_home_page_domain?(domain)
excluded_calculated_home_page_domains.include?(domain)
end
end
63 changes: 45 additions & 18 deletions spec/models/public_body_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1919,36 +1919,63 @@ 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 "should return the home page verbatim if it's present" do
public_body = PublicBody.new
public_body.home_page = "http://www.example.com"
expect(public_body.calculated_home_page).to eq("http://www.example.com")
describe 'calculated home page' do
it "returns the home page verbatim if it's present" do
public_body = PublicBody.new(home_page: 'http://www.example.com')
expect(public_body.calculated_home_page).to eq('http://www.example.com')
end

it 'ensures home page URLs start with https://' do
public_body = PublicBody.new(home_page: 'example.com')
expect(public_body.calculated_home_page).to eq('https://example.com')
end

it "should return the home page based on the request email domain if it has one" do
it 'does not add http when https is present' do
public_body = PublicBody.new(home_page: 'https://example.com')
expect(public_body.calculated_home_page).to eq('https://example.com')
end

it 'returns the home page based on the request email domain if it has one' do
public_body = PublicBody.new
allow(public_body).to receive(:request_email_domain).and_return "public-authority.com"
expect(public_body.calculated_home_page).to eq("https://www.public-authority.com")

allow(public_body).
to receive(:request_email_domain).and_return('public-authority.com')

expect(public_body.calculated_home_page).
to eq('https://www.public-authority.com')
end

it "should return nil if there's no home page and the email domain can't be worked out" do
it "returns nil if there's no home page and the email domain can't be worked out" do
public_body = PublicBody.new
allow(public_body).to receive(:request_email_domain).and_return nil
allow(public_body).to receive(:request_email_domain).and_return(nil)
expect(public_body.calculated_home_page).to be_nil
end

it "should ensure home page URLs start with https://" do
public_body = PublicBody.new
public_body.home_page = "example.com"
expect(public_body.calculated_home_page).to eq("https://example.com")
it 'ensures home page URLs start with https://' do
public_body = PublicBody.new(home_page: 'example.com')
expect(public_body.calculated_home_page).to eq('https://example.com')
end

it "should not add http when https is present" do
public_body = PublicBody.new
public_body.home_page = "https://example.com"
expect(public_body.calculated_home_page).to eq("https://example.com")
it 'does not add http when https is present' do
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

Expand Down