diff --git a/app/models/actions/contact_update.rb b/app/models/actions/contact_update.rb
index 5b94cf918..f8b39ecb4 100644
--- a/app/models/actions/contact_update.rb
+++ b/app/models/actions/contact_update.rb
@@ -23,7 +23,7 @@ module Actions
end
def maybe_remove_address
- return if Setting.address_processing?
+ return if Contact.address_processing?
new_attributes.delete(:city)
new_attributes.delete(:zip)
diff --git a/app/models/contact.rb b/app/models/contact.rb
index ac64b059f..4199e6dc7 100644
--- a/app/models/contact.rb
+++ b/app/models/contact.rb
@@ -62,6 +62,7 @@ class Contact < ApplicationRecord
mapping: [%w[ident code], %w[ident_type type], %w[ident_country_code country_code]]
after_save :update_related_whois_records
+ before_validation :clear_address_modifications, if: -> { !self.class.address_processing? }
self.ignored_columns = %w[legacy_id legacy_history_id]
@@ -507,6 +508,19 @@ class Contact < ApplicationRecord
]).present?
end
+ def clear_address_modifications
+ return unless modifies_address?
+
+ remove_address
+ end
+
+ def modifies_address?
+ modified = false
+ self.class.address_attribute_names.each { |field| modified = true if changes.key?(field) }
+
+ modified
+ end
+
def update_related_whois_records
# not doing anything if no real changes
ignored_columns = %w[updated_at created_at statuses status_notes]
diff --git a/app/models/epp/response/result/code.rb b/app/models/epp/response/result/code.rb
index 2a65f6747..1be4a3f7c 100644
--- a/app/models/epp/response/result/code.rb
+++ b/app/models/epp/response/result/code.rb
@@ -7,6 +7,7 @@ module Epp
KEY_TO_VALUE = {
completed_successfully: 1000,
completed_successfully_action_pending: 1001,
+ completed_without_address: 1100,
completed_successfully_no_messages: 1300,
completed_successfully_ack_to_dequeue: 1301,
completed_successfully_ending_session: 1500,
@@ -35,6 +36,7 @@ module Epp
DEFAULT_DESCRIPTIONS = {
1000 => 'Command completed successfully',
1001 => 'Command completed successfully; action pending',
+ 1100 => 'Command completed successfully; Postal address data discarded',
1300 => 'Command completed successfully; no messages',
1301 => 'Command completed successfully; ack to dequeue',
1500 => 'Command completed successfully; ending session',
diff --git a/test/integration/api/v1/registrant/contacts/update_test.rb b/test/integration/api/v1/registrant/contacts/update_test.rb
index 6e0c0eea3..e2e9abe9a 100644
--- a/test/integration/api/v1/registrant/contacts/update_test.rb
+++ b/test/integration/api/v1/registrant/contacts/update_test.rb
@@ -91,8 +91,8 @@ class RegistrantApiV1ContactUpdateTest < ActionDispatch::IntegrationTest
end
def test_address_is_optional_when_enabled
- @contact.update!(street: 'any', zip: 'any', city: 'any', state: 'any', country_code: 'US')
Setting.address_processing = true
+ @contact.update!(street: 'any', zip: 'any', city: 'any', state: 'any', country_code: 'US')
patch api_v1_registrant_contact_path(@contact.uuid), params: { name: 'any' },
as: :json,
diff --git a/test/integration/epp/contact/create/base_test.rb b/test/integration/epp/contact/create/base_test.rb
index e9a59b8d2..262487a1e 100644
--- a/test/integration/epp/contact/create/base_test.rb
+++ b/test/integration/epp/contact/create/base_test.rb
@@ -140,4 +140,115 @@ class EppContactCreateBaseTest < EppTestCase
end
assert_epp_response :required_parameter_missing
end
+
+ def test_does_not_save_address_when_address_processing_turned_off
+ name = 'new'
+ email = 'new@registrar.test'
+ phone = '+1.2'
+
+ request_xml = <<-XML
+
+
+
+
+
+
+ #{name}
+
+ 123 Example
+ Tallinn
+ FFF
+ 123456
+ EE
+
+
+ #{phone}
+ #{email}
+
+
+
+
+ 123
+
+
+
+
+ XML
+
+ assert_difference 'Contact.count' do
+ post epp_create_path, params: { frame: request_xml },
+ headers: { 'HTTP_COOKIE' => 'session=api_bestnames' }
+ end
+
+ assert_epp_response :completed_without_address
+ contact = Contact.find_by(name: name)
+ assert_equal name, contact.name
+ assert_equal email, contact.email
+ assert_equal phone, contact.phone
+ assert_not_empty contact.code
+ assert_nil contact.city
+ assert_nil contact.street
+ assert_nil contact.zip
+ assert_nil contact.country_code
+ assert_nil contact.state
+ end
+
+ def test_saves_address_when_address_processing_turned_on
+ Setting.address_processing = true
+
+ name = 'new'
+ email = 'new@registrar.test'
+ phone = '+1.2'
+ street = '123 Example'
+ city = 'Tallinn'
+ state = 'Harjumaa'
+ zip = '123456'
+ country_code = 'EE'
+
+ request_xml = <<-XML
+
+
+
+
+
+
+ #{name}
+
+ #{street}
+ #{city}
+ #{state}
+ #{zip}
+ #{country_code}
+
+
+ #{phone}
+ #{email}
+
+
+
+
+ 123
+
+
+
+
+ XML
+
+ assert_difference 'Contact.count' do
+ post epp_create_path, params: { frame: request_xml },
+ headers: { 'HTTP_COOKIE' => 'session=api_bestnames' }
+ end
+
+ assert_epp_response :completed_successfully
+ contact = Contact.find_by(name: name)
+ assert_equal name, contact.name
+ assert_equal email, contact.email
+ assert_equal phone, contact.phone
+ assert_not_empty contact.code
+ assert_equal city, contact.city
+ assert_equal street, contact.street
+ assert_equal zip, contact.zip
+ assert_equal country_code, contact.country_code
+ assert_equal state, contact.state
+ end
end
diff --git a/test/integration/epp/contact/update/base_test.rb b/test/integration/epp/contact/update/base_test.rb
index 3d332711f..98c0e4462 100644
--- a/test/integration/epp/contact/update/base_test.rb
+++ b/test/integration/epp/contact/update/base_test.rb
@@ -232,6 +232,99 @@ class EppContactUpdateBaseTest < EppTestCase
assert_epp_response :completed_successfully
end
+ def test_updates_address_when_address_processing_turned_on
+ @contact.update_columns(code: @contact.code.upcase)
+ Setting.address_processing = true
+
+ street = '123 Example'
+ city = 'Tallinn'
+ state = 'Harjumaa'
+ zip = '123456'
+ country_code = 'EE'
+
+ request_xml = <<-XML
+
+
+
+
+
+ #{@contact.code}
+
+
+
+ #{street}
+ #{city}
+ #{state}
+ #{zip}
+ #{country_code}
+
+
+
+
+
+
+
+ XML
+
+ post epp_update_path, params: { frame: request_xml },
+ headers: { 'HTTP_COOKIE' => 'session=api_bestnames' }
+
+ assert_epp_response :completed_successfully
+ @contact.reload
+
+ assert_equal city, @contact.city
+ assert_equal street, @contact.street
+ assert_equal zip, @contact.zip
+ assert_equal country_code, @contact.country_code
+ assert_equal state, @contact.state
+ end
+
+ def test_does_not_update_address_when_address_processing_turned_off
+ @contact.update_columns(code: @contact.code.upcase)
+
+ street = '123 Example'
+ city = 'Tallinn'
+ state = 'Harjumaa'
+ zip = '123456'
+ country_code = 'EE'
+
+ request_xml = <<-XML
+
+
+
+
+
+ #{@contact.code}
+
+
+
+ #{street}
+ #{city}
+ #{state}
+ #{zip}
+ #{country_code}
+
+
+
+
+
+
+
+ XML
+
+ post epp_update_path, params: { frame: request_xml },
+ headers: { 'HTTP_COOKIE' => 'session=api_bestnames' }
+
+ assert_epp_response :completed_without_address
+ @contact.reload
+
+ assert_nil @contact.city
+ assert_nil @contact.street
+ assert_nil @contact.zip
+ assert_nil @contact.country_code
+ assert_nil @contact.state
+ end
+
private
def make_contact_free_of_domains_where_it_acts_as_a_registrant(contact)
diff --git a/test/models/contact_test.rb b/test/models/contact_test.rb
index f833011c6..000333d57 100644
--- a/test/models/contact_test.rb
+++ b/test/models/contact_test.rb
@@ -152,6 +152,8 @@ class ContactTest < ActiveSupport::TestCase
end
def test_address
+ Setting.address_processing = true
+
address = Contact::Address.new('new street', '83746', 'new city', 'new state', 'EE')
@contact.address = address
@contact.save!
@@ -238,6 +240,7 @@ class ContactTest < ActiveSupport::TestCase
end
def test_normalizes_country_code
+ Setting.address_processing = true
contact = Contact.new(country_code: 'us')
contact.validate
assert_equal 'US', contact.country_code
diff --git a/test/models/epp/response/result/code_test.rb b/test/models/epp/response/result/code_test.rb
index 3c75303f1..f16013180 100644
--- a/test/models/epp/response/result/code_test.rb
+++ b/test/models/epp/response/result/code_test.rb
@@ -28,6 +28,7 @@ class EppResponseResultCodeTest < ActiveSupport::TestCase
codes = {
completed_successfully: 1000,
completed_successfully_action_pending: 1001,
+ completed_without_address: 1100,
completed_successfully_no_messages: 1300,
completed_successfully_ack_to_dequeue: 1301,
completed_successfully_ending_session: 1500,
@@ -58,6 +59,7 @@ class EppResponseResultCodeTest < ActiveSupport::TestCase
descriptions = {
1000 => 'Command completed successfully',
1001 => 'Command completed successfully; action pending',
+ 1100 => 'Command completed successfully; Postal address data discarded',
1300 => 'Command completed successfully; no messages',
1301 => 'Command completed successfully; ack to dequeue',
1500 => 'Command completed successfully; ending session',
diff --git a/test/test_helper.rb b/test/test_helper.rb
index bee6fdcf9..1b70baf49 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -47,6 +47,7 @@ class ActiveSupport::TestCase
teardown do
travel_back
+ Setting.address_processing = false
end
end
@@ -60,9 +61,14 @@ class ApplicationIntegrationTest < ActionDispatch::IntegrationTest
WebMock.reset!
Capybara.reset_sessions!
Capybara.use_default_driver
+ Setting.address_processing = false
end
end
class EppTestCase < ActionDispatch::IntegrationTest
include Assertions::EppAssertions
+
+ teardown do
+ Setting.address_processing = false
+ end
end