Skip to content

Commit

Permalink
Refactor domain mailer
Browse files Browse the repository at this point in the history
- Remove `Que::Mailer` (#895)
- Add preview
- DRY templates
- Add tests
- Extract translations
  • Loading branch information
Artur Beljajev committed Apr 14, 2019
1 parent 6fa1ce9 commit a64726a
Show file tree
Hide file tree
Showing 22 changed files with 211 additions and 108 deletions.
6 changes: 3 additions & 3 deletions app/jobs/domain_delete_confirm_email_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ def run(domain_id)
domain = Domain.find(domain_id)

log(domain)
DomainDeleteMailer.confirmation(domain: domain,
registrar: domain.registrar,
registrant: domain.registrant).deliver_now
DomainDeleteMailer.confirmation_request(domain: domain,
registrar: domain.registrar,
registrant: domain.registrant).deliver_now
end

private
Expand Down
8 changes: 7 additions & 1 deletion app/jobs/domain_delete_confirm_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ def run(domain_id, action, initiator = nil)
domain.save(validate: false)
raise_errors!(domain)

DomainMailer.pending_delete_rejected_notification(domain_id, true).deliver
if domain.registrant_verification_token.blank?
Rails.logger.warn "EMAIL NOT DELIVERED: registrant_verification_token is missing for #{domain.name}"
elsif domain.registrant_verification_asked_at.blank?
Rails.logger.warn "EMAIL NOT DELIVERED: registrant_verification_asked_at is missing for #{domain.name}"
else
DomainDeleteMailer.rejected(domain).deliver_now
end
end

destroy # it's best to destroy the job in the same transaction
Expand Down
23 changes: 22 additions & 1 deletion app/mailers/domain_delete_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def self.force_delete_templates
%w[private_person legal_person]
end

def confirmation(domain:, registrar:, registrant:)
def confirmation_request(domain:, registrar:, registrant:)
@domain = DomainPresenter.new(domain: domain, view: view_context)
@registrar = RegistrarPresenter.new(registrar: registrar, view: view_context)
@confirmation_url = confirmation_url(domain)
Expand All @@ -12,6 +12,27 @@ def confirmation(domain:, registrar:, registrant:)
mail(to: registrant.email, subject: subject)
end

def accepted(domain)
@domain = domain

subject = default_i18n_subject(domain: domain.name)
mail(to: domain.registrant.email, subject: subject)
end

def rejected(domain)
@domain = domain

subject = default_i18n_subject(domain: domain.name)
mail(to: domain.registrant.email, subject: subject)
end

def expired(domain)
@domain = domain

subject = default_i18n_subject(domain: domain.name)
mail(to: domain.registrant.email, subject: subject)
end

def forced(domain:, registrar:, registrant:, template_name:)
@domain = DomainPresenter.new(domain: domain, view: view_context)
@registrar = RegistrarPresenter.new(registrar: registrar, view: view_context)
Expand Down
45 changes: 0 additions & 45 deletions app/mailers/domain_mailer.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/models/domain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def statuses_uniqueness
errors.add(:statuses, :taken)
end

attr_accessor :registrant_typeahead, :update_me, :deliver_emails,
attr_accessor :registrant_typeahead, :update_me,
:epp_pending_update, :epp_pending_delete, :reserved_pw

def subordinate_nameservers
Expand Down
2 changes: 1 addition & 1 deletion app/models/domain_cron.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def self.clean_expired_pendings
RegistrantChangeExpiredEmailJob.enqueue(domain.id)
end
if domain.pending_delete? || domain.pending_delete_confirmation?
DomainMailer.pending_delete_expired_notification(domain.id, true).deliver
DomainDeleteMailer.expired(domain).deliver_now
end

domain.preclean_pendings
Expand Down
4 changes: 1 addition & 3 deletions app/models/epp/domain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,6 @@ def update(frame, current_user, verify = true)
registrant_verification_asked!(frame.to_s, current_user.id)
end

self.deliver_emails = true # turn on email delivery for epp

errors.empty? && super(at)
end

Expand All @@ -511,7 +509,7 @@ def apply_pending_delete!
preclean_pendings
statuses.delete(DomainStatus::PENDING_DELETE_CONFIRMATION)
statuses.delete(DomainStatus::PENDING_DELETE)
DomainMailer.delete_confirmation(id, deliver_emails).deliver
DomainDeleteMailer.accepted(self).deliver_now
clean_pendings!
set_pending_delete!
true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
Tere
<br><br>
Domeeni <%= @domain.name %> kustutamise taotlus on registreerija poolt kinnitatud. Domeen <%= @domain.name %> on peatatud ja kustub registrist.
<br><br>
Lugupidamisega<br>
Eesti Interneti Sihtasutus
<br><br>
<%= render 'mailers/shared/signatures/signature.et.html' %>
<hr>
<br><br>
Hi,
<br><br>
Domain <%= @domain.name %> deletion is confirmed by the registrant. Domain <%= @domain.name %> is suspended and will be deleted.
<br><br>
Best Regards,<br>
Estonian Internet Foundation
<%= render 'mailers/shared/signatures/signature.en.html' %>
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ Tere

Domeeni <%= @domain.name %> kustutamise taotlus on registreerija poolt kinnitatud. Domeen <%= @domain.name %> on peatatud ja kustub registrist.

Lugupidamisega
Eesti Interneti Sihtasutus

<%= render 'mailers/shared/signatures/signature.et.text' %>
--------------------------------------

Hi,

Domain <%= @domain.name %> deletion is confirmed by the registrant. Domain <%= @domain.name %> is suspended and will be deleted.

Best Regards,
Estonian Internet Foundation
<%= render 'mailers/shared/signatures/signature.en.text' %>
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
Tere
<br><br>
Domeeni <%= @domain.name %> kustutamise taotlust ei kinnitatud tähtaegselt registreerija <%= @domain.registrant.name %> poolt. Domeeni <%= @domain.name %> kustutamine on sellest tulenevalt tühistatud.
<br><br>
Lugupidamisega<br>
Eesti Interneti Sihtasutus
<br><br>
<%= render 'mailers/shared/signatures/signature.et.html' %>
<hr>
<br><br>
Hi,
<br><br>
Domain <%= @domain.name %> deletion was not approved in time by the registrant <%= @domain.registrant.name %>. Thus the deletion of domain <%= @domain.name %> is cancelled.
<br><br>
Best Regards,<br>
Estonian Internet Foundation
<%= render 'mailers/shared/signatures/signature.en.html' %>
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ Tere

Domeeni <%= @domain.name %> kustutamise taotlust ei kinnitatud tähtaegselt registreerija <%= @domain.registrant.name %> poolt. Domeeni <%= @domain.name %> kustutamine on sellest tulenevalt tühistatud.

Lugupidamisega
Eesti Interneti Sihtasutus

<%= render 'mailers/shared/signatures/signature.et.text' %>
--------------------------------------

Hi,

Domain <%= @domain.name %> deletion was not approved in time by the registrant <%= @domain.registrant.name %>. Thus the deletion of domain <%= @domain.name %> is cancelled.

Best Regards,
Estonian Internet Foundation
<%= render 'mailers/shared/signatures/signature.en.text' %>
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
Tere
<br><br>
Domeeni <%= @domain.name %> kustutamise taotlus on registreerija <%= @domain.registrant.name %> poolt tagasi lükatud.
<br><br>
Lugupidamisega<br>
Eesti Interneti Sihtasutus
<br><br>
<%= render 'mailers/shared/signatures/signature.et.html' %>
<hr>
<br><br>
Hi,
<br><br>
Domain <%= @domain.name %> deletion was rejected by the registrant <%= @domain.registrant.name %>.
<br><br>
Best Regards,<br>
Estonian Internet Foundation
<%= render 'mailers/shared/signatures/signature.en.html' %>
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ Tere

Domeeni <%= @domain.name %> kustutamise taotlus on registreerija <%= @domain.registrant.name %> poolt tagasi lükatud.

Lugupidamisega
Eesti Interneti Sihtasutus

<%= render 'mailers/shared/signatures/signature.et.text' %>
--------------------------------------

Hi,

Domain <%= @domain.name %> deletion was rejected by the registrant <%= @domain.registrant.name %>.

Best Regards,
Estonian Internet Foundation
<%= render 'mailers/shared/signatures/signature.en.text' %>
3 changes: 0 additions & 3 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -576,9 +576,6 @@ en:
unimplemented_object_service: 'Unimplemented object service'
contact_email_update_subject: 'Teie domeenide kontakt epostiaadress on muutunud / Contact e-mail addresses of your domains have changed'
object_status_prohibits_operation: 'Object status prohibits operation'
pending_delete_rejected_notification_subject: "Domeeni %{name} kustutamise taotlus tagasi lükatud / %{name} deletion declined"
pending_delete_expired_notification_subject: "Domeeni %{name} kustutamise taotlus on tühistatud / %{name} deletion cancelled"
delete_confirmation_subject: "Domeeni %{name} kustutatud / %{name} deleted"
whois: WHOIS
not_valid_domain_verification_title: Domain verification not available
not_valid_domain_verification_body: This could mean your verification has been expired or done already.<br><br>Please contact us if you think something is wrong.
Expand Down
14 changes: 13 additions & 1 deletion config/locales/mailers/domain_delete.en.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
en:
domain_delete_mailer:
confirmation:
confirmation_request:
subject: >-
Kinnitustaotlus domeeni %{domain_name} kustutamiseks .ee registrist
/ Application for approval for deletion of %{domain_name}
accepted:
subject: >-
Domeeni %{domain} kustutatud
/ %{domain} deleted
rejected:
subject: >-
Domeeni %{domain} kustutamise taotlus tagasi lükatud
/ %{domain} deletion declined
expired:
subject: >-
Domeeni %{domain} kustutamise taotlus on tühistatud
/ %{domain} deletion cancelled
forced:
subject: >-
Domeen %{domain_name} on kustutusmenetluses
Expand Down
36 changes: 36 additions & 0 deletions test/integration/epp/domain/delete/base_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'test_helper'

class EppDomainDeleteBaseTest < ApplicationIntegrationTest
include ActionMailer::TestHelper

setup do
@domain = domains(:shop)
ActionMailer::Base.deliveries.clear
end

def test_delivers_confirmation_request_email
assert_equal 'shop.test', @domain.name

request_xml = <<-XML
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<epp xmlns="https://epp.tld.ee/schema/epp-ee-1.0.xsd">
<command>
<delete>
<domain:delete xmlns:domain="https://epp.tld.ee/schema/domain-eis-1.0.xsd">
<domain:name>shop.test</domain:name>
</domain:delete>
</delete>
<extension>
<eis:extdata xmlns:eis="https://epp.tld.ee/schema/eis-1.0.xsd">
<eis:legalDocument type="pdf">dGVzdCBmYWlsCg==</eis:legalDocument>
</eis:extdata>
</extension>
</command>
</epp>
XML

post '/epp/command/delete', { frame: request_xml }, 'HTTP_COOKIE' => 'session=api_bestnames'

assert_emails 1
end
end
30 changes: 30 additions & 0 deletions test/integration/registrant_area/domain_delete_confirmations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'test_helper'

class RegistrantAreaDomainDeleteConfirmationIntegrationTest < ActionDispatch::IntegrationTest
include ActionMailer::TestHelper

setup do
@domain = domains(:shop)
ActionMailer::Base.deliveries.clear
end

def test_notifies_registrant_when_accepted
@domain.update!(registrant_verification_asked_at: Time.zone.now,
registrant_verification_token: 'test',
statuses: [DomainStatus::PENDING_DELETE_CONFIRMATION])

patch registrant_domain_delete_confirm_path(@domain, token: 'test', confirmed: true)

assert_emails 1
end

def test_notifies_registrant_when_rejected
@domain.update!(registrant_verification_asked_at: Time.zone.now,
registrant_verification_token: 'test',
statuses: [DomainStatus::PENDING_DELETE_CONFIRMATION])

patch registrant_domain_delete_confirm_path(@domain, token: 'test', rejected: true)

assert_emails 1
end
end
Loading

0 comments on commit a64726a

Please sign in to comment.