Merge pull request #2240 from internetee/2213-email-checking

Email checking for creating/updating domains
This commit is contained in:
Timo Võhmar 2022-04-06 13:54:13 +03:00 committed by GitHub
commit b15ef00d77
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 3 deletions

View file

@ -88,14 +88,16 @@ class Registrar
@domain_params[:period] = Depp::Domain.default_period @domain_params[:period] = Depp::Domain.default_period
end end
# rubocop:disable Metrics/CognitiveComplexity
def create def create
authorize! :create, Depp::Domain authorize! :create, Depp::Domain
@domain_params = domain_params.to_h @domain_params = domain_params.to_h
@data = @domain.create(@domain_params) @data = @domain.create(@domain_params)
if response_ok? if @data && response_ok?
redirect_to info_registrar_domains_url(domain_name: @domain_params[:name]) redirect_to info_registrar_domains_url(domain_name: @domain_params[:name])
else else
flash[:alert] = t('.email_error_message') unless @emails_check_result
render 'new' render 'new'
end end
end end
@ -113,13 +115,15 @@ class Registrar
@data = @domain.update(@domain_params) @data = @domain.update(@domain_params)
@dispute = Dispute.active.find_by(domain_name: @domain_params[:name]) @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]) redirect_to info_registrar_domains_url(domain_name: @domain_params[:name])
else else
flash[:alert] = t('.email_error_message') unless @emails_check_result
params[:domain_name] = @domain_params[:name] params[:domain_name] = @domain_params[:name]
render 'new' render 'new'
end end
end end
# rubocop:enable Metrics/CognitiveComplexity
def delete def delete
authorize! :delete, Depp::Domain authorize! :delete, Depp::Domain

View file

@ -45,6 +45,10 @@ module Actions
def assign_new_registrant def assign_new_registrant
domain.add_epp_error('2306', nil, nil, %i[registrant cannot_be_missing]) unless params[:registrant][:code] 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]) regt = Registrant.find_by(code: params[:registrant][:code])
unless regt unless regt
domain.add_epp_error('2303', 'registrant', params[:registrant], %i[registrant not_found]) 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 @dnskeys << { id: dnkey.id, _destroy: 1 } if dnkey
end 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 def assign_admin_contact_changes
props = gather_domain_contacts(params[:contacts].select { |c| c[:type] == 'admin' }) 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? if props.any? && domain.admin_change_prohibited?
domain.add_epp_error('2304', 'admin', DomainStatus::SERVER_ADMIN_CHANGE_PROHIBITED, domain.add_epp_error('2304', 'admin', DomainStatus::SERVER_ADMIN_CHANGE_PROHIBITED,
I18n.t(:object_status_prohibits_operation)) I18n.t(:object_status_prohibits_operation))
@ -136,6 +166,8 @@ module Actions
props = gather_domain_contacts(params[:contacts].select { |c| c[:type] == 'tech' }, props = gather_domain_contacts(params[:contacts].select { |c| c[:type] == 'tech' },
admin: false) admin: false)
start_validate_email(props) if props.present?
if props.any? && domain.tech_change_prohibited? if props.any? && domain.tech_change_prohibited?
domain.add_epp_error('2304', 'tech', DomainStatus::SERVER_TECH_CHANGE_PROHIBITED, domain.add_epp_error('2304', 'tech', DomainStatus::SERVER_TECH_CHANGE_PROHIBITED,
I18n.t(:object_status_prohibits_operation)) I18n.t(:object_status_prohibits_operation))

View file

@ -1,6 +1,8 @@
en: en:
registrar: registrar:
domains: domains:
email_error_message: 'Check contacts emails'
index: index:
header: Domains header: Domains
new_btn: New domain new_btn: New domain

View file

@ -52,6 +52,8 @@ class ReppV1DomainsContactsTest < ActionDispatch::IntegrationTest
end end
def test_can_remove_admin_contacts def test_can_remove_admin_contacts
Spy.on_instance_method(Actions::DomainUpdate, :validate_email).and_return(true)
contact = contacts(:john) contact = contacts(:john)
payload = { contacts: [ { code: contact.code, type: 'admin' } ] } payload = { contacts: [ { code: contact.code, type: 'admin' } ] }
post "/repp/v1/domains/#{@domain.name}/contacts", headers: @auth_headers, params: payload post "/repp/v1/domains/#{@domain.name}/contacts", headers: @auth_headers, params: payload
@ -68,6 +70,8 @@ class ReppV1DomainsContactsTest < ActionDispatch::IntegrationTest
end end
def test_can_remove_tech_contacts def test_can_remove_tech_contacts
Spy.on_instance_method(Actions::DomainUpdate, :validate_email).and_return(true)
contact = contacts(:john) contact = contacts(:john)
payload = { contacts: [ { code: contact.code, type: 'tech' } ] } payload = { contacts: [ { code: contact.code, type: 'tech' } ] }
post "/repp/v1/domains/#{@domain.name}/contacts", headers: @auth_headers, params: payload 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 delete "/repp/v1/domains/#{@domain.name}/contacts", headers: @auth_headers, params: payload
json = JSON.parse(response.body, symbolize_names: true) json = JSON.parse(response.body, symbolize_names: true)
@domain.reload
contact.reload
assert_response :ok assert_response :ok
assert_equal 1000, json[:code] assert_equal 1000, json[:code]
@ -84,6 +91,8 @@ class ReppV1DomainsContactsTest < ActionDispatch::IntegrationTest
end end
def test_can_not_remove_one_and_only_contact 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 contact = @domain.admin_contacts.last
payload = { contacts: [ { code: contact.code, type: 'admin' } ] } payload = { contacts: [ { code: contact.code, type: 'admin' } ] }
@ -96,5 +105,4 @@ class ReppV1DomainsContactsTest < ActionDispatch::IntegrationTest
assert @domain.admin_contacts.any? assert @domain.admin_contacts.any?
end end
end end