Merge pull request #1109 from internetee/fix-auction-integration

Revamp WHOIS for auctions
This commit is contained in:
Timo Võhmar 2019-03-25 16:25:37 +02:00 committed by GitHub
commit 0393e7ad31
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 141 additions and 127 deletions

View file

@ -30,6 +30,12 @@ module Api
raise "Invalid status #{params[:status]}" raise "Invalid status #{params[:status]}"
end end
if auction.payment_not_received? || auction.domain_not_registered?
update_whois_from_auction(Auction.pending(auction.domain))
else
update_whois_from_auction(auction)
end
render json: serializable_hash_for_update_action(auction) render json: serializable_hash_for_update_action(auction)
end end
@ -44,6 +50,11 @@ module Api
hash[:registration_code] = auction.registration_code if auction.payment_received? hash[:registration_code] = auction.registration_code if auction.payment_received?
hash hash
end end
def update_whois_from_auction(auction)
whois_record = Whois::Record.find_by!(name: auction.domain)
whois_record.update_from_auction(auction)
end
end end
end end
end end

View file

@ -14,19 +14,17 @@ class Auction < ActiveRecord::Base
statuses[:payment_received]].freeze statuses[:payment_received]].freeze
private_constant :PENDING_STATUSES private_constant :PENDING_STATUSES
def self.sell(domain_name)
create!(domain: domain_name.to_s, status: statuses[:started])
end
def self.pending(domain_name) def self.pending(domain_name)
find_by(domain: domain_name.to_s, status: PENDING_STATUSES) find_by(domain: domain_name.to_s, status: PENDING_STATUSES)
end end
def mark_as_no_bids def start
transaction do self.status = self.class.statuses[:started]
DNS::DomainName.new(domain).update_whois save!
no_bids!
end end
def mark_as_no_bids
no_bids!
end end
def mark_as_payment_received def mark_as_payment_received
@ -57,16 +55,17 @@ class Auction < ActiveRecord::Base
payment_received? && registration_code_matches?(registration_code) payment_received? && registration_code_matches?(registration_code)
end end
def restart
new_auction = self.class.new(domain: domain)
new_auction.start
end
private private
def generate_registration_code def generate_registration_code
self.registration_code = SecureRandom.hex self.registration_code = SecureRandom.hex
end end
def restart
self.class.create!(domain: domain, status: self.class.statuses[:started])
end
def registration_code_matches?(code) def registration_code_matches?(code)
registration_code == code registration_code == code
end end

View file

@ -33,8 +33,10 @@ module DNS
end end
def sell_at_auction def sell_at_auction
Auction.sell(self) auction = Auction.new
update_whois auction.domain = name
auction.start
update_whois_from_auction(auction)
end end
def at_auction? def at_auction?
@ -49,10 +51,6 @@ module DNS
pending_auction&.payment_received? pending_auction&.payment_received?
end end
def update_whois
Whois::Record.refresh(self)
end
def registered? def registered?
Domain.find_by_idn(name) Domain.find_by_idn(name)
end end
@ -92,5 +90,10 @@ module DNS
def pending_auction def pending_auction
Auction.pending(self) Auction.pending(self)
end end
def update_whois_from_auction(auction)
whois_record = Whois::Record.find_by!(name: name)
whois_record.update_from_auction(auction)
end
end end
end end

View file

@ -6,21 +6,17 @@ module Whois
Setting.registry_whois_disclaimer Setting.registry_whois_disclaimer
end end
def self.refresh(domain_name) def update_from_auction(auction)
if domain_name.at_auction? if auction.started?
# Remove original record since `Domain#update_whois_record` callback is disabled when update!(json: { name: auction.domain,
# domain is at auction
find_by(name: domain_name.to_s).try(:destroy!)
create!(name: domain_name, json: { name: domain_name.to_s,
status: ['AtAuction'], status: ['AtAuction'],
disclaimer: disclaimer }) disclaimer: self.class.disclaimer })
elsif domain_name.awaiting_payment? || domain_name.pending_registration? elsif auction.no_bids?
find_by(name: domain_name.to_s).update!(json: { name: domain_name.to_s, destroy!
elsif auction.awaiting_payment? || auction.payment_received?
update!(json: { name: auction.domain,
status: ['PendingRegistration'], status: ['PendingRegistration'],
disclaimer: disclaimer }) disclaimer: self.class.disclaimer })
else
find_by(name: domain_name.to_s).destroy!
end end
end end
end end

View file

@ -6,6 +6,21 @@ namespace :whois do
print "-----> Regenerate Registry whois_records table and sync with whois server..." print "-----> Regenerate Registry whois_records table and sync with whois server..."
ActiveRecord::Base.uncached do ActiveRecord::Base.uncached do
# Must be on top
print "\n-----> Update whois_records for auctions"
Auction.pluck('DISTINCT domain').each do |domain|
pending_auction = Auction.pending(domain)
if pending_auction
Whois::Record.transaction do
whois_record = Whois::Record.find_or_create_by!(name: domain)
whois_record.update_from_auction(pending_auction)
end
else
Whois::Record.find_by(name: domain)&.destroy!
end
end
print "\n-----> Update domains whois_records" print "\n-----> Update domains whois_records"
Domain.find_in_batches.each do |group| Domain.find_in_batches.each do |group|
UpdateWhoisRecordJob.enqueue group.map(&:name), 'domain' UpdateWhoisRecordJob.enqueue group.map(&:name), 'domain'
@ -20,7 +35,6 @@ namespace :whois do
ReservedDomain.find_in_batches.each do |group| ReservedDomain.find_in_batches.each do |group|
UpdateWhoisRecordJob.enqueue group.map(&:name), 'reserved' UpdateWhoisRecordJob.enqueue group.map(&:name), 'reserved'
end end
end end
puts "\n-----> all done in #{(Time.zone.now.to_f - start).round(2)} seconds" puts "\n-----> all done in #{(Time.zone.now.to_f - start).round(2)} seconds"
end end

View file

@ -5,6 +5,8 @@ class ApiV1AuctionUpdateTest < ActionDispatch::IntegrationTest
setup do setup do
@auction = auctions(:one) @auction = auctions(:one)
@whois_record = whois_records(:one)
@whois_record.update!(name: 'auction.test')
@original_auction_api_allowed_ips_setting = ENV['auction_api_allowed_ips'] @original_auction_api_allowed_ips_setting = ENV['auction_api_allowed_ips']
ENV['auction_api_allowed_ips'] = '127.0.0.1' ENV['auction_api_allowed_ips'] = '127.0.0.1'
@ -36,9 +38,6 @@ class ApiV1AuctionUpdateTest < ActionDispatch::IntegrationTest
end end
def test_marks_as_no_bids def test_marks_as_no_bids
assert_equal 'auction.test', @auction.domain
whois_records(:one).update!(name: 'auction.test')
patch api_v1_auction_path(@auction.uuid), { status: Auction.statuses[:no_bids] } patch api_v1_auction_path(@auction.uuid), { status: Auction.statuses[:no_bids] }
.to_json, 'Content-Type' => Mime::JSON.to_s .to_json, 'Content-Type' => Mime::JSON.to_s
@auction.reload @auction.reload
@ -59,6 +58,13 @@ class ApiV1AuctionUpdateTest < ActionDispatch::IntegrationTest
assert @auction.payment_not_received? assert @auction.payment_not_received?
end end
def test_marks_as_domain_not_registered
patch api_v1_auction_path(@auction.uuid), { status: Auction.statuses[:domain_not_registered] }
.to_json, 'Content-Type' => Mime::JSON.to_s
@auction.reload
assert @auction.domain_not_registered?
end
def test_reveals_registration_code_when_payment_is_received def test_reveals_registration_code_when_payment_is_received
@auction.update!(registration_code: 'auction-001', @auction.update!(registration_code: 'auction-001',
status: Auction.statuses[:awaiting_payment]) status: Auction.statuses[:awaiting_payment])
@ -80,22 +86,16 @@ class ApiV1AuctionUpdateTest < ActionDispatch::IntegrationTest
assert_nil response_json['registration_code'] assert_nil response_json['registration_code']
end end
def test_restarts_an_auction_when_the_payment_is_not_received def test_updates_whois
@auction.update!(domain: 'auction.test', status: Auction.statuses[:awaiting_payment]) travel_to Time.zone.parse('2010-07-05 10:00')
assert_equal 'auction.test', @auction.domain
@whois_record.update!(updated_at: '2010-07-04')
patch api_v1_auction_path(@auction.uuid), { status: Auction.statuses[:payment_not_received] } patch api_v1_auction_path(@auction.uuid), { status: Auction.statuses[:payment_received] }
.to_json, 'Content-Type' => Mime::JSON.to_s .to_json, 'Content-Type' => Mime::JSON.to_s
@whois_record.reload
assert DNS::DomainName.new('auction.test').at_auction? assert_equal Time.zone.parse('2010-07-05 10:00'), @whois_record.updated_at
end
def test_restarts_an_auction_when_domain_is_not_registered
@auction.update!(domain: 'auction.test', status: Auction.statuses[:payment_received])
patch api_v1_auction_path(@auction.uuid), { status: Auction.statuses[:domain_not_registered] }
.to_json, 'Content-Type' => Mime::JSON.to_s
assert DNS::DomainName.new('auction.test').at_auction?
end end
def test_inaccessible_when_ip_address_is_not_allowed def test_inaccessible_when_ip_address_is_not_allowed

View file

@ -19,15 +19,13 @@ class AuctionTest < ActiveSupport::TestCase
'domain_not_registered' => 'domain_not_registered' }), Auction.statuses 'domain_not_registered' => 'domain_not_registered' }), Auction.statuses
end end
def test_selling_domain_starts_new_auction def test_starts_an_auction
domain_name = DNS::DomainName.new('shop.test') assert_not @auction.started?
assert_difference 'Auction.count' do @auction.start
Auction.sell(domain_name) @auction.reload
end
auction = Auction.last assert @auction.started?
assert_equal domain_name.to_s, auction.domain
assert auction.started?
end end
def test_pending def test_pending
@ -84,16 +82,12 @@ class AuctionTest < ActiveSupport::TestCase
assert_nil @auction.registration_code assert_nil @auction.registration_code
end end
def test_restarts_an_auction_when_payment_is_not_received def test_marking_as_payment_not_received_restarts_an_auction
@auction.update!(domain: 'auction.test', status: Auction.statuses[:awaiting_payment]) @auction.update!(status: Auction.statuses[:awaiting_payment])
assert_difference 'Auction.count' do assert_difference 'Auction.count' do
@auction.mark_as_payment_not_received @auction.mark_as_payment_not_received
end end
new_auction = Auction.last
assert_equal 'auction.test', new_auction.domain
assert new_auction.started?
end end
def test_marking_as_domain_not_registered def test_marking_as_domain_not_registered
@ -105,16 +99,12 @@ class AuctionTest < ActiveSupport::TestCase
assert @auction.domain_not_registered? assert @auction.domain_not_registered?
end end
def test_restarts_an_auction_when_domain_is_not_registered def test_marking_as_domain_not_registered_restarts_an_auction
@auction.update!(domain: 'auction.test', status: Auction.statuses[:domain_not_registered]) @auction.update!(status: Auction.statuses[:payment_received])
assert_difference 'Auction.count' do assert_difference 'Auction.count' do
@auction.mark_as_domain_not_registered @auction.mark_as_domain_not_registered
end end
new_auction = Auction.last
assert_equal 'auction.test', new_auction.domain
assert new_auction.started?
end end
def test_domain_registrable def test_domain_registrable
@ -140,4 +130,16 @@ class AuctionTest < ActiveSupport::TestCase
assert_not @auction.domain_registrable?(nil) assert_not @auction.domain_registrable?(nil)
assert_not @auction.domain_registrable?('') assert_not @auction.domain_registrable?('')
end end
def test_restarts_an_auction
assert_equal 'auction.test', @auction.domain
assert_difference 'Auction.count' do
@auction.restart
end
new_auction = Auction.last
assert_equal 'auction.test', new_auction.domain
assert new_auction.started?
end
end end

View file

@ -71,8 +71,8 @@ class DNS::DomainNameTest < ActiveSupport::TestCase
assert_equal :awaiting_payment, domain_name.unavailability_reason assert_equal :awaiting_payment, domain_name.unavailability_reason
end end
def test_sell_at_auction def test_sells_at_auction
domain_name = DNS::DomainName.new('new-auction.test') domain_name = DNS::DomainName.new('shop.test')
assert_not domain_name.at_auction? assert_not domain_name.at_auction?
domain_name.sell_at_auction domain_name.sell_at_auction
@ -81,12 +81,15 @@ class DNS::DomainNameTest < ActiveSupport::TestCase
end end
def test_selling_at_auction_updates_whois def test_selling_at_auction_updates_whois
travel_to Time.zone.parse('2010-07-05 10:00')
@whois_record = whois_records(:one)
@whois_record.update!(name: 'new-auction.test', updated_at: '2010-07-04')
domain_name = DNS::DomainName.new('new-auction.test') domain_name = DNS::DomainName.new('new-auction.test')
assert_not domain_name.at_auction?
domain_name.sell_at_auction domain_name.sell_at_auction
@whois_record.reload
assert Whois::Record.find_by(name: 'new-auction.test') assert_equal Time.zone.parse('2010-07-05 10:00'), @whois_record.updated_at
end end
def test_at_auction def test_at_auction

View file

@ -49,16 +49,6 @@ class DomainReleasableAuctionableTest < ActiveSupport::TestCase
end end
end end
def test_updates_whois
assert_equal 'shop.test', @domain.name
@domain.update!(delete_at: Time.zone.parse('2010-07-05 07:59'))
travel_to Time.zone.parse('2010-07-05 08:00')
Domain.release_domains
assert Whois::Record.find_by(name: 'shop.test')
end
def test_ignores_domains_with_delete_at_in_the_future_or_now def test_ignores_domains_with_delete_at_in_the_future_or_now
@domain.update!(delete_at: Time.zone.parse('2010-07-05 08:00')) @domain.update!(delete_at: Time.zone.parse('2010-07-05 08:00'))
travel_to Time.zone.parse('2010-07-05 08:00') travel_to Time.zone.parse('2010-07-05 08:00')

View file

@ -4,64 +4,60 @@ class Whois::RecordTest < ActiveSupport::TestCase
fixtures 'whois/records' fixtures 'whois/records'
setup do setup do
@whois_record = whois_records(:one)
@auction = auctions(:one)
@original_disclaimer_setting = Setting.registry_whois_disclaimer @original_disclaimer_setting = Setting.registry_whois_disclaimer
Setting.registry_whois_disclaimer = 'disclaimer'
end end
teardown do teardown do
Setting.registry_whois_disclaimer = @original_disclaimer_setting Setting.registry_whois_disclaimer = @original_disclaimer_setting
end end
def test_reads_disclaimer_from_settings def test_reads_disclaimer_setting
Setting.registry_whois_disclaimer = 'test disclaimer' Setting.registry_whois_disclaimer = 'test disclaimer'
assert_equal 'test disclaimer', Whois::Record.disclaimer assert_equal 'test disclaimer', Whois::Record.disclaimer
end end
def test_creates_new_whois_record_when_domain_is_at_auction def test_updates_whois_record_from_auction_when_started
domain_name = DNS::DomainName.new('some.test') @auction.update!(domain: 'domain.test', status: Auction.statuses[:started])
Setting.registry_whois_disclaimer = 'disclaimer' @whois_record.update!(name: 'domain.test')
@whois_record.update_from_auction(@auction)
@whois_record.reload
domain_name.stub(:at_auction?, true) do assert_equal ({ 'name' => 'domain.test',
assert_difference 'Whois::Record.count' do
Whois::Record.refresh(domain_name)
end
end
whois_record = Whois::Record.last
assert_equal 'some.test', whois_record.name
assert_equal ({ 'name' => 'some.test',
'status' => ['AtAuction'], 'status' => ['AtAuction'],
'disclaimer' => 'disclaimer' }), whois_record.json 'disclaimer' => 'disclaimer' }), @whois_record.json
end end
def test_refreshes_whois_record_when_domain_auction_reaches_awaiting_payment_state def test_updates_whois_record_from_auction_when_no_bids
domain_name = DNS::DomainName.new('some.test') @auction.update!(domain: 'domain.test', status: Auction.statuses[:no_bids])
Setting.registry_whois_disclaimer = 'disclaimer' @whois_record.update!(name: 'domain.test')
whois_records(:one).update!(name: 'some.test') @whois_record.update_from_auction(@auction)
domain_name.stub(:awaiting_payment?, true) do assert_not Whois::Record.exists?(name: 'domain.test')
Whois::Record.refresh(domain_name)
end end
whois_record = Whois::Record.find_by(name: 'some.test') def test_updates_whois_record_from_auction_when_awaiting_payment
assert_equal 'some.test', whois_record.name @auction.update!(domain: 'domain.test', status: Auction.statuses[:awaiting_payment])
assert_equal ({ 'name' => 'some.test', @whois_record.update!(name: 'domain.test')
@whois_record.update_from_auction(@auction)
@whois_record.reload
assert_equal ({ 'name' => 'domain.test',
'status' => ['PendingRegistration'], 'status' => ['PendingRegistration'],
'disclaimer' => 'disclaimer' }), whois_record.json 'disclaimer' => 'disclaimer' }), @whois_record.json
end end
def test_refreshes_whois_record_when_domain_auction_reaches_pending_registration_state def test_updates_whois_record_from_auction_when_payment_received
domain_name = DNS::DomainName.new('some.test') @auction.update!(domain: 'domain.test', status: Auction.statuses[:payment_received])
Setting.registry_whois_disclaimer = 'disclaimer' @whois_record.update!(name: 'domain.test')
whois_records(:one).update!(name: 'some.test') @whois_record.update_from_auction(@auction)
@whois_record.reload
domain_name.stub(:pending_registration?, true) do assert_equal ({ 'name' => 'domain.test',
Whois::Record.refresh(domain_name)
end
whois_record = Whois::Record.find_by(name: 'some.test')
assert_equal 'some.test', whois_record.name
assert_equal ({ 'name' => 'some.test',
'status' => ['PendingRegistration'], 'status' => ['PendingRegistration'],
'disclaimer' => 'disclaimer' }), whois_record.json 'disclaimer' => 'disclaimer' }), @whois_record.json
end end
end end