diff --git a/app/models/domain.rb b/app/models/domain.rb index 8731eb5..a465fdc 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -10,7 +10,7 @@ class Domain < ActiveRecord::Base # name must be a sequence of word characters (a to z, plus 0 to 9, # plus '_'). Letters must be lowercase - validates_format_of :name, :with => /^([a-z0-9_]+\.)+[a-z0-9_]+$/, :message => N_('%{fn} must be composed only of lowercase latters (a to z), numbers (0 to 9) and "_"') + validates_format_of :name, :with => /^([a-z0-9_-]+\.)+[a-z0-9_-]+$/, :message => N_('%{fn} must be composed only of lowercase latters (a to z), numbers (0 to 9), "_" and "-"') # checks validations that could not be expressed using Rails' predefined # validations. In particular: diff --git a/test/unit/domain_test.rb b/test/unit/domain_test.rb index be2598c..a637292 100644 --- a/test/unit/domain_test.rb +++ b/test/unit/domain_test.rb @@ -7,23 +7,40 @@ class DomainTest < Test::Unit::TestCase Domain.clear_cache end - # Replace this with your real tests. - def test_domain_name_format - c = Domain.new - assert !c.valid? - assert c.errors.invalid?(:name) - - c.name = 'bliblibli' - assert !c.valid? - assert c.errors.invalid?(:name) - - c.name = 'EXAMPLE.NET' - assert !c.valid? - assert c.errors.invalid?(:name) - - c.name = 'test.net' - c.valid? - assert !c.errors.invalid?(:name) + should 'not allow domains without name' do + domain = Domain.new + domain.valid? + assert domain.errors.invalid?(:name) + end + + should 'not allow domain without dot' do + domain = Domain.new(:name => 'test') + domain.valid? + assert domain.errors.invalid?(:name) + end + + should 'allow domains with dot' do + domain = Domain.new(:name => 'test.org') + domain.valid? + assert !domain.errors.invalid?(:name) + end + + should 'not allow domains with upper cased letters' do + domain = Domain.new(:name => 'tEst.org') + domain.valid? + assert domain.errors.invalid?(:name) + end + + should 'allow domains with hyphen' do + domain = Domain.new(:name => 'test-domain.org') + domain.valid? + assert !domain.errors.invalid?(:name) + end + + should 'allow domains with underscore' do + domain = Domain.new(:name => 'test_domain.org') + domain.valid? + assert !domain.errors.invalid?(:name) end def test_owner -- libgit2 0.21.2