diff --git a/app/helpers/epp/contacts_helper.rb b/app/helpers/epp/contacts_helper.rb index c457358d3..3dbc7c280 100644 --- a/app/helpers/epp/contacts_helper.rb +++ b/app/helpers/epp/contacts_helper.rb @@ -1,11 +1,13 @@ module Epp::ContactsHelper def create_contact - @contact = Contact.new( contact_and_address_attributes ) stamp @contact - @contact.save - - render '/epp/contacts/create' + if @contact.save + render '/epp/contacts/create' + else + handle_contact_errors + render '/epp/error' + end end def delete_contact @@ -45,7 +47,7 @@ module Epp::ContactsHelper @contact = Contact.where(code: ph[:id]).first case has_rights when true - render '/epp/contacts/info' + render 'epp/contacts/info' when false epp_errors << { code: '2201', msg: t('errors.messages.epp_authorization_error') } render 'epp/error' @@ -98,4 +100,13 @@ module Epp::ContactsHelper return nil end + def handle_contact_errors # handle_errors conflicted with domain logic + handle_epp_errors({ + '2302' => [:epp_id_taken], + '2303' => [:not_found, :epp_obj_does_not_exist] + }, @contact + ) + end + + end diff --git a/app/models/contact.rb b/app/models/contact.rb index 160555b75..23b843c57 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -15,6 +15,7 @@ class Contact < ActiveRecord::Base validate :ident_must_be_valid validates :phone, format: { with: /\+\d{3}\.\d+/, message: "bad format" } + validates_uniqueness_of :code, message: :epp_id_taken IDENT_TYPE_ICO = 'ico' IDENT_TYPES = [ diff --git a/config/locales/en.yml b/config/locales/en.yml index 23088e67a..f5157c9e3 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -66,3 +66,4 @@ en: epp_command_failed: 'Command failed' epp_nameservers_range_fail: 'Domain must have %{min}-%{max} nameservers' epp_authorization_error: 'Authorization error' + epp_id_taken: 'Contact id already exists' diff --git a/spec/epp/contact_spec.rb b/spec/epp/contact_spec.rb index e0ab61726..80dc2ce93 100644 --- a/spec/epp/contact_spec.rb +++ b/spec/epp/contact_spec.rb @@ -19,6 +19,16 @@ describe 'EPP Contact', epp: true do expect(Contact.first.org_name).to eq('Example Inc.') end + it 'does not create duplicate contact' do + Fabricate(:contact, code: 'sh8013') + + response = epp_request('contacts/create.xml') + expect(response[:result_code]).to eq('2302') + expect(response[:msg]).to eq('Contact id already exists') + + expect(Contact.count).to eq(1) + end + it 'updates a contact with same ident', pending: true do pending 'fixing this as soon as contact#update is done' Fabricate(:contact) @@ -56,7 +66,6 @@ describe 'EPP Contact', epp: true do it 'checks contacts' do Fabricate(:contact) - Fabricate(:contact, id:'sh8914') response = epp_request('contacts/check.xml') expect(response[:result_code]).to eq('1000')