diff --git a/app/models/team.rb b/app/models/team.rb index 48990b488..15de8ab56 100644 --- a/app/models/team.rb +++ b/app/models/team.rb @@ -30,7 +30,7 @@ class Team < ApplicationRecord uniqueness: { scope: :account_id } before_validation do - self.name = name.downcase if attribute_present?('name') + self.name = name.gsub(/[[:cntrl:]]/, '').strip.downcase if attribute_present?('name') end # Adds multiple members to the team diff --git a/spec/models/team_spec.rb b/spec/models/team_spec.rb index cb55dba61..8272b5925 100644 --- a/spec/models/team_spec.rb +++ b/spec/models/team_spec.rb @@ -7,6 +7,31 @@ RSpec.describe Team do it { is_expected.to have_many(:team_members) } end + describe 'name normalization' do + let(:account) { create(:account) } + + it 'downcases the name' do + team = create(:team, account: account, name: 'Customer Support') + expect(team.name).to eq('customer support') + end + + it 'strips control characters and surrounding whitespace' do + team = create(:team, account: account, name: " Sales\n") + expect(team.name).to eq('sales') + end + + it 'removes control characters embedded within the name' do + team = create(:team, account: account, name: "su\npport") + expect(team.name).to eq('support') + end + + it 'is invalid when the name reduces to blank after sanitization' do + team = build(:team, account: account, name: "\t\n ") + expect(team).not_to be_valid + expect(team.errors[:name]).to include(I18n.t('errors.validations.presence')) + end + end + describe '#add_members' do let(:team) { FactoryBot.create(:team) }