Create tests for Dispute

This commit is contained in:
Karl Erik Õunapuu 2020-04-30 08:59:27 +03:00
parent f73833e478
commit 483eec554e
8 changed files with 228 additions and 23 deletions

View file

@ -1,14 +1,15 @@
class DisputeStatusUpdateJob < Que::Job
def run
@backlog = { 'activated': 0, 'closed': 0, 'activate_fail': [], 'close_fail': [] }
.with_indifferent_access
close_disputes
activate_disputes
Rails.logger.info "DisputeStatusUpdateJob - All done. Closed #{@backlog[:closed]} and " \
"activated #{@backlog[:closed]} disputes."
Rails.logger.info "DisputeStatusUpdateJob - All done. Closed #{@backlog['closed']} and " \
"activated #{@backlog['activated']} disputes."
show_failed_disputes unless @backlog[:activate_fail].empty? && @backlog[:close_fail].empty?
show_failed_disputes unless @backlog['activate_fail'].empty? && @backlog['close_fail'].empty?
end
def close_disputes
@ -36,7 +37,7 @@ class DisputeStatusUpdateJob < Que::Job
def create_backlog_entry(dispute:, intent:, successful:)
if successful
@backlog["#{intent}d"] << dispute.id
@backlog["#{intent}d"] += 1
Rails.logger.info "DisputeStatusUpdateJob - #{intent}d dispute " \
" for '#{dispute.domain_name}'"
else
@ -47,14 +48,14 @@ class DisputeStatusUpdateJob < Que::Job
end
def show_failed_disputes
if @backlog[:close_fail].any?
Rails.logger.info('DisputeStatuseCloseJob - Failed to close disputes with Ids:' \
"#{@backlog[:close_fail]}")
if @backlog['close_fail'].any?
Rails.logger.info('DisputeStatusUpdateJob - Failed to close disputes with Ids:' \
"#{@backlog['close_fail']}")
end
return unless @backlog[:activate_fail].any?
return unless @backlog['activate_fail'].any?
Rails.logger.info('DisputeStatuseCloseJob - Failed to activate disputes with Ids:' \
"#{@backlog[:activate_fail]}")
Rails.logger.info('DisputeStatusUpdateJob - Failed to activate disputes with Ids:' \
"#{@backlog['activate_fail']}")
end
end

View file

@ -16,7 +16,7 @@ class Dispute < ApplicationRecord
after_destroy :remove_data
scope :expired, -> { where('expires_at < ?', Time.zone.today) }
scope :active, -> { where('expires_at > ? AND closed = false', Time.zone.today) }
scope :active, -> { where('expires_at >= ? AND closed = false', Time.zone.today) }
scope :closed, -> { where(closed: true) }
alias_attribute :name, :domain_name
@ -52,24 +52,21 @@ class Dispute < ApplicationRecord
whois_record = Whois::Record.find_or_initialize_by(name: domain_name)
return true if remove_whois_data(whois_record)
false
end
def remove_whois_data(record)
record.json['status'] = record.json['status'].delete_if { |status| status == 'disputed' }
if record.json['status'].blank?
return true if record.destroy
return false
return true if record.destroy && record.json['status'].blank?
end
record.save
end
def generate_json(record)
status_arr = (record.json['status'] ||= [])
h = HashWithIndifferentAccess.new(name: domain_name, status: ['disputed'])
return h if record.json.blank?
status_arr = (record.json['status'] ||= [])
return record.json if status_arr.include? 'disputed'
status_arr.push('disputed')
@ -115,12 +112,13 @@ class Dispute < ApplicationRecord
end
def validate_domain_name_period_uniqueness
return unless new_record?
existing_dispute = Dispute.unscoped.where(domain_name: domain_name, closed: false)
.where('expires_at > ?', starts_at)
.where('expires_at >= ?', starts_at)
existing_dispute = existing_dispute.where.not(id: id) unless new_record?
return unless existing_dispute.any?
errors.add(:base, 'Dispute already exists for this domain at given timeframe')
errors.add(:starts_at, 'Dispute already exists for this domain at given timeframe')
end
end

View file

@ -43,7 +43,7 @@ class ReservedDomain < ApplicationRecord
end
def sync_dispute_password
dispute = Dispute.active.find_by(domain_name: domain_name)
dispute = Dispute.active.find_by(domain_name: name)
self.password = dispute.password if dispute.present?
end