Skip to content

Commit

Permalink
Merge pull request #1124 from internetee/refactor-force-delete
Browse files Browse the repository at this point in the history
Refactor force delete
  • Loading branch information
vohmar authored Apr 1, 2019
2 parents 26dac12 + 6d40e9e commit 176d167
Show file tree
Hide file tree
Showing 24 changed files with 86 additions and 52 deletions.
5 changes: 2 additions & 3 deletions app/models/concerns/domain/force_delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ def schedule_force_delete

preserve_current_statuses_for_force_delete
add_force_delete_statuses
self.force_delete_at = (Time.zone.now + (Setting.redemption_grace_period.days + 1.day)).utc
.beginning_of_day
self.force_delete_date = Time.zone.today + Setting.redemption_grace_period.days + 1.day
stop_all_pending_actions
allow_deletion
save(validate: false)
Expand All @@ -22,7 +21,7 @@ def schedule_force_delete
def cancel_force_delete
restore_statuses_before_force_delete
remove_force_delete_statuses
self.force_delete_at = nil
self.force_delete_date = nil
save(validate: false)
end

Expand Down
8 changes: 6 additions & 2 deletions app/models/concerns/domain/releasable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ def release_domains

def releasable_domains
if release_to_auction
where('delete_at < ? AND ? != ALL(coalesce(statuses, array[]::varchar[]))',
where('(delete_at < ? OR force_delete_date <= ?)' \
' AND ? != ALL(coalesce(statuses, array[]::varchar[]))',
Time.zone.now,
Time.zone.today,
DomainStatus::SERVER_DELETE_PROHIBITED)
else
where('delete_at < ? AND ? != ALL(coalesce(statuses, array[]::varchar[])) AND' \
where('(delete_at < ? OR force_delete_date <= ?)' \
' AND ? != ALL(coalesce(statuses, array[]::varchar[])) AND' \
' ? != ALL(COALESCE(statuses, array[]::varchar[]))',
Time.zone.now,
Time.zone.today,
DomainStatus::SERVER_DELETE_PROHIBITED,
DomainStatus::DELETE_CANDIDATE)
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/domain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ def admin_status_update(update)
when DomainStatus::PENDING_DELETE
self.delete_at = nil
when DomainStatus::SERVER_MANUAL_INZONE # removal causes server hold to set
self.outzone_at = Time.zone.now if self.force_delete_at.present?
self.outzone_at = Time.zone.now if force_delete_scheduled?
when DomainStatus::DomainStatus::EXPIRED # removal causes server hold to set
self.outzone_at = self.expire_time + 15.day
when DomainStatus::DomainStatus::SERVER_HOLD # removal causes server hold to set
Expand Down
14 changes: 0 additions & 14 deletions app/models/domain_cron.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,4 @@ def self.start_redemption_grace_period
STDOUT << "#{Time.zone.now.utc} - Successfully set server_hold to #{marked} of #{real} domains\n" unless Rails.env.test?
marked
end

def self.destroy_delete_candidates
STDOUT << "#{Time.zone.now.utc} - Destroying domains\n" unless Rails.env.test?

c = 0

Domain.where('force_delete_at <= ?', Time.zone.now.end_of_day.utc).each do |x|
DomainDeleteJob.enqueue(x.id, run_at: rand(((24*60) - (DateTime.now.hour * 60 + DateTime.now.minute))).minutes.from_now)
STDOUT << "#{Time.zone.now.utc} DomainCron.destroy_delete_candidates: job added by force delete time ##{x.id} (#{x.name})\n" unless Rails.env.test?
c += 1
end

STDOUT << "#{Time.zone.now.utc} - Job destroy added for #{c} domains\n" unless Rails.env.test?
end
end
3 changes: 2 additions & 1 deletion app/models/whois_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def generate_json
h[:changed] = domain.updated_at.try(:to_s, :iso8601)
h[:expire] = domain.valid_to.to_date.to_s
h[:outzone] = domain.outzone_at.try(:to_date).try(:to_s)
h[:delete] = [domain.delete_at, domain.force_delete_at].compact.min.try(:to_date).try(:to_s)
h[:delete] = [domain.delete_at, domain.force_delete_date].compact.min.try(:to_date)
.try(:to_s)

h[:registrant] = registrant.name
h[:registrant_kind] = registrant.kind
Expand Down
2 changes: 1 addition & 1 deletion app/presenters/domain_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def delete_date
end

def force_delete_date
view.l(domain.force_delete_at, format: :date) if domain.force_delete_at
view.l(domain.force_delete_date) if domain.force_delete_scheduled?
end

def admin_contact_names
Expand Down
4 changes: 2 additions & 2 deletions app/views/admin/domains/partials/_general.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
<dt><%= t('.delete_time') %></dt>
<dd><%= l(@domain.delete_at) %></dd>

<dt><%= Domain.human_attribute_name :force_delete_at %></dt>
<dd><%= l @domain.force_delete_at %></dd>
<dt><%= Domain.human_attribute_name :force_delete_date %></dt>
<dd><%= l @domain.force_delete_date %></dd>

<dt><%= t('.locked_by_registrant_at') %></dt>
<dd><%= l(@domain.locked_by_registrant_at) %></dd>
Expand Down
4 changes: 2 additions & 2 deletions app/views/registrant/domains/partials/_general.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
<dt><%= Domain.human_attribute_name :delete_at %></dt>
<dd><%= l(@domain.delete_at) %></dd>

<dt><%= Domain.human_attribute_name :force_delete_at %></dt>
<dd><%= l(@domain.force_delete_at) %></dd>
<dt><%= Domain.human_attribute_name :force_delete_date %></dt>
<dd><%= l @domain.force_delete_date %></dd>
</dl>
</div>
</div>
1 change: 0 additions & 1 deletion config/locales/admin/domains.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ en:
general:
outzone_time: Outzone time
delete_time: Delete time
force_delete_time: Force delete time
locked_by_registrant_at: Registry lock time

admin_contacts:
Expand Down
4 changes: 0 additions & 4 deletions config/schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@
runner 'Certificate.update_crl'
end

every 42.minutes do
runner 'DomainCron.destroy_delete_candidates'
end

every 45.minutes do
runner 'DomainCron.start_expire_period'
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ChangeDomainsForceDeleteAtToDate < ActiveRecord::Migration
def change
change_column :domains, :force_delete_at, :date
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RenameDomainsForceDeleteAtToForceDeleteDate < ActiveRecord::Migration
def change
rename_column :domains, :force_delete_at, :force_delete_date
end
end
6 changes: 5 additions & 1 deletion db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,7 @@ CREATE TABLE public.domains (
registrant_verification_asked_at timestamp without time zone,
registrant_verification_token character varying,
pending_json jsonb,
force_delete_at timestamp without time zone,
force_delete_date date,
statuses character varying[],
reserved boolean DEFAULT false,
status_notes public.hstore,
Expand Down Expand Up @@ -4935,3 +4935,7 @@ INSERT INTO schema_migrations (version) VALUES ('20190311111718');

INSERT INTO schema_migrations (version) VALUES ('20190312211614');

INSERT INTO schema_migrations (version) VALUES ('20190322152123');

INSERT INTO schema_migrations (version) VALUES ('20190322152529');

6 changes: 3 additions & 3 deletions doc/registrant-api/v1/domain.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Content-Type: application/json
"pending_json":{
},
"force_delete_at":null,
"force_delete_date":null,
"statuses":[
"ok"
],
Expand Down Expand Up @@ -187,7 +187,7 @@ Content-Type: application/json
"pending_json":{
},
"force_delete_at":null,
"force_delete_date":null,
"statuses":[
"ok"
],
Expand Down Expand Up @@ -295,7 +295,7 @@ Content-Type: application/json
"pending_json":{
},
"force_delete_at":null,
"force_delete_date":null,
"statuses":[
"ok"
],
Expand Down
4 changes: 2 additions & 2 deletions doc/registrant-api/v1/registry_lock.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Content-Type: application/json
"pending_json":{
},
"force_delete_at":null,
"force_delete_date":null,
"statuses":[
"serverUpdateProhibited",
"serverDeleteProhibited",
Expand Down Expand Up @@ -216,7 +216,7 @@ Content-Type: application/json
"pending_json":{
},
"force_delete_at":null,
"force_delete_date":null,
"statuses":[
"ok"
],
Expand Down
2 changes: 1 addition & 1 deletion doc/repp/v1/domain.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Content-Type: application/json
"registrant_verification_token": null,
"pending_json": {
},
"force_delete_at": null,
"force_delete_date": null,
"statuses": [
"ok"
],
Expand Down
2 changes: 1 addition & 1 deletion lib/serializers/registrant_api/domain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def to_json
registrant_verification_asked_at: domain.registrant_verification_asked_at,
registrant_verification_token: domain.registrant_verification_token,
pending_json: domain.pending_json,
force_delete_at: domain.force_delete_at,
force_delete_date: domain.force_delete_date,
statuses: domain.statuses,
locked_by_registrant_at: domain.locked_by_registrant_at,
reserved: domain.reserved,
Expand Down
6 changes: 3 additions & 3 deletions spec/presenters/domain_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@
subject(:force_delete_date) { presenter.force_delete_date }

context 'when present' do
let(:domain) { instance_double(Domain, force_delete_at: '05.07.2010') }
let(:domain) { instance_double(Domain, force_delete_date: '05.07.2010', force_delete_scheduled?: true) }

it 'returns localized date' do
expect(view).to receive(:l).with('05.07.2010', format: :date).and_return('delete date')
expect(view).to receive(:l).with('05.07.2010').and_return('delete date')
expect(force_delete_date).to eq('delete date')
end
end

context 'when absent' do
let(:domain) { instance_double(Domain, force_delete_at: nil) }
let(:domain) { instance_double(Domain, force_delete_date: nil, force_delete_scheduled?: false) }

specify { expect(force_delete_date).to be_nil }
end
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/domains.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ shop:
valid_to: <%= Time.zone.parse('2010-07-05').to_s(:db) %>
outzone_at: <%= Time.zone.parse('2010-07-06').to_s(:db) %>
delete_at: <%= Time.zone.parse('2010-07-07').to_s(:db) %>
force_delete_at: <%= Time.zone.parse('2010-07-08').to_s(:db) %>
force_delete_date: 2010-07-08
period: 1
period_unit: m
uuid: 1b3ee442-e8fe-4922-9492-8fcb9dccc69c
Expand Down
2 changes: 1 addition & 1 deletion test/lib/serializers/registrant_api/domain_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def test_other_fields_are_also_present
registrant tech_contacts admin_contacts transfer_code name_dirty name_puny period
period_unit creator_str updator_str legacy_id legacy_registrar_id legacy_registrant_id
outzone_at delete_at registrant_verification_asked_at
registrant_verification_token pending_json force_delete_at statuses
registrant_verification_token pending_json force_delete_date statuses
locked_by_registrant_at reserved status_notes nameservers]

assert_equal(keys, @json.keys & keys)
Expand Down
27 changes: 20 additions & 7 deletions test/models/domain/force_delete_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@
class DomainForceDeleteTest < ActiveSupport::TestCase
setup do
@domain = domains(:shop)
@original_redemption_grace_period = Setting.redemption_grace_period
end

def test_schedule_force_delete
@original_redemption_grace_period = Setting.redemption_grace_period
teardown do
Setting.redemption_grace_period = @original_redemption_grace_period
end

def test_schedules_force_delete
assert_not @domain.force_delete_scheduled?
Setting.redemption_grace_period = 30
travel_to Time.zone.parse('2010-07-05 00:00')
travel_to Time.zone.parse('2010-07-05')

@domain.schedule_force_delete
@domain.reload

assert @domain.force_delete_scheduled?
assert_equal Time.zone.parse('2010-08-04 03:00'), @domain.force_delete_at

travel_back
Setting.redemption_grace_period = @original_redemption_grace_period
assert_equal Date.parse('2010-08-05'), @domain.force_delete_date
end

def test_scheduling_force_delete_adds_corresponding_statuses
Expand Down Expand Up @@ -80,6 +82,17 @@ def test_force_delete_cannot_be_scheduled_when_a_domain_is_discarded
end
end

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

@domain.cancel_force_delete
@domain.reload

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

def test_cancelling_force_delete_bypasses_validation
@domain = domains(:invalid)
@domain.schedule_force_delete
Expand Down
9 changes: 9 additions & 0 deletions test/models/domain/releasable/auctionable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ def test_skips_auction_when_domains_is_reserved
assert_not @domain.domain_name.at_auction?
end

def test_sells_domains_with_scheduled_force_delete_procedure_at_auction
@domain.update!(force_delete_date: '2010-07-05')
travel_to Time.zone.parse('2010-07-05')

Domain.release_domains

assert @domain.domain_name.at_auction?
end

def test_deletes_registered_domain
@domain.update!(delete_at: Time.zone.parse('2010-07-05 07:59'))
travel_to Time.zone.parse('2010-07-05 08:00')
Expand Down
10 changes: 10 additions & 0 deletions test/models/domain/releasable/discardable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ def test_discards_domains_with_past_delete_at
assert @domain.discarded?
end

def test_discards_domains_with_scheduled_force_delete_procedure
@domain.update!(force_delete_date: '2010-07-05')
travel_to Time.zone.parse('2010-07-05')

Domain.release_domains
@domain.reload

assert @domain.discarded?
end

def test_ignores_domains_with_delete_at_in_the_future_or_now
@domain.update!(delete_at: Time.zone.parse('2010-07-05 08:00'))
travel_to Time.zone.parse('2010-07-05 08:00')
Expand Down
5 changes: 4 additions & 1 deletion test/system/registrant_area/domains/details_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ class RegistrantAreaDomainDetailsTest < ApplicationSystemTestCase
end

def test_general_data
@domain.update_columns(force_delete_date: '2010-07-08', statuses: [DomainStatus::FORCE_DELETE])

visit registrant_domain_url(@domain)

assert_text 'Name shop.test'
assert_text "Registered at #{l Time.zone.parse('2010-07-04')}"
assert_link 'Best Names', href: registrant_registrar_path(@domain.registrar)
Expand All @@ -18,7 +21,7 @@ def test_general_data
assert_text "Valid to #{l Time.zone.parse('2010-07-05')}"
assert_text "Outzone at #{l Time.zone.parse('2010-07-06')}"
assert_text "Delete at #{l Time.zone.parse('2010-07-07')}"
assert_text "Force delete at #{l Time.zone.parse('2010-07-08')}"
assert_text "Force delete date #{l Date.parse('2010-07-08')}"
end

def test_registrant
Expand Down

0 comments on commit 176d167

Please sign in to comment.