Skip to content

Commit

Permalink
Fix normalization of BatchInvitationUser#email
Browse files Browse the repository at this point in the history
Trello: https://trello.com/c/aO4nVCZT

Having implemented strip_whitespace_from_organisation_slug I realised
I'd probably missed a scenario for strip_whitespace_from_email. And sure
enough there was no test coverage for the case when
BatchInvitationUser#email is nil.

In this commit I've improved the test coverage for email validation
which for one thing explicitly forces the presence validation to be
enabled for the email attribute, and for another, it forces me to use
the safe navigation operator in strip_whitespace_from_email. This is
because the latter is called from a before_validation callback and so
email could be nil at this point.
  • Loading branch information
floehopper committed Sep 21, 2023
1 parent b8513bb commit 4f57c62
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/models/batch_invitation_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def strip_whitespace_from_name
end

def strip_whitespace_from_email
email.strip!
email&.strip!
end

def strip_whitespace_from_organisation_slug
Expand Down
14 changes: 10 additions & 4 deletions test/models/batch_invitation_user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,25 @@ class BatchInvitationUserTest < ActiveSupport::TestCase
end

context "validations" do
should "validate email address" do
should "validate presence of email address" do
user = build(:batch_invitation_user, email: nil)

assert_not user.valid?
assert_includes user.errors[:email], "can't be blank"
end

should "validate format of email address" do
user = build(:batch_invitation_user, email: "@gov.uk")

assert_not user.valid?
assert_equal ["is invalid"], user.errors[:email]
assert_includes user.errors[:email], "is invalid"
end

should "prevent user being created with a known non-government email address" do
user = build(:batch_invitation_user, email: "piers.quinn@yahoo.co.uk")

assert_not user.valid?
assert_equal ["not accepted. Please enter a workplace email to continue."],
user.errors[:email]
assert_includes user.errors[:email], "not accepted. Please enter a workplace email to continue."
end

should "not allow user to be updated with a known non-government email address" do
Expand Down

0 comments on commit 4f57c62

Please sign in to comment.