diff --git a/app/controllers/api/v1/registrant/base_controller.rb b/app/controllers/api/v1/registrant/base_controller.rb index 16980be8b6..d85368bbcb 100644 --- a/app/controllers/api/v1/registrant/base_controller.rb +++ b/app/controllers/api/v1/registrant/base_controller.rb @@ -54,6 +54,10 @@ def show_not_found_error def show_invalid_record_error(exception) render json: { errors: exception.record.errors }, status: :bad_request end + + def company_register_api_enabled? + Setting.company_register_api_enabled + end end end end diff --git a/app/controllers/api/v1/registrant/companies_controller.rb b/app/controllers/api/v1/registrant/companies_controller.rb index d2060276d9..0c7a1f42dc 100644 --- a/app/controllers/api/v1/registrant/companies_controller.rb +++ b/app/controllers/api/v1/registrant/companies_controller.rb @@ -16,6 +16,8 @@ def index end def current_user_companies + return [:ok, []] unless company_register_api_enabled? + [:ok, current_registrant_user.companies] rescue CompanyRegister::NotAvailableError [:service_unavailable, []] diff --git a/app/controllers/api/v1/registrant/contacts_controller.rb b/app/controllers/api/v1/registrant/contacts_controller.rb index 1c22277df7..a3b31bd224 100644 --- a/app/controllers/api/v1/registrant/contacts_controller.rb +++ b/app/controllers/api/v1/registrant/contacts_controller.rb @@ -35,11 +35,21 @@ def show end def do_need_update_contacts + unless company_register_api_enabled? + render json: { update_contacts: false, counter: 0 } + return + end + result = current_registrant_user.do_need_update_contacts? render json: { update_contacts: result[:result], counter: result[:counter] } end def update_contacts + unless company_register_api_enabled? + render json: { message: 'get it', contacts: [] } + return + end + contacts = current_registrant_user.update_contacts render json: { message: 'get it', contacts: contacts } @@ -85,6 +95,7 @@ def representable_contact(uuid) contact = Contact.find_by(uuid: uuid, ident: current_registrant_user.ident, ident_type: 'priv', ident_country_code: country) return contact if contact + return nil unless company_register_api_enabled? Contact.find_by(uuid: uuid, ident_type: 'org', ident: company_codes, ident_country_code: country) @@ -97,6 +108,8 @@ def company_codes end def current_user_contacts + return current_registrant_user.direct_contacts unless company_register_api_enabled? + current_registrant_user.contacts(representable: false) rescue CompanyRegister::NotAvailableError current_registrant_user.direct_contacts diff --git a/app/controllers/api/v1/registrant/domains_controller.rb b/app/controllers/api/v1/registrant/domains_controller.rb index 95475a4989..5840e326cb 100644 --- a/app/controllers/api/v1/registrant/domains_controller.rb +++ b/app/controllers/api/v1/registrant/domains_controller.rb @@ -53,12 +53,16 @@ def set_tech_flag end def current_user_domains_total_count + return current_registrant_user.direct_domains.count unless company_register_api_enabled? + current_registrant_user.domains.count rescue CompanyRegister::NotAvailableError current_registrant_user.direct_domains.count end def current_user_domains + return init_count_of_direct_domains unless company_register_api_enabled? + init_count_of_domains rescue CompanyRegister::NotAvailableError init_count_of_direct_domains diff --git a/app/interactions/actions/contact_create.rb b/app/interactions/actions/contact_create.rb index 782efdf9a4..6acc98f326 100644 --- a/app/interactions/actions/contact_create.rb +++ b/app/interactions/actions/contact_create.rb @@ -84,7 +84,7 @@ def validate_ident_birthday end def maybe_company_is_relevant - return true if ENV['allow_validate_business_contacts'] && ENV['allow_validate_business_contacts'] == 'false' + return true unless Setting.validate_business_contacts return true unless contact.org? return true unless contact.ident_country_code == 'EE' diff --git a/db/migrate/20260410130124_add_validate_business_contacts_to_settings.rb b/db/migrate/20260410130124_add_validate_business_contacts_to_settings.rb new file mode 100644 index 0000000000..a59af4d339 --- /dev/null +++ b/db/migrate/20260410130124_add_validate_business_contacts_to_settings.rb @@ -0,0 +1,17 @@ +class AddValidateBusinessContactsToSettings < ActiveRecord::Migration[6.1] + def up + safety_assured do + execute <<~SQL + INSERT INTO setting_entries (code, value, format, "group", created_at, updated_at) + SELECT 'validate_business_contacts', 'true', 'boolean', 'contacts', NOW(), NOW() + WHERE NOT EXISTS (SELECT 1 FROM setting_entries WHERE code = 'validate_business_contacts'); + SQL + end + end + + def down + safety_assured do + execute "DELETE FROM setting_entries WHERE code = 'validate_business_contacts';" + end + end +end diff --git a/db/migrate/20260413125925_add_company_register_api_enabled_to_settings.rb b/db/migrate/20260413125925_add_company_register_api_enabled_to_settings.rb new file mode 100644 index 0000000000..ed099e855e --- /dev/null +++ b/db/migrate/20260413125925_add_company_register_api_enabled_to_settings.rb @@ -0,0 +1,17 @@ +class AddCompanyRegisterApiEnabledToSettings < ActiveRecord::Migration[6.1] + def up + safety_assured do + execute <<~SQL + INSERT INTO setting_entries (code, value, format, "group", created_at, updated_at) + SELECT 'company_register_api_enabled', 'true', 'boolean', 'contacts', NOW(), NOW() + WHERE NOT EXISTS (SELECT 1 FROM setting_entries WHERE code = 'company_register_api_enabled'); + SQL + end + end + + def down + safety_assured do + execute "DELETE FROM setting_entries WHERE code = 'company_register_api_enabled';" + end + end +end diff --git a/db/structure.sql b/db/structure.sql index f9fcd64f8c..c0806d0f66 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -1,8 +1,10 @@ \restrict WmlyozFAnc1c6zHWXudb7s2jRC1uKwHPlCMDikRGHsbNPX1TGBaq3KQ01YXVO8T + -- Dumped from database version 13.4 (Debian 13.4-4.pgdg110+1) -- Dumped by pg_dump version 13.23 (Debian 13.23-1.pgdg11+1) + SET statement_timeout = 0; SET lock_timeout = 0; SET idle_in_transaction_session_timeout = 0; @@ -5863,6 +5865,6 @@ INSERT INTO "schema_migrations" (version) VALUES ('20260406125446'), ('20260529120000'), ('20260601120000'), -('20260608120000'); - - +('20260608120000'), +('20260410130124'), +('20260413125925'); diff --git a/test/fixtures/setting_entries.yml b/test/fixtures/setting_entries.yml index 2df870258e..8b74f035c4 100644 --- a/test/fixtures/setting_entries.yml +++ b/test/fixtures/setting_entries.yml @@ -493,3 +493,19 @@ admin_contacts_allowed_ident_type: format: array created_at: <%= Time.zone.parse('2010-07-05') %> updated_at: <%= Time.zone.parse('2010-07-05') %> + +validate_business_contacts: + code: validate_business_contacts + value: 'true' + group: contacts + format: boolean + created_at: <%= Time.zone.parse('2010-07-05') %> + updated_at: <%= Time.zone.parse('2010-07-05') %> + +company_register_api_enabled: + code: company_register_api_enabled + value: 'true' + group: contacts + format: boolean + created_at: <%= Time.zone.parse('2010-07-05') %> + updated_at: <%= Time.zone.parse('2010-07-05') %> diff --git a/test/integration/api/registrant/registrant_api_companies_test.rb b/test/integration/api/registrant/registrant_api_companies_test.rb index 4f23aeac1d..b101035f5d 100644 --- a/test/integration/api/registrant/registrant_api_companies_test.rb +++ b/test/integration/api/registrant/registrant_api_companies_test.rb @@ -34,6 +34,18 @@ def test_format assert_equal(:companies, response_json.keys.first) end + def test_returns_empty_companies_when_company_register_api_disabled + Setting.company_register_api_enabled = 'false' + + get '/api/v1/registrant/companies', headers: @auth_headers + response_json = JSON.parse(response.body, symbolize_names: true) + + assert_equal(200, response.status) + assert_equal([], response_json[:companies]) + ensure + Setting.company_register_api_enabled = 'true' + end + private def auth_token diff --git a/test/integration/api/registrant/registrant_api_contacts_test.rb b/test/integration/api/registrant/registrant_api_contacts_test.rb index f3998a2e91..cd8ff3d29b 100644 --- a/test/integration/api/registrant/registrant_api_contacts_test.rb +++ b/test/integration/api/registrant/registrant_api_contacts_test.rb @@ -66,6 +66,19 @@ def test_gets_contact_domain_links_when_requested assert_empty expected_links - response_json[:links] end + + def test_returns_only_direct_contacts_when_company_register_api_disabled + Setting.company_register_api_enabled = 'false' + + get '/api/v1/registrant/contacts', headers: @auth_headers + assert_equal(200, response.status) + + response_json = JSON.parse(response.body, symbolize_names: true) + assert response_json.is_a?(Array) + ensure + Setting.company_register_api_enabled = 'true' + end + private def auth_token diff --git a/test/integration/api/registrant/registrant_api_domains_test.rb b/test/integration/api/registrant/registrant_api_domains_test.rb index a8801f1b4a..bca8f3fd5b 100644 --- a/test/integration/api/registrant/registrant_api_domains_test.rb +++ b/test/integration/api/registrant/registrant_api_domains_test.rb @@ -153,6 +153,18 @@ def test_details_returns_401_without_authorization assert_equal({ errors: [base: ['Not authorized']] }, json_body) end + def test_returns_only_direct_domains_when_company_register_api_disabled + Setting.company_register_api_enabled = 'false' + + get '/api/v1/registrant/domains', headers: @auth_headers + assert_equal(200, response.status) + + response_json = JSON.parse(response.body, symbolize_names: true) + assert response_json[:domains].is_a?(Array) + ensure + Setting.company_register_api_enabled = 'true' + end + private def auth_token diff --git a/test/integration/repp/v1/contacts/create_test.rb b/test/integration/repp/v1/contacts/create_test.rb index a57e0d0d7c..a821846ee8 100644 --- a/test/integration/repp/v1/contacts/create_test.rb +++ b/test/integration/repp/v1/contacts/create_test.rb @@ -222,7 +222,7 @@ def object.simple_data(registration_number:) end def test_skip_company_validation_if_flag_is_set - ENV['allow_validate_business_contacts'] = 'false' + Setting.validate_business_contacts = false original_new_method = CompanyRegister::Client.method(:new) CompanyRegister::Client.define_singleton_method(:new) do object = original_new_method.call @@ -253,7 +253,7 @@ def object.simple_data(registration_number:) assert_equal 'Command completed successfully', json[:message] CompanyRegister::Client.define_singleton_method(:new, original_new_method) - ENV['allow_validate_business_contacts'] = 'true' + Setting.validate_business_contacts = true end diff --git a/test/interactions/actions/contact_create_test.rb b/test/interactions/actions/contact_create_test.rb new file mode 100644 index 0000000000..ac58f82b9f --- /dev/null +++ b/test/interactions/actions/contact_create_test.rb @@ -0,0 +1,47 @@ +require 'test_helper' + +class Actions::ContactCreateTest < ActiveSupport::TestCase + setup do + @contact = contacts(:acme_ltd) + @contact.ident_country_code = 'EE' + @ident = { ident: @contact.ident, + ident_type: @contact.ident_type, + ident_country_code: @contact.ident_country_code } + end + + teardown do + Setting.validate_business_contacts = 'true' + end + + def test_maybe_company_is_relevant_returns_true_when_toggle_disabled + Setting.validate_business_contacts = 'false' + + @contact.stub :return_company_status, ->(*) { flunk 'company register must not be called' } do + action = Actions::ContactCreate.new(@contact, nil, @ident) + assert_equal true, action.maybe_company_is_relevant + end + + epp_msgs = @contact.errors.where(:epp_errors).map { |e| e.options[:msg] } + assert_empty epp_msgs + end + + def test_maybe_company_is_relevant_checks_register_when_toggle_enabled_and_company_registered + Setting.validate_business_contacts = 'true' + + @contact.stub :return_company_status, Contact::REGISTERED do + action = Actions::ContactCreate.new(@contact, nil, @ident) + assert_equal true, action.maybe_company_is_relevant + end + end + + def test_maybe_company_is_relevant_adds_error_when_toggle_enabled_and_company_missing + Setting.validate_business_contacts = 'true' + + @contact.stub :return_company_status, 'N' do + action = Actions::ContactCreate.new(@contact, nil, @ident) + action.maybe_company_is_relevant + error_texts = @contact.errors.where(:epp_errors).map { |e| e.options[:msg] } + assert(error_texts.any? { |msg| msg.to_s.include?(I18n.t('errors.messages.company_not_registered')) }) + end + end +end