mirror of
https://github.com/internetee/registry.git
synced 2025-07-03 01:33:36 +02:00
Refactor dynamic validation, test for it
This commit is contained in:
parent
5258c43faa
commit
b0e9828a01
3 changed files with 25 additions and 8 deletions
|
@ -119,8 +119,9 @@ class Domain < ActiveRecord::Base
|
|||
### VALIDATIONS ###
|
||||
|
||||
def validate_nameservers_count
|
||||
sg = SettingGroup.find_by(code: SettingGroup::DOMAIN_VALIDATION_CODE)
|
||||
min, max = sg.get(:ns_min_count).to_i, sg.get(:ns_max_count).to_i
|
||||
sg = SettingGroup.domain_validation
|
||||
min, max = sg.setting(:ns_min_count).value.to_i, sg.setting(:ns_max_count).value.to_i
|
||||
|
||||
unless nameservers.length.between?(min, max)
|
||||
errors.add(:nameservers, :out_of_range, {min: min, max: max})
|
||||
end
|
||||
|
@ -152,7 +153,7 @@ class Domain < ActiveRecord::Base
|
|||
end
|
||||
|
||||
def epp_code_map
|
||||
domain_validation_sg = SettingGroup.find_by(code: SettingGroup::DOMAIN_VALIDATION_CODE)
|
||||
domain_validation_sg = SettingGroup.domain_validation
|
||||
|
||||
{
|
||||
'2302' => [ # Object exists
|
||||
|
@ -165,7 +166,7 @@ class Domain < ActiveRecord::Base
|
|||
[:valid_to, :epp_exp_dates_do_not_match]
|
||||
],
|
||||
'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]
|
||||
],
|
||||
'2303' => [ # Object does not exist
|
||||
|
|
|
@ -3,10 +3,13 @@ class SettingGroup < ActiveRecord::Base
|
|||
|
||||
accepts_nested_attributes_for :settings
|
||||
|
||||
DOMAIN_VALIDATION_CODE = 'domain_validation'
|
||||
def setting(key)
|
||||
settings.find_by(code: key.to_s)
|
||||
end
|
||||
|
||||
def get(key)
|
||||
s = settings.find_by(code: key.to_s)
|
||||
s.try(:value)
|
||||
class << self
|
||||
def domain_validation
|
||||
find_by(code: 'domain_validation')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -48,6 +48,19 @@ describe Domain do
|
|||
admin_contacts: ["Admin contact is missing"],
|
||||
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
|
||||
|
||||
it 'does not create a reserved domain' do
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue