requires_attribute helper

This commit is contained in:
Martin Lensment 2015-01-27 15:08:45 +02:00
parent 6187c3fc9f
commit 857ab488b8
4 changed files with 54 additions and 17 deletions

View file

@ -127,45 +127,46 @@ class Epp::DomainsController < EppController
def validate_create def validate_create
@prefix = 'create > create >' @prefix = 'create > create >'
requires('name', 'ns', 'registrant', 'ns > hostAttr') requires 'name', 'ns', 'registrant', 'ns > hostAttr'
@prefix = 'extension > create >' @prefix = 'extension > create >'
mutually_exclusive 'keyData', 'dsData' mutually_exclusive 'keyData', 'dsData'
@prefix = nil @prefix = nil
requires('extension > extdata > legalDocument') requires 'extension > extdata > legalDocument'
end end
def validate_renew def validate_renew
@prefix = 'renew > renew >' @prefix = 'renew > renew >'
requires('name', 'curExpDate', 'period') requires 'name', 'curExpDate', 'period'
end end
def validate_update def validate_update
if element_count('update > chg > registrant') > 0 if element_count('update > chg > registrant') > 0
requires('extension > extdata > legalDocument') requires 'extension > extdata > legalDocument'
end end
@prefix = 'update > update >' @prefix = 'update > update >'
requires('name') requires 'name'
end end
## TRANSFER ## TRANSFER
def validate_transfer def validate_transfer
@prefix = 'transfer > transfer >' requires 'transfer > transfer'
requires('name')
op = params[:parsed_frame].css('transfer').first[:op] @prefix = 'transfer > transfer >'
return if %w(approve query reject).include?(op) requires 'name'
epp_errors << { code: '2306', msg: I18n.t('errors.messages.attribute_op_is_invalid') }
@prefix = nil
requires_attribute 'transfer', 'op', values: %(approve, query, reject)
end end
## DELETE ## DELETE
def validate_delete def validate_delete
requires('extension > extdata > legalDocument') requires 'extension > extdata > legalDocument'
@prefix = 'delete > delete >' @prefix = 'delete > delete >'
requires('name') requires 'name'
end end
def domain_create_params def domain_create_params

View file

@ -62,20 +62,56 @@ class EppController < ApplicationController
handle_errors and return if epp_errors.any? handle_errors and return if epp_errors.any?
end end
# let's follow grape's validations: https://github.com/intridea/grape/#parameter-validation-and-coercion
# Example usage:
#
# requires 'transfer'
#
# Adds error to epp_errors if element is missing or blank
# Returns last element of selectors if it exists
#
# TODO: Add possibility to pass validations / options in the method
def requires(*selectors) def requires(*selectors)
el, missing = nil, nil
selectors.each do |selector| selectors.each do |selector|
full_selector = [@prefix, selector].join(' ') full_selector = [@prefix, selector].join(' ')
el = params[:parsed_frame].css(full_selector).first el = params[:parsed_frame].css(full_selector).first
missing = el.nil? || (el.text.blank? && el.children.none?)
epp_errors << { epp_errors << {
code: '2003', code: '2003',
msg: I18n.t('errors.messages.required_parameter_missing', key: el.try(:name) || selector) msg: I18n.t('errors.messages.required_parameter_missing', key: el.try(:name) || selector)
} if el.nil? || el.text.blank? } if missing
end end
epp_errors.empty? missing ? false : el # return last selector if it was present
end
# Example usage:
#
# requires_attribute 'transfer', 'op', values: %(approve, query, reject)
#
# Adds error to epp_errors if element or attribute is missing or attribute attribute is not one
# of the values
def requires_attribute(element_selector, attribute_selector, options)
element = requires(element_selector)
return unless element
attribute = element[attribute_selector]
values = options.delete(:values)
return if attribute && values.include?(attribute)
epp_errors << {
code: '2306',
msg: I18n.t('attribute_is_invalid', attribute: attribute_selector)
}
end end
# let's follow grape's validations: https://github.com/intridea/grape/#parameter-validation-and-coercion
def exactly_one_of(*selectors) def exactly_one_of(*selectors)
return if element_count(*selectors) == 1 return if element_count(*selectors) == 1

View file

@ -269,7 +269,6 @@ en:
invalid_type: 'PostalInfo type is invalid' invalid_type: 'PostalInfo type is invalid'
unimplemented_command: 'Unimplemented command' unimplemented_command: 'Unimplemented command'
domain_exists_but_belongs_to_other_registrar: 'Domain exists but belongs to other registrar' domain_exists_but_belongs_to_other_registrar: 'Domain exists but belongs to other registrar'
attribute_op_is_invalid: 'Attribute op is invalid'
code: 'Code' code: 'Code'
@ -501,3 +500,4 @@ en:
unknown_expiry_relative_pattern: 'Expiry relative must be compatible to ISO 8601' unknown_expiry_relative_pattern: 'Expiry relative must be compatible to ISO 8601'
unknown_expiry_absolute_pattern: 'Expiry absolute must be compatible to ISO 8601' unknown_expiry_absolute_pattern: 'Expiry absolute must be compatible to ISO 8601'
mutally_exclusive_params: 'Mutually exclusive parameters: %{params}' mutally_exclusive_params: 'Mutually exclusive parameters: %{params}'
attribute_is_invalid: 'Attribute is invalid: %{attribute}'

View file

@ -944,7 +944,7 @@ describe 'EPP Domain', epp: true do
it 'returns an error for incorrect op attribute' do it 'returns an error for incorrect op attribute' do
response = epp_plain_request(domain_transfer_xml({}, 'bla'), :xml) response = epp_plain_request(domain_transfer_xml({}, 'bla'), :xml)
response[:result_code].should == '2306' response[:result_code].should == '2306'
response[:msg].should == 'Attribute op is invalid' response[:msg].should == 'Attribute is invalid: op'
end end
it 'creates new pw after successful transfer' do it 'creates new pw after successful transfer' do