diff --git a/app/helpers/epp/contacts_helper.rb b/app/helpers/epp/contacts_helper.rb index 29e211c87..acb6fe339 100644 --- a/app/helpers/epp/contacts_helper.rb +++ b/app/helpers/epp/contacts_helper.rb @@ -13,7 +13,7 @@ module Epp::ContactsHelper #TODO add support for rem and add code = params_hash['epp']['command']['update']['update'][:id] @contact = Contact.where(code: code).first - if stamp(@contact) && @contact.update_attributes(contact_and_address_attributes.delete_if { |k, v| v.nil? }) + if stamp(@contact) && @contact.update_attributes(contact_and_address_attributes(:update)) render 'epp/contacts/update' else epp_errors << { code: '2303', msg: t('errors.messages.epp_obj_does_not_exist'), value: { obj: 'id', val: code } } if @contact == [] @@ -101,32 +101,17 @@ module Epp::ContactsHelper end - def contact_and_address_attributes - ph = params_hash['epp']['command'][params[:command]][params[:command]] - ph = ph[:chg] if params[:command] == 'update' - contact_hash = { - code: ph[:id], - phone: ph[:voice], - ident: ph[:ident], - ident_type: ident_type, - email: ph[:email], - } - - contact_hash = contact_hash.merge({ - name: ph[:postalInfo][:name], - org_name: ph[:postalInfo][:org] - }) if ph[:postalInfo].is_a? Hash - - contact_hash = contact_hash.merge({ - address_attributes: { - country_id: Country.find_by(iso: ph[:postalInfo][:addr][:cc]), - street: ph[:postalInfo][:addr][:street][0], - street2: ph[:postalInfo][:addr][:street][1], - street3: ph[:postalInfo][:addr][:street][2], - zip: ph[:postalInfo][:addr][:pc] - } - }) if ph[:postalInfo].is_a?(Hash) && ph[:postalInfo][:addr].is_a?(Hash) - + def contact_and_address_attributes( type=:create ) + case type + when :update + contact_hash = Contact.extract_attributes(@ph[:chg], type) + contact_hash[:address_attributes] = + Address.extract_attributes(( @ph.try(:[], :chg).try(:[], :postalInfo).try(:[], :addr) || [] ), type) + else + contact_hash = Contact.extract_attributes(@ph, type) + contact_hash[:address_attributes] = + Address.extract_attributes(( @ph.try(:[], :postalInfo).try(:[], :addr) || [] ), type) + end contact_hash end diff --git a/app/models/address.rb b/app/models/address.rb index 55937ecb7..3ddad202d 100644 --- a/app/models/address.rb +++ b/app/models/address.rb @@ -1,4 +1,20 @@ class Address < ActiveRecord::Base belongs_to :contact belongs_to :country + + class << self + def extract_attributes ah, type=:create + address_hash = {} + address_hash = ({ + country_id: Country.find_by(iso: ah[:cc]).try(:id), + city: ah[:city], + street: ah[:street][0], + street2: ah[:street][1], + street3: ah[:street][2], + zip: ah[:pc] + }) if ah.is_a?(Hash) + + address_hash.delete_if { |k, v| v.nil? } + end + end end diff --git a/app/models/contact.rb b/app/models/contact.rb index d59b623ee..553b93b98 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -67,6 +67,32 @@ class Contact < ActiveRecord::Base end class << self + + def extract_attributes ph, type=:create + + contact_hash = { + #code: ph[:id], + phone: ph[:voice], + ident: ph[:ident], + #ident_type: ident_type, + email: ph[:email] + } + + contact_hash = contact_hash.merge({ + name: ph[:postalInfo][:name], + org_name: ph[:postalInfo][:org] + }) if ph[:postalInfo].is_a? Hash + + contact_hash[:code] = ph[:id] if type == :create + + contact_hash.delete_if { |k, v| v.nil? } + end + + + def ident_type code + + end + def check_availability(codes) codes = [codes] if codes.is_a?(String) diff --git a/spec/epp/contact_spec.rb b/spec/epp/contact_spec.rb index 9cf0fe448..b8c7d920e 100644 --- a/spec/epp/contact_spec.rb +++ b/spec/epp/contact_spec.rb @@ -68,8 +68,8 @@ describe 'EPP Contact', epp: true do context 'update command' do it "fails if request is invalid" do - #response = epp_request('contacts/update_missing_attr.xml') - response = epp_request(contact_update_xml( {id: false} ), :xml) + response = epp_request('contacts/update_missing_attr.xml') + #response = epp_request(contact_update_xml( {id: false} ), :xml) expect(response[:results][0][:result_code]).to eq('2003') expect(response[:results][0][:msg]).to eq('Required parameter missing: id') @@ -88,16 +88,18 @@ describe 'EPP Contact', epp: true do it 'is succesful' do Fabricate(:contact, created_by_id: 1, email: 'not_updated@test.test', code: 'sh8013') - response = epp_request(contact_update_xml( { chg: { email: 'fred@bloggers.ee', postalInfo: { name: 'Fred Bloggers' } } } ), :xml) + #response = epp_request(contact_update_xml( { chg: { email: 'fred@bloggers.ee', postalInfo: { name: 'Fred Bloggers' } } } ), :xml) + response = epp_request('contacts/update.xml') expect(response[:msg]).to eq('Command completed successfully') - expect(Contact.first.name).to eq('Fred Bloggers') - expect(Contact.first.email).to eq('fred@bloggers.ee') + expect(Contact.first.name).to eq('John Doe') + expect(Contact.first.email).to eq('jdoe@example.com') end it 'returns phone and email error' do Fabricate(:contact, created_by_id: 1, email: 'not_updated@test.test', code: 'sh8013') - response = epp_request(contact_update_xml( { chg: { email: "qwe", phone: "123qweasd" } }), :xml) + #response = epp_request(contact_update_xml( { chg: { email: "qwe", phone: "123qweasd" } }), :xml) + response = epp_request('contacts/update_with_errors.xml') expect(response[:results][0][:result_code]).to eq('2005') expect(response[:results][0][:msg]).to eq('Phone nr is invalid') diff --git a/spec/fabricators/country_fabricator.rb b/spec/fabricators/country_fabricator.rb new file mode 100644 index 000000000..657628275 --- /dev/null +++ b/spec/fabricators/country_fabricator.rb @@ -0,0 +1,4 @@ +Fabricator(:country) do + iso Faker::Address.state_abbr + name Faker::Address.country +end diff --git a/spec/models/address_spec.rb b/spec/models/address_spec.rb index eaa9603e2..839f8a755 100644 --- a/spec/models/address_spec.rb +++ b/spec/models/address_spec.rb @@ -4,3 +4,19 @@ describe Address do it { should belong_to(:contact) } it { should belong_to(:country) } end + + +describe Address, '.extract_params' do + it 'returns params hash'do + Fabricate(:country, iso:'EE') + ph = { postalInfo: { name: "fred", addr: { cc: 'EE', city: 'Village', street: [ 'street1', 'street2' ] } } } + expect(Address.extract_attributes(ph[:postalInfo][:addr])).to eq( { + city: 'Village', + country_id: 1, + street: 'street1', + street2: 'street2' + } ) + end +end + + diff --git a/spec/models/contact_spec.rb b/spec/models/contact_spec.rb index fd028f372..c485ce362 100644 --- a/spec/models/contact_spec.rb +++ b/spec/models/contact_spec.rb @@ -65,6 +65,18 @@ describe Contact, '#upID' do end +describe Contact, '.extract_params' do + it 'returns params hash'do + ph = { id: '123123', email: 'jdoe@example.com', postalInfo: { name: "fred", addr: { cc: 'EE' } } } + expect(Contact.extract_attributes(ph)).to eq( { + code: '123123', + email: 'jdoe@example.com', + name: 'fred' + } ) + end +end + + describe Contact, '.check_availability' do before(:each) {