Domain renew refactor, extract XSD, new API for request validation

This commit is contained in:
Martin Lensment 2014-08-11 11:19:49 +03:00
parent 01cfddee95
commit 8baa2e12c9
7 changed files with 70 additions and 33 deletions

View file

@ -32,9 +32,11 @@ module Epp::Common
@current_epp_user ||= EppUser.find(epp_session[:epp_user_id]) if epp_session[:epp_user_id]
end
def handle_errors(obj)
obj.construct_epp_errors
@errors = obj.errors[:epp_errors]
def handle_errors(obj=nil)
if obj
obj.construct_epp_errors
@errors = obj.errors[:epp_errors]
end
render '/epp/error'
end
@ -52,15 +54,9 @@ module Epp::Common
end
def validate_request
type = OBJECT_TYPES[params_hash['epp']['xmlns:ns2']]
return unless type
xsd = Nokogiri::XML::Schema(File.read("doc/schemas/#{type}-1.0.xsd"))
doc = Nokogiri::XML(params[:frame])
ext_values = xsd.validate(doc)
if ext_values.any?
epp_errors << {code: '2001', msg: 'Command syntax error', ext_values: ext_values}
render '/epp/error' and return
validation_method = "validate_#{OBJECT_TYPES[params_hash['epp']['xmlns:ns2']]}_#{params[:command]}_request"
if respond_to?(validation_method, true)
handle_errors and return unless send(validation_method)
end
end
end

View file

@ -32,24 +32,30 @@ module Epp::DomainsHelper
end
def renew_domain
ph = params_hash['epp']['command']['renew']['renew']
@domain = find_domain
@domain = Domain.find_by(name: ph[:name])
unless @domain
epp_errors << {code: '2303', msg: I18n.t('errors.messages.epp_domain_not_found'), value: {obj: 'name', val: ph[:name]}}
render '/epp/error' and return
end
handle_errors(@domain) and return unless @domain
handle_errors(@domain) and return unless @domain.renew(@ph[:curExpDate], @ph[:period])
if @domain.renew(ph[:curExpDate], ph[:period])
render '/epp/domains/renew'
else
handle_errors(@domain)
end
render '/epp/domains/renew'
end
### HELPER METHODS ###
private
def validate_domain_renew_request
@ph = params_hash['epp']['command']['renew']['renew']
xml_attrs_present?(@ph, [['name'], ['curExpDate'], ['period']])
end
def find_domain
@domain = Domain.find_by(name: @ph[:name])
unless @domain
epp_errors << {code: '2303', msg: I18n.t('errors.messages.epp_domain_not_found'), value: {obj: 'name', val: @ph[:name]}}
end
@domain
end
def domain_create_params(ph)
{
name: ph[:name],

View file

@ -12,7 +12,8 @@ class Domain < ActiveRecord::Base
}
EPP_ATTR_MAP = {
owner_contact: 'registrant'
owner_contact: 'registrant',
name_dirty: 'name'
}
belongs_to :registrar
@ -33,6 +34,7 @@ class Domain < ActiveRecord::Base
validates :name_dirty, domain_name: true, uniqueness: true
validates :period, numericality: { only_integer: true, greater_than: 0, less_than: 100 }
validates :name, :owner_contact, presence: true
validate :validate_period
validates_associated :nameservers
def name=(value)
@ -118,14 +120,40 @@ class Domain < ActiveRecord::Base
end
def renew(cur_exp_date, period, unit='y')
if cur_exp_date.to_date == valid_to
self.valid_to = self.valid_to + period.to_i.years
self.period = period
save
# TODO Check how much time before domain exp date can it be renewed
validate_exp_dates(cur_exp_date)
return false if errors.any?
p = period.to_i.days if unit == 'd'
p = period.to_i.months if unit == 'm'
p = period.to_i.years if unit == 'y'
self.valid_to = self.valid_to + p
self.period = period
self.period_unit = unit
save
end
def validate_period
return unless period.present?
if period_unit == 'd'
valid_values = ['365', '366', '710', '712', '1065', '1068']
elsif period_unit == 'm'
valid_values = ['12', '24', '36']
else
errors[:base] << {msg: I18n.t('errors.messages.epp_exp_dates_do_not_match'), obj: 'curExpDate', val: cur_exp_date}
false
valid_values = ['1', '2', '3']
end
errors.add(:period, :step_error) unless valid_values.include?(period.to_s)
end
def validate_exp_dates(cur_exp_date)
errors.add(:valid_to, {
obj: 'curExpDate',
val: cur_exp_date,
msg: I18n.t('errors.messages.epp_exp_dates_do_not_match')
}) if cur_exp_date.to_date != valid_to
end
class << self

View file

@ -0,0 +1,5 @@
class AddPeriodUnitToDomain < ActiveRecord::Migration
def change
add_column :domains, :period_unit, :char
end
end

View file

@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20140804095654) do
ActiveRecord::Schema.define(version: 20140808132327) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -75,6 +75,7 @@ ActiveRecord::Schema.define(version: 20140804095654) do
t.string "name_dirty"
t.string "name_puny"
t.integer "period"
t.string "period_unit", limit: 1
end
create_table "domains_nameservers", force: true do |t|

View file

@ -2,5 +2,6 @@ Fabricator(:domain) do
name { "#{Faker::Internet.domain_word}.ee" }
valid_to Date.new(2014, 8, 7)
period 1
period_unit 'y'
owner_contact(fabricator: :contact)
end

View file

@ -52,8 +52,8 @@ describe Domain do
it 'validates period' do
expect(Fabricate.build(:domain, period: 0).valid?).to be false
expect(Fabricate.build(:domain, period: 120).valid?).to be false
expect(Fabricate.build(:domain, period: 99).valid?).to be true
expect(Fabricate.build(:domain, period: 4).valid?).to be false
expect(Fabricate.build(:domain, period: 3).valid?).to be true
end
end