Move domain dispute logic to concern

This commit is contained in:
Karl Erik Õunapuu 2020-05-11 16:06:35 +03:00
parent 5e152b3b9d
commit e7ad4a7c64
6 changed files with 70 additions and 29 deletions

View file

@ -0,0 +1,44 @@
# frozen_string_literal: true
module Concerns
module Domain
module Disputable
extend ActiveSupport::Concern
included do
validate :validate_disputed
end
def mark_as_disputed
statuses.push(DomainStatus::DISPUTED) unless statuses.include?(DomainStatus::DISPUTED)
save
end
def unmark_as_disputed
statuses.delete_if { |status| status == DomainStatus::DISPUTED }
save
end
def in_disputed_list?
@in_disputed_list ||= Dispute.active.find_by(domain_name: name).present?
end
def disputed?
Dispute.active.where(domain_name: name).any?
end
def validate_disputed
return if persisted? || !in_disputed_list?
if reserved_pw.blank?
errors.add(:base, :required_parameter_missing_reserved)
return false
end
return if Dispute.valid_auth?(name, reserved_pw)
errors.add(:base, :invalid_auth_information_reserved)
end
end
end
end

View file

@ -12,16 +12,24 @@ class Dispute < ApplicationRecord
after_destroy :remove_data after_destroy :remove_data
scope :expired, -> { where('expires_at < ?', Time.zone.today) } scope :expired, -> { where('expires_at < ?', Time.zone.today) }
scope :active, -> { where('expires_at >= ? AND closed = false', Time.zone.today) } scope :active, lambda {
where('starts_at <= ? AND expires_at >= ? AND closed = false', Time.zone.today, Time.zone.today)
}
scope :closed, -> { where(closed: true) } scope :closed, -> { where(closed: true) }
attr_readonly :domain_name attr_readonly :domain_name
alias_attribute :name, :domain_name alias_attribute :name, :domain_name
def domain
Domain.find_by(name: domain_name)
end
def self.close_by_domain(domain_name) def self.close_by_domain(domain_name)
dispute = Dispute.active.find_by(domain_name: domain_name) dispute = Dispute.active.find_by(domain_name: domain_name)
dispute.update(closed: true) if dispute.present? return false unless dispute
dispute.close
end end
def self.valid_auth?(domain_name, password) def self.valid_auth?(domain_name, password)
@ -40,6 +48,12 @@ class Dispute < ApplicationRecord
def generate_data def generate_data
return if starts_at > Time.zone.today return if starts_at > Time.zone.today
return if expires_at < Time.zone.today
domain = Domain.find_by_idn(domain_name)
domain&.mark_as_disputed
return if domain
wr = Whois::Record.find_or_initialize_by(name: domain_name) wr = Whois::Record.find_or_initialize_by(name: domain_name)
wr.json = generate_json(wr) wr.json = generate_json(wr)
@ -52,6 +66,8 @@ class Dispute < ApplicationRecord
return false unless update(closed: true) return false unless update(closed: true)
return if Dispute.active.where(domain_name: domain_name).any? return if Dispute.active.where(domain_name: domain_name).any?
Domain.find_by_idn(domain_name)&.unmark_as_disputed
domain = DNS::DomainName.new(domain_name) domain = DNS::DomainName.new(domain_name)
if domain.available? && domain.auctionable? if domain.available? && domain.auctionable?
domain.sell_at_auction domain.sell_at_auction

View file

@ -9,6 +9,7 @@ class Domain < ApplicationRecord
include Concerns::Domain::Transferable include Concerns::Domain::Transferable
include Concerns::Domain::RegistryLockable include Concerns::Domain::RegistryLockable
include Concerns::Domain::Releasable include Concerns::Domain::Releasable
include Concerns::Domain::Disputable
attr_accessor :roles attr_accessor :roles
@ -88,9 +89,8 @@ class Domain < ApplicationRecord
validates :puny_label, length: { maximum: 63 } validates :puny_label, length: { maximum: 63 }
validates :period, presence: true, numericality: { only_integer: true } validates :period, presence: true, numericality: { only_integer: true }
validates :transfer_code, presence: true validates :transfer_code, presence: true
validate :validate_reservation validate :validate_reservation
validate :validate_disputed
def validate_reservation def validate_reservation
return if persisted? || !in_reserved_list? return if persisted? || !in_reserved_list?
@ -104,19 +104,6 @@ class Domain < ApplicationRecord
errors.add(:base, :invalid_auth_information_reserved) errors.add(:base, :invalid_auth_information_reserved)
end end
def validate_disputed
return if persisted? || !in_disputed_list?
if reserved_pw.blank?
errors.add(:base, :required_parameter_missing_reserved)
return false
end
return if Dispute.valid_auth?(name, reserved_pw)
errors.add(:base, :invalid_auth_information_reserved)
end
validate :status_is_consistant validate :status_is_consistant
def status_is_consistant def status_is_consistant
has_error = (statuses.include?(DomainStatus::SERVER_HOLD) && statuses.include?(DomainStatus::SERVER_MANUAL_INZONE)) has_error = (statuses.include?(DomainStatus::SERVER_HOLD) && statuses.include?(DomainStatus::SERVER_MANUAL_INZONE))
@ -290,14 +277,6 @@ class Domain < ApplicationRecord
@in_reserved_list ||= ReservedDomain.by_domain(name).any? @in_reserved_list ||= ReservedDomain.by_domain(name).any?
end end
def in_disputed_list?
@in_disputed_list ||= Dispute.active.find_by(domain_name: name).present?
end
def disputed?
Dispute.active.where(domain_name: name).any?
end
def pending_transfer def pending_transfer
transfers.find_by(status: DomainTransfer::PENDING) transfers.find_by(status: DomainTransfer::PENDING)
end end
@ -318,8 +297,8 @@ class Domain < ApplicationRecord
return false if statuses.include_any?(DomainStatus::DELETE_CANDIDATE, DomainStatus::PENDING_RENEW, return false if statuses.include_any?(DomainStatus::DELETE_CANDIDATE, DomainStatus::PENDING_RENEW,
DomainStatus::PENDING_TRANSFER, DomainStatus::PENDING_DELETE, DomainStatus::PENDING_TRANSFER, DomainStatus::PENDING_DELETE,
DomainStatus::PENDING_UPDATE, DomainStatus::PENDING_DELETE_CONFIRMATION) DomainStatus::PENDING_UPDATE, DomainStatus::PENDING_DELETE_CONFIRMATION,
return false if disputed? DomainStatus::DISPUTED)
true true
end end

View file

@ -70,6 +70,7 @@ class DomainStatus < ApplicationRecord
FORCE_DELETE = 'serverForceDelete' FORCE_DELETE = 'serverForceDelete'
DELETE_CANDIDATE = 'deleteCandidate' DELETE_CANDIDATE = 'deleteCandidate'
EXPIRED = 'expired' EXPIRED = 'expired'
DISPUTED = 'disputed'
STATUSES = [ STATUSES = [
CLIENT_DELETE_PROHIBITED, SERVER_DELETE_PROHIBITED, CLIENT_HOLD, SERVER_HOLD, CLIENT_DELETE_PROHIBITED, SERVER_DELETE_PROHIBITED, CLIENT_HOLD, SERVER_HOLD,
@ -78,7 +79,7 @@ class DomainStatus < ApplicationRecord
INACTIVE, OK, PENDING_CREATE, PENDING_DELETE, PENDING_DELETE_CONFIRMATION, PENDING_RENEW, PENDING_TRANSFER, INACTIVE, OK, PENDING_CREATE, PENDING_DELETE, PENDING_DELETE_CONFIRMATION, PENDING_RENEW, PENDING_TRANSFER,
PENDING_UPDATE, SERVER_MANUAL_INZONE, SERVER_REGISTRANT_CHANGE_PROHIBITED, PENDING_UPDATE, SERVER_MANUAL_INZONE, SERVER_REGISTRANT_CHANGE_PROHIBITED,
SERVER_ADMIN_CHANGE_PROHIBITED, SERVER_TECH_CHANGE_PROHIBITED, FORCE_DELETE, SERVER_ADMIN_CHANGE_PROHIBITED, SERVER_TECH_CHANGE_PROHIBITED, FORCE_DELETE,
DELETE_CANDIDATE, EXPIRED DELETE_CANDIDATE, EXPIRED, DISPUTED
] ]
CLIENT_STATUSES = [ CLIENT_STATUSES = [

View file

@ -84,6 +84,7 @@ class WhoisRecord < ApplicationRecord
def populate def populate
return if domain_id.blank? return if domain_id.blank?
self.json = generated_json self.json = generated_json
self.name = json['name'] self.name = json['name']
self.registrar_id = domain.registrar_id if domain # for faster registrar updates self.registrar_id = domain.registrar_id if domain # for faster registrar updates

View file

@ -60,7 +60,7 @@ class AdminDisputesSystemTest < ApplicationSystemTestCase
def test_deletes_dispute def test_deletes_dispute
visit delete_admin_dispute_path(@dispute) visit delete_admin_dispute_path(@dispute)
assert_text 'Dispute was successfully destroyed.' assert_text 'Dispute was successfully closed.'
end end
def test_can_not_create_overlapping_dispute def test_can_not_create_overlapping_dispute