diff --git a/app/controllers/api/v1/registrant/contacts_controller.rb b/app/controllers/api/v1/registrant/contacts_controller.rb index d26255729..4a2be2159 100644 --- a/app/controllers/api/v1/registrant/contacts_controller.rb +++ b/app/controllers/api/v1/registrant/contacts_controller.rb @@ -53,7 +53,8 @@ module Api if disclosed_attributes if contact.org? - error_msg = "Legal person's data cannot be concealed. Please remove this parameter." + error_msg = "Legal person's data is visible by default and cannot be concealed." \ + ' Please remove this parameter.' render json: { errors: [{ disclosed_attributes: [error_msg] }] }, status: :bad_request return end diff --git a/app/controllers/registrant/contacts_controller.rb b/app/controllers/registrant/contacts_controller.rb index a2be10487..c9c14f4d6 100644 --- a/app/controllers/registrant/contacts_controller.rb +++ b/app/controllers/registrant/contacts_controller.rb @@ -106,13 +106,15 @@ class Registrant::ContactsController < RegistrantController def normalize_address_attributes_for_api(params) normalized = params + address_parts = {} Contact.address_attribute_names.each do |attr| attr = attr.to_sym - normalized["address[#{attr}]"] = params[attr] + address_parts[attr] = params[attr] normalized.delete(attr) end + normalized[:address] = address_parts normalized end @@ -120,7 +122,8 @@ class Registrant::ContactsController < RegistrantController uri = URI.parse("#{ENV['registrant_api_base_url']}/api/v1/registrant/contacts/#{uuid}") request = Net::HTTP::Patch.new(uri) request['Authorization'] = "Bearer #{access_token}" - request.form_data = contact_update_api_params + request['Content-type'] = 'application/json' + request.body = contact_update_api_params.to_json Net::HTTP.start(uri.hostname, uri.port, use_ssl: (uri.scheme == 'https')) do |http| http.request(request) diff --git a/config/initializers/inflections.rb b/config/initializers/inflections.rb index b4eccb451..1013f270b 100644 --- a/config/initializers/inflections.rb +++ b/config/initializers/inflections.rb @@ -12,4 +12,5 @@ ActiveSupport::Inflector.inflections(:en) do |inflect| inflect.acronym 'DNS' + inflect.irregular 'business_registry_cache', 'business_registry_caches' end diff --git a/test/fixtures/business_registry_caches.yml b/test/fixtures/business_registry_caches.yml index c55c98e0d..a54f1ffad 100644 --- a/test/fixtures/business_registry_caches.yml +++ b/test/fixtures/business_registry_caches.yml @@ -1,8 +1,7 @@ -first: +one: ident: 1234 ident_country_code: US - associated_businesses: - - 1234 + associated_businesses: [] retrieved_on: 2010-07-05 10:30 created_at: 2010-07-05 10:30 updated_at: 2010-07-05 10:30 diff --git a/test/integration/api/v1/registrant/contacts/update_test.rb b/test/integration/api/v1/registrant/contacts/update_test.rb index abf483d9c..00e66a1c8 100644 --- a/test/integration/api/v1/registrant/contacts/update_test.rb +++ b/test/integration/api/v1/registrant/contacts/update_test.rb @@ -9,6 +9,8 @@ class RegistrantApiV1ContactUpdateTest < ActionDispatch::IntegrationTest @original_business_registry_cache_setting = Setting.days_to_keep_business_registry_cache @original_fax_enabled_setting = ENV['fax_enabled'] + @current_user = users(:registrant) + Setting.days_to_keep_business_registry_cache = 1 travel_to Time.zone.parse('2010-07-05') end @@ -157,19 +159,24 @@ class RegistrantApiV1ContactUpdateTest < ActionDispatch::IntegrationTest assert_empty @contact.disclosed_attributes end - def test_legal_persons_data_cannot_be_concealed + def test_legal_persons_disclosed_attributes_cannot_be_changed + business_registry_caches(:one).update!(associated_businesses: %w[1234]) @contact.update!(ident_type: Contact::ORG, + ident: '1234', disclosed_attributes: %w[]) assert_no_changes -> { @contact.disclosed_attributes } do - patch api_v1_registrant_contact_path(@contact.uuid), { disclosed_attributes: %w[name] }.to_json, + patch api_v1_registrant_contact_path(@contact.uuid), { disclosed_attributes: %w[name] } + .to_json, 'HTTP_AUTHORIZATION' => auth_token, 'Accept' => Mime::JSON, 'Content-Type' => Mime::JSON.to_s @contact.reload end assert_response :bad_request - error_msg = "Legal person's data cannot be concealed. Please remove this parameter." + + error_msg = "Legal person's data is visible by default and cannot be concealed." \ + ' Please remove this parameter.' assert_equal ({ errors: [{ disclosed_attributes: [error_msg] }] }), JSON.parse(response.body, symbolize_names: true) end @@ -214,17 +221,18 @@ class RegistrantApiV1ContactUpdateTest < ActionDispatch::IntegrationTest symbolize_names: true) end - def test_contact_of_another_user_cannot_be_updated - @contact = contacts(:jack) + def test_unmanaged_contact_cannot_be_updated + @current_user.update!(registrant_ident: 'US-1234') + @contact.update!(ident: '12345') - patch api_v1_registrant_contact_path(@contact.uuid), { name: 'any' }.to_json, + patch api_v1_registrant_contact_path(@contact.uuid), { name: 'new name' }.to_json, 'HTTP_AUTHORIZATION' => auth_token, 'Accept' => Mime::JSON, 'Content-Type' => Mime::JSON.to_s + @contact.reload assert_response :not_found - @contact.reload - assert_not_equal 'any', @contact.name + assert_not_equal 'new name', @contact.name end def test_non_existent_contact @@ -244,7 +252,7 @@ class RegistrantApiV1ContactUpdateTest < ActionDispatch::IntegrationTest private def auth_token - token_creator = AuthTokenCreator.create_with_defaults(users(:registrant)) + token_creator = AuthTokenCreator.create_with_defaults(@current_user) hash = token_creator.token_in_hash "Bearer #{hash[:access_token]}" end diff --git a/test/system/registrant_area/contacts/update_test.rb b/test/system/registrant_area/contacts/update_test.rb index 7bb3e3909..58f44bc95 100644 --- a/test/system/registrant_area/contacts/update_test.rb +++ b/test/system/registrant_area/contacts/update_test.rb @@ -34,8 +34,9 @@ class RegistrantAreaContactUpdateTest < ApplicationIntegrationTest def test_update_contact stub_auth_request - request_body = { name: 'new name', email: 'new@inbox.test', phone: '+666.6' } - headers = { 'Authorization' => 'Bearer test-access-token' } + request_body = { name: 'new name', email: 'new@inbox.test', phone: '+666.6' }.to_json + headers = { 'Content-Type' => Mime::JSON, + 'Authorization' => 'Bearer test-access-token' } url = "https://api.test/api/v1/registrant/contacts/#{@contact.uuid}" update_request_stub = stub_request(:patch, url).with(body: request_body, headers: headers) .to_return(body: '{}', status: 200) @@ -104,17 +105,18 @@ class RegistrantAreaContactUpdateTest < ApplicationIntegrationTest Setting.address_processing = true stub_auth_request - request_body = { email: 'john@inbox.test', - name: 'John', + request_body = { name: 'John', + email: 'john@inbox.test', phone: '+555.555', address: { + city: 'new city', street: 'new street', zip: '93742', - city: 'new city', + country_code: 'AT', state: 'new state', - country_code: 'AT' - } } - headers = { 'Authorization' => 'Bearer test-access-token' } + } }.to_json + headers = { 'Content-type' => 'application/json', + 'Authorization' => 'Bearer test-access-token' } url = "https://api.test/api/v1/registrant/contacts/#{@contact.uuid}" update_request_stub = stub_request(:patch, url).with(body: request_body, headers: headers) .to_return(body: '{}', status: 200)