From 3b11f0bc20935cfef355e16e94324b547d470931 Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Mon, 15 Mar 2021 13:18:56 +0200 Subject: [PATCH 1/5] Test: Added test for legal doc sizes --- .../epp/domain/create/base_test.rb | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) diff --git a/test/integration/epp/domain/create/base_test.rb b/test/integration/epp/domain/create/base_test.rb index 35ef38179..fa04fc707 100644 --- a/test/integration/epp/domain/create/base_test.rb +++ b/test/integration/epp/domain/create/base_test.rb @@ -46,6 +46,109 @@ class EppDomainCreateBaseTest < EppTestCase assert_epp_response :parameter_value_syntax_error end + def test_too_small_legal_document + name = "new.#{dns_zones(:one).origin}" + contact = contacts(:john) + registrant = contact.becomes(Registrant) + + request_xml = <<-XML + + + + + + #{name} + #{registrant.code} + + + + + #{'test' * 2} + + + + + XML + assert_no_difference 'Domain.count' do + post epp_create_path, params: { frame: request_xml }, + headers: { 'HTTP_COOKIE' => 'session=api_bestnames' } + end + + assert_epp_response :parameter_value_policy_error + end + + def test_too_big_legal_document + name = "new.#{dns_zones(:one).origin}" + contact = contacts(:john) + registrant = contact.becomes(Registrant) + + # 8388608 bites == 8 mb + bignum_legaldoc = 't' * 8388608 + bignum_legaldoc+= "t" + + request_xml = <<-XML + + + + + + #{name} + #{registrant.code} + + + + + #{bignum_legaldoc} + + + + + XML + + assert_no_difference 'Domain.count' do + post epp_create_path, params: { frame: request_xml }, + headers: { 'HTTP_COOKIE' => 'session=api_bestnames' } + end + + assert_epp_response :parameter_value_policy_error + end + + def test_upper_limit_of_value_legal_document + name = "new.#{dns_zones(:one).origin}" + contact = contacts(:john) + registrant = contact.becomes(Registrant) + + # 8388608 bites == 8 mb + bignum_legaldoc = 't' * 8388608 + + request_xml = <<-XML + + + + + + #{name} + #{registrant.code} + + + + + #{bignum_legaldoc} + + + + + XML + + assert_difference 'Domain.count' do + post epp_create_path, params: { frame: request_xml }, + headers: { 'HTTP_COOKIE' => 'session=api_bestnames' } + end + + assert_epp_response :completed_successfully + end + + def test_not_registers_domain_without_legaldoc now = Time.zone.parse('2010-07-05') From 9f21b1704cd3eacb62f9d2157a2dcf584c0d932b Mon Sep 17 00:00:00 2001 From: Alex Sherman Date: Thu, 18 Mar 2021 15:12:30 +0500 Subject: [PATCH 2/5] Change error type if legaldoc file is more than 8 MB --- app/controllers/epp/base_controller.rb | 8 +++++++- test/integration/epp/domain/create/base_test.rb | 15 +++++++-------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/app/controllers/epp/base_controller.rb b/app/controllers/epp/base_controller.rb index 99c0ead35..ff711c490 100644 --- a/app/controllers/epp/base_controller.rb +++ b/app/controllers/epp/base_controller.rb @@ -23,6 +23,8 @@ module Epp rescue_from ActiveRecord::RecordNotFound, with: :respond_with_object_does_not_exist_error before_action :set_paper_trail_whodunnit + EIGHT_MEGABYTES = 8_388_608 + protected def respond_with_command_failed_error(exception) @@ -62,13 +64,17 @@ module Epp return if %w[hello error].include?(params[:action]) schema.validate(params[:nokogiri_frame]).each do |error| epp_errors << { - code: 2001, + code: error_code(error), msg: error } end handle_errors and return if epp_errors.any? end + def error_code(error) + error.str1.present? && error.str1.size > EIGHT_MEGABYTES ? 2306 : 2001 + end + def schema EPP_ALL_SCHEMA end diff --git a/test/integration/epp/domain/create/base_test.rb b/test/integration/epp/domain/create/base_test.rb index fa04fc707..777f01abc 100644 --- a/test/integration/epp/domain/create/base_test.rb +++ b/test/integration/epp/domain/create/base_test.rb @@ -73,7 +73,7 @@ class EppDomainCreateBaseTest < EppTestCase post epp_create_path, params: { frame: request_xml }, headers: { 'HTTP_COOKIE' => 'session=api_bestnames' } end - + assert_epp_response :parameter_value_policy_error end @@ -82,9 +82,8 @@ class EppDomainCreateBaseTest < EppTestCase contact = contacts(:john) registrant = contact.becomes(Registrant) - # 8388608 bites == 8 mb - bignum_legaldoc = 't' * 8388608 - bignum_legaldoc+= "t" + # 8388608 bytes == 8 mb + bignum_legaldoc = 't' * (8388608 + 1) request_xml = <<-XML @@ -109,7 +108,7 @@ class EppDomainCreateBaseTest < EppTestCase post epp_create_path, params: { frame: request_xml }, headers: { 'HTTP_COOKIE' => 'session=api_bestnames' } end - + assert_epp_response :parameter_value_policy_error end @@ -118,7 +117,7 @@ class EppDomainCreateBaseTest < EppTestCase contact = contacts(:john) registrant = contact.becomes(Registrant) - # 8388608 bites == 8 mb + # 8388608 bytes == 8 mb bignum_legaldoc = 't' * 8388608 request_xml = <<-XML @@ -139,12 +138,12 @@ class EppDomainCreateBaseTest < EppTestCase XML - + assert_difference 'Domain.count' do post epp_create_path, params: { frame: request_xml }, headers: { 'HTTP_COOKIE' => 'session=api_bestnames' } end - + assert_epp_response :completed_successfully end From 31a463c58714f0cd75de6f8ad5a1618bbc2b4f02 Mon Sep 17 00:00:00 2001 From: Alex Sherman Date: Thu, 18 Mar 2021 15:22:02 +0500 Subject: [PATCH 3/5] Move size constant to LegalDocument class --- app/controllers/epp/base_controller.rb | 4 +--- app/models/legal_document.rb | 1 + test/integration/epp/domain/create/base_test.rb | 6 ++---- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/app/controllers/epp/base_controller.rb b/app/controllers/epp/base_controller.rb index ff711c490..205b7f844 100644 --- a/app/controllers/epp/base_controller.rb +++ b/app/controllers/epp/base_controller.rb @@ -23,8 +23,6 @@ module Epp rescue_from ActiveRecord::RecordNotFound, with: :respond_with_object_does_not_exist_error before_action :set_paper_trail_whodunnit - EIGHT_MEGABYTES = 8_388_608 - protected def respond_with_command_failed_error(exception) @@ -72,7 +70,7 @@ module Epp end def error_code(error) - error.str1.present? && error.str1.size > EIGHT_MEGABYTES ? 2306 : 2001 + error.str1.present? && error.str1.size > LegalDocument::MAX_BODY_SIZE ? 2306 : 2001 end def schema diff --git a/app/models/legal_document.rb b/app/models/legal_document.rb index 377a9fdde..24b965996 100644 --- a/app/models/legal_document.rb +++ b/app/models/legal_document.rb @@ -1,6 +1,7 @@ class LegalDocument < ApplicationRecord include EppErrors MIN_BODY_SIZE = (1.37 * 3.kilobytes).ceil + MAX_BODY_SIZE = 8.megabytes if ENV['legal_document_types'].present? TYPES = ENV['legal_document_types'].split(',').map(&:strip) diff --git a/test/integration/epp/domain/create/base_test.rb b/test/integration/epp/domain/create/base_test.rb index 777f01abc..5308de519 100644 --- a/test/integration/epp/domain/create/base_test.rb +++ b/test/integration/epp/domain/create/base_test.rb @@ -82,8 +82,7 @@ class EppDomainCreateBaseTest < EppTestCase contact = contacts(:john) registrant = contact.becomes(Registrant) - # 8388608 bytes == 8 mb - bignum_legaldoc = 't' * (8388608 + 1) + bignum_legaldoc = 't' * (LegalDocument::MAX_BODY_SIZE + 1) request_xml = <<-XML @@ -117,8 +116,7 @@ class EppDomainCreateBaseTest < EppTestCase contact = contacts(:john) registrant = contact.becomes(Registrant) - # 8388608 bytes == 8 mb - bignum_legaldoc = 't' * 8388608 + bignum_legaldoc = 't' * LegalDocument::MAX_BODY_SIZE request_xml = <<-XML From 0f58e475131808e534045ba67da1540e794dd182 Mon Sep 17 00:00:00 2001 From: Alex Sherman Date: Fri, 19 Mar 2021 15:35:55 +0500 Subject: [PATCH 4/5] Fix test to have file at Base64, fix size validation --- app/controllers/epp/base_controller.rb | 6 +----- app/models/legal_document.rb | 13 +++++++++---- config/locales/en.yml | 3 ++- test/integration/epp/domain/create/base_test.rb | 4 +++- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/app/controllers/epp/base_controller.rb b/app/controllers/epp/base_controller.rb index 205b7f844..99c0ead35 100644 --- a/app/controllers/epp/base_controller.rb +++ b/app/controllers/epp/base_controller.rb @@ -62,17 +62,13 @@ module Epp return if %w[hello error].include?(params[:action]) schema.validate(params[:nokogiri_frame]).each do |error| epp_errors << { - code: error_code(error), + code: 2001, msg: error } end handle_errors and return if epp_errors.any? end - def error_code(error) - error.str1.present? && error.str1.size > LegalDocument::MAX_BODY_SIZE ? 2306 : 2001 - end - def schema EPP_ALL_SCHEMA end diff --git a/app/models/legal_document.rb b/app/models/legal_document.rb index 24b965996..eba250991 100644 --- a/app/models/legal_document.rb +++ b/app/models/legal_document.rb @@ -21,14 +21,19 @@ class LegalDocument < ApplicationRecord def epp_code_map { - '2306' => [ - [:body, :length] - ] + '2306' => [ + %i[body length_more_than], + %i[body length_less_than], + ] } end def val_body_length - errors.add(:body, :length) if body.nil? || body.size < MIN_BODY_SIZE + if body.nil? || body.size < MIN_BODY_SIZE + errors.add(:body, :length_more_than) + elsif body.size > MAX_BODY_SIZE + errors.add(:body, :length_less_than) + end end def save_to_filesystem diff --git a/config/locales/en.yml b/config/locales/en.yml index 97c968996..f5800127e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -138,7 +138,8 @@ en: legal_document: attributes: body: - length: 'Parameter value policy error: legalDocument size should be more than 3kB' + length_more_than: 'Parameter value policy error: legalDocument size should be more than 3kB' + length_less_than: 'Parameter value policy error: legalDocument size should be less than 8mB' attributes: diff --git a/test/integration/epp/domain/create/base_test.rb b/test/integration/epp/domain/create/base_test.rb index 5308de519..5069ddf41 100644 --- a/test/integration/epp/domain/create/base_test.rb +++ b/test/integration/epp/domain/create/base_test.rb @@ -82,7 +82,7 @@ class EppDomainCreateBaseTest < EppTestCase contact = contacts(:john) registrant = contact.becomes(Registrant) - bignum_legaldoc = 't' * (LegalDocument::MAX_BODY_SIZE + 1) + bignum_legaldoc = Base64.encode64('t' * (LegalDocument::MAX_BODY_SIZE + 1)).gsub(/\n/,"") request_xml = <<-XML @@ -109,6 +109,8 @@ class EppDomainCreateBaseTest < EppTestCase end assert_epp_response :parameter_value_policy_error + error_description = 'legalDocument size should be less than 8mB' + assert response.body.include? error_description end def test_upper_limit_of_value_legal_document From c52c66933913c46f1335dfa463f6abc53c1a35d7 Mon Sep 17 00:00:00 2001 From: Alex Sherman Date: Wed, 28 Apr 2021 14:02:31 +0500 Subject: [PATCH 5/5] Fix error code/message --- app/models/legal_document.rb | 2 +- config/locales/en.yml | 4 ++-- test/integration/epp/domain/create/base_test.rb | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/models/legal_document.rb b/app/models/legal_document.rb index eba250991..07c50c5dc 100644 --- a/app/models/legal_document.rb +++ b/app/models/legal_document.rb @@ -21,7 +21,7 @@ class LegalDocument < ApplicationRecord def epp_code_map { - '2306' => [ + '2308' => [ %i[body length_more_than], %i[body length_less_than], ] diff --git a/config/locales/en.yml b/config/locales/en.yml index f5800127e..a909dfcf4 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -138,8 +138,8 @@ en: legal_document: attributes: body: - length_more_than: 'Parameter value policy error: legalDocument size should be more than 3kB' - length_less_than: 'Parameter value policy error: legalDocument size should be less than 8mB' + length_more_than: 'Parameter value policy error: Legaldoc size is less than minimum allowed size of 3kB' + length_less_than: 'Parameter value policy error: Legaldoc size exceeds maximum allowed size of 8mB' attributes: diff --git a/test/integration/epp/domain/create/base_test.rb b/test/integration/epp/domain/create/base_test.rb index 5069ddf41..59a8ddf77 100644 --- a/test/integration/epp/domain/create/base_test.rb +++ b/test/integration/epp/domain/create/base_test.rb @@ -74,7 +74,7 @@ class EppDomainCreateBaseTest < EppTestCase headers: { 'HTTP_COOKIE' => 'session=api_bestnames' } end - assert_epp_response :parameter_value_policy_error + assert_epp_response :data_management_policy_violation end def test_too_big_legal_document @@ -108,8 +108,8 @@ class EppDomainCreateBaseTest < EppTestCase headers: { 'HTTP_COOKIE' => 'session=api_bestnames' } end - assert_epp_response :parameter_value_policy_error - error_description = 'legalDocument size should be less than 8mB' + assert_epp_response :data_management_policy_violation + error_description = 'Legaldoc size exceeds maximum allowed size of 8mB' assert response.body.include? error_description end