107821878-refactoring

This commit is contained in:
Stas 2015-11-20 18:27:00 +02:00
parent 90ad9ff887
commit 99a66259f0
9 changed files with 46 additions and 46 deletions

View file

@ -9,7 +9,7 @@ class DomainDeleteConfirmJob < Que::Job
domain.apply_pending_delete! domain.apply_pending_delete!
domain.clean_pendings! domain.clean_pendings!
when RegistrantVerification::REJECTED when RegistrantVerification::REJECTED
DomainMailer.pending_delete_rejected_notification(domain_id).deliver DomainMailer.pending_delete_rejected_notification(domain_id, deliver_emails).deliver
domain.statuses.delete(DomainStatus::PENDING_DELETE_CONFIRMATION) domain.statuses.delete(DomainStatus::PENDING_DELETE_CONFIRMATION)
domain.poll_message!(:poll_pending_delete_rejected_by_registrant) domain.poll_message!(:poll_pending_delete_rejected_by_registrant)
domain.clean_pendings! domain.clean_pendings!

View file

@ -17,8 +17,8 @@ class ApplicationMailer < ActionMailer::Base
end end
# turn on delivery on specific (epp) request only, thus rake tasks does not deliver anything # turn on delivery on specific (epp) request only, thus rake tasks does not deliver anything
def delivery_off?(model) def delivery_off?(model, deliver_email= false)
return false if model.deliver_emails == true return false if deliver_emails == true
logger.info "EMAIL SENDING WAS NOT ACTIVATED " \ logger.info "EMAIL SENDING WAS NOT ACTIVATED " \
"BY MODEL OBJECT: id ##{model.try(:id)} deliver_emails returned false" "BY MODEL OBJECT: id ##{model.try(:id)} deliver_emails returned false"
true true

View file

@ -1,10 +1,10 @@
class ContactMailer < ApplicationMailer class ContactMailer < ApplicationMailer
include Que::Mailer include Que::Mailer
def email_updated(email, contact_id) def email_updated(email, contact_id, should_deliver)
@contact = Contact.find_by(id: contact_id) @contact = Contact.find_by(id: contact_id)
return unless email || @contact return unless email || @contact
return if delivery_off?(contact) return if delivery_off?(contact, should_deliver)
return if whitelist_blocked?(email) return if whitelist_blocked?(email)
begin begin

View file

@ -1,10 +1,10 @@
class DomainMailer < ApplicationMailer class DomainMailer < ApplicationMailer
include Que::Mailer include Que::Mailer
def pending_update_request_for_old_registrant(domain_id) def pending_update_request_for_old_registrant(domain_id, should_deliver)
@domain = Domain.find_by(id: domain_id) @domain = Domain.find_by(id: domain_id)
return unless @domain return unless @domain
return if delivery_off?(@domain) return if delivery_off?(@domain, should_deliver)
if @domain.registrant_verification_token.blank? if @domain.registrant_verification_token.blank?
logger.warn "EMAIL NOT DELIVERED: registrant_verification_token is missing for #{@domain.name}" logger.warn "EMAIL NOT DELIVERED: registrant_verification_token is missing for #{@domain.name}"
@ -27,10 +27,10 @@ class DomainMailer < ApplicationMailer
name: @domain.name)} [#{@domain.name}]") name: @domain.name)} [#{@domain.name}]")
end end
def pending_update_notification_for_new_registrant(domain_id) def pending_update_notification_for_new_registrant(domain_id, should_deliver)
@domain = Domain.find_by(id: domain_id) @domain = Domain.find_by(id: domain_id)
return unless @domain return unless @domain
return if delivery_off?(@domain) return if delivery_off?(@domain, should_deliver)
if @domain.registrant_verification_token.blank? if @domain.registrant_verification_token.blank?
logger.warn "EMAIL NOT DELIVERED: registrant_verification_token is missing for #{@domain.name}" logger.warn "EMAIL NOT DELIVERED: registrant_verification_token is missing for #{@domain.name}"
@ -51,10 +51,10 @@ class DomainMailer < ApplicationMailer
name: @domain.name)} [#{@domain.name}]") name: @domain.name)} [#{@domain.name}]")
end end
def registrant_updated_notification_for_new_registrant(domain_id) def registrant_updated_notification_for_new_registrant(domain_id, should_deliver)
@domain = Domain.find_by(id: domain_id) @domain = Domain.find_by(id: domain_id)
return unless @domain return unless @domain
return if delivery_off?(@domain) return if delivery_off?(@domain, should_deliver)
return if whitelist_blocked?(@domain.registrant_email) return if whitelist_blocked?(@domain.registrant_email)
mail(to: format(@domain.registrant_email), mail(to: format(@domain.registrant_email),
@ -62,10 +62,10 @@ class DomainMailer < ApplicationMailer
name: @domain.name)} [#{@domain.name}]") name: @domain.name)} [#{@domain.name}]")
end end
def registrant_updated_notification_for_old_registrant(domain_id) def registrant_updated_notification_for_old_registrant(domain_id, should_deliver)
domain = Domain.find_by(id: domain_id) domain = Domain.find_by(id: domain_id)
return unless domain return unless domain
return if delivery_off?(@domain) return if delivery_off?(@domain, should_deliver)
@old_registrant_email = domain.registrant_email # Nb! before applying pending updates @old_registrant_email = domain.registrant_email # Nb! before applying pending updates
@ -107,10 +107,10 @@ class DomainMailer < ApplicationMailer
name: @domain.name)} [#{@domain.name}]") name: @domain.name)} [#{@domain.name}]")
end end
def pending_deleted(domain_id) def pending_deleted(domain_id, should_deliver)
@domain = Domain.find_by(id: domain_id) @domain = Domain.find_by(id: domain_id)
return unless @domain return unless @domain
return if delivery_off?(@domain) return if delivery_off?(@domain, should_deliver)
if @domain.registrant_verification_token.blank? if @domain.registrant_verification_token.blank?
logger.warn "EMAIL NOT DELIVERED: registrant_verification_token is missing for #{@domain.name}" logger.warn "EMAIL NOT DELIVERED: registrant_verification_token is missing for #{@domain.name}"
@ -133,10 +133,10 @@ class DomainMailer < ApplicationMailer
name: @domain.name)} [#{@domain.name}]") name: @domain.name)} [#{@domain.name}]")
end end
def pending_delete_rejected_notification(domain_id) def pending_delete_rejected_notification(domain_id, should_deliver)
@domain = Domain.find_by(id: domain_id) @domain = Domain.find_by(id: domain_id)
return unless @domain return unless @domain
return if delivery_off?(@domain) return if delivery_off?(@domain, should_deliver)
# no delivery off control, driggered by que, no epp request # no delivery off control, driggered by que, no epp request
if @domain.registrant_verification_token.blank? if @domain.registrant_verification_token.blank?
@ -155,10 +155,10 @@ class DomainMailer < ApplicationMailer
name: @domain.name)} [#{@domain.name}]") name: @domain.name)} [#{@domain.name}]")
end end
def pending_delete_expired_notification(domain_id) def pending_delete_expired_notification(domain_id, should_deliver)
@domain = Domain.find_by(id: domain_id) @domain = Domain.find_by(id: domain_id)
return unless @domain return unless @domain
return if delivery_off?(@domain) return if delivery_off?(@domain, should_deliver)
# no delivery off control, driggered by cron, no epp request # no delivery off control, driggered by cron, no epp request
return if whitelist_blocked?(@domain.registrant.email) return if whitelist_blocked?(@domain.registrant.email)
@ -167,10 +167,10 @@ class DomainMailer < ApplicationMailer
name: @domain.name)} [#{@domain.name}]") name: @domain.name)} [#{@domain.name}]")
end end
def delete_confirmation(domain_id) def delete_confirmation(domain_id, should_deliver)
@domain = Domain.find_by(id: domain_id) @domain = Domain.find_by(id: domain_id)
return unless @domain return unless @domain
return if delivery_off?(@domain) return if delivery_off?(@domain, should_deliver)
return if whitelist_blocked?(@domain.registrant.email) return if whitelist_blocked?(@domain.registrant.email)
mail(to: format(@domain.registrant.email), mail(to: format(@domain.registrant.email),
@ -178,9 +178,9 @@ class DomainMailer < ApplicationMailer
name: @domain.name)} [#{@domain.name}]") name: @domain.name)} [#{@domain.name}]")
end end
def force_delete(domain_id) def force_delete(domain_id, should_deliver)
@domain = Domain.find_by(id: domain_id) @domain = Domain.find_by(id: domain_id)
return if delivery_off?(@domain) return if delivery_off?(@domain, should_deliver)
emails = ([@domain.registrant.email] + @domain.admin_contacts.map { |x| format(x.email) }).uniq emails = ([@domain.registrant.email] + @domain.admin_contacts.map { |x| format(x.email) }).uniq
return if whitelist_blocked?(emails) return if whitelist_blocked?(emails)

View file

@ -50,7 +50,7 @@ class Contact < ActiveRecord::Base
emails << domains.map(&:registrant_email) if domains.present? emails << domains.map(&:registrant_email) if domains.present?
emails = emails.flatten.uniq emails = emails.flatten.uniq
emails.each do |e| emails.each do |e|
ContactMailer.email_updated(e, id).deliver ContactMailer.email_updated(e, id, deliver_emails).deliver
end end
end end

View file

@ -241,7 +241,7 @@ class Domain < ActiveRecord::Base
DomainMailer.pending_update_expired_notification_for_new_registrant(id).deliver DomainMailer.pending_update_expired_notification_for_new_registrant(id).deliver
end end
if domain.pending_delete? || domain.pending_delete_confirmation? if domain.pending_delete? || domain.pending_delete_confirmation?
DomainMailer.pending_delete_expired_notification(id).deliver DomainMailer.pending_delete_expired_notification(id, deliver_emails).deliver
end end
domain.clean_pendings! domain.clean_pendings!
unless Rails.env.test? unless Rails.env.test?
@ -428,8 +428,8 @@ class Domain < ActiveRecord::Base
new_registrant_email = registrant.email new_registrant_email = registrant.email
new_registrant_name = registrant.name new_registrant_name = registrant.name
DomainMailer.pending_update_request_for_old_registrant(id).deliver DomainMailer.pending_update_request_for_old_registrant(id, deliver_emails).deliver
DomainMailer.pending_update_notification_for_new_registrant(id).deliver DomainMailer.pending_update_notification_for_new_registrant(id, deliver_emails).deliver
reload # revert back to original reload # revert back to original
@ -489,7 +489,7 @@ class Domain < ActiveRecord::Base
pending_delete_confirmation! pending_delete_confirmation!
save(validate: false) # should check if this did succeed save(validate: false) # should check if this did succeed
DomainMailer.pending_deleted(id).deliver DomainMailer.pending_deleted(id, deliver_emails).deliver
end end
def pricelist(operation, period_i = nil, unit = nil) def pricelist(operation, period_i = nil, unit = nil)
@ -619,7 +619,7 @@ class Domain < ActiveRecord::Base
registrar.messages.create!( registrar.messages.create!(
body: I18n.t('force_delete_set_on_domain', domain: name) body: I18n.t('force_delete_set_on_domain', domain: name)
) )
DomainMailer.force_delete(id).deliver DomainMailer.force_delete(id, deliver_emails).deliver
return true return true
end end
false false

View file

@ -478,7 +478,7 @@ class Epp::Domain < Domain
# rubocop: enable Metrics/CyclomaticComplexity # rubocop: enable Metrics/CyclomaticComplexity
def apply_pending_update! def apply_pending_update!
old_registrant_email = DomainMailer.registrant_updated_notification_for_old_registrant(id) old_registrant_email = DomainMailer.registrant_updated_notification_for_old_registrant(id, deliver_emails)
preclean_pendings preclean_pendings
user = ApiUser.find(pending_json['current_user_id']) user = ApiUser.find(pending_json['current_user_id'])
frame = Nokogiri::XML(pending_json['frame']) frame = Nokogiri::XML(pending_json['frame'])
@ -488,7 +488,7 @@ class Epp::Domain < Domain
return unless update(frame, user, false) return unless update(frame, user, false)
clean_pendings! clean_pendings!
self.deliver_emails = true # turn on email delivery self.deliver_emails = true # turn on email delivery
DomainMailer.registrant_updated_notification_for_new_registrant(id).deliver DomainMailer.registrant_updated_notification_for_new_registrant(id, deliver_emails).deliver
old_registrant_email.deliver old_registrant_email.deliver
true true
end end
@ -497,7 +497,7 @@ class Epp::Domain < Domain
preclean_pendings preclean_pendings
statuses.delete(DomainStatus::PENDING_DELETE_CONFIRMATION) statuses.delete(DomainStatus::PENDING_DELETE_CONFIRMATION)
statuses.delete(DomainStatus::PENDING_DELETE) statuses.delete(DomainStatus::PENDING_DELETE)
DomainMailer.delete_confirmation(id).deliver DomainMailer.delete_confirmation(id, deliver_emails).deliver
# TODO: confirm that this actually makes sense # TODO: confirm that this actually makes sense
clean_pendings! if valid? && set_pending_delete! clean_pendings! if valid? && set_pending_delete!

View file

@ -8,7 +8,7 @@ describe ContactMailer do
describe 'email changed notification when delivery turned off' do describe 'email changed notification when delivery turned off' do
before :all do before :all do
@contact = Fabricate(:contact, email: 'test@example.ee') @contact = Fabricate(:contact, email: 'test@example.ee')
@mail = ContactMailer.email_updated('test@example.com', @contact.id) @mail = ContactMailer.email_updated('test@example.com', @contact.id, deliver_emails)
end end
it 'should not render email subject' do it 'should not render email subject' do
@ -34,7 +34,7 @@ describe ContactMailer do
@contact = @domain.registrant @contact = @domain.registrant
@contact.reload # until figured out why registrant_domains not loaded @contact.reload # until figured out why registrant_domains not loaded
@contact.deliver_emails = true @contact.deliver_emails = true
@mail = ContactMailer.email_updated('info@example.org', @contact.id) @mail = ContactMailer.email_updated('info@example.org', @contact.id, deliver_emails)
end end
it 'should render email subject' do it 'should render email subject' do
@ -60,7 +60,7 @@ describe ContactMailer do
@contact = @domain.registrant @contact = @domain.registrant
@contact.reload # until figured out why registrant_domains not loaded @contact.reload # until figured out why registrant_domains not loaded
@contact.deliver_emails = true @contact.deliver_emails = true
@mail = ContactMailer.email_updated('info@ääöü.org', @contact.id) @mail = ContactMailer.email_updated('info@ääöü.org', @contact.id, deliver_emails)
end end
it 'should render email subject' do it 'should render email subject' do

View file

@ -9,7 +9,7 @@ describe DomainMailer do
before :all do before :all do
@registrant = Fabricate(:registrant, email: 'test@example.com') @registrant = Fabricate(:registrant, email: 'test@example.com')
@domain = Fabricate(:domain, registrant: @registrant) @domain = Fabricate(:domain, registrant: @registrant)
@mail = DomainMailer.pending_update_request_for_old_registrant(@domain.id) @mail = DomainMailer.pending_update_request_for_old_registrant(@domain.id, deliver_emails)
end end
it 'should not render email subject' do it 'should not render email subject' do
@ -38,7 +38,7 @@ describe DomainMailer do
@domain.registrant_verification_token = '123' @domain.registrant_verification_token = '123'
@domain.registrant_verification_asked_at = Time.zone.now @domain.registrant_verification_asked_at = Time.zone.now
@domain.registrant = @new_registrant @domain.registrant = @new_registrant
@mail = DomainMailer.pending_update_request_for_old_registrant(@domain.id) @mail = DomainMailer.pending_update_request_for_old_registrant(@domain.id, deliver_emails)
end end
it 'should render email subject' do it 'should render email subject' do
@ -71,7 +71,7 @@ describe DomainMailer do
@domain.registrant_verification_token = '123' @domain.registrant_verification_token = '123'
@domain.registrant_verification_asked_at = Time.zone.now @domain.registrant_verification_asked_at = Time.zone.now
@domain.registrant = @new_registrant @domain.registrant = @new_registrant
@mail = DomainMailer.pending_update_notification_for_new_registrant(@domain.id) @mail = DomainMailer.pending_update_notification_for_new_registrant(@domain.id, deliver_emails)
end end
it 'should render email subject' do it 'should render email subject' do
@ -100,7 +100,7 @@ describe DomainMailer do
@domain.registrant_verification_token = '123' @domain.registrant_verification_token = '123'
@domain.registrant_verification_asked_at = Time.zone.now @domain.registrant_verification_asked_at = Time.zone.now
@domain.registrant = @new_registrant @domain.registrant = @new_registrant
@mail = DomainMailer.pending_update_notification_for_new_registrant(@domain.id) @mail = DomainMailer.pending_update_notification_for_new_registrant(@domain.id, deliver_emails)
end end
it 'should render email subject' do it 'should render email subject' do
@ -153,7 +153,7 @@ describe DomainMailer do
@registrant = Fabricate(:registrant, email: 'test@example.com') @registrant = Fabricate(:registrant, email: 'test@example.com')
@domain = Fabricate(:domain, registrant: @registrant) @domain = Fabricate(:domain, registrant: @registrant)
@domain.deliver_emails = true @domain.deliver_emails = true
@mail = DomainMailer.registrant_updated_notification_for_new_registrant(@domain.id) @mail = DomainMailer.registrant_updated_notification_for_new_registrant(@domain.id, deliver_emails)
end end
it 'should render email subject' do it 'should render email subject' do
@ -178,7 +178,7 @@ describe DomainMailer do
@registrant = Fabricate(:registrant, email: 'test@example.com') @registrant = Fabricate(:registrant, email: 'test@example.com')
@domain = Fabricate(:domain, registrant: @registrant) @domain = Fabricate(:domain, registrant: @registrant)
@domain.deliver_emails = true @domain.deliver_emails = true
@mail = DomainMailer.registrant_updated_notification_for_old_registrant(@domain.id) @mail = DomainMailer.registrant_updated_notification_for_old_registrant(@domain.id, deliver_emails)
end end
it 'should render email subject' do it 'should render email subject' do
@ -202,7 +202,7 @@ describe DomainMailer do
before :all do before :all do
@registrant = Fabricate(:registrant, email: 'test@example.com') @registrant = Fabricate(:registrant, email: 'test@example.com')
@domain = Fabricate(:domain, registrant: @registrant) @domain = Fabricate(:domain, registrant: @registrant)
@mail = DomainMailer.pending_deleted(@domain.id) @mail = DomainMailer.pending_deleted(@domain.id, deliver_emails)
end end
it 'should not render email subject' do it 'should not render email subject' do
@ -229,7 +229,7 @@ describe DomainMailer do
@domain.deliver_emails = true @domain.deliver_emails = true
@domain.registrant_verification_token = '123' @domain.registrant_verification_token = '123'
@domain.registrant_verification_asked_at = Time.zone.now @domain.registrant_verification_asked_at = Time.zone.now
@mail = DomainMailer.pending_deleted(@domain.id) @mail = DomainMailer.pending_deleted(@domain.id, deliver_emails)
end end
it 'should render email subject' do it 'should render email subject' do
@ -260,7 +260,7 @@ describe DomainMailer do
@domain.deliver_emails = true @domain.deliver_emails = true
@domain.registrant_verification_token = '123' @domain.registrant_verification_token = '123'
@domain.registrant_verification_asked_at = Time.zone.now @domain.registrant_verification_asked_at = Time.zone.now
@mail = DomainMailer.pending_delete_rejected_notification(@domain.id) @mail = DomainMailer.pending_delete_rejected_notification(@domain.id, deliver_emails)
end end
it 'should render email subject' do it 'should render email subject' do
@ -287,7 +287,7 @@ describe DomainMailer do
@domain.deliver_emails = true @domain.deliver_emails = true
@domain.registrant_verification_token = '123' @domain.registrant_verification_token = '123'
@domain.registrant_verification_asked_at = Time.zone.now @domain.registrant_verification_asked_at = Time.zone.now
@mail = DomainMailer.pending_delete_expired_notification(@domain.id) @mail = DomainMailer.pending_delete_expired_notification(@domain.id, deliver_emails)
end end
it 'should render email subject' do it 'should render email subject' do
@ -314,7 +314,7 @@ describe DomainMailer do
@domain.deliver_emails = true @domain.deliver_emails = true
@domain.registrant_verification_token = '123' @domain.registrant_verification_token = '123'
@domain.registrant_verification_asked_at = Time.zone.now @domain.registrant_verification_asked_at = Time.zone.now
@mail = DomainMailer.delete_confirmation(@domain.id) @mail = DomainMailer.delete_confirmation(@domain.id, deliver_emails)
end end
it 'should render email subject' do it 'should render email subject' do