diff --git a/app/models/contact.rb b/app/models/contact.rb
index 8b1cfaa77..f6b180a5e 100644
--- a/app/models/contact.rb
+++ b/app/models/contact.rb
@@ -548,4 +548,8 @@ class Contact < ActiveRecord::Base
def managed_by?(registrant_user)
ident == registrant_user.ident
end
+
+ def registrant?
+ registrant_domains.any?
+ end
end
diff --git a/app/models/epp/contact.rb b/app/models/epp/contact.rb
index 38ddfa230..a33e55a0e 100644
--- a/app/models/epp/contact.rb
+++ b/app/models/epp/contact.rb
@@ -179,7 +179,7 @@ class Epp::Contact < Contact
old_email = email_was
updated = save
- if updated && email_changed
+ if updated && email_changed && registrant?
ContactMailer.email_changed(contact: self, old_email: old_email).deliver_now
end
diff --git a/test/integration/epp/contact/update/base_test.rb b/test/integration/epp/contact/update/base_test.rb
index bcb4a0f6e..2c0486a07 100644
--- a/test/integration/epp/contact/update/base_test.rb
+++ b/test/integration/epp/contact/update/base_test.rb
@@ -50,7 +50,7 @@ class EppContactUpdateBaseTest < ActionDispatch::IntegrationTest
def test_notifies_contact_by_email_when_email_is_changed
assert_equal 'john-001', @contact.code
- assert_not_equal 'new@inbox.test', @contact.email
+ assert_not_equal 'john-new@inbox.test', @contact.email
# https://github.com/internetee/registry/issues/415
@contact.update_columns(code: @contact.code.upcase)
@@ -63,7 +63,7 @@ class EppContactUpdateBaseTest < ActionDispatch::IntegrationTest
john-001
- new@inbox.test
+ john-new@inbox.test
@@ -104,6 +104,37 @@ class EppContactUpdateBaseTest < ActionDispatch::IntegrationTest
assert_no_emails
end
+ def test_skips_notifying_a_contact_when_a_contact_is_not_a_registrant
+ assert_equal 'john-001', @contact.code
+ assert_not_equal 'john-new@inbox.test', @contact.email
+
+ make_contact_free_of_domains_where_it_acts_as_a_registrant(@contact)
+ assert_not @contact.registrant?
+
+ # https://github.com/internetee/registry/issues/415
+ @contact.update_columns(code: @contact.code.upcase)
+
+ request_xml = <<-XML
+
+
+
+
+
+ john-001
+
+ john-new@inbox.test
+
+
+
+
+
+ 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')
@@ -130,4 +161,12 @@ class EppContactUpdateBaseTest < ActionDispatch::IntegrationTest
response_xml = Nokogiri::XML(response.body)
assert_equal '2303', response_xml.at_css('result')[:code]
end
+
+ private
+
+ def make_contact_free_of_domains_where_it_acts_as_a_registrant(contact)
+ other_contact = contacts(:william)
+ assert_not_equal other_contact, contact
+ Domain.update_all(registrant_id: other_contact)
+ end
end
\ No newline at end of file
diff --git a/test/models/contact_test.rb b/test/models/contact_test.rb
index 0cafdb8ba..20b43f2d0 100644
--- a/test/models/contact_test.rb
+++ b/test/models/contact_test.rb
@@ -79,4 +79,20 @@ class ContactTest < ActiveSupport::TestCase
assert_equal [@contact], Contact.registrant_user_contacts(registrant_user)
end
end
+
+ def test_contact_is_a_registrant
+ assert_equal @contact.becomes(Registrant), domains(:shop).registrant
+ assert @contact.registrant?
+
+ make_contact_free_of_domains_where_it_acts_as_a_registrant(@contact)
+ assert_not @contact.registrant?
+ end
+
+ private
+
+ def make_contact_free_of_domains_where_it_acts_as_a_registrant(contact)
+ other_contact = contacts(:william)
+ assert_not_equal other_contact, contact
+ Domain.update_all(registrant_id: other_contact)
+ end
end
\ No newline at end of file