Domain reservation validation

This commit is contained in:
Martin Lensment 2014-07-30 12:13:31 +03:00
parent fb12d056ce
commit f87c8839be
6 changed files with 36 additions and 6 deletions

View file

@ -5,7 +5,7 @@ module Epp::DomainsHelper
if domain.save
render '/epp/domains/create'
else
if domain.errors.added?(:name_dirty, :taken)
if domain.errors.added?(:name, :taken)
@code = '2302'
@msg = 'Domain name already exists'
end

View file

@ -8,9 +8,8 @@ class Domain < ActiveRecord::Base
belongs_to :technical_contact, class_name: 'Contact'
belongs_to :admin_contact, class_name: 'Contact'
validates :name, domain_name: true
validates :name, domain_name: true, uniqueness: true
validates :name_puny, domain_name: true
validates :name_dirty, uniqueness: true
def name=(value)
value.strip!
@ -25,11 +24,16 @@ class Domain < ActiveRecord::Base
res = []
domains.each do |x|
if !DomainNameValidator.validate(x)
if !DomainNameValidator.validate_format(x)
res << {name: x, avail: 0, reason: 'invalid format'}
next
end
if !DomainNameValidator.validate_reservation(x)
res << {name: x, avail: 0, reason: 'Domain name is reserved or restricted'}
next
end
if Domain.find_by(name: x)
res << {name: x, avail: 0, reason: 'in use'} #confirm reason with current API
else

View file

@ -7,13 +7,15 @@ class DomainNameValidator < ActiveModel::EachValidator
# lower level domains are fixed for .ee and can add statically into settings
def validate_each(record, attribute, value)
unless self.class.validate(value)
if !self.class.validate_format(value)
record.errors[attribute] << (options[:message] || 'invalid format')
elsif !self.class.validate_reservation(value)
record.errors[attribute] << (options[:message] || 'Domain name is reserved or restricted')
end
end
class << self
def validate(value)
def validate_format(value)
value = value.mb_chars.downcase.strip
general_domains = /(.pri.ee|.com.ee|.fie.ee|.med.ee|.ee)/
@ -31,5 +33,8 @@ class DomainNameValidator < ActiveModel::EachValidator
!!(value =~ regexp)
end
def validate_reservation(value)
!ReservedDomain.exists?(name: value.mb_chars.downcase.strip)
end
end
end

View file

@ -0,0 +1,13 @@
class DomainReservationValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless self.class.validate(value)
record.errors[attribute] << (options[:message] || 'Domain name is reserved or restricted')
end
end
class << self
def validate(value)
!ReservedDomain.exists?(name: value.mb_chars.downcase.strip)
end
end
end

View file

@ -0,0 +1,3 @@
Fabricator(:reserved_domain) do
name '1162.ee'
end

View file

@ -35,4 +35,9 @@ describe Domain do
expect(Fabricate.build(:domain, name: x).valid?).to be true
end
end
it 'does not create a reserved domain' do
Fabricate(:reserved_domain)
expect(Fabricate.build(:domain, name: '1162.ee').valid?).to be false
end
end