From 928ce241321024da4983418238ab38aedd32d1b3 Mon Sep 17 00:00:00 2001 From: Priit Tark Date: Wed, 6 May 2015 14:01:40 +0300 Subject: [PATCH] All system now supports legacy CID format --- app/controllers/epp/contacts_controller.rb | 7 +-- app/models/contact.rb | 17 +------- app/models/epp/contact.rb | 21 +++++++++ app/models/epp/domain.rb | 4 +- spec/epp/contact_spec.rb | 51 ++++++++++++++++++++-- spec/epp/domain_spec.rb | 28 ++++++++++++ spec/models/contact_spec.rb | 31 ------------- spec/models/epp_contact_spec.rb | 32 ++++++++++++++ spec/models/registrar_spec.rb | 2 +- 9 files changed, 137 insertions(+), 56 deletions(-) create mode 100644 spec/models/epp_contact_spec.rb diff --git a/app/controllers/epp/contacts_controller.rb b/app/controllers/epp/contacts_controller.rb index 78f1a6c22..9a9e77c84 100644 --- a/app/controllers/epp/contacts_controller.rb +++ b/app/controllers/epp/contacts_controller.rb @@ -11,7 +11,7 @@ class Epp::ContactsController < EppController authorize! :check, Epp::Contact ids = params[:parsed_frame].css('id').map(&:text) - @results = Contact.check_availability(ids) + @results = Epp::Contact.check_availability(ids) render_epp_response '/epp/contacts/check' end @@ -59,8 +59,9 @@ class Epp::ContactsController < EppController end def find_contact - code = params[:parsed_frame].css('id').text.strip.upcase - @contact = Epp::Contact.find_by(code: code) + code = params[:parsed_frame].css('id').text.strip.upcase.sub(/^CID:/, '') + + @contact = Epp::Contact.find_by_epp_code(code) if @contact.blank? epp_errors << { diff --git a/app/models/contact.rb b/app/models/contact.rb index d49804fb0..9ad8a1223 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -54,21 +54,6 @@ class Contact < ActiveRecord::Base res.reduce([]) { |o, v| o << { id: v[:id], display_key: "#{v.name} (#{v.code})" } } end - def check_availability(codes) - codes = [codes] if codes.is_a?(String) - - res = [] - codes.each do |x| - if Contact.find_by(code: x) - res << { code: x, avail: 0, reason: 'in use' } - else - res << { code: x, avail: 1 } - end - end - - res - end - def find_orphans Contact.where(' NOT EXISTS( @@ -125,7 +110,7 @@ class Contact < ActiveRecord::Base def code=(code) self[:code] = code if new_record? # cannot change code later end - + def prefix_code return nil if registrar.blank? code = self[:code] diff --git a/app/models/epp/contact.rb b/app/models/epp/contact.rb index ea3e33eaf..9edbdc70f 100644 --- a/app/models/epp/contact.rb +++ b/app/models/epp/contact.rb @@ -6,6 +6,11 @@ class Epp::Contact < Contact self.inheritance_column = :sti_disabled class << self + # support legacy search + def find_by_epp_code(code) + find_by(code: code.sub(/^CID:/, '')) + end + # rubocop: disable Metrics/PerceivedComplexity # rubocop: disable Metrics/CyclomaticComplexity # rubocop: disable Metrics/MethodLength @@ -82,6 +87,22 @@ class Epp::Contact < Contact document_type: legal_frame.attr('type') }] end + + def check_availability(codes) + codes = [codes] if codes.is_a?(String) + + res = [] + codes.each do |x| + contact = find_by_epp_code(x) + if contact + res << { code: contact.code, avail: 0, reason: 'in use' } + else + res << { code: x, avail: 1 } + end + end + + res + end end def epp_code_map # rubocop:disable Metrics/MethodLength diff --git a/app/models/epp/domain.rb b/app/models/epp/domain.rb index 0942abf5a..df8ec371c 100644 --- a/app/models/epp/domain.rb +++ b/app/models/epp/domain.rb @@ -84,7 +84,7 @@ class Epp::Domain < Domain code = frame.css('registrant').first.try(:text) if code.present? - oc = Contact.find_by(code: code).try(:id) + oc = Epp::Contact.find_by_epp_code(code).try(:id) if oc at[:registrant_id] = oc @@ -206,7 +206,7 @@ class Epp::Domain < Domain frame.css('contact').each do |x| next if x['type'] != type - c = Contact.find_by(code: x.text) + c = Epp::Contact.find_by_epp_code(x.text) unless c add_epp_error('2303', 'contact', x.text, [:domain_contacts, :not_found]) next diff --git a/spec/epp/contact_spec.rb b/spec/epp/contact_spec.rb index f7a814740..fa6696795 100644 --- a/spec/epp/contact_spec.rb +++ b/spec/epp/contact_spec.rb @@ -790,6 +790,24 @@ describe 'EPP Contact', epp: true do ids[0].text.should == 'FIXED:CHECK-1234' ids[1].text.should == 'check-4321' end + + it 'should support legacy CID farmat' do + contact = Fabricate(:contact, code: 'check-LEGACY') + contact.code.should == 'FIXED:CHECK-LEGACY' + + response = epp_plain_request(check_multiple_legacy_contacts_xml, :xml) + + response[:msg].should == 'Command completed successfully' + response[:result_code].should == '1000' + ids = response[:parsed].css('resData chkData id') + + ids[0].text.should == 'FIXED:CHECK-LEGACY' + ids[1].text.should == 'FIXED:CHECK-LEGACY' + + ids[0].attributes['avail'].text.should == '0' + ids[1].attributes['avail'].text.should == '0' + end + end context 'info command' do @@ -819,10 +837,20 @@ describe 'EPP Contact', epp: true do end it 'return info about contact' do - @registrar1_contact = Fabricate( - :contact, code: 'INFO-4444', registrar: @registrar1, name: 'Johnny Awesome') + Fabricate(:contact, code: 'INFO-4444', name: 'Johnny Awesome') - response = info_request({ id: { value: @registrar1_contact.code } }) + response = info_request({ id: { value: 'FIXED:INFO-4444' } }) + response[:msg].should == 'Command completed successfully' + response[:result_code].should == '1000' + + contact = response[:parsed].css('resData infData') + contact.css('name').first.text.should == 'Johnny Awesome' + end + + it 'should honour legacy CID format' do + Fabricate(:contact, code: 'INFO-5555', name: 'Johnny Awesome') + + response = info_request({ id: { value: 'CID:FIXED:INFO-5555' } }) response[:msg].should == 'Command completed successfully' response[:result_code].should == '1000' @@ -937,4 +965,21 @@ describe 'EPP Contact', epp: true do ' end + + def check_multiple_legacy_contacts_xml + ' + + + + + FIXED:CHECK-LEGACY + CID:FIXED:CHECK-LEGACY + + + ABC-12345 + + ' + end + end diff --git a/spec/epp/domain_spec.rb b/spec/epp/domain_spec.rb index 9aace945f..57fae70f7 100644 --- a/spec/epp/domain_spec.rb +++ b/spec/epp/domain_spec.rb @@ -1378,6 +1378,34 @@ describe 'EPP Domain', epp: true do d.auth_info.should == existing_pw end + it 'updates a domain with legacy CID format' do + existing_pw = domain.auth_info + + xml_params = { + name: { value: domain.name }, + chg: [ + registrant: { value: 'CID:FIXED:CITIZEN_1234' } + ] + } + + response = epp_plain_request(domain_update_xml(xml_params, {}, { + _anonymus: [ + legalDocument: { + value: 'JVBERi0xLjQKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0Zp==', + attrs: { type: 'pdf' } + } + ] + }), :xml) + + response[:results][0][:msg].should == 'Command completed successfully' + response[:results][0][:result_code].should == '1000' + + d = Domain.last + + d.registrant_code.should == 'FIXED:CITIZEN_1234' + d.auth_info.should == existing_pw + end + it 'updates domain and adds objects' do xml = domain_update_xml({ name: { value: domain.name }, diff --git a/spec/models/contact_spec.rb b/spec/models/contact_spec.rb index 05be04d48..7176c390d 100644 --- a/spec/models/contact_spec.rb +++ b/spec/models/contact_spec.rb @@ -305,37 +305,6 @@ describe Contact do end end -describe Contact, '.check_availability' do - before do - Fabricate(:contact, code: 'asd12') - Fabricate(:contact, code: 'asd13') - end - - it 'should return array if argument is string' do - response = Contact.check_availability('asd12') - response.class.should == Array - response.length.should == 1 - end - - it 'should return in_use and available codes' do - code = Contact.first.code - code_ = Contact.last.code - - response = Contact.check_availability([code, code_, 'asd14']) - response.class.should == Array - response.length.should == 3 - - response[0][:avail].should == 0 - response[0][:code].should == code - - response[1][:avail].should == 0 - response[1][:code].should == code_ - - response[2][:avail].should == 1 - response[2][:code].should == 'asd14' - end -end - describe Contact, '.destroy_orphans' do before do @contact_1 = Fabricate(:contact, code: 'asd12') diff --git a/spec/models/epp_contact_spec.rb b/spec/models/epp_contact_spec.rb new file mode 100644 index 000000000..5f5d2eafa --- /dev/null +++ b/spec/models/epp_contact_spec.rb @@ -0,0 +1,32 @@ +require 'rails_helper' + +describe Epp::Contact, '.check_availability' do + before do + Fabricate(:contact, code: 'asd12') + Fabricate(:contact, code: 'asd13') + end + + it 'should return array if argument is string' do + response = Epp::Contact.check_availability('asd12') + response.class.should == Array + response.length.should == 1 + end + + it 'should return in_use and available codes' do + code = Contact.first.code + code_ = Contact.last.code + + response = Epp::Contact.check_availability([code, code_, 'asd14']) + response.class.should == Array + response.length.should == 3 + + response[0][:avail].should == 0 + response[0][:code].should == code + + response[1][:avail].should == 0 + response[1][:code].should == code_ + + response[2][:avail].should == 1 + response[2][:code].should == 'asd14' + end +end diff --git a/spec/models/registrar_spec.rb b/spec/models/registrar_spec.rb index 764eaf036..42b0655a7 100644 --- a/spec/models/registrar_spec.rb +++ b/spec/models/registrar_spec.rb @@ -121,7 +121,7 @@ describe Registrar do i.description.should == 'add some money' end - fit 'should not allaw to use CID as code for leagcy reasons' do + it 'should not allaw to use CID as code for leagcy reasons' do registrar = Fabricate.build(:registrar, code: 'CID') registrar.valid? registrar.errors.full_messages.should == ['Code is forbidden to use']