Skip to content
4 changes: 4 additions & 0 deletions app/controllers/api/v1/registrant/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/api/v1/registrant/companies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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, []]
Expand Down
13 changes: 13 additions & 0 deletions app/controllers/api/v1/registrant/contacts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/api/v1/registrant/domains_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/interactions/actions/contact_create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
8 changes: 5 additions & 3 deletions db/structure.sql
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -5863,6 +5865,6 @@ INSERT INTO "schema_migrations" (version) VALUES
('20260406125446'),
('20260529120000'),
('20260601120000'),
('20260608120000');


('20260608120000'),
('20260410130124'),
('20260413125925');
16 changes: 16 additions & 0 deletions test/fixtures/setting_entries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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') %>
12 changes: 12 additions & 0 deletions test/integration/api/registrant/registrant_api_companies_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions test/integration/api/registrant/registrant_api_contacts_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions test/integration/api/registrant/registrant_api_domains_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/integration/repp/v1/contacts/create_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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


Expand Down
47 changes: 47 additions & 0 deletions test/interactions/actions/contact_create_test.rb
Original file line number Diff line number Diff line change
@@ -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