diff --git a/app/controllers/admin/auctions_controller.rb b/app/controllers/admin/auctions_controller.rb index 05bfc239f..645b9e0ee 100644 --- a/app/controllers/admin/auctions_controller.rb +++ b/app/controllers/admin/auctions_controller.rb @@ -31,7 +31,7 @@ module Admin def create auction = Auction.new(domain: params[:domain], status: Auction.statuses[:started], platform: 'manual') - if domain_exists_in_blocked_disputed_and_registered?(params[:domain]) + if Auction.domain_exists_in_blocked_disputed_and_registered?(params[:domain]) flash[:alert] = "Adding #{params[:domain]} failed - domain registered or regsitration is blocked" redirect_to admin_auctions_path and return end @@ -81,7 +81,7 @@ module Admin table.each do |row| record = row.to_h - if domain_exists_in_blocked_disputed_and_registered?(record['name']) + if Auction.domain_exists_in_blocked_disputed_and_registered?(record['name']) failed_names << record['name'] next @@ -119,13 +119,6 @@ module Admin Epp::Domain.check_availability(domain_name) end - def domain_exists_in_blocked_disputed_and_registered?(domain_name) - Domain.exists?(name: domain_name) || - BlockedDomain.exists?(name: domain_name) || - Dispute.exists?(domain_name: domain_name) || - Auction.exists?(domain: domain_name) - end - def validate_table(table) first_row = table.headers first_row.include? 'name' diff --git a/app/models/auction.rb b/app/models/auction.rb index d1c46d44e..f0aa1fe6d 100644 --- a/app/models/auction.rb +++ b/app/models/auction.rb @@ -37,6 +37,24 @@ class Auction < ApplicationRecord find_by(domain: domain_name.to_s, status: PENDING_STATUSES) end + def self.domain_exists_in_blocked_disputed_and_registered?(domain_name) + Domain.exists?(name: domain_name) || + BlockedDomain.exists?(name: domain_name) || + Dispute.exists?(domain_name: domain_name) || + exception_for_registred_or_unbided_existed_auctions(domain_name) + end + + def self.exception_for_registred_or_unbided_existed_auctions(domain_name) + return false unless Auction.exists?(domain: domain_name) + + auctions = Auction.where(domain: domain_name).order(:created_at) + last_record = auctions.last + + return false if last_record.domain_registered? || last_record.no_bids? + + true + end + def start self.status = self.class.statuses[:started] save! diff --git a/test/models/auction_test.rb b/test/models/auction_test.rb index 5df7166b2..3f2a6932e 100644 --- a/test/models/auction_test.rb +++ b/test/models/auction_test.rb @@ -28,6 +28,51 @@ class AuctionTest < ActiveSupport::TestCase assert @auction.started? end + def test_auction_with_no_bids_dont_have_any_restriction + @auction.update(status: :no_bids) + @auction.reload + + res = Auction.domain_exists_in_blocked_disputed_and_registered?(@auction.domain) + refute res + end + + def test_auction_with_domain_registered_dont_have_any_restriction + @auction.update(status: :domain_registered) + @auction.reload + + res = Auction.domain_exists_in_blocked_disputed_and_registered?(@auction.domain) + refute res + end + + def test_auction_with_started_has_restriction + @auction.update(status: :started) + @auction.reload + + res = Auction.domain_exists_in_blocked_disputed_and_registered?(@auction.domain) + assert res + end + + def test_blocked_domain_has_restriction + blocked_domain = blocked_domains(:one) + + res = Auction.domain_exists_in_blocked_disputed_and_registered?(blocked_domain.name) + assert res + end + + def test_dispute_domain_has_restriction + dispute_domain = disputes(:active) + + res = Auction.domain_exists_in_blocked_disputed_and_registered?(dispute_domain.domain_name) + assert res + end + + def test_exist_domain_has_restriction + domain = domains(:shop) + + res = Auction.domain_exists_in_blocked_disputed_and_registered?(domain.name) + assert res + end + def test_pending domain_name = DNS::DomainName.new('auction.test') assert_equal 'auction.test', @auction.domain