Skip to content

Commit

Permalink
Handle nil name in BatchInvitationUser#strip_whitespace_from_name
Browse files Browse the repository at this point in the history
This was causing a migration [1] to fail in integration with:

    NoMethodError: undefined method `strip!' for nil:NilClass

This makes sense from the point of view that there is no validation on
BatchInvitationUser#name and so there's nothing forcing it to be
present.

I've added a unit test to force me to fix the problem.

[1]: https://github.com/alphagov/signon/blob/afe562d1905c90def59e80a5e483375538fa0935/db/migrate/20230925093143_strip_whitespace_from_batch_invitation_user_organisation_slug.rb
  • Loading branch information
floehopper committed Sep 25, 2023
1 parent afe562d commit 12f0417
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/models/batch_invitation_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions test/models/batch_invitation_user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 12f0417

Please sign in to comment.