Skip to content

Commit

Permalink
move restriction check from controller to model, added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
olegphenomenon committed Jul 21, 2022
1 parent f81daff commit 0c79158
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 20 deletions.
22 changes: 2 additions & 20 deletions app/controllers/admin/auctions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def index
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
Expand Down Expand Up @@ -69,7 +69,7 @@ def upload_spreadsheet
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
Expand Down Expand Up @@ -107,24 +107,6 @@ def check_availability(domain_name)
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) ||
exception_for_registred_or_unbided_existed_auctions(domain_name)
end

def 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 validate_table(table)
first_row = table.headers
first_row.include? 'name'
Expand Down
18 changes: 18 additions & 0 deletions app/models/auction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@ def self.pending(domain_name)
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!
Expand Down
45 changes: 45 additions & 0 deletions test/models/auction_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,51 @@ def test_starts_an_auction
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
Expand Down

0 comments on commit 0c79158

Please sign in to comment.