From a002e87d7cc9d60776174c6e9d4208afa2ce5b97 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Sun, 6 Jan 2019 23:20:31 +0200 Subject: [PATCH 1/6] Move test case --- test/models/{domain => }/domain_test.rb | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename test/models/{domain => }/domain_test.rb (100%) diff --git a/test/models/domain/domain_test.rb b/test/models/domain_test.rb similarity index 100% rename from test/models/domain/domain_test.rb rename to test/models/domain_test.rb From 3d4d66983878943df6c825647a748641920b4b79 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Mon, 7 Jan 2019 00:54:14 +0200 Subject: [PATCH 2/6] Improve readability --- app/controllers/epp/domains_controller.rb | 4 ++-- app/models/epp/domain.rb | 29 ++++++++++++----------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/app/controllers/epp/domains_controller.rb b/app/controllers/epp/domains_controller.rb index 12b2277ef..f39aee81b 100644 --- a/app/controllers/epp/domains_controller.rb +++ b/app/controllers/epp/domains_controller.rb @@ -87,8 +87,8 @@ class Epp::DomainsController < EppController def check authorize! :check, Epp::Domain - names = params[:parsed_frame].css('name').map(&:text) - @domains = Epp::Domain.check_availability(names) + domain_names = params[:parsed_frame].css('name').map(&:text) + @domains = Epp::Domain.check_availability(domain_names) render_epp_response '/epp/domains/check' end diff --git a/app/models/epp/domain.rb b/app/models/epp/domain.rb index 2f25f3da5..ed04720af 100644 --- a/app/models/epp/domain.rb +++ b/app/models/epp/domain.rb @@ -788,31 +788,32 @@ class Epp::Domain < Domain } end - def check_availability(domains) - domains = [domains] if domains.is_a?(String) + def check_availability(domain_names) + domain_names = [domain_names] if domain_names.is_a?(String) - res = [] - domains.each do |x| - x.strip! - x.downcase! - unless DomainNameValidator.validate_format(x) - res << { name: x, avail: 0, reason: 'invalid format' } + 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' } next end - if ReservedDomain.pw_for(x).present? - res << { name: x, avail: 0, reason: I18n.t('errors.messages.epp_domain_reserved') } + if ReservedDomain.pw_for(domain_name).present? + result << { name: domain_name, avail: 0, reason: I18n.t('errors.messages.epp_domain_reserved') } next end - if Domain.find_by_idn x - res << { name: x, avail: 0, reason: 'in use' } + if Domain.find_by_idn domain_name + result << { name: domain_name, avail: 0, reason: 'in use' } else - res << { name: x, avail: 1 } + result << { name: domain_name, avail: 1 } end end - res + result end end From 3772f47613564ce9b904437d7480cf6bb854d3ee Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Mon, 7 Jan 2019 01:12:52 +0200 Subject: [PATCH 3/6] 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 From cad457fb72ca0b0ed9d2e8c3a7cdf695a35620c4 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Mon, 7 Jan 2019 01:30:25 +0200 Subject: [PATCH 4/6] Make domain available when reserved --- app/models/epp/domain.rb | 5 -- config/locales/en.yml | 1 - .../integration/epp/domain/check/base_test.rb | 48 +++++++++---------- 3 files changed, 24 insertions(+), 30 deletions(-) diff --git a/app/models/epp/domain.rb b/app/models/epp/domain.rb index 618c23521..ba6195710 100644 --- a/app/models/epp/domain.rb +++ b/app/models/epp/domain.rb @@ -802,11 +802,6 @@ class Epp::Domain < Domain next end - 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 - domain_name = DNS::DomainName.new(domain_name_as_string) if domain_name.unavailable? diff --git a/config/locales/en.yml b/config/locales/en.yml index ab35222e7..cdc78101f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -196,7 +196,6 @@ en: errors: 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' diff --git a/test/integration/epp/domain/check/base_test.rb b/test/integration/epp/domain/check/base_test.rb index 2c1b0ca66..8185ef5fd 100644 --- a/test/integration/epp/domain/check/base_test.rb +++ b/test/integration/epp/domain/check/base_test.rb @@ -23,7 +23,7 @@ class EppDomainCheckBaseTest < ApplicationIntegrationTest assert_equal 'some.test', response_xml.at_xpath('//domain:name', 'domain' => 'https://epp.tld.ee/schema/domain-eis-1.0.xsd').text end - def test_domain_is_available_when_not_registered_blocked_nor_reserved + def test_domain_is_available_when_not_registered_or_blocked request_xml = <<-XML @@ -44,6 +44,29 @@ class EppDomainCheckBaseTest < ApplicationIntegrationTest assert_nil response_xml.at_xpath('//domain:reason', 'domain' => 'https://epp.tld.ee/schema/domain-eis-1.0.xsd') end + def test_domain_is_available_when_reserved + assert_equal 'reserved.test', reserved_domains(:one).name + + request_xml = <<-XML + + + + + + reserved.test + + + + + XML + + post '/epp/command/check', { frame: request_xml }, 'HTTP_COOKIE' => 'session=api_bestnames' + + response_xml = Nokogiri::XML(response.body) + assert_equal '1', response_xml.at_xpath('//domain:name', 'domain' => 'https://epp.tld.ee/schema/domain-eis-1.0.xsd')['avail'] + assert_nil response_xml.at_xpath('//domain:reason', 'domain' => 'https://epp.tld.ee/schema/domain-eis-1.0.xsd') + end + def test_domain_is_unavailable_when_format_is_invalid request_xml = <<-XML @@ -111,29 +134,6 @@ class EppDomainCheckBaseTest < ApplicationIntegrationTest 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 - - request_xml = <<-XML - - - - - - reserved.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 'Domain name is reserved', response_xml.at_xpath('//domain:reason', 'domain' => 'https://epp.tld.ee/schema/domain-eis-1.0.xsd').text - end - def test_multiple_domains request_xml = <<-XML From ee864c807f45a3ed08e55accc716f97a90885771 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Mon, 7 Jan 2019 02:46:20 +0200 Subject: [PATCH 5/6] Refactor --- app/models/dns/domain_name.rb | 12 ++++++++++-- app/models/epp/domain.rb | 6 +----- config/locales/en.yml | 1 + test/models/dns/domain_name_test.rb | 8 ++++++++ 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/app/models/dns/domain_name.rb b/app/models/dns/domain_name.rb index baed7e2fe..0f8e7d4b0 100644 --- a/app/models/dns/domain_name.rb +++ b/app/models/dns/domain_name.rb @@ -7,17 +7,25 @@ module DNS end def unavailable? - blocked? + registered? || blocked? end def unavailability_reason - :blocked if blocked? + if registered? + :registered + elsif blocked? + :blocked + end end private attr_reader :name + def registered? + Domain.find_by_idn(name) + end + def blocked? BlockedDomain.where(name: name).any? end diff --git a/app/models/epp/domain.rb b/app/models/epp/domain.rb index ba6195710..09eb49481 100644 --- a/app/models/epp/domain.rb +++ b/app/models/epp/domain.rb @@ -810,11 +810,7 @@ class Epp::Domain < Domain 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_as_string, avail: 1 } - end + result << { name: domain_name_as_string, avail: 1 } end result diff --git a/config/locales/en.yml b/config/locales/en.yml index cdc78101f..ebf5b5b15 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -196,6 +196,7 @@ en: errors: messages: blank: 'is missing' + epp_domain_registered: in use epp_domain_blocked: Blocked epp_obj_does_not_exist: 'Object does not exist' epp_authorization_error: 'Authorization error' diff --git a/test/models/dns/domain_name_test.rb b/test/models/dns/domain_name_test.rb index ba7622936..122803e0e 100644 --- a/test/models/dns/domain_name_test.rb +++ b/test/models/dns/domain_name_test.rb @@ -1,6 +1,14 @@ require 'test_helper' class DNS::DomainNameTest < ActiveSupport::TestCase + def test_unavailable_when_registered + domain_name = DNS::DomainName.new('shop.test') + assert_equal 'shop.test', domains(:shop).name + + assert domain_name.unavailable? + assert_equal :registered, domain_name.unavailability_reason + end + def test_unavailable_when_blocked domain_name = DNS::DomainName.new('blocked.test') assert_equal 'blocked.test', blocked_domains(:one).name From d40dc28484e6cdfc03a7834646e108be5af6baf8 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Mon, 7 Jan 2019 03:20:55 +0200 Subject: [PATCH 6/6] Make domain unavailable when zone with the same origin exists --- app/models/dns/domain_name.rb | 8 ++++++- app/models/epp/domain.rb | 4 ++-- config/locales/en.yml | 1 + .../integration/epp/domain/check/base_test.rb | 23 +++++++++++++++++++ test/models/dns/domain_name_test.rb | 8 +++++++ 5 files changed, 41 insertions(+), 3 deletions(-) diff --git a/app/models/dns/domain_name.rb b/app/models/dns/domain_name.rb index 0f8e7d4b0..f388a975d 100644 --- a/app/models/dns/domain_name.rb +++ b/app/models/dns/domain_name.rb @@ -7,7 +7,7 @@ module DNS end def unavailable? - registered? || blocked? + registered? || blocked? || zone_with_same_origin? end def unavailability_reason @@ -15,6 +15,8 @@ module DNS :registered elsif blocked? :blocked + elsif zone_with_same_origin? + :zone_with_same_origin end end @@ -29,5 +31,9 @@ module DNS def blocked? BlockedDomain.where(name: name).any? end + + def zone_with_same_origin? + DNS::Zone.where(origin: name).any? + end end end diff --git a/app/models/epp/domain.rb b/app/models/epp/domain.rb index 09eb49481..4e3d20305 100644 --- a/app/models/epp/domain.rb +++ b/app/models/epp/domain.rb @@ -805,8 +805,8 @@ class Epp::Domain < Domain 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}") } + reason = I18n.t("errors.messages.epp_domain_#{domain_name.unavailability_reason}") + result << { name: domain_name_as_string, avail: 0, reason: reason } next end diff --git a/config/locales/en.yml b/config/locales/en.yml index ebf5b5b15..a01604919 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -198,6 +198,7 @@ en: blank: 'is missing' epp_domain_registered: in use epp_domain_blocked: Blocked + epp_domain_zone_with_same_origin: Zone with the same origin exists 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 8185ef5fd..65e746947 100644 --- a/test/integration/epp/domain/check/base_test.rb +++ b/test/integration/epp/domain/check/base_test.rb @@ -134,6 +134,29 @@ class EppDomainCheckBaseTest < ApplicationIntegrationTest 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_zone_with_the_same_origin_exists + assert_equal 'test', dns_zones(:one).origin + + request_xml = <<-XML + + + + + + 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 'Zone with the same origin exists', response_xml.at_xpath('//domain:reason', 'domain' => 'https://epp.tld.ee/schema/domain-eis-1.0.xsd').text + end + def test_multiple_domains request_xml = <<-XML diff --git a/test/models/dns/domain_name_test.rb b/test/models/dns/domain_name_test.rb index 122803e0e..c0445cf9e 100644 --- a/test/models/dns/domain_name_test.rb +++ b/test/models/dns/domain_name_test.rb @@ -16,4 +16,12 @@ class DNS::DomainNameTest < ActiveSupport::TestCase assert domain_name.unavailable? assert_equal :blocked, domain_name.unavailability_reason end + + def test_unavailable_when_zone_with_the_same_origin_exists + domain_name = DNS::DomainName.new('test') + assert_equal 'test', dns_zones(:one).origin + + assert domain_name.unavailable? + assert_equal :zone_with_same_origin, domain_name.unavailability_reason + end end