Skip to content

Commit

Permalink
Merge pull request #3519 from DFE-Digital/CAPT-2081-max-email-address…
Browse files Browse the repository at this point in the history
…-length

CAPT-2081 Lower email address max length to 129
  • Loading branch information
kenfodder authored Jan 22, 2025
2 parents 7c77952 + cc754f5 commit 369a9c4
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 12 deletions.
4 changes: 2 additions & 2 deletions app/forms/email_address_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class EmailAddressForm < Form
message: ->(form, _) { form.i18n_errors_path("format") }
},
length: {
maximum: 256,
message: ->(form, _) { form.i18n_errors_path("length") }
maximum: Rails.application.config.email_max_length,
message: ->(form, _) { form.i18n_errors_path("length", length: Rails.application.config.email_max_length) }
},
if: -> { email_address.present? }

Expand Down
4 changes: 2 additions & 2 deletions app/forms/reminders/personal_details_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class PersonalDetailsForm < Form
message: i18n_error_message("email_address.invalid")
},
length: {
maximum: 256,
message: i18n_error_message("email_address.length")
maximum: Rails.application.config.email_max_length,
message: i18n_error_message("email_address.length", length: Rails.application.config.email_max_length)
}

def save!
Expand Down
2 changes: 1 addition & 1 deletion app/forms/select_email_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class SelectEmailForm < Form
validates :email_address_check, inclusion: {in: [true, false], message: i18n_error_message(:select_email)}
validates :email_address, presence: {message: i18n_error_message(:invalid_email)}, if: -> { email_address_check == true }
validates :email_address, format: {with: Rails.application.config.email_regexp, message: i18n_error_message(:invalid_email)},
length: {maximum: 256, message: i18n_error_message(:invalid_email)}, if: -> { email_address.present? }
length: {maximum: Rails.application.config.email_max_length, message: i18n_error_message(:invalid_email)}, if: -> { email_address.present? }

before_validation :determine_dependant_attributes

Expand Down
2 changes: 1 addition & 1 deletion app/models/eligible_fe_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class EligibleFeProvider < ApplicationRecord
validates :primary_key_contact_email_address,
presence: true,
format: {with: Rails.application.config.email_regexp},
length: {maximum: 256}
length: {maximum: Rails.application.config.email_max_length}

def self.csv_for_academic_year(academic_year)
attribute_names = [:ukprn, :max_award_amount, :lower_award_amount, :primary_key_contact_email_address]
Expand Down
4 changes: 4 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ class Application < Rails::Application

config.email_regexp = /\A[a-zA-Z0-9.!\#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+\z/

# Max length is based on the lowest requirement on services Claim interacts, in this case Payroll
# https://www.gov.uk/government/publications/real-time-information-internet-submissions-2024-to-2025-technical-specifications
config.email_max_length = 129

config.active_support.to_time_preserves_timezone = :offset
end
end
4 changes: 2 additions & 2 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ en:
errors:
presence: "Enter an email address"
format: "Enter an email address in the correct format, like [email protected]"
length: "Email address must be 256 characters or less"
length: "Email address must be %{length} characters or less"
mobile_number:
errors:
invalid: "Enter a mobile number, like 07700 900 982 or +44 7700 900 982"
Expand Down Expand Up @@ -586,7 +586,7 @@ en:
email_address:
blank: Enter an email address
invalid: Enter an email address in the correct format, like [email protected]
length: Email address must be 256 characters or less
length: Email address must be %{length} characters or less
unauthorised: Only authorised email addresses can be used when using a team-only API key
check_your_answers:
part_one:
Expand Down
17 changes: 15 additions & 2 deletions spec/forms/email_address_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,28 @@
describe "validations" do
subject { form }

let(:domain) { "@example.com" }

describe "email_address" do
context "when missing" do
let(:email_address) { nil }
it { is_expected.not_to be_valid }
end

context "when too long" do
let(:email_address) { "a" * 257 }
it { is_expected.not_to be_valid }
let(:email_address) { "#{"a" * (130 - domain.length)}#{domain}" }
it do
subject.valid?

expect(form).not_to be_valid
expect(form.errors.added?(:email_address, :too_long, count: 129)).to be true
expect(form.errors.messages[:email_address]).to include("Email address must be 129 characters or less")
end
end

context "when as long as it can get" do
let(:email_address) { "#{"a" * (129 - domain.length)}#{domain}" }
it { is_expected.to be_valid }
end

context "when the wrong format" do
Expand Down
60 changes: 59 additions & 1 deletion spec/forms/reminders/personal_details_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
let(:journey) { Journeys::FurtherEducationPayments }
let(:journey_session) { create(:further_education_payments_session) }
let(:claim) { create(:claim, :submitted) }
let(:params) { ActionController::Parameters.new }

subject do
described_class.new(
journey_session:,
journey:,
params: ActionController::Parameters.new,
params: params,
session: {"submitted_claim_id" => claim.id}
)
end
Expand All @@ -40,4 +41,61 @@
end
end
end

describe "validations" do
let(:domain) { "@example.com" }

let(:params) do
ActionController::Parameters.new(claim: {reminder_email_address: email_address})
end

before do
subject.valid?
end

describe "email_address" do
context "when missing" do
let(:email_address) { nil }

it do
expect(subject).not_to be_valid
expect(subject.errors.added?(:reminder_email_address, :blank)).to be true
end
end

context "when too long" do
let(:email_address) { "#{"a" * (130 - domain.length)}#{domain}" }

it do
expect(subject).not_to be_valid
expect(subject.errors.added?(:reminder_email_address, :too_long, count: 129)).to be true
expect(subject.errors.messages[:reminder_email_address]).to include("Email address must be 129 characters or less")
end
end

context "when as long as it can get" do
let(:email_address) { "#{"a" * (129 - domain.length)}#{domain}" }

it do
expect(subject.errors.added?(:reminder_email_address, :too_long, count: 129)).to be false
end
end

context "when the wrong format" do
let(:email_address) { "not_an_email" }

it do
expect(subject.errors.added?(:reminder_email_address, :invalid, value: "not_an_email")).to be true
end
end

context "when the correct format" do
let(:email_address) { "[email protected]" }

it do
expect(subject.errors.added?(:reminder_email_address, :invalid, value: "not_an_email")).to be false
end
end
end
end
end
2 changes: 1 addition & 1 deletion spec/forms/select_email_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def stub_email(email)
is_expected.to be_invalid
expect(form.errors[:email_address]).to eq([form.i18n_errors_path(:invalid_email)])

stub_email("a" * 245 + "@example.com") # 257 chars
stub_email("a" * (130 - 12) + "@example.com")
is_expected.to be_invalid
expect(form.errors[:email_address]).to eq([form.i18n_errors_path(:invalid_email)])
end
Expand Down

0 comments on commit 369a9c4

Please sign in to comment.