Fix tests & interactor process

This commit is contained in:
Alex Sherman 2020-11-10 14:06:37 +05:00
parent 03754b542b
commit f97dff6002
14 changed files with 93 additions and 182 deletions

View file

@ -5,29 +5,12 @@ module Admin
authorize! :manage, domain authorize! :manage, domain
domain.transaction do domain.transaction do
# domain.schedule_force_delete(type: force_delete_type) domain.schedule_force_delete(type: force_delete_type, notify_by_email: notify_by_email?)
# domain.registrar.notifications.create!(text: t('force_delete_set_on_domain',
# domain_name: domain.name,
# outzone_date: domain.outzone_date,
# purge_date: domain.purge_date)) # added to interactor
#
# notify_by_email if notify_by_email? # added to interactor
Domain::ForceDelete::Base.call(domain: domain, type: force_delete_type)
end end
redirect_to edit_admin_domain_url(domain), notice: t('.scheduled') redirect_to edit_admin_domain_url(domain), notice: t('.scheduled')
end end
def notify_by_email
# added to interactor
if force_delete_type == :fast_track
send_email
domain.update(contact_notification_sent_date: Time.zone.today)
else
domain.update(template_name: domain.notification_template)
end
end
def destroy def destroy
authorize! :manage, domain authorize! :manage, domain
domain.cancel_force_delete domain.cancel_force_delete
@ -41,18 +24,9 @@ module Admin
end end
def notify_by_email? def notify_by_email?
# added to interactor
ActiveRecord::Type::Boolean.new.cast(params[:notify_by_email]) ActiveRecord::Type::Boolean.new.cast(params[:notify_by_email])
end end
def send_email
# added to interactor
DomainDeleteMailer.forced(domain: domain,
registrar: domain.registrar,
registrant: domain.registrant,
template_name: domain.notification_template).deliver_now
end
def force_delete_type def force_delete_type
soft_delete? ? :soft : :fast_track soft_delete? ? :soft : :fast_track
end end

View file

@ -1,16 +0,0 @@
class Domain
module ForceDelete
class Base
include Interactor::Organizer
# As per https://github.com/collectiveidea/interactor#organizers
organize Domain::ForceDelete::CheckDiscarded,
Domain::ForceDelete::PrepareDomain,
Domain::ForceDelete::SetStatus,
Domain::ForceDelete::PostSetProcess,
Domain::ForceDelete::NotifyRegistrar,
Domain::ForceDelete::NotifyByEmail
end
end
end

View file

@ -1,31 +0,0 @@
class Domain
module ForceDelete
class NotifyByEmail
include Interactor
def call
return unless notify_by_email?
if context.type == :fast_track
send_email
context.domain.update(contact_notification_sent_date: Time.zone.today)
else
context.domain.update(template_name: context.domain.notification_template)
end
end
private
def notify_by_email?
ActiveRecord::Type::Boolean.new.cast(params[:notify_by_email])
end
def send_email
DomainDeleteMailer.forced(domain: context.domain,
registrar: context.domain.registrar,
registrant: context.domain.registrant,
template_name: context.domain.notification_template).deliver_now
end
end
end
end

View file

@ -1,16 +0,0 @@
class Domain
module ForceDelete
class NotifyRegistrar
include Interactor
def call
domain = context.domain
domain.registrar.notifications.create!(text: t('force_delete_set_on_domain',
domain_name: domain.name,
outzone_date: domain.outzone_date,
purge_date: domain.purge_date))
end
end
end
end

View file

@ -0,0 +1,16 @@
class Domain
module ForceDeleteInteractor
class Base
include Interactor::Organizer
# As per https://github.com/collectiveidea/interactor#organizers
organize Domain::ForceDeleteInteractor::CheckDiscarded,
Domain::ForceDeleteInteractor::PrepareDomain,
Domain::ForceDeleteInteractor::SetStatus,
Domain::ForceDeleteInteractor::PostSetProcess,
Domain::ForceDeleteInteractor::NotifyRegistrar,
Domain::ForceDeleteInteractor::NotifyByEmail
end
end
end

View file

@ -1,5 +1,5 @@
class Domain class Domain
module ForceDelete module ForceDeleteInteractor
class CheckDiscarded class CheckDiscarded
include Interactor include Interactor
@ -7,7 +7,7 @@ class Domain
return unless context.domain.discarded? return unless context.domain.discarded?
message = 'Force delete procedure cannot be scheduled while a domain is discarded' message = 'Force delete procedure cannot be scheduled while a domain is discarded'
context.fail!( message: message ) context.fail!(message: message)
end end
end end
end end

View file

@ -0,0 +1,31 @@
class Domain
module ForceDeleteInteractor
class NotifyByEmail
include Interactor
def call
return unless context.notify_by_email
if context.type == :fast_track
send_email
domain.update(contact_notification_sent_date: Time.zone.today)
else
domain.update(template_name: context.domain.notification_template)
end
end
private
def domain
@domain ||= context.domain
end
def send_email
DomainDeleteMailer.forced(domain: domain,
registrar: domain.registrar,
registrant: domain.registrant,
template_name: domain.notification_template).deliver_now
end
end
end
end

View file

@ -0,0 +1,16 @@
class Domain
module ForceDeleteInteractor
class NotifyRegistrar
include Interactor
def call
domain = context.domain
domain.registrar.notifications.create!(text: I18n.t('force_delete_set_on_domain',
domain_name: domain.name,
outzone_date: domain.outzone_date,
purge_date: domain.purge_date))
end
end
end
end

View file

@ -1,5 +1,5 @@
class Domain class Domain
module ForceDelete module ForceDeleteInteractor
class PostSetProcess class PostSetProcess
include Interactor include Interactor

View file

@ -1,5 +1,5 @@
class Domain class Domain
module ForceDelete module ForceDeleteInteractor
class PrepareDomain class PrepareDomain
include Interactor include Interactor

View file

@ -1,5 +1,5 @@
class Domain class Domain
module ForceDelete module ForceDeleteInteractor
class SetStatus class SetStatus
include Interactor include Interactor
@ -9,23 +9,20 @@ class Domain
domain.save(validate: false) domain.save(validate: false)
end end
private
def domain def domain
@domain |= context.domain @domain ||= context.domain
end end
def force_delete_fast_track def force_delete_fast_track
domain.force_delete_date = Time.zone.today + domain.force_delete_date = Time.zone.today +
Setting.expire_warning_period.days + expire_warning_period_days +
Setting.redemption_grace_period.days + redemption_grace_period_days
1.day
domain.force_delete_start = Time.zone.today + 1.day domain.force_delete_start = Time.zone.today + 1.day
end end
def force_delete_soft def force_delete_soft
years = (valid_to.to_date - Time.zone.today).to_i / 365 years = (domain.valid_to.to_date - Time.zone.today).to_i / 365
soft_forcedelete_dates(years) soft_forcedelete_dates(years) if years.positive?
end end
def soft_forcedelete_dates(years) def soft_forcedelete_dates(years)
@ -34,6 +31,16 @@ class Domain
Setting.expire_warning_period.days + Setting.expire_warning_period.days +
Setting.redemption_grace_period.days Setting.redemption_grace_period.days
end end
private
def redemption_grace_period_days
Setting.redemption_grace_period.days + 1.day
end
def expire_warning_period_days
Setting.expire_warning_period.days
end
end end
end end
end end

View file

@ -52,38 +52,10 @@ module Concerns::Domain::ForceDelete # rubocop:disable Metrics/ModuleLength
force_delete_start + Setting.expire_warning_period.days <= valid_to force_delete_start + Setting.expire_warning_period.days <= valid_to
end end
def schedule_force_delete(type: :fast_track) def schedule_force_delete(type: :fast_track, notify_by_email: false)
# added to interactor Domain::ForceDeleteInteractor::Base.call(domain: self,
if discarded? type: type,
raise StandardError, 'Force delete procedure cannot be scheduled while a domain is discarded' notify_by_email: notify_by_email)
end
type == :fast_track ? force_delete_fast_track : force_delete_soft
end
def add_force_delete_type(force_delete_type)
self.force_delete_type = force_delete_type
end
def force_delete_fast_track
preserve_current_statuses_for_force_delete
add_force_delete_statuses
add_force_delete_type(:fast)
self.force_delete_date = force_delete_fast_track_start_date + 1.day # added to interactor
self.force_delete_start = Time.zone.today + 1.day # added to interactor
stop_all_pending_actions
allow_deletion
save(validate: false)
end
def force_delete_soft
preserve_current_statuses_for_force_delete
add_force_delete_statuses
add_force_delete_type(:soft)
calculate_soft_delete_date
stop_all_pending_actions
allow_deletion
save(validate: false)
end end
def clear_force_delete_data def clear_force_delete_data
@ -111,58 +83,15 @@ module Concerns::Domain::ForceDelete # rubocop:disable Metrics/ModuleLength
private private
def calculate_soft_delete_date
years = (valid_to.to_date - Time.zone.today).to_i / 365
soft_delete_dates(years) if years.positive?
end
def soft_delete_dates(years)
# added to interactor
self.force_delete_start = valid_to - years.years
self.force_delete_date = force_delete_start + Setting.expire_warning_period.days +
Setting.redemption_grace_period.days
end
def stop_all_pending_actions
# added to interactor
statuses.delete(DomainStatus::PENDING_UPDATE)
statuses.delete(DomainStatus::PENDING_TRANSFER)
statuses.delete(DomainStatus::PENDING_RENEW)
statuses.delete(DomainStatus::PENDING_CREATE)
end
def preserve_current_statuses_for_force_delete
# added to interactor
update(statuses_before_force_delete: statuses)
end
def restore_statuses_before_force_delete def restore_statuses_before_force_delete
self.statuses = statuses_before_force_delete self.statuses = statuses_before_force_delete
self.statuses_before_force_delete = nil self.statuses_before_force_delete = nil
end end
def add_force_delete_statuses
# added to interactor
self.statuses |= [DomainStatus::FORCE_DELETE,
DomainStatus::SERVER_RENEW_PROHIBITED,
DomainStatus::SERVER_TRANSFER_PROHIBITED]
end
def remove_force_delete_statuses def remove_force_delete_statuses
statuses.delete(DomainStatus::FORCE_DELETE) statuses.delete(DomainStatus::FORCE_DELETE)
statuses.delete(DomainStatus::SERVER_RENEW_PROHIBITED) statuses.delete(DomainStatus::SERVER_RENEW_PROHIBITED)
statuses.delete(DomainStatus::SERVER_TRANSFER_PROHIBITED) statuses.delete(DomainStatus::SERVER_TRANSFER_PROHIBITED)
statuses.delete(DomainStatus::CLIENT_HOLD) statuses.delete(DomainStatus::CLIENT_HOLD)
end end
def allow_deletion
# added to interactor
statuses.delete(DomainStatus::CLIENT_DELETE_PROHIBITED)
statuses.delete(DomainStatus::SERVER_DELETE_PROHIBITED)
end
def force_delete_fast_track_start_date
# added to interactor
Time.zone.today + Setting.expire_warning_period.days + Setting.redemption_grace_period.days
end
end end

View file

@ -111,9 +111,10 @@ class NewDomainForceDeleteTest < ActiveSupport::TestCase
def test_force_delete_cannot_be_scheduled_when_a_domain_is_discarded def test_force_delete_cannot_be_scheduled_when_a_domain_is_discarded
@domain.update!(statuses: [DomainStatus::DELETE_CANDIDATE]) @domain.update!(statuses: [DomainStatus::DELETE_CANDIDATE])
assert_raises StandardError do context = Domain::ForceDeleteInteractor::Base.call(domain: @domain, type: :fast_track)
@domain.schedule_force_delete(type: :fast_track)
end assert_not context.success?
assert_equal 'Force delete procedure cannot be scheduled while a domain is discarded', context.message
end end
def test_cancels_force_delete def test_cancels_force_delete

View file

@ -414,7 +414,7 @@ class DomainTest < ActiveSupport::TestCase
force_delete_date: nil) force_delete_date: nil)
@domain.update(template_name: 'legal_person') @domain.update(template_name: 'legal_person')
travel_to Time.zone.parse('2010-07-05') travel_to Time.zone.parse('2010-07-05')
@domain.schedule_force_delete(type: :fast_track) Domain::ForceDeleteInteractor::Base.call(domain: @domain, type: :fast_track)
assert(@domain.force_delete_scheduled?) assert(@domain.force_delete_scheduled?)
other_registrant = Registrant.find_by(code: 'jane-001') other_registrant = Registrant.find_by(code: 'jane-001')
@domain.pending_json['new_registrant_id'] = other_registrant.id @domain.pending_json['new_registrant_id'] = other_registrant.id