Skip to content

Commit

Permalink
Add force_delete_start field to ForceDelete & add tests
Browse files Browse the repository at this point in the history
Add new test file new_force_delete_test.rb to check if new ForceDeleteProcedures are correct.
ATM it's just a scaffold of some sort, all tests are red, magic numbers and so - just a proof of concept.

See #1428
  • Loading branch information
yulgolem committed Dec 4, 2019
1 parent b4be9e7 commit 49190b8
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 2 deletions.
9 changes: 9 additions & 0 deletions app/models/concerns/domain/force_delete.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Concerns::Domain::ForceDelete
extend ActiveSupport::Concern

DAYS_TO_START_HOLD = 15.days

def force_delete_scheduled?
statuses.include?(DomainStatus::FORCE_DELETE)
end
Expand All @@ -25,6 +27,12 @@ def cancel_force_delete
save(validate: false)
end

def check_hold
if force_delete_start < valid_to && (force_delete_date + DAYS_TO_START_HOLD) > Time.zone.today
statuses << DomainStatus::CLIENT_HOLD
end
end

private

def stop_all_pending_actions
Expand Down Expand Up @@ -62,6 +70,7 @@ def remove_force_delete_statuses
statuses.delete(DomainStatus::SERVER_UPDATE_PROHIBITED)
statuses.delete(DomainStatus::PENDING_DELETE)
statuses.delete(DomainStatus::SERVER_MANUAL_INZONE)
statuses.delete(DomainStatus::CLIENT_HOLD)
end

def allow_deletion
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddForceDeleteStartToDomains < ActiveRecord::Migration[5.0]
def change
add_column :domains, :force_delete_start, :datetime
end
end
7 changes: 5 additions & 2 deletions db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;

--
Expand Down Expand Up @@ -842,7 +843,8 @@ CREATE TABLE public.domains (
upid integer,
up_date timestamp without time zone,
uuid uuid DEFAULT public.gen_random_uuid() NOT NULL,
locked_by_registrant_at timestamp without time zone
locked_by_registrant_at timestamp without time zone,
force_delete_start timestamp without time zone
);


Expand Down Expand Up @@ -4361,6 +4363,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20191007123000'),
('20191008024334'),
('20191024153351'),
('20191024160038');
('20191024160038'),
('20191203083643');


189 changes: 189 additions & 0 deletions test/models/domain/new_force_delete_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
require 'test_helper'

class NewDomainForceDeleteTest < ActiveSupport::TestCase
setup do
@domain = domains(:shop)
@original_redemption_grace_period = Setting.redemption_grace_period
end

teardown do
Setting.redemption_grace_period = @original_redemption_grace_period
end

def test_force_delete_raises_on_wrong_type
assert_raises StandardError do
@domain.schedule_force_delete(type: :wrong_type)
end
end

def test_schedules_force_delete_fast_track
assert_not @domain.force_delete_scheduled?
Setting.redemption_grace_period = 45
travel_to Time.zone.parse('2010-07-05')

@domain.schedule_force_delete(type: :fast_track)
@domain.reload

assert @domain.force_delete_scheduled?
assert_equal Date.parse('2010-08-19'), @domain.force_delete_date
assert_equal Date.parse('2010-07-05'), @domain.force_delete_start
end

def test_schedules_force_delete_soft_year_ahead
@domain.update(valid_to: Time.zone.parse('2012-08-05'))
assert_not @domain.force_delete_scheduled?
Setting.redemption_grace_period = 45
travel_to Time.zone.parse('2010-07-05')

@domain.schedule_force_delete(type: :soft)
@domain.reload

assert @domain.force_delete_scheduled?
assert_equal Date.parse('2010-09-19'), @domain.force_delete_date
assert_equal Date.parse('2010-08-05'), @domain.force_delete_start.to_date
assert (@domain.statuses.include?(DomainStatus::CLIENT_HOLD))
end

def test_schedules_force_delete_soft_less_than_year_ahead
@domain.update(valid_to: Time.zone.parse('2011-08-05'))
assert_not @domain.force_delete_scheduled?
Setting.redemption_grace_period = 45
travel_to Time.zone.parse('2010-07-05')

@domain.schedule_force_delete(type: :soft)
@domain.reload

assert @domain.force_delete_scheduled?
assert_equal Date.parse('2010-08-19'), @domain.force_delete_date
assert_equal Date.parse('2010-07-05'), @domain.force_delete_start
end

def test_scheduling_soft_force_delete_adds_corresponding_statuses
statuses_to_be_added = [
DomainStatus::FORCE_DELETE,
DomainStatus::SERVER_RENEW_PROHIBITED,
DomainStatus::SERVER_TRANSFER_PROHIBITED,
]

@domain.schedule_force_delete(type: :soft)
@domain.reload
assert (@domain.statuses & statuses_to_be_added) == statuses_to_be_added
end

def test_scheduling_fast_track_force_delete_adds_corresponding_statuses
statuses_to_be_added = [
DomainStatus::FORCE_DELETE,
DomainStatus::SERVER_RENEW_PROHIBITED,
DomainStatus::SERVER_TRANSFER_PROHIBITED,
]

@domain.schedule_force_delete(type: :fast_track)
@domain.reload
assert (@domain.statuses & statuses_to_be_added) == statuses_to_be_added
end

def test_scheduling_force_delete_allows_domain_deletion
statuses_to_be_removed = [
DomainStatus::CLIENT_DELETE_PROHIBITED,
DomainStatus::SERVER_DELETE_PROHIBITED,
]

@domain.statuses = statuses_to_be_removed + %w[other-status]
@domain.schedule_force_delete(type: :fast_track)
@domain.reload
assert_empty @domain.statuses & statuses_to_be_removed
end

def test_scheduling_force_delete_stops_pending_actions
Setting.redemption_grace_period = 45
statuses_to_be_removed = [
DomainStatus::PENDING_UPDATE,
DomainStatus::PENDING_TRANSFER,
DomainStatus::PENDING_RENEW,
DomainStatus::PENDING_CREATE,
]

@domain.statuses = statuses_to_be_removed + %w[other-status]
@domain.schedule_force_delete(type: :fast_track)
@domain.reload
assert_empty @domain.statuses & statuses_to_be_removed, 'Pending actions should be stopped'
end

def test_scheduling_force_delete_preserves_current_statuses
@domain.statuses = %w[test1 test2]
@domain.schedule_force_delete(type: :fast_track)
@domain.reload
assert_equal %w[test1 test2], @domain.statuses_before_force_delete
end

def test_scheduling_force_delete_bypasses_validation
@domain = domains(:invalid)
@domain.schedule_force_delete(type: :fast_track)
assert @domain.force_delete_scheduled?
end

def test_force_delete_cannot_be_scheduled_when_a_domain_is_discarded
@domain.update!(statuses: [DomainStatus::DELETE_CANDIDATE])
assert_raises StandardError do
@domain.schedule_force_delete(type: :fast_track)
end
end

def test_force_delete_sets_client_hold_on_condition
statuses_to_be_added = [DomainStatus::CLIENT_HOLD]
@domain.update_columns(statuses: [DomainStatus::FORCE_DELETE],
force_delete_date: Time.zone.parse('2010-08-05'),
force_delete_start: Time.zone.parse('2010-08-05') - 45.days)
travel_to Time.zone.parse('2010-07-05')
assert @domain.force_delete_scheduled?

@domain.check_hold
assert (@domain.statuses & statuses_to_be_added) == statuses_to_be_added
end

def test_cancels_force_delete
@domain.update_columns(statuses: [DomainStatus::FORCE_DELETE],
force_delete_date: Time.zone.parse('2010-07-05'),
force_delete_start: Time.zone.parse('2010-07-05') - 45.days)
assert @domain.force_delete_scheduled?

@domain.cancel_force_delete
@domain.reload

assert_not @domain.force_delete_scheduled?
assert_nil @domain.force_delete_date
assert_nil @domain.force_delete_start
end

def test_cancelling_force_delete_bypasses_validation
@domain = domains(:invalid)
@domain.schedule_force_delete(type: :fast_track)
@domain.cancel_force_delete
assert_not @domain.force_delete_scheduled?
end

def test_cancelling_force_delete_removes_statuses_that_were_set_on_force_delete
statuses = [
DomainStatus::FORCE_DELETE,
DomainStatus::SERVER_RENEW_PROHIBITED,
DomainStatus::SERVER_TRANSFER_PROHIBITED,
]
@domain.statuses = @domain.statuses + statuses
@domain.schedule_force_delete(type: :fast_track)

@domain.cancel_force_delete
@domain.reload

assert_empty @domain.statuses & statuses
end

def test_cancelling_force_delete_restores_statuses_that_a_domain_had_before_force_delete
@domain.statuses_before_force_delete = ['test1', DomainStatus::DELETE_CANDIDATE]

@domain.cancel_force_delete
@domain.reload

assert_equal ['test1', DomainStatus::DELETE_CANDIDATE], @domain.statuses
assert_nil @domain.statuses_before_force_delete
end
end

0 comments on commit 49190b8

Please sign in to comment.