Replaced address with local and international addr

This commit is contained in:
Andres Keskküla 2014-08-25 17:05:57 +03:00
parent fe54f327d9
commit 2655da4555
16 changed files with 208 additions and 57 deletions

View file

@ -57,6 +57,15 @@ module Epp::Common
epp_errors.empty? epp_errors.empty?
end end
def xml_nested_attrs_present?(array_ph, attributes )
[array_ph].flatten.each do |ph|
attributes.each do |x|
epp_errors << {code: '2003', msg: I18n.t('errors.messages.required_parameter_missing', key: x.last)} unless has_attribute(ph, x)
end
end
epp_errors.empty?
end
def has_attribute(ph, path) def has_attribute(ph, path)
path.inject(ph) do |location, key| path.inject(ph) do |location, key|
location.respond_to?(:keys) ? location[key] : nil location.respond_to?(:keys) ? location[key] : nil

View file

@ -48,9 +48,14 @@ module Epp::ContactsHelper
@ph = params_hash['epp']['command']['create']['create'] @ph = params_hash['epp']['command']['create']['create']
xml_attrs_present?(@ph, [['id'], xml_attrs_present?(@ph, [['id'],
['authInfo', 'pw'], ['authInfo', 'pw'],
['postalInfo', 'name'], ['postalInfo']])
['postalInfo', 'addr', 'city'], # ['postalInfo', 'addr', 'city'],
['postalInfo', 'addr', 'cc']]) # ['postalInfo', 'addr', 'cc']])
if @ph['postalInfo'].is_a?(Hash) || @ph['postalInfo'].is_a?(Array)
xml_nested_attrs_present?( @ph['postalInfo'], [['name'],
['addr', 'city'],
['addr', 'cc']] )
end
end end
## UPDATE ## UPDATE
@ -101,12 +106,17 @@ module Epp::ContactsHelper
case type case type
when :update when :update
contact_hash = Contact.extract_attributes(@ph[:chg], type) contact_hash = Contact.extract_attributes(@ph[:chg], type)
contact_hash[:address_attributes] = #contact_hash[:address_attributes] =
Address.extract_attributes(( @ph.try(:[], :chg).try(:[], :postalInfo).try(:[], :addr) || [] ), type) contact_hash = contact_hash.merge(
Address.extract_attributes(( @ph.try(:[], :chg).try(:[], :postalInfo) || [] ), type)
)
else else
contact_hash = Contact.extract_attributes(@ph, type) contact_hash = Contact.extract_attributes(@ph, type)
contact_hash[:address_attributes] = #contact_hash[:address_attributes] =
Address.extract_attributes(( @ph.try(:[], :postalInfo).try(:[], :addr) || [] ), type) # Address.extract_attributes(( @ph.try(:[], :postalInfo) || [] ), type)
contact_hash = contact_hash.merge(
Address.extract_attributes(( @ph.try(:[], :postalInfo) || [] ), type)
)
end end
contact_hash[:ident_type] = ident_type unless ident_type.nil? contact_hash[:ident_type] = ident_type unless ident_type.nil?
contact_hash contact_hash

View file

@ -1,20 +1,47 @@
class Address < ActiveRecord::Base class Address < ActiveRecord::Base
LOCAL_TYPE_SHORT = 'loc'
INTERNATIONAL_TYPE_SHORT = 'int'
LOCAL_TYPE = 'LocalAddress'
TYPES = [
LOCAL_TYPE_SHORT,
INTERNATIONAL_TYPE_SHORT
]
belongs_to :contact belongs_to :contact
belongs_to :country belongs_to :country
#validates_inclusion_of :type, in: TYPES
class << self class << self
def extract_attributes ah, type=:create def extract_attributes ah, type=:create
address_hash = {} 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? } [ah].flatten.each do |pi|
address_hash[local?(pi)] = addr_hash_from_params(pi)
end
address_hash
end
private
def local?(postal_info)
return :local_address_attributes if postal_info[:type] == LOCAL_TYPE_SHORT
:international_address_attributes
end
def addr_hash_from_params(addr)
return {} unless addr[:addr].is_a?(Hash)
{
name: addr[:name],
org_name: addr[:org],
country_id: Country.find_by(iso: addr[:addr][:cc]).try(:id),
city: addr[:addr][:city],
street: addr[:addr][:street][0],
street2: addr[:addr][:street][1],
street3: addr[:addr][:street][2],
zip: addr[:addr][:pc]
}.delete_if { |k, v| v.nil? }
end end
end end
end end

View file

@ -6,16 +6,18 @@ class Contact < ActiveRecord::Base
EPP_ATTR_MAP = {} EPP_ATTR_MAP = {}
has_one :address has_one :local_address#, class_name: 'Address'#, foreign_key: 'local_address_id'
has_one :international_address
has_many :domain_contacts has_many :domain_contacts
has_many :domains, through: :domain_contacts has_many :domains, through: :domain_contacts
belongs_to :created_by, class_name: 'EppUser', foreign_key: :created_by_id belongs_to :created_by, class_name: 'EppUser', foreign_key: :created_by_id
belongs_to :updated_by, class_name: 'EppUser', foreign_key: :updated_by_id belongs_to :updated_by, class_name: 'EppUser', foreign_key: :updated_by_id
accepts_nested_attributes_for :address accepts_nested_attributes_for :local_address, :international_address
validates_presence_of :code, :name, :phone, :email, :ident validates_presence_of :code, :phone, :email, :ident
validate :ident_must_be_valid validate :ident_must_be_valid
@ -36,6 +38,16 @@ class Contact < ActiveRecord::Base
CONTACT_TYPE_ADMIN = 'admin' CONTACT_TYPE_ADMIN = 'admin'
CONTACT_TYPES = [CONTACT_TYPE_TECH, CONTACT_TYPE_ADMIN] CONTACT_TYPES = [CONTACT_TYPE_TECH, CONTACT_TYPE_ADMIN]
#TEMP METHODS for transaction to STI
def name
international_address.name
end
def address
international_address
end
##
def ident_must_be_valid def ident_must_be_valid
#TODO Ident can also be passport number or company registry code. #TODO Ident can also be passport number or company registry code.
#so have to make changes to validations (and doc/schema) accordingly #so have to make changes to validations (and doc/schema) accordingly
@ -114,11 +126,6 @@ class Contact < ActiveRecord::Base
email: ph[:email] 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[:code] = ph[:id] if type == :create
contact_hash.delete_if { |k, v| v.nil? } contact_hash.delete_if { |k, v| v.nil? }

View file

@ -0,0 +1,3 @@
class InternationalAddress < Address
end

View file

@ -0,0 +1,3 @@
class LocalAddress < Address
end

View file

@ -0,0 +1,27 @@
if @contact.international_address
address = @contact.international_address
xml.tag!('contact:postalInfo', type: 'int') do # TODO instance method of defining type
xml.tag!('contact:name', address.name)
xml.tag!('contact:org', address.org_name)
xml.tag!('contact:addr') do
xml.tag!('contact:street', address.street) if address.street
xml.tag!('contact:street', address.street2) if address.street2
xml.tag!('contact:street', address.street3) if address.street3
xml.tag!('contact:cc', address.try(:country).try(:iso)) unless address.try(:country).nil?
end
end
end
if @contact.local_address
address = @contact.local_address
xml.tag!('contact:postalInfo', type: 'loc') do
xml.tag!('contact:name', address.name)
xml.tag!('contact:org', address.org_name)
xml.tag!('contact:addr') do
xml.tag!('contact:street', address.street) if address.street
xml.tag!('contact:street', address.street2) if address.street2
xml.tag!('contact:street', address.street3) if address.street3
xml.tag!('contact:cc', address.try(:country).try(:iso)) unless address.try(:country).nil?
end
end
end

View file

@ -6,15 +6,7 @@ xml.epp_head do
xml.resData do xml.resData do
xml.tag!('contact:chkData', 'xmlns:contact' => 'urn:ietf:params:xml:ns:contact-1.0') do xml.tag!('contact:chkData', 'xmlns:contact' => 'urn:ietf:params:xml:ns:contact-1.0') do
xml.tag!('contact:name', @contact.name) xml << render('/epp/contacts/postal_info')
xml.tag!('contact:org', @contact.org_name)
xml.tag!('contact:addr') do
address = @contact.address
xml.tag!('contact:street', address.street) if address.street
xml.tag!('contact:street', address.street2) if address.street2
xml.tag!('contact:street', address.street3) if address.street3
xml.tag!('contact:cc', address.try(:country).try(:iso)) unless address.try(:country).nil?
end
xml.tag!('contact:voice', @contact.phone) xml.tag!('contact:voice', @contact.phone)
xml.tag!('contact:fax', @contact.fax) xml.tag!('contact:fax', @contact.fax)
xml.tag!('contact:email', @contact.email) xml.tag!('contact:email', @contact.email)

View file

@ -0,0 +1,10 @@
class AddPostalInfoToAddress < ActiveRecord::Migration
def change
add_column :addresses, :name, :string
add_column :addresses, :org_name, :string
add_column :addresses, :type, :string
remove_column :contacts, :name, :string
remove_column :contacts, :org_name, :string
end
end

View file

@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20140819103517) do ActiveRecord::Schema.define(version: 20140822122938) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -26,11 +26,13 @@ ActiveRecord::Schema.define(version: 20140819103517) do
t.datetime "updated_at" t.datetime "updated_at"
t.string "street2" t.string "street2"
t.string "street3" t.string "street3"
t.string "name"
t.string "org_name"
t.string "type"
end end
create_table "contacts", force: true do |t| create_table "contacts", force: true do |t|
t.string "code" t.string "code"
t.string "name"
t.string "type" t.string "type"
t.string "reg_no" t.string "reg_no"
t.string "phone" t.string "phone"
@ -40,7 +42,6 @@ ActiveRecord::Schema.define(version: 20140819103517) do
t.datetime "updated_at" t.datetime "updated_at"
t.string "ident" t.string "ident"
t.string "ident_type" t.string "ident_type"
t.string "org_name"
t.integer "created_by_id" t.integer "created_by_id"
t.integer "updated_by_id" t.integer "updated_by_id"
t.string "auth_info" t.string "auth_info"

View file

@ -29,17 +29,30 @@ describe 'EPP Contact', epp: true do
expect(response[:result_code]).to eq('1000') expect(response[:result_code]).to eq('1000')
expect(response[:msg]).to eq('Command completed successfully') expect(response[:msg]).to eq('Command completed successfully')
expect(response[:clTRID]).to eq('ABC-12345') expect(response[:clTRID]).to eq('ABC-12345')
expect(Contact.first.created_by_id).to be 1 expect(Contact.first.created_by_id).to eq 1
expect(Contact.first.updated_by_id).to be nil expect(Contact.first.updated_by_id).to eq nil
expect(Contact.count).to eq(1) expect(Contact.count).to eq(1)
expect(Contact.first.org_name).to eq('Example Inc.')
expect(Contact.first.international_address.org_name).to eq('Example Inc.')
expect(Contact.first.ident).to eq '37605030299' expect(Contact.first.ident).to eq '37605030299'
expect(Contact.first.ident_type).to eq 'op' expect(Contact.first.ident_type).to eq 'op'
expect(Contact.first.address.street).to eq('123 Example Dr.') expect(Contact.first.international_address.street).to eq('123 Example Dr.')
expect(Contact.first.address.street2).to eq('Suite 100') expect(Contact.first.international_address.street2).to eq('Suite 100')
expect(Contact.first.address.street3).to eq nil expect(Contact.first.international_address.street3).to eq nil
end
it 'successfully creates contact with 2 addresses' do
response = epp_request('contacts/create_with_two_addresses.xml')
expect(response[:result_code]).to eq('1000')
expect(Contact.count).to eq(1)
expect(Contact.first.address).to_not eq Contact.first.local_address
expect(Address.count).to eq(2)
expect(LocalAddress.count).to eq(1)
end end
it 'returns result data upon success' do it 'returns result data upon success' do
@ -62,6 +75,7 @@ describe 'EPP Contact', epp: true do
response = epp_request(contact_create_xml, :xml) response = epp_request(contact_create_xml, :xml)
expect(response[:result_code]).to eq('2302') expect(response[:result_code]).to eq('2302')
expect(response[:msg]).to eq('Contact id already exists') expect(response[:msg]).to eq('Contact id already exists')
@ -207,8 +221,9 @@ describe 'EPP Contact', epp: true do
end end
it 'returns info about contact' do it 'returns info about contact' do
Fabricate(:contact, name: "Johnny Awesome", created_by_id: '1', code: 'info-4444', auth_info: '2fooBAR') Fabricate(:contact, created_by_id: '1', code: 'info-4444', auth_info: '2fooBAR',
Fabricate(:address) international_address: Fabricate(:international_address, name: 'Johnny Awesome'))
#Fabricate(:international_address, name: 'Johnny Awesome')
response = epp_request('contacts/info.xml') response = epp_request('contacts/info.xml')
contact = response[:parsed].css('resData chkData') contact = response[:parsed].css('resData chkData')
@ -220,7 +235,7 @@ describe 'EPP Contact', epp: true do
end end
it 'doesn\'t display unassociated object' do it 'doesn\'t display unassociated object' do
Fabricate(:contact, name:"Johnny Awesome", code: 'info-4444') Fabricate(:contact, code: 'info-4444')
response = epp_request('contacts/info.xml') response = epp_request('contacts/info.xml')
expect(response[:result_code]).to eq('2201') expect(response[:result_code]).to eq('2201')

View file

@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<epp xmlns="urn:ietf:params:xml:ns:epp-1.0">
<command>
<create>
<contact:create
xmlns:contact="urn:ietf:params:xml:ns:contact-1.0">
<contact:id>loc_int</contact:id>
<contact:postalInfo type="int">
<contact:name>John Doe Int</contact:name>
<contact:org>Example Int Inc.</contact:org>
<contact:addr>
<contact:street>International street 1</contact:street>
<contact:city>Dulles</contact:city>
<contact:sp>VA</contact:sp>
<contact:pc>20166-6503</contact:pc>
<contact:cc>EE</contact:cc>
</contact:addr>
</contact:postalInfo>
<contact:postalInfo type="loc">
<contact:name>John Doe Loc</contact:name>
<contact:org>Example Loc Inc.</contact:org>
<contact:addr>
<contact:street>Local street 1</contact:street>
<contact:city>Vancouver</contact:city>
<contact:sp>BC</contact:sp>
<contact:pc>27123</contact:pc>
<contact:cc>CA</contact:cc>
</contact:addr>
</contact:postalInfo>
<contact:voice x="1234">+123.7035555555</contact:voice>
<contact:fax>+1.7035555556</contact:fax>
<contact:email>jdoe@example.com</contact:email>
<contact:ident type="op">37605030299</contact:ident>
<contact:authInfo>
<contact:pw>2fooBAR</contact:pw>
</contact:authInfo>
<contact:disclose flag="0">
<contact:voice/>
<contact:email/>
</contact:disclose>
</contact:create>
</create>
<clTRID>ABC-12345</clTRID>
</command>
</epp>

View file

@ -1,10 +1,9 @@
Fabricator(:contact) do Fabricator(:contact) do
name Faker::Name.name
phone '+372.12345678' phone '+372.12345678'
email Faker::Internet.email email Faker::Internet.email
ident '37605030299' ident '37605030299'
code { "sh#{Faker::Number.number(4)}" } code { "sh#{Faker::Number.number(4)}" }
ident_type 'op' ident_type 'op'
auth_info 'ccds4324pok' auth_info 'ccds4324pok'
address international_address
end end

View file

@ -1,4 +1,5 @@
Fabricator(:address) do Fabricator(:international_address) do
name Faker::Name.name
city Faker::Address.city city Faker::Address.city
street Faker::Address.street_name street Faker::Address.street_name
street2 Faker::Address.street_name street2 Faker::Address.street_name

View file

@ -10,11 +10,14 @@ describe Address, '.extract_params' do
it 'returns params hash'do it 'returns params hash'do
Fabricate(:country, iso:'EE') Fabricate(:country, iso:'EE')
ph = { postalInfo: { name: "fred", addr: { cc: 'EE', city: 'Village', street: [ 'street1', 'street2' ] } } } ph = { postalInfo: { name: "fred", addr: { cc: 'EE', city: 'Village', street: [ 'street1', 'street2' ] } } }
expect(Address.extract_attributes(ph[:postalInfo][:addr])).to eq( { expect(Address.extract_attributes(ph[:postalInfo])).to eq( {
international_address_attributes: {
name: 'fred',
city: 'Village', city: 'Village',
country_id: 1, country_id: 1,
street: 'street1', street: 'street1',
street2: 'street2' street2: 'street2',
}
} ) } )
end end
end end

View file

@ -1,7 +1,8 @@
require "rails_helper" require "rails_helper"
describe Contact do describe Contact do
it { should have_one(:address) } it { should have_one(:local_address) }
it { should have_one(:international_address) }
context 'with invalid attribute' do context 'with invalid attribute' do
before(:each) { @contact = Fabricate(:contact) } before(:each) { @contact = Fabricate(:contact) }
@ -22,7 +23,6 @@ describe Contact do
expect(@contact.errors.messages).to match_array({ expect(@contact.errors.messages).to match_array({
:code=>["Required parameter missing - code"], :code=>["Required parameter missing - code"],
:name=>["Required parameter missing - name"],
:phone=>["Required parameter missing - phone", "Phone nr is invalid"], :phone=>["Required parameter missing - phone", "Phone nr is invalid"],
:email=>["Required parameter missing - email", "Email is invalid"], :email=>["Required parameter missing - email", "Email is invalid"],
:ident=>["Required parameter missing - ident"] :ident=>["Required parameter missing - ident"]
@ -90,8 +90,7 @@ describe Contact, '.extract_params' do
ph = { id: '123123', email: 'jdoe@example.com', postalInfo: { name: "fred", addr: { cc: 'EE' } } } ph = { id: '123123', email: 'jdoe@example.com', postalInfo: { name: "fred", addr: { cc: 'EE' } } }
expect(Contact.extract_attributes(ph)).to eq( { expect(Contact.extract_attributes(ph)).to eq( {
code: '123123', code: '123123',
email: 'jdoe@example.com', email: 'jdoe@example.com'
name: 'fred'
} ) } )
end end
end end