Make domain unavailable when blocked

This commit is contained in:
Artur Beljajev 2019-01-07 01:12:52 +02:00
parent 3d4d669838
commit 3772f47613
5 changed files with 79 additions and 10 deletions

View file

@ -0,0 +1,25 @@
module DNS
# Namespace is needed, because a class with the same name is defined by `domain_name` gem,
# a dependency of `actionmailer`,
class DomainName
def initialize(name)
@name = name
end
def unavailable?
blocked?
end
def unavailability_reason
:blocked if blocked?
end
private
attr_reader :name
def blocked?
BlockedDomain.where(name: name).any?
end
end
end

View file

@ -793,23 +793,32 @@ class Epp::Domain < Domain
result = [] result = []
domain_names.each do |domain_name| domain_names.each do |domain_name_as_string|
domain_name.strip! domain_name_as_string.strip!
domain_name.downcase! domain_name_as_string.downcase!
unless DomainNameValidator.validate_format(domain_name)
result << { name: domain_name, avail: 0, reason: 'invalid format' } unless DomainNameValidator.validate_format(domain_name_as_string)
result << { name: domain_name_as_string, avail: 0, reason: 'invalid format' }
next next
end end
if ReservedDomain.pw_for(domain_name).present? if ReservedDomain.pw_for(domain_name_as_string).present?
result << { name: domain_name, avail: 0, reason: I18n.t('errors.messages.epp_domain_reserved') } result << { name: domain_name_as_string, avail: 0, reason: I18n.t('errors.messages.epp_domain_reserved') }
next next
end end
if Domain.find_by_idn domain_name domain_name = DNS::DomainName.new(domain_name_as_string)
result << { name: domain_name, avail: 0, reason: 'in use' }
if domain_name.unavailable?
reason = domain_name.unavailability_reason
result << { name: domain_name_as_string, avail: 0, reason: I18n.t("errors.messages.epp_domain_#{reason}") }
next
end
if Domain.find_by_idn domain_name_as_string
result << { name: domain_name_as_string, avail: 0, reason: 'in use' }
else else
result << { name: domain_name, avail: 1 } result << { name: domain_name_as_string, avail: 1 }
end end
end end

View file

@ -197,6 +197,7 @@ en:
messages: messages:
blank: 'is missing' blank: 'is missing'
epp_domain_reserved: 'Domain name is reserved' epp_domain_reserved: 'Domain name is reserved'
epp_domain_blocked: Blocked
epp_obj_does_not_exist: 'Object does not exist' epp_obj_does_not_exist: 'Object does not exist'
epp_authorization_error: 'Authorization error' epp_authorization_error: 'Authorization error'
epp_id_taken: 'Contact id already exists' epp_id_taken: 'Contact id already exists'

View file

@ -88,6 +88,29 @@ class EppDomainCheckBaseTest < ApplicationIntegrationTest
assert_equal 'in use', response_xml.at_xpath('//domain:reason', 'domain' => 'https://epp.tld.ee/schema/domain-eis-1.0.xsd').text assert_equal 'in use', response_xml.at_xpath('//domain:reason', 'domain' => 'https://epp.tld.ee/schema/domain-eis-1.0.xsd').text
end end
def test_domain_is_unavailable_when_blocked
assert_equal 'blocked.test', blocked_domains(:one).name
request_xml = <<-XML
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<epp xmlns="https://epp.tld.ee/schema/epp-ee-1.0.xsd">
<command>
<check>
<domain:check xmlns:domain="https://epp.tld.ee/schema/domain-eis-1.0.xsd">
<domain:name>blocked.test</domain:name>
</domain:check>
</check>
</command>
</epp>
XML
post '/epp/command/check', { frame: request_xml }, 'HTTP_COOKIE' => 'session=api_bestnames'
response_xml = Nokogiri::XML(response.body)
assert_equal '0', response_xml.at_xpath('//domain:name', 'domain' => 'https://epp.tld.ee/schema/domain-eis-1.0.xsd')['avail']
assert_equal 'Blocked', response_xml.at_xpath('//domain:reason', 'domain' => 'https://epp.tld.ee/schema/domain-eis-1.0.xsd').text
end
def test_domain_is_unavailable_when_reserved def test_domain_is_unavailable_when_reserved
assert_equal 'reserved.test', reserved_domains(:one).name assert_equal 'reserved.test', reserved_domains(:one).name

View file

@ -0,0 +1,11 @@
require 'test_helper'
class DNS::DomainNameTest < ActiveSupport::TestCase
def test_unavailable_when_blocked
domain_name = DNS::DomainName.new('blocked.test')
assert_equal 'blocked.test', blocked_domains(:one).name
assert domain_name.unavailable?
assert_equal :blocked, domain_name.unavailability_reason
end
end