From d46640713b4d037aae06e111c113fc308e66832b Mon Sep 17 00:00:00 2001 From: Martin Lensment Date: Tue, 5 Aug 2014 17:19:23 +0300 Subject: [PATCH] Attach nameservers with attributes --- app/helpers/epp/domains_helper.rb | 4 ++- app/models/domain.rb | 24 +++++++++++++-- app/models/nameserver.rb | 1 + app/validators/nameserver_ip_validator.rb | 15 ++++++++++ spec/epp/domain_spec.rb | 8 +++++ .../requests/domains/create_w_host_attrs.xml | 30 +++++++++++++++++++ 6 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 app/validators/nameserver_ip_validator.rb create mode 100644 spec/epp/requests/domains/create_w_host_attrs.xml diff --git a/app/helpers/epp/domains_helper.rb b/app/helpers/epp/domains_helper.rb index aae54521e..7ba753864 100644 --- a/app/helpers/epp/domains_helper.rb +++ b/app/helpers/epp/domains_helper.rb @@ -53,7 +53,9 @@ module Epp::DomainsHelper def domain_nameservers ph = params_hash['epp']['command']['create']['create']['ns'] return [] unless ph - ph[:hostObj] + return ph[:hostObj] if ph[:hostObj] + return ph[:hostAttr] if ph[:hostAttr] + [] end def handle_errors diff --git a/app/models/domain.rb b/app/models/domain.rb index 70e4381a1..5644b81fc 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -63,8 +63,20 @@ class Domain < ActiveRecord::Base end def attach_nameservers(ns_list) - ns_list.each do |x| - self.nameservers.build(hostname: x) + ns_list.each do |ns| + #ns with detailed attributes + if ns.is_a?(Hash) + attrs = {hostname: ns[:hostName]} + + ns[:hostAddr].each do |ip| + attrs[:ip] = ip unless attrs[:ip] + end if ns[:hostAddr] + + self.nameservers.build(attrs) + #ns with just hostname + else + self.nameservers.build(hostname: ns) + end end save @@ -76,6 +88,14 @@ class Domain < ActiveRecord::Base errors.empty? end + # def validate_nameservers + # nameservers.each do |x| + # x.errors.each do |err| + # errors.add(:nameservers) + # end + # end + # end + def add_child_collection_errors(attr_key, epp_obj_name) send(attr_key).each do |obj| obj.errors.keys.each do |key| diff --git a/app/models/nameserver.rb b/app/models/nameserver.rb index 7c8134df6..4f8789e47 100644 --- a/app/models/nameserver.rb +++ b/app/models/nameserver.rb @@ -3,4 +3,5 @@ class Nameserver < ActiveRecord::Base has_and_belongs_to_many :domains validates :hostname, hostname: true + validates :ip, 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_nil: true} end diff --git a/app/validators/nameserver_ip_validator.rb b/app/validators/nameserver_ip_validator.rb new file mode 100644 index 000000000..2816b1b0e --- /dev/null +++ b/app/validators/nameserver_ip_validator.rb @@ -0,0 +1,15 @@ +class NameserverIpValidator < ActiveModel::EachValidator + def validate_each(record, attribute, value) + if record.domain_ + + if !self.class.validate_format(value) + record.errors.add(attribute, (options[:message] || :invalid)) + end + end + + class << self + def validate_format(value) + !!(value =~ /\A(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])\z/) + end + end +end diff --git a/spec/epp/domain_spec.rb b/spec/epp/domain_spec.rb index 79cc6a985..277b579f7 100644 --- a/spec/epp/domain_spec.rb +++ b/spec/epp/domain_spec.rb @@ -84,6 +84,14 @@ describe 'EPP Domain', epp: true do expect(response[:result_code]).to eq('2005') expect(response[:msg]).to eq('Hostname is invalid') end + + + it 'creates domain with nameservers with ips' do + response = epp_request('domains/create_w_host_attrs.xml') + expect(Domain.first.nameservers.count).to eq(2) + ns = Domain.first.nameservers.first + expect(ns.ip).to eq('192.0.2.2') + end end context 'with juridical persion as an owner' do diff --git a/spec/epp/requests/domains/create_w_host_attrs.xml b/spec/epp/requests/domains/create_w_host_attrs.xml new file mode 100644 index 000000000..173649aff --- /dev/null +++ b/spec/epp/requests/domains/create_w_host_attrs.xml @@ -0,0 +1,30 @@ + + + + + + example.ee + 1 + + + ns1.example.net + 192.0.2.2 + 1080:0:0:0:8:800:200C:417A + + + ns2.example.net + + + jd1234 + sh8013 + sh8013 + sh801333 + + 2fooBAR + + + + ABC-12345 + +