Fix domain name validation #2609

This commit is contained in:
Martin Lensment 2015-06-03 14:34:04 +03:00
parent a7def0b7ae
commit c1b5fc8265
2 changed files with 14 additions and 3 deletions

View file

@ -31,9 +31,8 @@ class DomainNameValidator < ActiveModel::EachValidator
# rubocop: disable Metrics/LineLength # rubocop: disable Metrics/LineLength
unicode_chars = /\u00E4\u00F5\u00F6\u00FC\u0161\u017E/ # äõöüšž unicode_chars = /\u00E4\u00F5\u00F6\u00FC\u0161\u017E/ # äõöüšž
regexp = /\A[a-zA-Z0-9#{unicode_chars}][a-zA-Z0-9#{unicode_chars}-]{0,61}[a-zA-Z0-9#{unicode_chars}]#{general_domains}\z/ regexp = /\A[a-zA-Z0-9#{unicode_chars.source}][a-zA-Z0-9#{unicode_chars.source}-]{0,61}[a-zA-Z0-9#{unicode_chars.source}]#{general_domains.source}\z/
# rubocop: enable Metrics/LineLength # rubocop: enable Metrics/LineLength
# rubocop: disable Style/DoubleNegation # rubocop: disable Style/DoubleNegation
!!(value =~ regexp) !!(value =~ regexp)
# rubocop: enable Style/DoubleNegation # rubocop: enable Style/DoubleNegation

View file

@ -242,7 +242,7 @@ describe Domain do
end end
it 'should not be valid when name length is longer than 63 characters' do it 'should not be valid when name length is longer than 63 characters' do
d = Fabricate.build(:domain, d = Fabricate.build(:domain,
name: "xn--4caaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.ee") name: "xn--4caaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.ee")
d.valid? d.valid?
d.errors.full_messages.should match_array([ d.errors.full_messages.should match_array([
@ -294,6 +294,18 @@ describe Domain do
d.errors.full_messages.should == ["Domain name Domain name is invalid"] d.errors.full_messages.should == ["Domain name Domain name is invalid"]
end end
it 'should not be valid with at character' do
d = Fabricate.build(:domain, name: 'dass@sf.ee')
d.valid?
d.errors.full_messages.should == ["Domain name Domain name is invalid"]
end
it 'should not be valid with invalid characters' do
d = Fabricate.build(:domain, name: '@ba)s(?ä_:-df.ee')
d.valid?
d.errors.full_messages.should == ["Domain name Domain name is invalid"]
end
it 'should be valid when name length is two pynicodes' do it 'should be valid when name length is two pynicodes' do
d = Fabricate.build(:domain, name: "xn--4caa.ee") d = Fabricate.build(:domain, name: "xn--4caa.ee")
d.valid? d.valid?