diff --git a/app/controllers/api/v1/auctions_controller.rb b/app/controllers/api/v1/auctions_controller.rb index 7438acf2e8..5d896e1af5 100644 --- a/app/controllers/api/v1/auctions_controller.rb +++ b/app/controllers/api/v1/auctions_controller.rb @@ -30,6 +30,12 @@ def update raise "Invalid status #{params[:status]}" 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) end @@ -44,6 +50,11 @@ def serializable_hash_for_update_action(auction) hash[:registration_code] = auction.registration_code if auction.payment_received? hash 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 diff --git a/app/models/auction.rb b/app/models/auction.rb index 750f326b9a..80df11eef5 100644 --- a/app/models/auction.rb +++ b/app/models/auction.rb @@ -14,19 +14,17 @@ class Auction < ActiveRecord::Base statuses[:payment_received]].freeze private_constant :PENDING_STATUSES - def self.sell(domain_name) - create!(domain: domain_name.to_s, status: statuses[:started]) - end - def self.pending(domain_name) find_by(domain: domain_name.to_s, status: PENDING_STATUSES) end + def start + self.status = self.class.statuses[:started] + save! + end + def mark_as_no_bids - transaction do - DNS::DomainName.new(domain).update_whois - no_bids! - end + no_bids! end def mark_as_payment_received @@ -57,17 +55,18 @@ def domain_registrable?(registration_code = nil) payment_received? && registration_code_matches?(registration_code) end + def restart + new_auction = self.class.new(domain: domain) + new_auction.start + end + private def generate_registration_code self.registration_code = SecureRandom.hex end - def restart - self.class.create!(domain: domain, status: self.class.statuses[:started]) - end - def registration_code_matches?(code) registration_code == code end -end +end \ No newline at end of file diff --git a/app/models/dns/domain_name.rb b/app/models/dns/domain_name.rb index a5b56df1b4..0381d6241f 100644 --- a/app/models/dns/domain_name.rb +++ b/app/models/dns/domain_name.rb @@ -33,8 +33,10 @@ def unavailability_reason end def sell_at_auction - Auction.sell(self) - update_whois + auction = Auction.new + auction.domain = name + auction.start + update_whois_from_auction(auction) end def at_auction? @@ -49,10 +51,6 @@ def pending_registration? pending_auction&.payment_received? end - def update_whois - Whois::Record.refresh(self) - end - def registered? Domain.find_by_idn(name) end @@ -92,5 +90,10 @@ def zone_with_same_origin? def pending_auction Auction.pending(self) end + + def update_whois_from_auction(auction) + whois_record = Whois::Record.find_by!(name: name) + whois_record.update_from_auction(auction) + end end end diff --git a/app/models/whois/record.rb b/app/models/whois/record.rb index 4c1b28aeca..d94c723ad5 100644 --- a/app/models/whois/record.rb +++ b/app/models/whois/record.rb @@ -6,22 +6,18 @@ def self.disclaimer Setting.registry_whois_disclaimer end - def self.refresh(domain_name) - if domain_name.at_auction? - # Remove original record since `Domain#update_whois_record` callback is disabled when - # 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'], - disclaimer: disclaimer }) - elsif domain_name.awaiting_payment? || domain_name.pending_registration? - find_by(name: domain_name.to_s).update!(json: { name: domain_name.to_s, - status: ['PendingRegistration'], - disclaimer: disclaimer }) - else - find_by(name: domain_name.to_s).destroy! + def update_from_auction(auction) + if auction.started? + update!(json: { name: auction.domain, + status: ['AtAuction'], + disclaimer: self.class.disclaimer }) + elsif auction.no_bids? + destroy! + elsif auction.awaiting_payment? || auction.payment_received? + update!(json: { name: auction.domain, + status: ['PendingRegistration'], + disclaimer: self.class.disclaimer }) end end end -end +end \ No newline at end of file diff --git a/lib/tasks/whois.rake b/lib/tasks/whois.rake index af1e33941e..775d900027 100644 --- a/lib/tasks/whois.rake +++ b/lib/tasks/whois.rake @@ -6,6 +6,21 @@ namespace :whois do print "-----> Regenerate Registry whois_records table and sync with whois server..." 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" Domain.find_in_batches.each do |group| UpdateWhoisRecordJob.enqueue group.map(&:name), 'domain' @@ -20,7 +35,6 @@ namespace :whois do ReservedDomain.find_in_batches.each do |group| UpdateWhoisRecordJob.enqueue group.map(&:name), 'reserved' end - end puts "\n-----> all done in #{(Time.zone.now.to_f - start).round(2)} seconds" end @@ -32,7 +46,7 @@ namespace :whois do puts "\n------------------------ Create #{whois_db} ---------------------------------------\n" ActiveRecord::Base.clear_all_connections! conf = ActiveRecord::Base.configurations - + ActiveRecord::Base.connection.create_database(conf[whois_db]['database'].to_sym, conf[whois_db]) rescue => e puts "\n#{e}" diff --git a/test/integration/api/v1/auctions/update_test.rb b/test/integration/api/v1/auctions/update_test.rb index 09dbf2fe70..262b11cdfc 100644 --- a/test/integration/api/v1/auctions/update_test.rb +++ b/test/integration/api/v1/auctions/update_test.rb @@ -5,6 +5,8 @@ class ApiV1AuctionUpdateTest < ActionDispatch::IntegrationTest setup do @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'] ENV['auction_api_allowed_ips'] = '127.0.0.1' @@ -36,9 +38,6 @@ def test_marks_as_awaiting_payment end 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] } .to_json, 'Content-Type' => Mime::JSON.to_s @auction.reload @@ -59,6 +58,13 @@ def test_marks_as_payment_not_received assert @auction.payment_not_received? 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 @auction.update!(registration_code: 'auction-001', status: Auction.statuses[:awaiting_payment]) @@ -80,22 +86,16 @@ def test_conceals_registration_code_when_payment_is_not_received assert_nil response_json['registration_code'] end - def test_restarts_an_auction_when_the_payment_is_not_received - @auction.update!(domain: 'auction.test', status: Auction.statuses[:awaiting_payment]) - - patch api_v1_auction_path(@auction.uuid), { status: Auction.statuses[:payment_not_received] } - .to_json, 'Content-Type' => Mime::JSON.to_s - - assert DNS::DomainName.new('auction.test').at_auction? - end - - def test_restarts_an_auction_when_domain_is_not_registered - @auction.update!(domain: 'auction.test', status: Auction.statuses[:payment_received]) + def test_updates_whois + 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[:domain_not_registered] } + patch api_v1_auction_path(@auction.uuid), { status: Auction.statuses[:payment_received] } .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_inaccessible_when_ip_address_is_not_allowed diff --git a/test/models/auction_test.rb b/test/models/auction_test.rb index 9417c4a8ca..bdac5c79d1 100644 --- a/test/models/auction_test.rb +++ b/test/models/auction_test.rb @@ -19,15 +19,13 @@ def test_statuses 'domain_not_registered' => 'domain_not_registered' }), Auction.statuses end - def test_selling_domain_starts_new_auction - domain_name = DNS::DomainName.new('shop.test') + def test_starts_an_auction + assert_not @auction.started? - assert_difference 'Auction.count' do - Auction.sell(domain_name) - end - auction = Auction.last - assert_equal domain_name.to_s, auction.domain - assert auction.started? + @auction.start + @auction.reload + + assert @auction.started? end def test_pending @@ -84,16 +82,12 @@ def test_marking_as_payment_not_received assert_nil @auction.registration_code end - def test_restarts_an_auction_when_payment_is_not_received - @auction.update!(domain: 'auction.test', status: Auction.statuses[:awaiting_payment]) + def test_marking_as_payment_not_received_restarts_an_auction + @auction.update!(status: Auction.statuses[:awaiting_payment]) assert_difference 'Auction.count' do @auction.mark_as_payment_not_received end - - new_auction = Auction.last - assert_equal 'auction.test', new_auction.domain - assert new_auction.started? end def test_marking_as_domain_not_registered @@ -105,16 +99,12 @@ def test_marking_as_domain_not_registered assert @auction.domain_not_registered? end - def test_restarts_an_auction_when_domain_is_not_registered - @auction.update!(domain: 'auction.test', status: Auction.statuses[:domain_not_registered]) + def test_marking_as_domain_not_registered_restarts_an_auction + @auction.update!(status: Auction.statuses[:payment_received]) assert_difference 'Auction.count' do @auction.mark_as_domain_not_registered end - - new_auction = Auction.last - assert_equal 'auction.test', new_auction.domain - assert new_auction.started? end def test_domain_registrable @@ -140,4 +130,16 @@ def test_domain_unregistrable assert_not @auction.domain_registrable?(nil) 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 \ No newline at end of file diff --git a/test/models/dns/domain_name_test.rb b/test/models/dns/domain_name_test.rb index 5385c1c0b5..e8b33e316e 100644 --- a/test/models/dns/domain_name_test.rb +++ b/test/models/dns/domain_name_test.rb @@ -71,8 +71,8 @@ def test_unavailable_when_awaiting_payment assert_equal :awaiting_payment, domain_name.unavailability_reason end - def test_sell_at_auction - domain_name = DNS::DomainName.new('new-auction.test') + def test_sells_at_auction + domain_name = DNS::DomainName.new('shop.test') assert_not domain_name.at_auction? domain_name.sell_at_auction @@ -81,12 +81,15 @@ def test_sell_at_auction end 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') - assert_not domain_name.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 def test_at_auction @@ -159,4 +162,4 @@ def test_not_auctionable_when_reserved assert_equal 'reserved.test', reserved_domains(:one).name assert_not DNS::DomainName.new('reserved.test').auctionable? end -end +end \ No newline at end of file diff --git a/test/models/domain/releasable/auctionable_test.rb b/test/models/domain/releasable/auctionable_test.rb index 751e0be88a..84f298b0a9 100644 --- a/test/models/domain/releasable/auctionable_test.rb +++ b/test/models/domain/releasable/auctionable_test.rb @@ -49,16 +49,6 @@ def test_deletes_registered_domain 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 @domain.update!(delete_at: Time.zone.parse('2010-07-05 08:00')) travel_to Time.zone.parse('2010-07-05 08:00') diff --git a/test/models/whois/record_test.rb b/test/models/whois/record_test.rb index f158682952..43707b1869 100644 --- a/test/models/whois/record_test.rb +++ b/test/models/whois/record_test.rb @@ -4,64 +4,60 @@ class Whois::RecordTest < ActiveSupport::TestCase fixtures 'whois/records' setup do + @whois_record = whois_records(:one) + @auction = auctions(:one) + @original_disclaimer_setting = Setting.registry_whois_disclaimer + Setting.registry_whois_disclaimer = 'disclaimer' end teardown do Setting.registry_whois_disclaimer = @original_disclaimer_setting end - def test_reads_disclaimer_from_settings + def test_reads_disclaimer_setting Setting.registry_whois_disclaimer = 'test disclaimer' assert_equal 'test disclaimer', Whois::Record.disclaimer end - def test_creates_new_whois_record_when_domain_is_at_auction - domain_name = DNS::DomainName.new('some.test') - Setting.registry_whois_disclaimer = 'disclaimer' + def test_updates_whois_record_from_auction_when_started + @auction.update!(domain: 'domain.test', status: Auction.statuses[:started]) + @whois_record.update!(name: 'domain.test') + @whois_record.update_from_auction(@auction) + @whois_record.reload - domain_name.stub(:at_auction?, true) do - 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', + assert_equal ({ 'name' => 'domain.test', 'status' => ['AtAuction'], - 'disclaimer' => 'disclaimer' }), whois_record.json + 'disclaimer' => 'disclaimer' }), @whois_record.json end - def test_refreshes_whois_record_when_domain_auction_reaches_awaiting_payment_state - domain_name = DNS::DomainName.new('some.test') - Setting.registry_whois_disclaimer = 'disclaimer' - whois_records(:one).update!(name: 'some.test') + def test_updates_whois_record_from_auction_when_no_bids + @auction.update!(domain: 'domain.test', status: Auction.statuses[:no_bids]) + @whois_record.update!(name: 'domain.test') + @whois_record.update_from_auction(@auction) - domain_name.stub(:awaiting_payment?, true) do - Whois::Record.refresh(domain_name) - end + assert_not Whois::Record.exists?(name: 'domain.test') + end + + def test_updates_whois_record_from_auction_when_awaiting_payment + @auction.update!(domain: 'domain.test', status: Auction.statuses[:awaiting_payment]) + @whois_record.update!(name: 'domain.test') + @whois_record.update_from_auction(@auction) + @whois_record.reload - whois_record = Whois::Record.find_by(name: 'some.test') - assert_equal 'some.test', whois_record.name - assert_equal ({ 'name' => 'some.test', + assert_equal ({ 'name' => 'domain.test', 'status' => ['PendingRegistration'], - 'disclaimer' => 'disclaimer' }), whois_record.json + 'disclaimer' => 'disclaimer' }), @whois_record.json end - def test_refreshes_whois_record_when_domain_auction_reaches_pending_registration_state - domain_name = DNS::DomainName.new('some.test') - Setting.registry_whois_disclaimer = 'disclaimer' - whois_records(:one).update!(name: 'some.test') - - domain_name.stub(:pending_registration?, true) do - Whois::Record.refresh(domain_name) - end + def test_updates_whois_record_from_auction_when_payment_received + @auction.update!(domain: 'domain.test', status: Auction.statuses[:payment_received]) + @whois_record.update!(name: 'domain.test') + @whois_record.update_from_auction(@auction) + @whois_record.reload - whois_record = Whois::Record.find_by(name: 'some.test') - assert_equal 'some.test', whois_record.name - assert_equal ({ 'name' => 'some.test', + assert_equal ({ 'name' => 'domain.test', 'status' => ['PendingRegistration'], - 'disclaimer' => 'disclaimer' }), whois_record.json + 'disclaimer' => 'disclaimer' }), @whois_record.json end -end +end \ No newline at end of file