Take into account address_processing setting when creating and updating the contact via EPP

#251
This commit is contained in:
Artur Beljajev 2016-12-14 00:38:20 +02:00
parent a77a0ae558
commit d3b1a23e92
11 changed files with 311 additions and 164 deletions

View file

@ -24,13 +24,15 @@ class Epp::ContactsController < EppController
@contact.generate_code @contact.generate_code
if @contact.save if @contact.save
@response_code = if Contact.address_processing? if !Contact.address_processing? && address_given?
1000 @response_code = 1100
@response_description = t('epp.contacts.completed_without_address')
else else
frame.css('postalInfo addr').size != 0 ? 1100 : 1000 @response_code = 1000
@response_description = t('epp.contacts.completed')
end end
render_epp_response '/epp/contacts/create' render_epp_response '/epp/contacts/save'
else else
handle_errors(@contact) handle_errors(@contact)
end end
@ -39,8 +41,18 @@ class Epp::ContactsController < EppController
def update def update
authorize! :update, @contact, @password authorize! :update, @contact, @password
if @contact.update_attributes(params[:parsed_frame], current_user) frame = params[:parsed_frame]
render_epp_response 'epp/contacts/update'
if @contact.update_attributes(frame, current_user)
if !Contact.address_processing? && address_given?
@response_code = 1100
@response_description = t('epp.contacts.completed_without_address')
else
@response_code = 1000
@response_description = t('epp.contacts.completed')
end
render_epp_response 'epp/contacts/save'
else else
handle_errors(@contact) handle_errors(@contact)
end end
@ -99,10 +111,23 @@ class Epp::ContactsController < EppController
def validate_create def validate_create
@prefix = 'create > create >' @prefix = 'create > create >'
requires(
'postalInfo > name', 'postalInfo > addr > street', 'postalInfo > addr > city', required_attributes = [
'postalInfo > addr > pc', 'postalInfo > addr > cc', 'voice', 'email' 'postalInfo > name',
) 'voice',
'email'
]
address_attributes = [
'postalInfo > addr > street',
'postalInfo > addr > city',
'postalInfo > addr > pc',
'postalInfo > addr > cc',
]
required_attributes.concat(address_attributes) if Contact.address_processing?
requires(*required_attributes)
ident = params[:parsed_frame].css('ident') ident = params[:parsed_frame].css('ident')
if ident.present? && ident.attr('type').blank? if ident.present? && ident.attr('type').blank?
@ -173,4 +198,8 @@ class Epp::ContactsController < EppController
msg: "#{I18n.t(:client_side_status_editing_error)}: status [status]" msg: "#{I18n.t(:client_side_status_editing_error)}: status [status]"
} }
end end
def address_given?
params[:parsed_frame].css('postalInfo addr').size != 0
end
end end

View file

@ -37,7 +37,8 @@ class Contact < ActiveRecord::Base
validate :val_ident_type validate :val_ident_type
validate :val_ident_valid_format? validate :val_ident_valid_format?
validate :validate_html validate :validate_html
validate :val_country_code validate :validate_country_code
validate :validate_ident_country_code
after_initialize do after_initialize do
self.status_notes = {} if status_notes.nil? self.status_notes = {} if status_notes.nil?
@ -417,9 +418,13 @@ class Contact < ActiveRecord::Base
self.country_code = country_code.upcase if country_code self.country_code = country_code.upcase if country_code
end end
def val_country_code def validate_country_code
return unless country_code
errors.add(:country_code, :invalid) unless Country.new(country_code)
end
def validate_ident_country_code
errors.add(:ident, :invalid_country_code) unless Country.new(ident_country_code) errors.add(:ident, :invalid_country_code) unless Country.new(ident_country_code)
errors.add(:ident, :invalid_country_code) unless Country.new(country_code)
end end
def related_domain_descriptions def related_domain_descriptions

View file

@ -30,11 +30,15 @@ class Epp::Contact < Contact
at[:email] = f.css('email').text if f.css('email').present? at[:email] = f.css('email').text if f.css('email').present?
at[:fax] = f.css('fax').text if f.css('fax').present? at[:fax] = f.css('fax').text if f.css('fax').present?
at[:phone] = f.css('voice').text if f.css('voice').present? at[:phone] = f.css('voice').text if f.css('voice').present?
if address_processing?
at[:city] = f.css('postalInfo addr city').text if f.css('postalInfo addr city').present? at[:city] = f.css('postalInfo addr city').text if f.css('postalInfo addr city').present?
at[:zip] = f.css('postalInfo addr pc').text if f.css('postalInfo addr pc').present? at[:zip] = f.css('postalInfo addr pc').text if f.css('postalInfo addr pc').present?
at[:street] = f.css('postalInfo addr street').text if f.css('postalInfo addr street').present? at[:street] = f.css('postalInfo addr street').text if f.css('postalInfo addr street').present?
at[:state] = f.css('postalInfo addr sp').text if f.css('postalInfo addr sp').present? at[:state] = f.css('postalInfo addr sp').text if f.css('postalInfo addr sp').present?
at[:country_code] = f.css('postalInfo addr cc').text if f.css('postalInfo addr cc').present? at[:country_code] = f.css('postalInfo addr cc').text if f.css('postalInfo addr cc').present?
end
at[:auth_info] = f.css('authInfo pw').text if f.css('authInfo pw').present? at[:auth_info] = f.css('authInfo pw').text if f.css('authInfo pw').present?
@ -125,6 +129,7 @@ class Epp::Contact < Contact
[:ident, :invalid_EE_identity_format_update], [:ident, :invalid_EE_identity_format_update],
[:ident, :invalid_birthday_format], [:ident, :invalid_birthday_format],
[:ident, :invalid_country_code], [:ident, :invalid_country_code],
[:country_code, :invalid],
[:ident_type, :missing], [:ident_type, :missing],
[:code, :invalid], [:code, :invalid],
[:code, :too_long_contact_code] [:code, :too_long_contact_code]

View file

@ -1,7 +1,7 @@
xml.epp_head do xml.epp_head do
xml.response do xml.response do
xml.result('code' => @response_code) do xml.result('code' => @response_code) do
xml.msg 'Command completed successfully' xml.msg @response_description
end end
xml.resData do xml.resData do

View file

@ -1,16 +0,0 @@
xml.epp_head do
xml.response do
xml.result('code' => '1000') do
xml.msg 'Command completed successfully'
end
xml.resData do
xml.tag!('contact:creData', 'xmlns:contact' => 'https://epp.tld.ee/schema/contact-ee-1.1.xsd') do
xml.tag!('contact:id', @contact.code)
xml.tag!('contact:crDate', @contact.created_at.try(:iso8601))
end
end
render('epp/shared/trID', builder: xml)
end
end

View file

@ -0,0 +1,8 @@
en:
activerecord:
errors:
models:
contact:
attributes:
country_code:
invalid: Country code is not valid, should be in ISO_3166-1 alpha 2 format

View file

@ -0,0 +1,5 @@
en:
epp:
contacts:
completed: Command completed successfully
completed_without_address: Command completed successfully; Postal address data discarded

View file

@ -438,4 +438,14 @@ RSpec.describe Contact, db: false do
end end
end end
end end
describe 'country code validation' do
let(:contact) { described_class.new(country_code: 'test') }
it 'rejects invalid' do
contact.country_code = 'invalid'
contact.validate
expect(contact.errors).to have_key(:country_code)
end
end
end end

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long