From 483eec554e769527c027d27c52cfa57f18ab07db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Thu, 30 Apr 2020 08:59:27 +0300 Subject: [PATCH] Create tests for Dispute --- app/jobs/dispute_status_update_job.rb | 21 +++--- app/models/dispute.rb | 20 +++--- app/models/reserved_domain.rb | 2 +- test/fixtures/disputes.yml | 24 +++++++ test/integration/admin_area/disputes_test.rb | 60 ++++++++++++++++ test/jobs/dispute_status_update_job_test.rb | 70 +++++++++++++++++++ test/models/disputed_domain_test.rb | 52 ++++++++++++++ test/system/admin_area/bank_statement_test.rb | 2 +- 8 files changed, 228 insertions(+), 23 deletions(-) create mode 100644 test/fixtures/disputes.yml create mode 100644 test/integration/admin_area/disputes_test.rb create mode 100644 test/jobs/dispute_status_update_job_test.rb create mode 100644 test/models/disputed_domain_test.rb diff --git a/app/jobs/dispute_status_update_job.rb b/app/jobs/dispute_status_update_job.rb index c1cca5a78..736b65236 100644 --- a/app/jobs/dispute_status_update_job.rb +++ b/app/jobs/dispute_status_update_job.rb @@ -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 diff --git a/app/models/dispute.rb b/app/models/dispute.rb index c0579cd2c..19644c5f8 100644 --- a/app/models/dispute.rb +++ b/app/models/dispute.rb @@ -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 diff --git a/app/models/reserved_domain.rb b/app/models/reserved_domain.rb index 898b9efee..9ee85548b 100644 --- a/app/models/reserved_domain.rb +++ b/app/models/reserved_domain.rb @@ -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 diff --git a/test/fixtures/disputes.yml b/test/fixtures/disputes.yml new file mode 100644 index 000000000..3e6b882f4 --- /dev/null +++ b/test/fixtures/disputes.yml @@ -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 diff --git a/test/integration/admin_area/disputes_test.rb b/test/integration/admin_area/disputes_test.rb new file mode 100644 index 000000000..fcd06f759 --- /dev/null +++ b/test/integration/admin_area/disputes_test.rb @@ -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 diff --git a/test/jobs/dispute_status_update_job_test.rb b/test/jobs/dispute_status_update_job_test.rb new file mode 100644 index 000000000..a91dc63d5 --- /dev/null +++ b/test/jobs/dispute_status_update_job_test.rb @@ -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 diff --git a/test/models/disputed_domain_test.rb b/test/models/disputed_domain_test.rb new file mode 100644 index 000000000..01897e19b --- /dev/null +++ b/test/models/disputed_domain_test.rb @@ -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 diff --git a/test/system/admin_area/bank_statement_test.rb b/test/system/admin_area/bank_statement_test.rb index c95035a8d..29ed4f312 100644 --- a/test/system/admin_area/bank_statement_test.rb +++ b/test/system/admin_area/bank_statement_test.rb @@ -1,6 +1,6 @@ require 'application_system_test_case' -class BankStatementTest < ApplicationSystemTestCase +class AdminBankStatementsSystemTest < ApplicationSystemTestCase setup do sign_in users(:admin) travel_to Time.zone.parse('2010-07-05 00:30:00')