From 64fec42a690f351c2317d60be94a50ea8c68ca4e Mon Sep 17 00:00:00 2001 From: Priit Tark Date: Tue, 12 May 2015 13:45:55 +0300 Subject: [PATCH] EPP email change will send notification to registrant --- app/mailers/contact_mailer.rb | 13 ++ app/mailers/invoice_mailer.rb | 6 +- app/models/contact.rb | 9 ++ app/models/domain_contact.rb | 6 + app/models/epp/contact.rb | 2 +- .../contact_mailer/email_updated.html.erb | 51 +++++++ .../contact_mailer/email_updated.text.erb | 51 +++++++ config/initializers/settings.rb | 11 ++ config/locales/en.yml | 1 + .../admin_domain_contact_fabricator.rb | 6 - spec/fabricators/domain_contact_fabricator.rb | 13 +- .../tech_domain_contact_fabricator.rb | 6 - spec/mailers/contact_mailer_spec.rb | 52 ++++++++ spec/models/domain_contact_spec.rb | 125 ++++++++++++++++++ 14 files changed, 330 insertions(+), 22 deletions(-) create mode 100644 app/mailers/contact_mailer.rb create mode 100644 app/views/contact_mailer/email_updated.html.erb create mode 100644 app/views/contact_mailer/email_updated.text.erb create mode 100644 config/initializers/settings.rb delete mode 100644 spec/fabricators/admin_domain_contact_fabricator.rb delete mode 100644 spec/fabricators/tech_domain_contact_fabricator.rb create mode 100644 spec/mailers/contact_mailer_spec.rb create mode 100644 spec/models/domain_contact_spec.rb diff --git a/app/mailers/contact_mailer.rb b/app/mailers/contact_mailer.rb new file mode 100644 index 000000000..174b8e8f0 --- /dev/null +++ b/app/mailers/contact_mailer.rb @@ -0,0 +1,13 @@ +class ContactMailer < ApplicationMailer + def email_updated(contact) + unless Rails.env.production? + return unless TEST_EMAILS.include?(contact.email) || TEST_EMAILS.include?(contact.email_was) + end + + # turn on delivery on specific request only, thus rake tasks does not deliver anything + return if contact.deliver_emails != true + + @contact = contact + mail(to: [@contact.email, @contact.email_was], subject: I18n.t(:contact_email_update_subject)) + end +end diff --git a/app/mailers/invoice_mailer.rb b/app/mailers/invoice_mailer.rb index 2cc4a8b99..ea30783be 100644 --- a/app/mailers/invoice_mailer.rb +++ b/app/mailers/invoice_mailer.rb @@ -1,12 +1,8 @@ class InvoiceMailer < ApplicationMailer def invoice_email(invoice, pdf) - unless Rails.env.production? - test_emails = ['martin@gitlab.eu', 'priit@gitlab.eu'] - return unless test_emails.include?(invoice.billing_email) - end + return if Rails.env.production? ? false : TEST_EMAILS.include?(invoice.billing_email) @invoice = invoice - attachments[invoice.pdf_name] = pdf mail(to: invoice.billing_email, subject: invoice) end diff --git a/app/models/contact.rb b/app/models/contact.rb index 9cd758314..34d3c53ae 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -29,6 +29,13 @@ class Contact < ActiveRecord::Base before_validation :set_ident_country_code before_validation :prefix_code before_create :generate_auth_info + + before_update :manage_emails + def manage_emails + return nil unless email_changed? + ContactMailer.email_updated(self).deliver_now + end + after_save :manage_statuses def manage_statuses ContactStatus.manage(statuses, self) @@ -48,6 +55,8 @@ class Contact < ActiveRecord::Base BIRTHDAY # Birthday date ] + attr_accessor :deliver_emails + class << self def search_by_query(query) res = search(code_cont: query).result diff --git a/app/models/domain_contact.rb b/app/models/domain_contact.rb index ec6247876..097681f2c 100644 --- a/app/models/domain_contact.rb +++ b/app/models/domain_contact.rb @@ -16,6 +16,12 @@ class DomainContact < ActiveRecord::Base } end + def name + return 'Tech' if type == 'TechDomainContact' + return 'Admin' if type == 'AdminDomainContact' + '' + end + validates :contact, presence: true before_save :update_contact_code_cache diff --git a/app/models/epp/contact.rb b/app/models/epp/contact.rb index 64927dc74..f106a4247 100644 --- a/app/models/epp/contact.rb +++ b/app/models/epp/contact.rb @@ -145,7 +145,7 @@ class Epp::Contact < Contact at.merge!(self.class.ident_attrs(frame.css('ident').first)) legal_frame = frame.css('legalDocument').first at[:legal_documents_attributes] = self.class.legal_document_attrs(legal_frame) - + self.deliver_emails = true # turn on email delivery for epp super(at) end end diff --git a/app/views/contact_mailer/email_updated.html.erb b/app/views/contact_mailer/email_updated.html.erb new file mode 100644 index 000000000..6fe64ef32 --- /dev/null +++ b/app/views/contact_mailer/email_updated.html.erb @@ -0,0 +1,51 @@ +Tere <%= @contact.name %> +

+Kontakti <%= @contact.name %> eposti aadress on muudetud
+endine aadress: <%= @contact.email_was %>
+uus aadress: <%= @contact.email %> +

+Eposti aadressile saadetakse domeenidega seotud infot seal hulgas kinnitustaotluseid omaniku vahetuse ja domeeni kustutamise korral. Palun veenduge, et muudatus on korrektne ning probleemide korral pöörduga oma registripidaja poole. Teie registripidaja on <%= @contact.registrar.name %> +

+Muudatusega seotud domeenid:
+<% @contact.domain_contacts.each do |dc| %> + <%= dc.domain.name %> (<%= dc.name %>)
+<% end %> +
+Kontaktandmed:
+Nimi: <%= @contact.name %>
+Isikukood: <%= @contact.ident %>
+Epost: <%= @contact.email %>
+Tel: <%= @contact.phone %>
+Tänav: <%= @contact.street %>
+Linn: <%= @contact.city %>
+Riik: <%= @contact.country %> +

+Lugupidamisega +Eesti Interneti SA +

+
+

+Hi <%= @contact.name %> +

+E-mail address of <% @contact.name %> has been changed
+previous address: <% @contact.email_was %>
+new address: <% @contact.email %> +

+E-mail addresses are used to send important information regarding your registered domains including applications for approval of registrant change and domain deletion. Please make sure that the update and contact information are correct. Incase of problems please turn to your registrar. Your registrar is <%= @contact.registrar.name %> +

+Domains affected by this update:
+<% @contact.domain_contacts.each do |dc| %> + <%= dc.domain.name %> (<%= dc.name %>)
+<% end %> +
+Contact information:
+Name: <%= @contact.name %>
+Identity Code: <%= @contact.ident %>
+E-mail: <%= @contact.email %>
+Tel: <%= @contact.phone %>
+Street: <%= @contact.street %>
+City: <%= @contact.city %>
+Country: <%= @contact.country %> +

+Best Regards, +Eesti Interneti SA diff --git a/app/views/contact_mailer/email_updated.text.erb b/app/views/contact_mailer/email_updated.text.erb new file mode 100644 index 000000000..508dd2fe1 --- /dev/null +++ b/app/views/contact_mailer/email_updated.text.erb @@ -0,0 +1,51 @@ +Tere <%= @contact.name %> + +Kontakti <%= @contact.name %> eposti aadress on muudetud +endine aadress: <%= @contact.email_was %> +uus aadress: <%= @contact.email %> + +Eposti aadressile saadetakse domeenidega seotud infot seal hulgas kinnitustaotluseid omaniku vahetuse ja domeeni kustutamise korral. Palun veenduge, et muudatus on korrektne ning probleemide korral pöörduga oma registripidaja poole. Teie registripidaja on <%= @contact.registrar.name %> + +Muudatusega seotud domeenid: +<% @contact.domain_contacts.each do |dc| %> + <%= dc.domain.name %> (<%= dc.name %>) +<% end %> + +Kontaktandmed: +Nimi: <%= @contact.name %> +Isikukood: <%= @contact.ident %> +Epost: <%= @contact.email %> +Tel: <%= @contact.phone %> +Tänav: <%= @contact.street %> +Linn: <%= @contact.city %> +Riik: <%= @contact.country %> + +Lugupidamisega +Eesti Interneti SA + +---------------------------------------------------------------------------------- + +Hi <%= @contact.name %> + +E-mail address of <% @contact.name %> has been changed +previous address: <% @contact.email_was %> +new address: <% @contact.email %> + +E-mail addresses are used to send important information regarding your registered domains including applications for approval of registrant change and domain deletion. Please make sure that the update and contact information are correct. Incase of problems please turn to your registrar. Your registrar is <%= @contact.registrar.name %> + +Domains affected by this update: +<% @contact.domain_contacts.each do |dc| %> + <%= dc.domain.name %> (<%= dc.name %>) +<% end %> + +Contact information: +Name: <%= @contact.name %> +Identity Code: <%= @contact.ident %> +E-mail: <%= @contact.email %> +Tel: <%= @contact.phone %> +Street: <%= @contact.street %> +City: <%= @contact.city %> +Country: <%= @contact.country %> + +Best Regards, +Eesti Interneti SA diff --git a/config/initializers/settings.rb b/config/initializers/settings.rb new file mode 100644 index 000000000..4309f8852 --- /dev/null +++ b/config/initializers/settings.rb @@ -0,0 +1,11 @@ +TEST_EMAILS = %w( + test@example.ee + test@example.com + martin@gitlab.eu + priit@gitlab.eu + timo.vohmar@internet.ee + rene.vahtel@internet.ee + martin.mettig@internet.ee + hannes.klausen@internet.ee + georg.kahest@internet.ee +) diff --git a/config/locales/en.yml b/config/locales/en.yml index 8d8330764..b7fa7a05d 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -761,3 +761,4 @@ en: no_hostnames_replaced: 'No hostnames replaced' forbidden_code: 'is forbidden to use' 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' diff --git a/spec/fabricators/admin_domain_contact_fabricator.rb b/spec/fabricators/admin_domain_contact_fabricator.rb deleted file mode 100644 index 1fc4e8376..000000000 --- a/spec/fabricators/admin_domain_contact_fabricator.rb +++ /dev/null @@ -1,6 +0,0 @@ -Fabricator(:admin_domain_contact) do - contact { Fabricate(:contact) } - after_build do |x| - x.contact_code_cache = x.contact.code - end -end diff --git a/spec/fabricators/domain_contact_fabricator.rb b/spec/fabricators/domain_contact_fabricator.rb index 54f0598fa..9f693ef51 100644 --- a/spec/fabricators/domain_contact_fabricator.rb +++ b/spec/fabricators/domain_contact_fabricator.rb @@ -1,7 +1,12 @@ Fabricator(:domain_contact) do contact { Fabricate(:contact) } - contact_type 'admin' - after_build do |x| - x.contact_code_cache = x.contact.code - end + type 'TechDomainContact' +end + +Fabricator(:tech_domain_contact, from: :domain_contact) do + type 'TechDomainContact' +end + +Fabricator(:admin_domain_contact, from: :domain_contact) do + type 'AdminDomainContact' end diff --git a/spec/fabricators/tech_domain_contact_fabricator.rb b/spec/fabricators/tech_domain_contact_fabricator.rb deleted file mode 100644 index 34e53b1b6..000000000 --- a/spec/fabricators/tech_domain_contact_fabricator.rb +++ /dev/null @@ -1,6 +0,0 @@ -Fabricator(:tech_domain_contact) do - contact { Fabricate(:contact) } - after_build do |x| - x.contact_code_cache = x.contact.code - end -end diff --git a/spec/mailers/contact_mailer_spec.rb b/spec/mailers/contact_mailer_spec.rb new file mode 100644 index 000000000..519f2bfcc --- /dev/null +++ b/spec/mailers/contact_mailer_spec.rb @@ -0,0 +1,52 @@ +require 'rails_helper' + +describe ContactMailer do + describe 'email changed notification when delivery turned off' do + before :all do + @contact = Fabricate(:contact, email: 'test@example.ee') + @contact.email = 'test@example.com' # new email + @mail = ContactMailer.email_updated(@contact) + end + + it 'should not render email subject' do + @mail.subject.should == nil + end + + it 'should not have sender email' do + @mail.from.should == nil + end + + it 'should not have reveiver email' do + @mail.to.should == nil + end + + it 'should not render body' do + @mail.body.should == '' + end + end + + describe 'email changed notification' do + before :all do + @contact = Fabricate(:contact, email: 'test@example.ee') + @contact.deliver_emails = true + @contact.email = 'test@example.com' # new email + @mail = ContactMailer.email_updated(@contact) + end + + it 'should render email subject' do + @mail.subject.should =~ /Teie domeenide kontakt epostiaadress on muutunud/ + end + + it 'should have sender email' do + @mail.from.should == ["noreply@internet.ee"] + end + + it 'should have both old and new receiver email' do + @mail.to.should == ['test@example.com', 'test@example.ee'] + end + + it 'should render body' do + @mail.body.encoded.should =~ /Kontaktandmed:/ + end + end +end diff --git a/spec/models/domain_contact_spec.rb b/spec/models/domain_contact_spec.rb new file mode 100644 index 000000000..de62276df --- /dev/null +++ b/spec/models/domain_contact_spec.rb @@ -0,0 +1,125 @@ +require 'rails_helper' + +describe DomainContact do + before :all do + @api_user = Fabricate(:domain_contact) + end + + context 'with invalid attribute' do + before :all do + @domain_contact = DomainContact.new + end + + it 'should not be valid' do + @domain_contact.valid? + @domain_contact.errors.full_messages.should match_array([ + "Contact Contact was not found" + ]) + end + + it 'should not have creator' do + @domain_contact.creator.should == nil + end + + it 'should not have updater' do + @domain_contact.updator.should == nil + end + + it 'should not have any name' do + @domain_contact.name.should == '' + end + end + + context 'with valid attributes' do + before :all do + @domain_contact = Fabricate(:domain_contact) + end + + it 'should be valid' do + @domain_contact.valid? + @domain_contact.errors.full_messages.should match_array([]) + end + + it 'should be valid twice' do + @domain_contact = Fabricate(:domain_contact) + @domain_contact.valid? + @domain_contact.errors.full_messages.should match_array([]) + end + + it 'should have Tech name' do + @domain_contact.name.should == 'Tech' + end + + it 'should have one version' do + with_versioning do + @domain_contact.versions.reload.should == [] + @domain_contact.updated_at = Time.zone.now # trigger new version + @domain_contact.save + @domain_contact.errors.full_messages.should match_array([]) + @domain_contact.versions.size.should == 1 + end + end + end + + context 'with valid attributes with tech domain contact' do + before :all do + @domain_contact = Fabricate(:tech_domain_contact) + end + + it 'should be valid' do + @domain_contact.valid? + @domain_contact.errors.full_messages.should match_array([]) + end + + it 'should be valid twice' do + @domain_contact = Fabricate(:tech_domain_contact) + @domain_contact.valid? + @domain_contact.errors.full_messages.should match_array([]) + end + + it 'should have Tech name' do + @domain_contact.name.should == 'Tech' + end + + it 'should have one version' do + with_versioning do + @domain_contact.versions.reload.should == [] + @domain_contact.updated_at = Time.zone.now # trigger new version + @domain_contact.save + @domain_contact.errors.full_messages.should match_array([]) + @domain_contact.versions.size.should == 1 + end + end + end + + context 'with valid attributes with admin domain contact' do + before :all do + @domain_contact = Fabricate(:admin_domain_contact) + end + + it 'should be valid' do + @domain_contact.valid? + @domain_contact.errors.full_messages.should match_array([]) + end + + it 'should be valid twice' do + @domain_contact = Fabricate(:admin_domain_contact) + @domain_contact.valid? + @domain_contact.errors.full_messages.should match_array([]) + end + + it 'should have Tech name' do + @domain_contact.name.should == 'Admin' + end + + it 'should have one version' do + with_versioning do + @domain_contact.versions.reload.should == [] + @domain_contact.updated_at = Time.zone.now # trigger new version + @domain_contact.save + @domain_contact.errors.full_messages.should match_array([]) + @domain_contact.versions.size.should == 1 + end + end + end +end