From 3772f47613564ce9b904437d7480cf6bb854d3ee Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Mon, 7 Jan 2019 01:12:52 +0200 Subject: [PATCH] Make domain unavailable when blocked --- app/models/dns/domain_name.rb | 25 ++++++++++++++++ app/models/epp/domain.rb | 29 ++++++++++++------- config/locales/en.yml | 1 + .../integration/epp/domain/check/base_test.rb | 23 +++++++++++++++ test/models/dns/domain_name_test.rb | 11 +++++++ 5 files changed, 79 insertions(+), 10 deletions(-) create mode 100644 app/models/dns/domain_name.rb create mode 100644 test/models/dns/domain_name_test.rb diff --git a/app/models/dns/domain_name.rb b/app/models/dns/domain_name.rb new file mode 100644 index 000000000..baed7e2fe --- /dev/null +++ b/app/models/dns/domain_name.rb @@ -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 diff --git a/app/models/epp/domain.rb b/app/models/epp/domain.rb index ed04720af..618c23521 100644 --- a/app/models/epp/domain.rb +++ b/app/models/epp/domain.rb @@ -793,23 +793,32 @@ class Epp::Domain < Domain result = [] - domain_names.each do |domain_name| - domain_name.strip! - domain_name.downcase! - unless DomainNameValidator.validate_format(domain_name) - result << { name: domain_name, avail: 0, reason: 'invalid format' } + domain_names.each do |domain_name_as_string| + domain_name_as_string.strip! + domain_name_as_string.downcase! + + unless DomainNameValidator.validate_format(domain_name_as_string) + result << { name: domain_name_as_string, avail: 0, reason: 'invalid format' } next end - if ReservedDomain.pw_for(domain_name).present? - result << { name: domain_name, avail: 0, reason: I18n.t('errors.messages.epp_domain_reserved') } + if ReservedDomain.pw_for(domain_name_as_string).present? + result << { name: domain_name_as_string, avail: 0, reason: I18n.t('errors.messages.epp_domain_reserved') } next end - if Domain.find_by_idn domain_name - result << { name: domain_name, avail: 0, reason: 'in use' } + domain_name = DNS::DomainName.new(domain_name_as_string) + + 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 - result << { name: domain_name, avail: 1 } + result << { name: domain_name_as_string, avail: 1 } end end diff --git a/config/locales/en.yml b/config/locales/en.yml index e9e499def..ab35222e7 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -197,6 +197,7 @@ en: messages: blank: 'is missing' epp_domain_reserved: 'Domain name is reserved' + epp_domain_blocked: Blocked epp_obj_does_not_exist: 'Object does not exist' epp_authorization_error: 'Authorization error' epp_id_taken: 'Contact id already exists' diff --git a/test/integration/epp/domain/check/base_test.rb b/test/integration/epp/domain/check/base_test.rb index 5d98b60ff..2c1b0ca66 100644 --- a/test/integration/epp/domain/check/base_test.rb +++ b/test/integration/epp/domain/check/base_test.rb @@ -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 end + def test_domain_is_unavailable_when_blocked + assert_equal 'blocked.test', blocked_domains(:one).name + + request_xml = <<-XML + + + + + + blocked.test + + + + + 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 assert_equal 'reserved.test', reserved_domains(:one).name diff --git a/test/models/dns/domain_name_test.rb b/test/models/dns/domain_name_test.rb new file mode 100644 index 000000000..ba7622936 --- /dev/null +++ b/test/models/dns/domain_name_test.rb @@ -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