diff --git a/app/models/domain.rb b/app/models/domain.rb index 1c85df786..949e26657 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -54,7 +54,7 @@ class Domain < ActiveRecord::Base def attach_objects(ph, parsed_frame) attach_owner_contact(ph[:registrant]) attach_contacts(self.class.parse_contacts_from_frame(parsed_frame)) - attach_nameservers(self.class.parse_nameservers_from_params(ph[:ns])) + attach_nameservers(self.class.parse_nameservers_from_frame(parsed_frame)) errors.empty? end @@ -100,29 +100,11 @@ class Domain < ActiveRecord::Base end def attach_nameservers(ns_list) - ns_list.each do |ns| - attach_nameserver(ns) + ns_list.each do |ns_attrs| + self.nameservers.build(ns_attrs) end end - def attach_nameserver(ns) - self.nameservers.build(hostname: ns) and return if ns.is_a?(String) - - attrs = {hostname: ns[:hostName]} - - if ns[:hostAddr] - if ns[:hostAddr].is_a?(Array) - ns[:hostAddr].each do |ip| - attrs[:ipv4] = ip unless attrs[:ipv4] - end - else - attrs[:ipv4] = ns[:hostAddr] - end - end - - self.nameservers.build(attrs) - end - ### RENEW ### def renew(cur_exp_date, period, unit='y') @@ -190,11 +172,23 @@ class Domain < ActiveRecord::Base res end - def parse_nameservers_from_params(ph) - return [] unless ph - return ph[:hostObj] if ph[:hostObj] - return ph[:hostAttr] if ph[:hostAttr] - [] + def parse_nameservers_from_frame(parsed_frame) + res = [] + parsed_frame.css('hostAttr').each do |x| + res << { + hostname: x.css('hostName').first.try(:text), + ipv4: x.css('hostAddr[ip="v4"]').first.try(:text), + ipv6: x.css('hostAddr[ip="v6"]').first.try(:text) + } + end + + parsed_frame.css('hostObj').each do |x| + res << { + hostname: x.text + } + end + + res end def parse_period_unit_from_frame(parsed_frame) diff --git a/app/models/nameserver.rb b/app/models/nameserver.rb index d866bd5f6..e033d29b4 100644 --- a/app/models/nameserver.rb +++ b/app/models/nameserver.rb @@ -2,7 +2,7 @@ class Nameserver < ActiveRecord::Base include EppErrors EPP_CODE_MAP = { - '2005' => ['Hostname is invalid', 'IPv4 is invalid'] + '2005' => ['Hostname is invalid', 'IPv4 is invalid', 'IPv6 is invalid'] } EPP_ATTR_MAP = { @@ -14,4 +14,5 @@ class Nameserver < ActiveRecord::Base validates :hostname, format: { with: /\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/ } 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_nil: 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_nil: true } end diff --git a/config/locales/en.yml b/config/locales/en.yml index 0b1f12d7e..2b15d25f2 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -60,6 +60,8 @@ en: invalid: 'Hostname is invalid' ipv4: invalid: 'IPv4 is invalid' + ipv6: + invalid: 'IPv6 is invalid' attributes: domain: name: 'Domain name' diff --git a/spec/epp/domain_spec.rb b/spec/epp/domain_spec.rb index c4a0e2bf0..f2fcc5533 100644 --- a/spec/epp/domain_spec.rb +++ b/spec/epp/domain_spec.rb @@ -105,12 +105,19 @@ describe 'EPP Domain', epp: true do expect(Domain.first.nameservers.count).to eq(2) ns = Domain.first.nameservers.first expect(ns.ipv4).to eq('192.0.2.2') + expect(ns.ipv6).to eq('1080:0:0:0:8:800:200C:417A') end - it 'returns error when nameserver has invalid ip' do + it 'returns error when nameserver has invalid ips' do response = epp_request('domains/create_w_invalid_ns_ip.xml') expect(response[:results][0][:result_code]).to eq '2005' expect(response[:results][0][:msg]).to eq 'IPv4 is invalid' + expect(response[:results][0][:value]).to eq '192.0.2.2.invalid' + expect(response[:results][1][:result_code]).to eq '2005' + expect(response[:results][1][:msg]).to eq 'IPv6 is invalid' + expect(response[:results][1][:value]).to eq 'invalid_ipv6' + expect(Domain.count).to eq(0) + expect(Nameserver.count).to eq(0) end it 'creates a domain with period in days' do diff --git a/spec/epp/requests/domains/create_w_invalid_ns_ip.xml b/spec/epp/requests/domains/create_w_invalid_ns_ip.xml index ca7237ce2..80ef19ec2 100644 --- a/spec/epp/requests/domains/create_w_invalid_ns_ip.xml +++ b/spec/epp/requests/domains/create_w_invalid_ns_ip.xml @@ -12,7 +12,8 @@ 192.0.2.2.invalid - ns2.example.net + ns2.example.net + invalid_ipv6 jd1234