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

View file

@ -16,7 +16,7 @@ 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, -> { where('expires_at >= ? AND closed = false', Time.zone.today) }
scope :closed, -> { where(closed: true) } scope :closed, -> { where(closed: true) }
alias_attribute :name, :domain_name alias_attribute :name, :domain_name
@ -52,24 +52,21 @@ class Dispute < ApplicationRecord
whois_record = Whois::Record.find_or_initialize_by(name: domain_name) whois_record = Whois::Record.find_or_initialize_by(name: domain_name)
return true if remove_whois_data(whois_record) return true if remove_whois_data(whois_record)
false
end end
def remove_whois_data(record) def remove_whois_data(record)
record.json['status'] = record.json['status'].delete_if { |status| status == 'disputed' } record.json['status'] = record.json['status'].delete_if { |status| status == 'disputed' }
if record.json['status'].blank? if record.json['status'].blank?
return true if record.destroy return true if record.destroy && record.json['status'].blank?
return false
end end
record.save record.save
end end
def generate_json(record) def generate_json(record)
status_arr = (record.json['status'] ||= [])
h = HashWithIndifferentAccess.new(name: domain_name, status: ['disputed']) h = HashWithIndifferentAccess.new(name: domain_name, status: ['disputed'])
return h if record.json.blank? return h if record.json.blank?
status_arr = (record.json['status'] ||= [])
return record.json if status_arr.include? 'disputed' return record.json if status_arr.include? 'disputed'
status_arr.push('disputed') status_arr.push('disputed')
@ -115,12 +112,13 @@ class Dispute < ApplicationRecord
end end
def validate_domain_name_period_uniqueness def validate_domain_name_period_uniqueness
return unless new_record?
existing_dispute = Dispute.unscoped.where(domain_name: domain_name, closed: false) 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? 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
end end

View file

@ -43,7 +43,7 @@ class ReservedDomain < ApplicationRecord
end end
def sync_dispute_password 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? self.password = dispute.password if dispute.present?
end end

24
test/fixtures/disputes.yml vendored Normal file
View file

@ -0,0 +1,24 @@
active:
domain_name: active-dispute.test
password: active-001
starts_at: <%= Date.parse '2010-07-05' %>
expires_at: <%= Date.parse '2013-07-05' %>
closed: false
future:
domain_name: future-dispute.test
password: active-001
starts_at: <%= Date.parse '2010-10-05' %>
expires_at: <%= Date.parse '2013-10-05' %>
closed: false
expired:
domain_name: expired-dispute.test
password: active-001
starts_at: <%= Date.parse '2010-07-05' %>
expires_at: <%= Date.parse '2013-07-05' %>
closed: true
closed:
domain_name: closed_dispute.test
password: active-001
starts_at: <%= Date.parse '2010-07-05' %>
expires_at: <%= Date.parse '2013-07-05' %>
closed: true

View file

@ -0,0 +1,60 @@
require 'application_system_test_case'
require 'test_helper'
class AdminDisputesSystemTest < ApplicationSystemTestCase
include ActionView::Helpers::NumberHelper
setup do
@dispute = disputes(:active)
@original_default_language = Setting.default_language
sign_in users(:admin)
end
teardown do
Setting.default_language = @original_default_language
end
def test_creates_new_dispute
assert_nil Dispute.active.find_by(domain_name: 'disputed.test')
visit admin_disputes_path
click_on 'New domain dispute'
fill_in 'Domain name', with: 'disputed.test'
fill_in 'Password', with: '1234'
fill_in 'Starts at', with: Time.zone.today.to_s
fill_in 'Comment', with: 'Sample comment'
click_on 'Save'
assert_text 'Dispute was successfully created.'
assert_text 'disputed.test'
end
def test_updates_dispute
assert_not_equal Time.zone.today, @dispute.starts_at
visit edit_admin_dispute_path(@dispute)
fill_in 'Starts at', with: Time.zone.today.to_s
click_link_or_button 'Save'
assert_text 'Dispute was successfully updated'
assert_text Time.zone.today
end
def test_deletes_dispute
visit delete_admin_dispute_path(@dispute)
assert_text 'Dispute was successfully destroyed.'
end
def test_can_not_create_overlapping_dispute
visit admin_disputes_path
click_on 'New domain dispute'
fill_in 'Domain name', with: 'active-dispute.test'
fill_in 'Starts at', with: @dispute.starts_at + 1.day
click_on 'Save'
assert_text 'Dispute already exists for this domain at given timeframe'
end
end

View file

@ -0,0 +1,70 @@
require "test_helper"
class DisputeStatusUpdateJobTest < ActiveSupport::TestCase
setup do
travel_to Time.zone.parse('2010-10-05')
end
def test_nothing_is_raised
assert_nothing_raised do
DisputeStatusUpdateJob.run
end
end
def test_whois_data_added_when_dispute_activated
dispute = disputes(:future)
DisputeStatusUpdateJob.run
whois_record = Whois::Record.find_by(name: dispute.domain_name)
assert whois_record.present?
assert_includes whois_record.json['status'], 'disputed'
end
def test_unregistered_domain_whois_data_is_deleted
dispute = disputes(:active)
dispute.update!(starts_at: Time.zone.today - 3.years - 1.day)
DisputeStatusUpdateJob.run
dispute.reload
assert dispute.closed
whois_record = Whois::Record.find_by(name: dispute.domain_name)
assert whois_record.nil?
end
def test_registered_domain_whois_data_is_added
Dispute.create(domain_name: 'shop.test', starts_at: '2010-07-05')
travel_to Time.zone.parse('2010-07-05')
DisputeStatusUpdateJob.run
whois_record = Whois::Record.find_by(name: 'shop.test')
assert_includes whois_record.json['status'], 'disputed'
end
def test_registered_domain_whois_data_is_removed
travel_to Time.zone.parse('2010-07-05')
domain = domains(:shop)
domain.update(valid_to: Time.zone.parse('2015-07-05').to_s(:db),
outzone_at: Time.zone.parse('2015-07-06').to_s(:db),
delete_date: nil,
force_delete_date: nil)
# Dispute status is added automatically if starts_at is not in future
Dispute.create(domain_name: 'shop.test', starts_at: Time.zone.parse('2010-07-05'))
domain.reload
whois_record = Whois::Record.find_by(name: 'shop.test')
assert_includes whois_record.json['status'], 'disputed'
# Dispute status is removed night time day after it's ended
travel_to Time.zone.parse('2010-07-05') + 3.years + 1.day
DisputeStatusUpdateJob.run
whois_record.reload
assert_not whois_record.json['status'].include? 'disputed'
puts whois_record.json['status']
end
end

View file

@ -0,0 +1,52 @@
require 'test_helper'
class DisputedDomainTest < ActiveSupport::TestCase
setup do
@dispute = disputes(:active)
end
def test_fixture_is_valid
assert @dispute.valid?
end
def test_can_be_closed_by_domain_name
travel_to Time.zone.parse('2010-10-05')
Dispute.close_by_domain(@dispute.domain_name)
@dispute.reload
assert @dispute.closed
end
def test_syncs_password_to_reserved
dispute = Dispute.new(domain_name: 'reserved.test', starts_at: Time.zone.today, password: 'disputepw')
dispute.save
dispute.reload
assert_equal dispute.password, ReservedDomain.find_by(name: dispute.domain_name).password
end
def test_domain_name_zone_is_validated
dispute = Dispute.new(domain_name: 'correct.test', starts_at: Time.zone.today)
assert dispute.valid?
dispute.domain_name = 'zone.is.unrecognized.test'
assert_not dispute.valid?
end
def test_dispute_can_not_be_created_if_another_active_is_present
dispute = Dispute.new(domain_name: @dispute.domain_name,
starts_at: @dispute.starts_at + 1.day)
assert_not dispute.valid?
end
def test_expires_at_date_is_appended_automatically
dispute = Dispute.new(domain_name: 'random.test', starts_at: Time.zone.today)
assert dispute.valid?
assert_equal dispute.expires_at, dispute.starts_at + 3.years
end
def test_starts_at_must_be_present
dispute = Dispute.new(domain_name: 'random.test')
assert_not dispute.valid?
end
end

View file

@ -1,6 +1,6 @@
require 'application_system_test_case' require 'application_system_test_case'
class BankStatementTest < ApplicationSystemTestCase class AdminBankStatementsSystemTest < ApplicationSystemTestCase
setup do setup do
sign_in users(:admin) sign_in users(:admin)
travel_to Time.zone.parse('2010-07-05 00:30:00') travel_to Time.zone.parse('2010-07-05 00:30:00')