From 4f21dc701a30e9ad84272cc3ea49eac177e9f322 Mon Sep 17 00:00:00 2001 From: oleghasjanov Date: Fri, 29 Nov 2024 13:05:49 +0200 Subject: [PATCH] Add expiration handling for reserved domains and update tests - Add expiration check in DomainAvailabilityCheckerService to automatically remove expired domains - Update InvoiceResponseStruct to include reserved_domain_names in response - Add tests for expired domain handling in DomainAvailabilityCheckerService - Add tests for expired domain handling in ReserveDomainInvoice - Remove debug logging from EisBilling service The changes ensure that expired reserved domains are automatically removed during availability checks, making them available for new reservations. Domains with null expire_at remain permanently reserved. --- app/models/reserve_domain_invoice.rb | 7 ++- .../domain_availability_checker_service.rb | 10 +++- .../get_reserved_domains_invoice_status.rb | 4 -- test/models/reserve_domain_invoice_test.rb | 40 +++++++++++++ ...omain_availability_checker_service_test.rb | 57 +++++++++++++++++++ 5 files changed, 109 insertions(+), 9 deletions(-) diff --git a/app/models/reserve_domain_invoice.rb b/app/models/reserve_domain_invoice.rb index 718333eece..827f82ad1b 100644 --- a/app/models/reserve_domain_invoice.rb +++ b/app/models/reserve_domain_invoice.rb @@ -6,7 +6,7 @@ class InvoiceStruct < Struct.new(:total, :number, :buyer_name, :buyer_email, keyword_init: true) end - class InvoiceResponseStruct < Struct.new(:status_code_success, :linkpay_url, :invoice_number, :details, :user_unique_id, + class InvoiceResponseStruct < Struct.new(:status_code_success, :linkpay_url, :invoice_number, :details, :reserved_domain_names, :user_unique_id, keyword_init: true) end @@ -41,7 +41,7 @@ def create_list_of_domains(domain_names) result = process_invoice(invoice) create_reserve_domain_invoice(invoice.number, available_names, invoice.user_unique_id) - build_response(result, invoice.number, invoice.user_unique_id) + build_response(result, invoice.number, invoice.user_unique_id, available_names) end def are_domains_intersect?(domain_names) @@ -146,7 +146,7 @@ def create_reserve_domain_invoice(invoice_number, domain_names, user_unique_id) ) end - def build_response(result, invoice_number, user_unique_id) + def build_response(result, invoice_number, user_unique_id, domain_names) parsed_result = JSON.parse(result.body) # link = JSON.parse(result.body)['everypay_link'] @@ -155,6 +155,7 @@ def build_response(result, invoice_number, user_unique_id) linkpay_url: parsed_result['everypay_link'], invoice_number: invoice_number, user_unique_id: user_unique_id, + reserved_domain_names: domain_names, details: parsed_result ) end diff --git a/app/services/business_registry/domain_availability_checker_service.rb b/app/services/business_registry/domain_availability_checker_service.rb index 9b6b9b3d91..39c7846194 100644 --- a/app/services/business_registry/domain_availability_checker_service.rb +++ b/app/services/business_registry/domain_availability_checker_service.rb @@ -4,14 +4,20 @@ def self.filter_available(domains) result = Epp::Domain.check_availability(domains) available_domains = domains.select { |domain| result.find { |r| r[:name] == domain }[:avail].positive? } - domains_in_reserved = ReservedDomain.where(name: available_domains).pluck(:name) - available_domains - domains_in_reserved + domains_in_reserved = ReservedDomain.where(name: available_domains) + domains_in_reserved.each(&:destroy_if_expired) + + + available_domains - ReservedDomain.where(name: available_domains).pluck(:name) end def self.is_domain_available?(domain) result = Epp::Domain.check_availability([domain]) result = result.find { |r| r[:name] == domain }[:avail].positive? + reserved_domain = ReservedDomain.find_by(name: domain) + reserved_domain&.destroy_if_expired + result && ReservedDomain.find_by(name: domain).nil? end end diff --git a/app/services/eis_billing/get_reserved_domains_invoice_status.rb b/app/services/eis_billing/get_reserved_domains_invoice_status.rb index 27d3c0527f..812e9ccc8e 100644 --- a/app/services/eis_billing/get_reserved_domains_invoice_status.rb +++ b/app/services/eis_billing/get_reserved_domains_invoice_status.rb @@ -20,10 +20,6 @@ def call http = EisBilling::Base.base_request res = http.get(reserved_domain_invoice_statuses_url, EisBilling::Base.headers) - puts '---' - puts "res: #{res.inspect}" - puts '---' - wrap_result(res) end diff --git a/test/models/reserve_domain_invoice_test.rb b/test/models/reserve_domain_invoice_test.rb index 7037ecacb9..ace3d10e0f 100644 --- a/test/models/reserve_domain_invoice_test.rb +++ b/test/models/reserve_domain_invoice_test.rb @@ -173,6 +173,46 @@ def teardown end end + test "should handle expired reserved domains in domain filtering" do + expired_domain = ReservedDomain.create!( + name: @domain_names.first, + expire_at: 1.day.ago + ) + + result = ReserveDomainInvoice.create_list_of_domains(@domain_names) + + assert result.status_code_success + assert_includes result.reserved_domain_names, @domain_names.second + assert_nil ReservedDomain.find_by(id: expired_domain.id) + end + + test "should not affect non-expired reserved domains in domain filtering" do + active_domain = ReservedDomain.create!( + name: @domain_names.first, + expire_at: 1.day.from_now + ) + + result = ReserveDomainInvoice.create_list_of_domains(@domain_names) + + assert result.status_code_success + refute_includes result.reserved_domain_names, @domain_names.first + assert ReservedDomain.exists?(id: active_domain.id) + end + + test "should not affect permanent reserved domains in domain filtering" do + permanent_domain = ReservedDomain.create!( + name: @domain_names.first, + expire_at: nil + ) + + result = ReserveDomainInvoice.create_list_of_domains(@domain_names) + + assert result.status_code_success + puts result + refute_includes result.reserved_domain_names, @domain_names.first + assert ReservedDomain.exists?(id: permanent_domain.id) + end + private def stub_invoice_number_request diff --git a/test/services/business_registry/domain_availability_checker_service_test.rb b/test/services/business_registry/domain_availability_checker_service_test.rb index 16fad8efd4..2bb208a7c5 100644 --- a/test/services/business_registry/domain_availability_checker_service_test.rb +++ b/test/services/business_registry/domain_availability_checker_service_test.rb @@ -41,4 +41,61 @@ def teardown available = BusinessRegistry::DomainAvailabilityCheckerService.filter_available(domain_names) assert_empty available end + + test "should remove expired reserved domain and make it available" do + expired_domain = ReservedDomain.create!( + name: "expired-domain.test", + expire_at: 1.day.ago + ) + + Epp::Domain.stub :check_availability, [ + {name: "expired-domain.test", avail: 1} + ] do + available = BusinessRegistry::DomainAvailabilityCheckerService.filter_available(["expired-domain.test"]) + assert_equal ["expired-domain.test"], available + assert_nil ReservedDomain.find_by(id: expired_domain.id) + end + end + + test "should keep non-expired reserved domain as unavailable" do + active_domain = ReservedDomain.create!( + name: "active-domain.test", + expire_at: 1.day.from_now + ) + + Epp::Domain.stub :check_availability, [ + {name: "active-domain.test", avail: 1} + ] do + available = BusinessRegistry::DomainAvailabilityCheckerService.filter_available(["active-domain.test"]) + assert_empty available + assert ReservedDomain.exists?(id: active_domain.id) + end + end + + test "should keep reserved domain without expiration as unavailable" do + permanent_domain = ReservedDomain.create!( + name: "permanent-domain.test", + expire_at: nil + ) + + Epp::Domain.stub :check_availability, [ + {name: "permanent-domain.test", avail: 1} + ] do + available = BusinessRegistry::DomainAvailabilityCheckerService.filter_available(["permanent-domain.test"]) + assert_empty available + assert ReservedDomain.exists?(id: permanent_domain.id) + end + end + + test "is_domain_available? should handle expired domains" do + expired_domain = ReservedDomain.create!( + name: "expired-domain.test", + expire_at: 1.day.ago + ) + + Epp::Domain.stub :check_availability, [{name: "expired-domain.test", avail: 1}] do + assert BusinessRegistry::DomainAvailabilityCheckerService.is_domain_available?("expired-domain.test") + assert_nil ReservedDomain.find_by(id: expired_domain.id) + end + end end