Skip to content

Commit

Permalink
Add expiration handling for reserved domains and update tests
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
OlegPhenomenon committed Nov 29, 2024
1 parent feb0ff2 commit 4f21dc7
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 9 deletions.
7 changes: 4 additions & 3 deletions app/models/reserve_domain_invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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']

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
40 changes: 40 additions & 0 deletions test/models/reserve_domain_invoice_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 4f21dc7

Please sign in to comment.