From cb2a5f07238eb72558f3f1b3f2ad2d222167824d Mon Sep 17 00:00:00 2001 From: Martin Lensment Date: Tue, 12 Aug 2014 15:19:17 +0300 Subject: [PATCH] Unit support for domain creating --- app/controllers/concerns/epp/common.rb | 4 ++++ app/helpers/epp/domains_helper.rb | 5 ++-- app/models/domain.rb | 19 +++++++++------ spec/epp/domain_spec.rb | 6 +++++ .../domains/create_w_period_in_days.xml | 24 +++++++++++++++++++ 5 files changed, 49 insertions(+), 9 deletions(-) create mode 100644 spec/epp/requests/domains/create_w_period_in_days.xml diff --git a/app/controllers/concerns/epp/common.rb b/app/controllers/concerns/epp/common.rb index 0eb30d6fa..23b9d0f3c 100644 --- a/app/controllers/concerns/epp/common.rb +++ b/app/controllers/concerns/epp/common.rb @@ -20,6 +20,10 @@ module Epp::Common @params_hash ||= Hash.from_xml(params[:frame]).with_indifferent_access end + def parsed_frame + @parsed_frame ||= Nokogiri::XML(params[:frame]).remove_namespaces! + end + def epp_session EppSession.find_or_initialize_by(session_id: cookies['session']) end diff --git a/app/helpers/epp/domains_helper.rb b/app/helpers/epp/domains_helper.rb index 6e32660b7..bd74f94d1 100644 --- a/app/helpers/epp/domains_helper.rb +++ b/app/helpers/epp/domains_helper.rb @@ -3,7 +3,7 @@ module Epp::DomainsHelper Domain.transaction do @domain = Domain.new(domain_create_params) - handle_errors(@domain) and return unless @domain.attach_objects(@ph, params[:frame]) + handle_errors(@domain) and return unless @domain.attach_objects(@ph, parsed_frame) handle_errors(@domain) and return unless @domain.save render '/epp/domains/create' @@ -40,7 +40,8 @@ module Epp::DomainsHelper name: @ph[:name], registrar_id: current_epp_user.registrar.try(:id), registered_at: Time.now, - period: @ph[:period].to_i, + period: (@ph[:period].to_i == 0) ? 1 : @ph[:period].to_i, + period_unit: Domain.parse_period_unit_from_frame(parsed_frame) || 'y', valid_from: Date.today, valid_to: Date.today + @ph[:period].to_i.years, auth_info: @ph[:authInfo][:pw] diff --git a/app/models/domain.rb b/app/models/domain.rb index e3040d5eb..f681ca65a 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -13,7 +13,8 @@ class Domain < ActiveRecord::Base EPP_ATTR_MAP = { owner_contact: 'registrant', - name_dirty: 'name' + name_dirty: 'name', + period: 'period' } belongs_to :registrar @@ -32,7 +33,7 @@ class Domain < ActiveRecord::Base has_and_belongs_to_many :nameservers validates :name_dirty, domain_name: true, uniqueness: true - validates :period, numericality: { only_integer: true, greater_than: 0, less_than: 100 } + validates :period, numericality: { only_integer: true } validates :name, :owner_contact, presence: true validates_associated :nameservers @@ -50,9 +51,9 @@ class Domain < ActiveRecord::Base ### CREATE ### - def attach_objects(ph, frame) + def attach_objects(ph, parsed_frame) attach_owner_contact(ph[:registrant]) - attach_contacts(self.class.parse_contacts_from_frame(frame)) + attach_contacts(self.class.parse_contacts_from_frame(parsed_frame)) attach_nameservers(self.class.parse_nameservers_from_params(ph[:ns])) errors.empty? @@ -173,9 +174,7 @@ class Domain < ActiveRecord::Base end class << self - def parse_contacts_from_frame(frame) - parsed_frame = Nokogiri::XML(frame).remove_namespaces! - + def parse_contacts_from_frame(parsed_frame) res = {} Contact::CONTACT_TYPES.each do |ct| res[ct.to_sym] ||= [] @@ -194,6 +193,12 @@ class Domain < ActiveRecord::Base [] end + def parse_period_unit_from_frame(parsed_frame) + p = parsed_frame.css('period').first + return nil unless p + p[:unit] + end + def check_availability(domains) domains = [domains] if domains.is_a?(String) diff --git a/spec/epp/domain_spec.rb b/spec/epp/domain_spec.rb index 95e353b92..2c6493d45 100644 --- a/spec/epp/domain_spec.rb +++ b/spec/epp/domain_spec.rb @@ -101,6 +101,12 @@ describe 'EPP Domain', epp: true do expect(response[:results][0][:result_code]).to eq '2005' expect(response[:results][0][:msg]).to eq 'IP is invalid' end + + it 'creates a domain with period in days' do + response = epp_request('domains/create_w_period_in_days.xml') + expect(response[:result_code]).to eq('1000') + expect(response[:msg]).to eq('Command completed successfully') + end end context 'with juridical persion as an owner' do diff --git a/spec/epp/requests/domains/create_w_period_in_days.xml b/spec/epp/requests/domains/create_w_period_in_days.xml new file mode 100644 index 000000000..e5567b66c --- /dev/null +++ b/spec/epp/requests/domains/create_w_period_in_days.xml @@ -0,0 +1,24 @@ + + + + + + example.ee + 365 + + ns1.example.net + ns2.example.net + + jd1234 + sh8013 + sh8013 + sh801333 + + 2fooBAR + + + + ABC-12345 + +