From a740ed34f3705818ea2371b58d5f855404585982 Mon Sep 17 00:00:00 2001 From: Martin Lensment Date: Thu, 9 Oct 2014 17:56:07 +0300 Subject: [PATCH] Refactor EPP Errors once again --- app/controllers/concerns/epp/common.rb | 2 +- app/models/concerns/epp_errors.rb | 30 +++++++++++++++++++++ app/models/delegation_signer.rb | 20 ++++++++++++++ app/models/dnskey.rb | 2 ++ app/models/domain.rb | 14 ---------- app/models/nameserver.rb | 2 ++ spec/epp/domain_spec.rb | 36 ++++++++++++++------------ 7 files changed, 74 insertions(+), 32 deletions(-) diff --git a/app/controllers/concerns/epp/common.rb b/app/controllers/concerns/epp/common.rb index c18ed2ea0..201fd7abe 100644 --- a/app/controllers/concerns/epp/common.rb +++ b/app/controllers/concerns/epp/common.rb @@ -41,7 +41,7 @@ module Epp::Common def handle_errors(obj = nil) @errors ||= [] if obj - obj.construct_epp_errors + obj.generate_epp_errors @errors += obj.errors[:epp_errors] end diff --git a/app/models/concerns/epp_errors.rb b/app/models/concerns/epp_errors.rb index 8e637fd74..1c1c2c5b0 100644 --- a/app/models/concerns/epp_errors.rb +++ b/app/models/concerns/epp_errors.rb @@ -1,6 +1,36 @@ module EppErrors extend ActiveSupport::Concern + def generate_epp_errors + epp_errors = [] + errors.messages.each do |key, values| + key = key.to_s.split('.')[0].to_sym + if self.class.reflect_on_association(key) + send(key).each do |x| + epp_errors << x.generate_epp_errors + end + end + + epp_errors << collect_parent_errors(values) + end + + errors[:epp_errors] = epp_errors + errors[:epp_errors].flatten! + # epp_errors = [] + # errors.messages.each do |key, values| + # if self.class.reflect_on_association(key) + # send(key).each do |x| + # epp_errors << x.errors[:epp_errors] + # end + # end + + # epp_errors << collect_parent_errors(values) + # end + + # errors[:epp_errors] = epp_errors + # errors[:epp_errors].flatten! + end + def construct_epp_errors epp_errors = [] errors.messages.each do |key, values| diff --git a/app/models/delegation_signer.rb b/app/models/delegation_signer.rb index 6fbff83e4..4c97feb5d 100644 --- a/app/models/delegation_signer.rb +++ b/app/models/delegation_signer.rb @@ -1,3 +1,23 @@ class DelegationSigner < ActiveRecord::Base + include EppErrors has_many :dnskeys + + validate :validate_dnskeys_uniqueness + + def epp_code_map + {} + end + +def validate_dnskeys_uniqueness + validated = [] + list = dnskeys.reject(&:marked_for_destruction?) + list.each do |dnskey| + next if dnskey.public_key.blank? + existing = list.select { |x| x.public_key == dnskey.public_key } + next unless existing.length > 1 + validated << dnskey.public_key + errors.add(:dnskeys, :invalid) if errors[:dnskeys].blank? + dnskey.errors.add(:public_key, :taken) + end + end end diff --git a/app/models/dnskey.rb b/app/models/dnskey.rb index 74dbf7a0a..f990804d8 100644 --- a/app/models/dnskey.rb +++ b/app/models/dnskey.rb @@ -9,6 +9,8 @@ class Dnskey < ActiveRecord::Base validate :validate_protocol validate :validate_flags + # after_validation :generate_epp_errors + ALGORITHMS = %w(3 5 6 7 8 252 253 254 255) PROTOCOLS = %w(3) FLAGS = %w(0 256 257) diff --git a/app/models/domain.rb b/app/models/domain.rb index 5ba50bc39..82aa2580d 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -54,7 +54,6 @@ class Domain < ActiveRecord::Base validate :validate_tech_contacts_uniqueness validate :validate_admin_contacts_uniqueness validate :validate_domain_statuses_uniqueness - #validate :validate_dnskeys_uniqueness validate :validate_nameserver_ips attr_accessor :owner_contact_typeahead @@ -159,19 +158,6 @@ class Domain < ActiveRecord::Base end end - def validate_dnskeys_uniqueness - validated = [] - list = dnskeys.reject(&:marked_for_destruction?) - list.each do |dnskey| - next if dnskey.public_key.blank? - existing = list.select { |x| x.public_key == dnskey.public_key } - next unless existing.length > 1 - validated << dnskey.public_key - errors.add(:dnskeys, :invalid) if errors[:dnskeys].blank? - dnskey.errors.add(:public_key, :taken) - end - end - def validate_period return unless period.present? if period_unit == 'd' diff --git a/app/models/nameserver.rb b/app/models/nameserver.rb index 6ced6cbd2..2d1f33c2a 100644 --- a/app/models/nameserver.rb +++ b/app/models/nameserver.rb @@ -8,6 +8,8 @@ class Nameserver < ActiveRecord::Base validates :ipv4, format: { with: /\A(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\z/, allow_blank: true } validates :ipv6, format: { with: /(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))/, allow_blank: true } + + # archiving has_paper_trail class_name: 'NameserverVersion' diff --git a/spec/epp/domain_spec.rb b/spec/epp/domain_spec.rb index 63b79fe43..3f558bec2 100644 --- a/spec/epp/domain_spec.rb +++ b/spec/epp/domain_spec.rb @@ -203,8 +203,12 @@ describe 'EPP Domain', epp: true do expect(d.nameservers.count).to eq(2) expect(d.auth_info).not_to be_empty - expect(d.dnskeys.count).to eq(1) - key = d.dnskeys.first + expect(d.delegation_signers.count).to eq(1) + ds = d.delegation_signers.first + + expect(ds.dnskeys.count).to eq(1) + + key = ds.dnskeys.first expect(key.flags).to eq(257) expect(key.protocol).to eq(3) @@ -221,7 +225,7 @@ describe 'EPP Domain', epp: true do }) response = epp_request(xml, :xml) - + po response expect(response[:result_code]).to eq('2306') expect(response[:msg]).to eq('IPv4 is missing') end @@ -373,10 +377,10 @@ describe 'EPP Domain', epp: true do end it 'does not create a domain when dnskeys are invalid' do - xml = domain_create_xml({ - dnssec: [ - { - dnskey: { + + xml = domain_create_xml({}, { + _other: [ + { keyData: { flags: { value: '250' }, protocol: { value: '4' }, alg: { value: '9' }, @@ -384,7 +388,7 @@ describe 'EPP Domain', epp: true do } }, { - dnskey: { + keyData: { flags: { value: '1' }, protocol: { value: '3' }, alg: { value: '10' }, @@ -392,7 +396,7 @@ describe 'EPP Domain', epp: true do } }, { - dnskey: { + keyData: { flags: { value: '256' }, protocol: { value: '5' }, alg: { value: '254' }, @@ -426,10 +430,9 @@ describe 'EPP Domain', epp: true do end it 'does not create a domain with two identical dnskeys' do - xml = domain_create_xml({ - dnssec: [ - { - dnskey: { + xml = domain_create_xml({}, { + _other: [ + { keyData: { flags: { value: '257' }, protocol: { value: '3' }, alg: { value: '3' }, @@ -437,15 +440,14 @@ describe 'EPP Domain', epp: true do } }, { - dnskey: { + keyData: { flags: { value: '0' }, protocol: { value: '3' }, alg: { value: '5' }, pubKey: { value: '700b97b591ed27ec2590d19f06f88bba700b97b591ed27ec2590d19f' } } - } - ] - }) + }] + }) response = epp_request(xml, :xml)