diff --git a/app/models/batch_invitation_user.rb b/app/models/batch_invitation_user.rb index ad88f349e..c487e207a 100644 --- a/app/models/batch_invitation_user.rb +++ b/app/models/batch_invitation_user.rb @@ -99,7 +99,7 @@ def sanitise_attributes_for_inviting_user_role(raw_attributes, inviting_user) end def strip_whitespace_from_name - name.strip! + name&.strip! end def strip_whitespace_from_email diff --git a/test/models/batch_invitation_user_test.rb b/test/models/batch_invitation_user_test.rb index 8f6604899..4e06ba330 100644 --- a/test/models/batch_invitation_user_test.rb +++ b/test/models/batch_invitation_user_test.rb @@ -20,6 +20,12 @@ class BatchInvitationUserTest < ActiveSupport::TestCase assert_equal "cabinet-office", user.organisation_slug end + + should "strip unwanted whitespace from organisation_slug before persisting even if name is nil" do + user = create(:batch_invitation_user, organisation_slug: " cabinet-office ", name: nil) + + assert_equal "cabinet-office", user.organisation_slug + end end context "validations" do