Refactor test for user detail sanitization

What does this MR do and why?

Refactors the UserDetail model's sanitize_attrs method and its corresponding tests for improved code organization and maintainability.

Key changes:

  1. Move sanitize_attrs to private section: The method was previously declared as public but is only called internally via before_validation callback. Moving it to the private section better reflects its intended usage.

  2. Refactor tests to use parameterized table syntax: The #sanitize_attrs describe block in spec/models/user_detail_spec.rb has been refactored from shared examples with loops to use RSpec's parameterized table syntax (where/with_them), making the tests more readable and easier to extend.

Behavioral changes:

None. This is a pure refactoring with no changes to runtime behavior. The sanitization logic remains exactly the same:

  • HTML tags are stripped from all fields
  • Script and iframe elements are removed
  • Ampersands (&) are encoded to & for linkedin, twitter, and website_url fields
  • Ampersands are preserved (not encoded) for location and organization fields

References

  • Related to issue #577060 (closed)
  • Sanitizable concern: app/models/concerns/sanitizable.rb

Screenshots or screen recordings

N/A - Backend refactoring with no UI changes

How to set up and validate locally

  1. Run the test suite to verify the refactored tests pass:

    bundle exec rspec spec/models/user_detail_spec.rb -e "sanitize_attrs"
  2. Verify sanitization still works correctly in Rails console:

    user = User.first
    ud = user.user_detail
    
    # HTML tags should be stripped
    ud.linkedin = '<script>alert("xss")</script>test'
    ud.save
    ud.linkedin # Should output: 'test'
    
    # Ampersand handling for linkedin/twitter/website_url (encodes)
    ud.twitter = 'test&value'
    ud.save
    ud.twitter # Should output: 'test&amp;value'
    
    # Ampersand handling for location/organization (preserves)
    ud.location = 'San Francisco & Bay Area'
    ud.save
    ud.location # Should output: 'San Francisco & Bay Area'

MR acceptance checklist

Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Merge request reports

Loading