Refactor dynamic validation, test for it

This commit is contained in:
Martin Lensment 2014-08-18 16:33:56 +03:00
parent 5258c43faa
commit b0e9828a01
3 changed files with 25 additions and 8 deletions

View file

@ -119,8 +119,9 @@ class Domain < ActiveRecord::Base
### VALIDATIONS ### ### VALIDATIONS ###
def validate_nameservers_count def validate_nameservers_count
sg = SettingGroup.find_by(code: SettingGroup::DOMAIN_VALIDATION_CODE) sg = SettingGroup.domain_validation
min, max = sg.get(:ns_min_count).to_i, sg.get(:ns_max_count).to_i min, max = sg.setting(:ns_min_count).value.to_i, sg.setting(:ns_max_count).value.to_i
unless nameservers.length.between?(min, max) unless nameservers.length.between?(min, max)
errors.add(:nameservers, :out_of_range, {min: min, max: max}) errors.add(:nameservers, :out_of_range, {min: min, max: max})
end end
@ -152,7 +153,7 @@ class Domain < ActiveRecord::Base
end end
def epp_code_map def epp_code_map
domain_validation_sg = SettingGroup.find_by(code: SettingGroup::DOMAIN_VALIDATION_CODE) domain_validation_sg = SettingGroup.domain_validation
{ {
'2302' => [ # Object exists '2302' => [ # Object exists
@ -165,7 +166,7 @@ class Domain < ActiveRecord::Base
[:valid_to, :epp_exp_dates_do_not_match] [:valid_to, :epp_exp_dates_do_not_match]
], ],
'2004' => [ # Parameter value range error '2004' => [ # Parameter value range error
[:nameservers, :out_of_range, {min: domain_validation_sg.get(:ns_min_count), max: domain_validation_sg.get(:ns_max_count)}], [:nameservers, :out_of_range, {min: domain_validation_sg.setting(:ns_min_count).value, max: domain_validation_sg.setting(:ns_max_count).value}],
[:period, :out_of_range] [:period, :out_of_range]
], ],
'2303' => [ # Object does not exist '2303' => [ # Object does not exist

View file

@ -3,10 +3,13 @@ class SettingGroup < ActiveRecord::Base
accepts_nested_attributes_for :settings accepts_nested_attributes_for :settings
DOMAIN_VALIDATION_CODE = 'domain_validation' def setting(key)
settings.find_by(code: key.to_s)
end
def get(key) class << self
s = settings.find_by(code: key.to_s) def domain_validation
s.try(:value) find_by(code: 'domain_validation')
end
end end
end end

View file

@ -48,6 +48,19 @@ describe Domain do
admin_contacts: ["Admin contact is missing"], admin_contacts: ["Admin contact is missing"],
nameservers: ["Nameservers count must be between 1-13"] nameservers: ["Nameservers count must be between 1-13"]
}) })
sg = SettingGroup.domain_validation
min = sg.setting(:ns_min_count)
max = sg.setting(:ns_max_count)
min.value = 2
min.save
max.value = 7
max.save
expect(d.valid?).to be false
expect(d.errors.messages[:nameservers]).to eq(['Nameservers count must be between 2-7'])
end end
it 'does not create a reserved domain' do it 'does not create a reserved domain' do