mirror of
https://github.com/internetee/registry.git
synced 2025-07-03 01:33:36 +02:00
Refactor validations + tech contacts count validation
This commit is contained in:
parent
48e3e716e7
commit
252527c3db
8 changed files with 93 additions and 45 deletions
|
@ -8,7 +8,7 @@ class Admin::DomainsController < AdminController
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
@domain.all_dependencies_valid?
|
@domain.valid?
|
||||||
end
|
end
|
||||||
|
|
||||||
def edit
|
def edit
|
||||||
|
|
|
@ -53,11 +53,32 @@ class Domain < ActiveRecord::Base
|
||||||
validates :owner_contact, :registrar, presence: true
|
validates :owner_contact, :registrar, presence: true
|
||||||
|
|
||||||
validate :validate_period
|
validate :validate_period
|
||||||
validate :validate_nameservers_count
|
|
||||||
validate :validate_admin_contacts_count
|
|
||||||
validate :validate_dnskeys_count
|
|
||||||
|
|
||||||
validates :nameservers, uniqueness_multi: { attribute: 'hostname' }
|
validates :nameservers, object_count: {
|
||||||
|
min: -> { Setting.ns_min_count },
|
||||||
|
max: -> { Setting.ns_max_count }
|
||||||
|
}
|
||||||
|
|
||||||
|
validates :dnskeys, object_count: {
|
||||||
|
min: -> { Setting.dnskeys_min_count },
|
||||||
|
max: -> { Setting.dnskeys_max_count }
|
||||||
|
}
|
||||||
|
|
||||||
|
validates :admin_domain_contacts, object_count: {
|
||||||
|
association: 'admin_contacts',
|
||||||
|
min: -> { Setting.admin_contacts_min_count },
|
||||||
|
max: -> { Setting.admin_contacts_max_count }
|
||||||
|
}
|
||||||
|
|
||||||
|
validates :tech_domain_contacts, object_count: {
|
||||||
|
association: 'tech_contacts',
|
||||||
|
min: -> { Setting.tech_contacts_min_count },
|
||||||
|
max: -> { Setting.tech_contacts_max_count }
|
||||||
|
}
|
||||||
|
|
||||||
|
validates :nameservers, uniqueness_multi: {
|
||||||
|
attribute: 'hostname'
|
||||||
|
}
|
||||||
|
|
||||||
validates :tech_domain_contacts, uniqueness_multi: {
|
validates :tech_domain_contacts, uniqueness_multi: {
|
||||||
association: 'domain_contacts',
|
association: 'domain_contacts',
|
||||||
|
@ -69,8 +90,14 @@ class Domain < ActiveRecord::Base
|
||||||
attribute: 'contact_code_cache'
|
attribute: 'contact_code_cache'
|
||||||
}
|
}
|
||||||
|
|
||||||
validates :domain_statuses, uniqueness_multi: { attribute: 'value' }
|
validates :domain_statuses, uniqueness_multi: {
|
||||||
validates :dnskeys, uniqueness_multi: { attribute: 'public_key' }
|
attribute: 'value'
|
||||||
|
}
|
||||||
|
|
||||||
|
validates :dnskeys, uniqueness_multi: {
|
||||||
|
attribute: 'public_key'
|
||||||
|
}
|
||||||
|
|
||||||
validate :validate_nameserver_ips
|
validate :validate_nameserver_ips
|
||||||
|
|
||||||
attr_accessor :owner_contact_typeahead, :update_me
|
attr_accessor :owner_contact_typeahead, :update_me
|
||||||
|
@ -151,21 +178,6 @@ class Domain < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
### VALIDATIONS ###
|
### VALIDATIONS ###
|
||||||
def validate_nameservers_count
|
|
||||||
min, max = Setting.ns_min_count, Setting.ns_max_count
|
|
||||||
return if nameservers.reject(&:marked_for_destruction?).length.between?(min, max)
|
|
||||||
errors.add(:nameservers, :out_of_range, { min: min, max: max })
|
|
||||||
end
|
|
||||||
|
|
||||||
def validate_admin_contacts_count
|
|
||||||
errors.add(:admin_contacts, :out_of_range) if admin_contacts_count.zero?
|
|
||||||
end
|
|
||||||
|
|
||||||
def validate_dnskeys_count
|
|
||||||
min, max = Setting.dnskeys_min_count, Setting.dnskeys_max_count
|
|
||||||
return if dnskeys.reject(&:marked_for_destruction?).length.between?(min, max)
|
|
||||||
errors.add(:dnskeys, :out_of_range, { min: min, max: max })
|
|
||||||
end
|
|
||||||
|
|
||||||
def validate_nameserver_ips
|
def validate_nameserver_ips
|
||||||
nameservers.each do |ns|
|
nameservers.each do |ns|
|
||||||
|
@ -189,14 +201,6 @@ class Domain < ActiveRecord::Base
|
||||||
errors.add(:period, :out_of_range) unless valid_values.include?(period.to_s)
|
errors.add(:period, :out_of_range) unless valid_values.include?(period.to_s)
|
||||||
end
|
end
|
||||||
|
|
||||||
def all_dependencies_valid?
|
|
||||||
validate_nameservers_count
|
|
||||||
validate_dnskeys_count
|
|
||||||
validate_admin_contacts_count
|
|
||||||
|
|
||||||
errors.empty?
|
|
||||||
end
|
|
||||||
|
|
||||||
# used for highlighting form tabs
|
# used for highlighting form tabs
|
||||||
def parent_valid?
|
def parent_valid?
|
||||||
assoc_errors = errors.keys.select { |x| x.match(/\./) }
|
assoc_errors = errors.keys.select { |x| x.match(/\./) }
|
||||||
|
|
|
@ -2,9 +2,6 @@
|
||||||
class Epp::EppDomain < Domain
|
class Epp::EppDomain < Domain
|
||||||
include EppErrors
|
include EppErrors
|
||||||
|
|
||||||
validate :validate_nameservers_count
|
|
||||||
validate :validate_admin_contacts_count
|
|
||||||
|
|
||||||
def epp_code_map # rubocop:disable Metrics/MethodLength
|
def epp_code_map # rubocop:disable Metrics/MethodLength
|
||||||
{
|
{
|
||||||
'2002' => [
|
'2002' => [
|
||||||
|
@ -19,7 +16,6 @@ class Epp::EppDomain < Domain
|
||||||
],
|
],
|
||||||
'2306' => [ # Parameter policy error
|
'2306' => [ # Parameter policy error
|
||||||
[:owner_contact, :blank],
|
[:owner_contact, :blank],
|
||||||
[:admin_contacts, :out_of_range],
|
|
||||||
[:base, :ds_data_with_key_not_allowed],
|
[:base, :ds_data_with_key_not_allowed],
|
||||||
[:base, :ds_data_not_allowed],
|
[:base, :ds_data_not_allowed],
|
||||||
[:base, :key_data_not_allowed],
|
[:base, :key_data_not_allowed],
|
||||||
|
@ -27,17 +23,29 @@ class Epp::EppDomain < Domain
|
||||||
],
|
],
|
||||||
'2004' => [ # Parameter value range error
|
'2004' => [ # Parameter value range error
|
||||||
[:nameservers, :out_of_range,
|
[:nameservers, :out_of_range,
|
||||||
{
|
{
|
||||||
min: Setting.ns_min_count,
|
min: Setting.ns_min_count,
|
||||||
max: Setting.ns_max_count
|
max: Setting.ns_max_count
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
[:period, :out_of_range, { value: { obj: 'period', val: period } }],
|
[:period, :out_of_range, { value: { obj: 'period', val: period } }],
|
||||||
[:dnskeys, :out_of_range,
|
[:dnskeys, :out_of_range,
|
||||||
{
|
{
|
||||||
min: Setting.dnskeys_min_count,
|
min: Setting.dnskeys_min_count,
|
||||||
max: Setting.dnskeys_max_count
|
max: Setting.dnskeys_max_count
|
||||||
}
|
}
|
||||||
|
],
|
||||||
|
[:admin_contacts, :out_of_range,
|
||||||
|
{
|
||||||
|
min: Setting.admin_contacts_min_count,
|
||||||
|
max: Setting.admin_contacts_max_count
|
||||||
|
}
|
||||||
|
],
|
||||||
|
[:tech_contacts, :out_of_range,
|
||||||
|
{
|
||||||
|
min: Setting.tech_contacts_min_count,
|
||||||
|
max: Setting.tech_contacts_max_count
|
||||||
|
}
|
||||||
]
|
]
|
||||||
],
|
],
|
||||||
'2005' => [
|
'2005' => [
|
||||||
|
|
8
app/validators/object_count_validator.rb
Normal file
8
app/validators/object_count_validator.rb
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
class ObjectCountValidator < ActiveModel::EachValidator
|
||||||
|
def validate_each(record, attribute, value)
|
||||||
|
min, max = options[:min].call, options[:max].call
|
||||||
|
return if value.reject(&:marked_for_destruction?).length.between?(min, max)
|
||||||
|
association = options[:association] || attribute
|
||||||
|
record.errors.add(association, :out_of_range, { min: min, max: max })
|
||||||
|
end
|
||||||
|
end
|
|
@ -6,4 +6,21 @@ if ActiveRecord::Base.connection.table_exists? 'settings' # otherwise rake not w
|
||||||
Setting.disclosure_phone = false if Setting.disclosure_phone.nil?
|
Setting.disclosure_phone = false if Setting.disclosure_phone.nil?
|
||||||
Setting.disclosure_fax = false if Setting.disclosure_fax.nil?
|
Setting.disclosure_fax = false if Setting.disclosure_fax.nil?
|
||||||
Setting.disclosure_address = false if Setting.disclosure_address.nil?
|
Setting.disclosure_address = false if Setting.disclosure_address.nil?
|
||||||
|
|
||||||
|
Setting.save_default(:admin_contacts_min_count, 1)
|
||||||
|
Setting.save_default(:admin_contacts_max_count, 10)
|
||||||
|
Setting.save_default(:tech_contacts_min_count, 1)
|
||||||
|
Setting.save_default(:tech_contacts_max_count, 10)
|
||||||
|
|
||||||
|
Setting.save_default(:ds_algorithm, 2)
|
||||||
|
Setting.save_default(:ds_data_allowed, true)
|
||||||
|
Setting.save_default(:ds_data_with_key_allowed, true)
|
||||||
|
Setting.save_default(:key_data_allowed, true)
|
||||||
|
|
||||||
|
Setting.save_default(:dnskeys_min_count, 0)
|
||||||
|
Setting.save_default(:dnskeys_max_count, 9)
|
||||||
|
Setting.save_default(:ns_min_count, 2)
|
||||||
|
Setting.save_default(:ns_max_count, 11)
|
||||||
|
|
||||||
|
Setting.save_default(:transfer_wait_time, 0)
|
||||||
end
|
end
|
||||||
|
|
|
@ -89,9 +89,11 @@ en:
|
||||||
invalid: 'Contacts are invalid'
|
invalid: 'Contacts are invalid'
|
||||||
not_found: 'Contact was not found'
|
not_found: 'Contact was not found'
|
||||||
admin_contacts:
|
admin_contacts:
|
||||||
out_of_range: 'Admin contacts count must be between 1 - infinity'
|
out_of_range: 'Admin contacts count must be between %{min}-%{max}'
|
||||||
less_than_or_equal_to: 'Admin contacts count must be less than or equal to %{count}'
|
less_than_or_equal_to: 'Admin contacts count must be less than or equal to %{count}'
|
||||||
greater_than_or_equal_to: 'Admin contacts count must be greater than or equal to %{count}'
|
greater_than_or_equal_to: 'Admin contacts count must be greater than or equal to %{count}'
|
||||||
|
tech_contacts:
|
||||||
|
out_of_range: 'Tech contacts count must be between %{min}-%{max}'
|
||||||
nameservers:
|
nameservers:
|
||||||
invalid: 'Nameservers are invalid'
|
invalid: 'Nameservers are invalid'
|
||||||
out_of_range: 'Nameservers count must be between %{min}-%{max}'
|
out_of_range: 'Nameservers count must be between %{min}-%{max}'
|
||||||
|
@ -472,3 +474,7 @@ en:
|
||||||
created_at: 'Created at'
|
created_at: 'Created at'
|
||||||
type: 'Type'
|
type: 'Type'
|
||||||
legal_documents: 'Legal documents'
|
legal_documents: 'Legal documents'
|
||||||
|
admin_contacts_min_count: 'Admin contacts minimum count'
|
||||||
|
admin_contacts_max_count: 'Admin contacts maximum count'
|
||||||
|
tech_contacts_min_count: 'Tech contacts minimum count'
|
||||||
|
tech_contacts_max_count: 'Tech contacts maximum count'
|
||||||
|
|
|
@ -747,8 +747,8 @@ describe 'EPP Domain', epp: true do
|
||||||
})
|
})
|
||||||
|
|
||||||
response = epp_request(xml, :xml)
|
response = epp_request(xml, :xml)
|
||||||
expect(response[:result_code]).to eq('2306')
|
expect(response[:result_code]).to eq('2004')
|
||||||
expect(response[:msg]).to eq('Admin contacts count must be between 1 - infinity')
|
expect(response[:msg]).to eq('Admin contacts count must be between 1-10')
|
||||||
expect(response[:clTRID]).to eq('ABC-12345')
|
expect(response[:clTRID]).to eq('ABC-12345')
|
||||||
|
|
||||||
expect(Domain.count).to eq 0
|
expect(Domain.count).to eq 0
|
||||||
|
|
|
@ -11,6 +11,11 @@ module General
|
||||||
Setting.ns_max_count = 11
|
Setting.ns_max_count = 11
|
||||||
|
|
||||||
Setting.transfer_wait_time = 0
|
Setting.transfer_wait_time = 0
|
||||||
|
|
||||||
|
Setting.admin_contacts_min_count = 1
|
||||||
|
Setting.admin_contacts_max_count = 10
|
||||||
|
Setting.tech_contacts_min_count = 0
|
||||||
|
Setting.tech_contacts_max_count = 10
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_disclosure_settings
|
def create_disclosure_settings
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue