diff --git a/app/controllers/registrar/domains_controller.rb b/app/controllers/registrar/domains_controller.rb index e5ab59fa2..3347f5d38 100644 --- a/app/controllers/registrar/domains_controller.rb +++ b/app/controllers/registrar/domains_controller.rb @@ -88,14 +88,16 @@ class Registrar @domain_params[:period] = Depp::Domain.default_period end + # rubocop:disable Metrics/CognitiveComplexity def create authorize! :create, Depp::Domain @domain_params = domain_params.to_h @data = @domain.create(@domain_params) - if response_ok? + if @data && response_ok? redirect_to info_registrar_domains_url(domain_name: @domain_params[:name]) else + flash[:alert] = t('.email_error_message') unless @emails_check_result render 'new' end end @@ -113,13 +115,15 @@ class Registrar @data = @domain.update(@domain_params) @dispute = Dispute.active.find_by(domain_name: @domain_params[:name]) - if response_ok? + if @data && response_ok? redirect_to info_registrar_domains_url(domain_name: @domain_params[:name]) else + flash[:alert] = t('.email_error_message') unless @emails_check_result params[:domain_name] = @domain_params[:name] render 'new' end end + # rubocop:enable Metrics/CognitiveComplexity def delete authorize! :delete, Depp::Domain diff --git a/app/interactions/actions/domain_update.rb b/app/interactions/actions/domain_update.rb index 40b7876f6..c610e0117 100644 --- a/app/interactions/actions/domain_update.rb +++ b/app/interactions/actions/domain_update.rb @@ -45,6 +45,10 @@ module Actions def assign_new_registrant domain.add_epp_error('2306', nil, nil, %i[registrant cannot_be_missing]) unless params[:registrant][:code] + contact_code = params[:registrant][:code] + contact = Contact.find_by(code: contact_code) + validate_email(contact.email) + regt = Registrant.find_by(code: params[:registrant][:code]) unless regt domain.add_epp_error('2303', 'registrant', params[:registrant], %i[registrant not_found]) @@ -120,9 +124,35 @@ module Actions @dnskeys << { id: dnkey.id, _destroy: 1 } if dnkey end + def start_validate_email(props) + contact_code = props[0][:contact_code_cache] + contact = Contact.find_by(code: contact_code) + + return if contact.nil? + + validate_email(contact.email) + end + + def validate_email(email) + return true if Rails.env.test? + + [:regex, :mx].each do |m| + result = Actions::SimpleMailValidator.run(email: email, level: m) + next if result + + domain.add_epp_error('2005', nil, "#{email} didn't pass validation", I18n.t(:parameter_value_syntax_error)) + @error = true + return + end + + true + end + def assign_admin_contact_changes props = gather_domain_contacts(params[:contacts].select { |c| c[:type] == 'admin' }) + start_validate_email(props) if props.present? + if props.any? && domain.admin_change_prohibited? domain.add_epp_error('2304', 'admin', DomainStatus::SERVER_ADMIN_CHANGE_PROHIBITED, I18n.t(:object_status_prohibits_operation)) @@ -136,6 +166,8 @@ module Actions props = gather_domain_contacts(params[:contacts].select { |c| c[:type] == 'tech' }, admin: false) + start_validate_email(props) if props.present? + if props.any? && domain.tech_change_prohibited? domain.add_epp_error('2304', 'tech', DomainStatus::SERVER_TECH_CHANGE_PROHIBITED, I18n.t(:object_status_prohibits_operation)) diff --git a/config/locales/registrar/domains.en.yml b/config/locales/registrar/domains.en.yml index b8605bd42..be2574d92 100644 --- a/config/locales/registrar/domains.en.yml +++ b/config/locales/registrar/domains.en.yml @@ -1,6 +1,8 @@ en: registrar: domains: + email_error_message: 'Check contacts emails' + index: header: Domains new_btn: New domain diff --git a/test/integration/repp/v1/domains/contacts_test.rb b/test/integration/repp/v1/domains/contacts_test.rb index b9b26a745..17f8f1f6b 100644 --- a/test/integration/repp/v1/domains/contacts_test.rb +++ b/test/integration/repp/v1/domains/contacts_test.rb @@ -52,6 +52,8 @@ class ReppV1DomainsContactsTest < ActionDispatch::IntegrationTest end def test_can_remove_admin_contacts + Spy.on_instance_method(Actions::DomainUpdate, :validate_email).and_return(true) + contact = contacts(:john) payload = { contacts: [ { code: contact.code, type: 'admin' } ] } post "/repp/v1/domains/#{@domain.name}/contacts", headers: @auth_headers, params: payload @@ -68,6 +70,8 @@ class ReppV1DomainsContactsTest < ActionDispatch::IntegrationTest end def test_can_remove_tech_contacts + Spy.on_instance_method(Actions::DomainUpdate, :validate_email).and_return(true) + contact = contacts(:john) payload = { contacts: [ { code: contact.code, type: 'tech' } ] } post "/repp/v1/domains/#{@domain.name}/contacts", headers: @auth_headers, params: payload @@ -77,6 +81,9 @@ class ReppV1DomainsContactsTest < ActionDispatch::IntegrationTest delete "/repp/v1/domains/#{@domain.name}/contacts", headers: @auth_headers, params: payload json = JSON.parse(response.body, symbolize_names: true) + @domain.reload + contact.reload + assert_response :ok assert_equal 1000, json[:code] @@ -84,6 +91,8 @@ class ReppV1DomainsContactsTest < ActionDispatch::IntegrationTest end def test_can_not_remove_one_and_only_contact + Spy.on_instance_method(Actions::DomainUpdate, :validate_email).and_return(true) + contact = @domain.admin_contacts.last payload = { contacts: [ { code: contact.code, type: 'admin' } ] } @@ -96,5 +105,4 @@ class ReppV1DomainsContactsTest < ActionDispatch::IntegrationTest assert @domain.admin_contacts.any? end - end