mirror of
https://github.com/internetee/registry.git
synced 2025-05-17 01:47:18 +02:00
Handle children errors
This commit is contained in:
parent
3dca771bcb
commit
9ce6efc4db
8 changed files with 86 additions and 9 deletions
|
@ -32,12 +32,15 @@ module Epp::Common
|
||||||
@current_epp_user ||= EppUser.find(epp_session[:epp_user_id]) if epp_session[:epp_user_id]
|
@current_epp_user ||= EppUser.find(epp_session[:epp_user_id]) if epp_session[:epp_user_id]
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_errors(error_code_map, obj)
|
def handle_epp_errors(error_code_map, obj)
|
||||||
obj.errors.each do |key, err|
|
obj.errors.each do |key, err|
|
||||||
error_code_map.each do |code, values|
|
error_code_map.each do |code, values|
|
||||||
|
|
||||||
has_error = Proc.new do |x|
|
has_error = Proc.new do |x|
|
||||||
x.is_a?(Array) ? obj.errors.added?(key, x[0], x[1]) : obj.errors.added?(key, x)
|
if x.is_a?(Array)
|
||||||
|
obj.errors.generate_message(key, x[0], x[1]) == err
|
||||||
|
else
|
||||||
|
obj.errors.generate_message(key, x) == err
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if err.is_a?(Hash)
|
if err.is_a?(Hash)
|
||||||
|
|
|
@ -57,10 +57,11 @@ module Epp::DomainsHelper
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_errors
|
def handle_errors
|
||||||
super({
|
handle_epp_errors({
|
||||||
'2302' => [:epp_domain_taken, :epp_domain_reserved],
|
'2302' => [:epp_domain_taken, :epp_domain_reserved],
|
||||||
'2306' => [:blank, [:out_of_range, {min: 1, max: 13}]],
|
'2306' => [:blank, [:out_of_range, {min: 1, max: 13}]],
|
||||||
'2303' => [:not_found]
|
'2303' => [:not_found],
|
||||||
|
'2005' => [:hostname_invalid]
|
||||||
}, @domain
|
}, @domain
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
|
@ -20,6 +20,7 @@ class Domain < ActiveRecord::Base
|
||||||
validates :name_dirty, domain_name: true, uniqueness: { message: :epp_domain_taken }
|
validates :name_dirty, domain_name: true, uniqueness: { message: :epp_domain_taken }
|
||||||
validates :period, numericality: { only_integer: true, greater_than: 0, less_than: 100 }
|
validates :period, numericality: { only_integer: true, greater_than: 0, less_than: 100 }
|
||||||
validates :name, :owner_contact, presence: true
|
validates :name, :owner_contact, presence: true
|
||||||
|
validates_associated :nameservers
|
||||||
|
|
||||||
def name=(value)
|
def name=(value)
|
||||||
value.strip!
|
value.strip!
|
||||||
|
@ -61,17 +62,38 @@ class Domain < ActiveRecord::Base
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def attach_nameservers(nameservers)
|
def attach_nameservers(ns_list)
|
||||||
nameservers.each do |x|
|
ns_list.each do |x|
|
||||||
self.nameservers << Nameserver.find_or_create_by(hostname: x)
|
self.nameservers.build(hostname: x)
|
||||||
end
|
end
|
||||||
save!
|
|
||||||
|
save
|
||||||
|
|
||||||
|
add_child_collection_errors(:nameservers, :ns)
|
||||||
|
|
||||||
validate_nameservers_count
|
validate_nameservers_count
|
||||||
|
|
||||||
errors.empty?
|
errors.empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def add_child_collection_errors(attr_key, epp_obj_name)
|
||||||
|
send(attr_key).each do |obj|
|
||||||
|
obj.errors.keys.each do |key|
|
||||||
|
add_errors_with_value(attr_key, epp_obj_name, obj, key)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def add_errors_with_value(attr_key, epp_obj_name, obj, key)
|
||||||
|
obj.errors[key].each do |x|
|
||||||
|
errors.add(attr_key, {
|
||||||
|
obj: epp_obj_name,
|
||||||
|
val: obj.send(key),
|
||||||
|
msg: x
|
||||||
|
})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def validate_nameservers_count
|
def validate_nameservers_count
|
||||||
errors.add(:nameservers, :blank) if nameservers.empty?
|
errors.add(:nameservers, :blank) if nameservers.empty?
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
class Nameserver < ActiveRecord::Base
|
class Nameserver < ActiveRecord::Base
|
||||||
belongs_to :registrar
|
belongs_to :registrar
|
||||||
has_and_belongs_to_many :domains
|
has_and_belongs_to_many :domains
|
||||||
|
|
||||||
|
validates :hostname, hostname: true
|
||||||
end
|
end
|
||||||
|
|
13
app/validators/hostname_validator.rb
Normal file
13
app/validators/hostname_validator.rb
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
class HostnameValidator < ActiveModel::EachValidator
|
||||||
|
def validate_each(record, attribute, value)
|
||||||
|
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
|
|
@ -48,6 +48,12 @@ en:
|
||||||
nameservers:
|
nameservers:
|
||||||
blank: 'Required parameter missing - nameserver'
|
blank: 'Required parameter missing - nameserver'
|
||||||
out_of_range: 'Domain must have %{min}-%{max} nameservers'
|
out_of_range: 'Domain must have %{min}-%{max} nameservers'
|
||||||
|
hostname_invalid: 'Hostname is invalid'
|
||||||
|
nameserver:
|
||||||
|
attributes:
|
||||||
|
hostname:
|
||||||
|
invalid: 'Hostname is invalid'
|
||||||
|
|
||||||
|
|
||||||
errors:
|
errors:
|
||||||
messages:
|
messages:
|
||||||
|
|
|
@ -78,6 +78,12 @@ describe 'EPP Domain', epp: true do
|
||||||
expect(response[:result_code]).to eq('2306')
|
expect(response[:result_code]).to eq('2306')
|
||||||
expect(response[:msg]).to eq('Domain must have 1-13 nameservers')
|
expect(response[:msg]).to eq('Domain must have 1-13 nameservers')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'returns error when invalid nameservers are present' do
|
||||||
|
response = epp_request('domains/create_w_invalid_nameservers.xml')
|
||||||
|
expect(response[:result_code]).to eq('2005')
|
||||||
|
expect(response[:msg]).to eq('Hostname is invalid')
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'with juridical persion as an owner' do
|
context 'with juridical persion as an owner' do
|
||||||
|
|
24
spec/epp/requests/domains/create_w_invalid_nameservers.xml
Normal file
24
spec/epp/requests/domains/create_w_invalid_nameservers.xml
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||||
|
<epp xmlns="urn:ietf:params:xml:ns:epp-1.0">
|
||||||
|
<command>
|
||||||
|
<create>
|
||||||
|
<domain:create
|
||||||
|
xmlns:domain="urn:ietf:params:xml:ns:domain-1.0">
|
||||||
|
<domain:name>example.ee</domain:name>
|
||||||
|
<domain:period unit="y">1</domain:period>
|
||||||
|
<domain:ns>
|
||||||
|
<domain:hostObj>invalid1-</domain:hostObj>
|
||||||
|
<domain:hostObj>-invalid2</domain:hostObj>
|
||||||
|
</domain:ns>
|
||||||
|
<domain:registrant>jd1234</domain:registrant>
|
||||||
|
<domain:contact type="admin">sh8013</domain:contact>
|
||||||
|
<domain:contact type="tech">sh8013</domain:contact>
|
||||||
|
<domain:contact type="tech">sh801333</domain:contact>
|
||||||
|
<domain:authInfo>
|
||||||
|
<domain:pw>2fooBAR</domain:pw>
|
||||||
|
</domain:authInfo>
|
||||||
|
</domain:create>
|
||||||
|
</create>
|
||||||
|
<clTRID>ABC-12345</clTRID>
|
||||||
|
</command>
|
||||||
|
</epp>
|
Loading…
Add table
Add a link
Reference in a new issue