Merge remote-tracking branch 'origin/registry-238' into registry-186

# Conflicts:
#	app/models/domain.rb
#	spec/models/domain_spec.rb
This commit is contained in:
Artur Beljajev 2016-11-08 12:43:01 +02:00
commit 4075024d7e
5 changed files with 189 additions and 41 deletions

View file

@ -0,0 +1,31 @@
module Concerns::Domain::Expirable
extend ActiveSupport::Concern
included do
alias_attribute :expire_time, :valid_to
end
class_methods do
def expired
where("#{attribute_alias(:expire_time)} <= ?", Time.zone.now)
end
end
def registered?
!expired?
end
def expired?
expire_time <= Time.zone.now
end
def expirable?
return false if expire_time > Time.zone.now
if statuses.include?(DomainStatus::EXPIRED) && outzone_at.present? && delete_at.present?
return false
end
true
end
end

View file

@ -3,6 +3,8 @@ class Domain < ActiveRecord::Base
include UserEvents
include Versions # version/domain_version.rb
include Statuses
include Concerns::Domain::Expirable
has_paper_trail class_name: "DomainVersion", meta: { children: :children_log }
attr_accessor :roles
@ -11,6 +13,8 @@ class Domain < ActiveRecord::Base
alias_attribute :on_hold_time, :outzone_at
alias_attribute :force_delete_time, :force_delete_at
alias_attribute :outzone_time, :outzone_at
alias_attribute :delete_time, :delete_at
# TODO: whois requests ip whitelist for full info for own domains and partial info for other domains
# TODO: most inputs should be trimmed before validatation, probably some global logic?
@ -282,16 +286,6 @@ class Domain < ActiveRecord::Base
domain_transfers.find_by(status: DomainTransfer::PENDING)
end
def expirable?
return false if valid_to > Time.zone.now
if statuses.include?(DomainStatus::EXPIRED) && outzone_at.present? && delete_at.present?
return false
end
true
end
def server_holdable?
return false if statuses.include?(DomainStatus::SERVER_HOLD)
return false if statuses.include?(DomainStatus::SERVER_MANUAL_INZONE)
@ -308,7 +302,7 @@ class Domain < ActiveRecord::Base
def renewable?
if Setting.days_to_renew_domain_before_expire != 0
# if you can renew domain at days_to_renew before domain expiration
if (valid_to.to_date - Date.today) + 1 > Setting.days_to_renew_domain_before_expire
if (expire_time.to_date - Date.today) + 1 > Setting.days_to_renew_domain_before_expire
return false
end
end
@ -596,7 +590,7 @@ class Domain < ActiveRecord::Base
end
def set_graceful_expired
self.outzone_at = valid_to + self.class.expire_warning_period
self.outzone_at = expire_time + self.class.expire_warning_period
self.delete_at = outzone_at + self.class.redemption_grace_period
self.statuses |= [DomainStatus::EXPIRED]
end
@ -626,7 +620,7 @@ class Domain < ActiveRecord::Base
when DomainStatus::SERVER_MANUAL_INZONE # removal causes server hold to set
self.outzone_at = Time.zone.now if self.force_delete_at.present?
when DomainStatus::DomainStatus::EXPIRED # removal causes server hold to set
self.outzone_at = self.valid_to + 15.day
self.outzone_at = self.expire_time + 15.day
when DomainStatus::DomainStatus::SERVER_HOLD # removal causes server hold to set
self.outzone_at = nil
end
@ -765,5 +759,13 @@ class Domain < ActiveRecord::Base
def self.redemption_grace_period
Setting.redemption_grace_period.days
end
def self.outzone_candidates
where("#{attribute_alias(:outzone_time)} < ?", Time.zone.now)
end
def self.delete_candidates
where("#{attribute_alias(:delete_time)} < ?", Time.zone.now)
end
end
# rubocop: enable Metrics/ClassLength

View file

@ -35,7 +35,7 @@ class DomainCron
Rails.logger.info('Expiring domains')
::PaperTrail.whodunnit = "cron - #{__method__}"
domains = Domain.where('valid_to <= ?', Time.zone.now)
domains = Domain.expired
marked = 0
real = 0
domains.each do |domain|
@ -60,10 +60,12 @@ class DomainCron
STDOUT << "#{Time.zone.now.utc} - Setting server_hold to domains\n" unless Rails.env.test?
::PaperTrail.whodunnit = "cron - #{__method__}"
d = Domain.where('outzone_at <= ?', Time.zone.now)
domains = Domain.outzone_candidates
marked = 0
real = 0
d.each do |domain|
domains.each do |domain|
next unless domain.server_holdable?
real += 1
domain.statuses << DomainStatus::SERVER_HOLD
@ -103,16 +105,18 @@ class DomainCron
c = 0
Domain.where('delete_at <= ?', Time.zone.now.end_of_day.utc).each do |x|
next unless x.delete_candidateable?
domains = Domain.delete_candidates
x.statuses << DomainStatus::DELETE_CANDIDATE
domains.each do |domain|
next unless domain.delete_candidateable?
domain.statuses << DomainStatus::DELETE_CANDIDATE
# If domain successfully saved, add it to delete schedule
if x.save(validate: false)
if domain.save(validate: false)
::PaperTrail.whodunnit = "cron - #{__method__}"
DomainDeleteJob.enqueue(x.id, run_at: rand(((24*60) - (DateTime.now.hour * 60 + DateTime.now.minute))).minutes.from_now)
STDOUT << "#{Time.zone.now.utc} Domain.destroy_delete_candidates: job added by deleteCandidate status ##{x.id} (#{x.name})\n" unless Rails.env.test?
DomainDeleteJob.enqueue(domain.id, run_at: rand(((24*60) - (DateTime.now.hour * 60 + DateTime.now.minute))).minutes.from_now)
STDOUT << "#{Time.zone.now.utc} Domain.destroy_delete_candidates: job added by deleteCandidate status ##{domain.id} (#{domain.name})\n" unless Rails.env.test?
c += 1
end
end

View file

@ -0,0 +1,65 @@
require 'rails_helper'
RSpec.describe Domain, db: false do
it { is_expected.to alias_attribute(:expire_time, :valid_to) }
describe '::expired', db: true do
before :example do
travel_to Time.zone.parse('05.07.2010 00:00')
Fabricate(:zonefile_setting, origin: 'ee')
Fabricate.create(:domain, id: 1, expire_time: Time.zone.parse('04.07.2010 23:59'))
Fabricate.create(:domain, id: 2, expire_time: Time.zone.parse('05.07.2010 00:00'))
Fabricate.create(:domain, id: 3, expire_time: Time.zone.parse('05.07.2010 00:01'))
end
it 'returns expired domains' do
expect(described_class.expired.ids).to eq([1, 2])
end
end
describe '#registered?' do
let(:domain) { described_class.new }
context 'when not expired' do
before :example do
expect(domain).to receive(:expired?).and_return(false)
end
specify { expect(domain).to be_registered }
end
context 'when expired' do
before :example do
expect(domain).to receive(:expired?).and_return(true)
end
specify { expect(domain).to_not be_registered }
end
end
describe '#expired?' do
before :example do
travel_to Time.zone.parse('05.07.2010 00:00')
end
context 'when :expire_time is in the past' do
let(:domain) { described_class.new(expire_time: Time.zone.parse('04.07.2010 23:59')) }
specify { expect(domain).to be_expired }
end
context 'when :expire_time is now' do
let(:domain) { described_class.new(expire_time: Time.zone.parse('05.07.2010 00:00')) }
specify { expect(domain).to be_expired }
end
context 'when :expire_time is in the future' do
let(:domain) { described_class.new(expire_time: Time.zone.parse('05.07.2010 00:01')) }
specify { expect(domain).to_not be_expired }
end
end
end

View file

@ -2,25 +2,6 @@ require 'rails_helper'
RSpec.describe Domain do
before :example do
Setting.ds_algorithm = 2
Setting.ds_data_allowed = true
Setting.ds_data_with_key_allowed = true
Setting.key_data_allowed = true
Setting.dnskeys_min_count = 0
Setting.dnskeys_max_count = 9
Setting.ns_min_count = 2
Setting.ns_max_count = 11
Setting.transfer_wait_time = 0
Setting.admin_contacts_min_count = 1
Setting.admin_contacts_max_count = 10
Setting.tech_contacts_min_count = 0
Setting.tech_contacts_max_count = 10
Setting.client_side_status_editing_enabled = true
Fabricate(:zonefile_setting, origin: 'ee')
Fabricate(:zonefile_setting, origin: 'pri.ee')
Fabricate(:zonefile_setting, origin: 'med.ee')
@ -143,6 +124,38 @@ RSpec.describe Domain do
domain.pending_json.should == {}
end
it 'should expire domains' do
Setting.expire_warning_period = 1
Setting.redemption_grace_period = 1
DomainCron.start_expire_period
@domain.statuses.include?(DomainStatus::EXPIRED).should == false
old_valid_to = Time.zone.now - 10.days
@domain.valid_to = old_valid_to
@domain.save
DomainCron.start_expire_period
@domain.reload
@domain.statuses.include?(DomainStatus::EXPIRED).should == true
DomainCron.start_expire_period
@domain.reload
@domain.statuses.include?(DomainStatus::EXPIRED).should == true
end
it 'should start redemption grace period' do
old_valid_to = Time.zone.now - 10.days
@domain.valid_to = old_valid_to
@domain.statuses = [DomainStatus::EXPIRED]
@domain.outzone_at, @domain.delete_at = nil, nil
@domain.save
DomainCron.start_expire_period
@domain.reload
@domain.statuses.include?(DomainStatus::EXPIRED).should == true
end
it 'should start redemption grace period' do
DomainCron.start_redemption_grace_period
@domain.reload
@ -715,6 +728,7 @@ RSpec.describe Domain, db: false do
it { is_expected.to alias_attribute(:on_hold_time, :outzone_at) }
it { is_expected.to alias_attribute(:delete_time, :delete_at) }
it { is_expected.to alias_attribute(:force_delete_time, :force_delete_at) }
it { is_expected.to alias_attribute(:outzone_time, :outzone_at) }
describe '::expire_warning_period', db: true do
before :example do
@ -854,4 +868,36 @@ RSpec.describe Domain, db: false do
expect(domain.delete_at).to eq(Time.zone.parse('08.07.2010 10:30'))
end
end
describe '::outzone_candidates', db: true do
before :example do
travel_to Time.zone.parse('05.07.2010 00:00')
Fabricate(:zonefile_setting, origin: 'ee')
Fabricate.create(:domain, id: 1, outzone_time: Time.zone.parse('04.07.2010 23:59'))
Fabricate.create(:domain, id: 2, outzone_time: Time.zone.parse('05.07.2010 00:00'))
Fabricate.create(:domain, id: 3, outzone_time: Time.zone.parse('05.07.2010 00:01'))
end
it 'returns domains with outzone time in the past' do
expect(described_class.outzone_candidates.ids).to eq([1])
end
end
describe '::delete_candidates', db: true do
before :example do
travel_to Time.zone.parse('05.07.2010 00:00')
Fabricate(:zonefile_setting, origin: 'ee')
Fabricate.create(:domain, id: 1, delete_time: Time.zone.parse('04.07.2010 23:59'))
Fabricate.create(:domain, id: 2, delete_time: Time.zone.parse('05.07.2010 00:00'))
Fabricate.create(:domain, id: 3, delete_time: Time.zone.parse('05.07.2010 00:01'))
end
it 'returns domains with delete time in the past' do
expect(described_class.delete_candidates.ids).to eq([1])
end
end
end