Refactor contact mailer

- Send single email with new email in `to` and old email in `bcc` fields
- Remove `Que::Mailer` (#895)
- Add preview
- DRY templates
- Add tests
- Extract translations
- Remove useless specs
This commit is contained in:
Artur Beljajev 2019-04-07 17:02:43 +03:00
parent 6fa1ce9128
commit 5674071838
12 changed files with 122 additions and 125 deletions

View file

@ -1,8 +1,11 @@
require 'test_helper'
class EppContactUpdateBaseTest < ActionDispatch::IntegrationTest
include ActionMailer::TestHelper
setup do
@contact = contacts(:john)
ActionMailer::Base.deliveries.clear
end
def test_updates_contact
@ -45,6 +48,62 @@ class EppContactUpdateBaseTest < ActionDispatch::IntegrationTest
assert_equal '+123.4', @contact.phone
end
def test_notifies_a_contact_when_an_email_is_changed
assert_equal 'john-001', @contact.code
assert_not_equal 'new@inbox.test', @contact.email
# https://github.com/internetee/registry/issues/415
@contact.update_columns(code: @contact.code.upcase)
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>
<update>
<contact:update xmlns:contact="https://epp.tld.ee/schema/contact-ee-1.1.xsd">
<contact:id>john-001</contact:id>
<contact:chg>
<contact:email>new@inbox.test</contact:email>
</contact:chg>
</contact:update>
</update>
</command>
</epp>
XML
post '/epp/command/update', { frame: request_xml }, 'HTTP_COOKIE' => 'session=api_bestnames'
assert_emails 1
end
def test_skips_notifying_a_contact_when_an_email_is_not_changed
assert_equal 'john-001', @contact.code
assert_equal 'john@inbox.test', @contact.email
# https://github.com/internetee/registry/issues/415
@contact.update_columns(code: @contact.code.upcase)
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>
<update>
<contact:update xmlns:contact="https://epp.tld.ee/schema/contact-ee-1.1.xsd">
<contact:id>john-001</contact:id>
<contact:chg>
<contact:email>john@inbox.test</contact:email>
</contact:chg>
</contact:update>
</update>
</command>
</epp>
XML
post '/epp/command/update', { frame: request_xml }, 'HTTP_COOKIE' => 'session=api_bestnames'
assert_no_emails
end
def test_non_existing_contact
assert_nil Contact.find_by(code: 'non-existing')

View file

@ -0,0 +1,24 @@
require 'test_helper'
class ContactMailerTest < ActiveSupport::TestCase
include ActionMailer::TestHelper
setup do
@contact = contacts(:john)
ActionMailer::Base.deliveries.clear
end
def test_delivers_email_changed_email
assert_equal 'john-001', @contact.code
assert_equal 'john@inbox.test', @contact.email
email = ContactMailer.email_changed(contact: @contact, old_email: 'john-old@inbox.test')
.deliver_now
assert_emails 1
assert_equal %w[john@inbox.test], email.to
assert_equal %w[john-old@inbox.test], email.bcc
assert_equal 'Teie domeenide kontakt epostiaadress on muutunud' \
' / Contact e-mail addresses of your domains have changed [john-001]', email.subject
end
end

View file

@ -0,0 +1,13 @@
class ContactMailerPreview < ActionMailer::Preview
def email_changed
# Replace with `Contact.in_use` once https://github.com/internetee/registry/pull/1146 is merged
contact = Contact.where('EXISTS(SELECT 1 FROM domains WHERE domains.registrant_id = contacts.id)
OR
EXISTS(SELECT 1 FROM domain_contacts WHERE domain_contacts.contact_id =
contacts.id)')
contact = contact.where.not(email: nil, country_code: nil, code: nil).first
ContactMailer.email_changed(contact: contact, old_email: 'old@inbox.test')
end
end