From 5003a3ff20ddbb69681bc034ed1c4821b327c680 Mon Sep 17 00:00:00 2001 From: Martin Lensment Date: Thu, 31 Jul 2014 14:04:22 +0300 Subject: [PATCH] Better contact validation --- app/helpers/epp/domains_helper.rb | 8 +++++++ app/models/domain.rb | 10 +++++++-- config/locales/en.yml | 2 ++ spec/epp/domain_spec.rb | 17 ++++++++++++--- .../requests/domains/create_wo_contacts.xml | 21 +++++++++++++++++++ 5 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 spec/epp/requests/domains/create_wo_contacts.xml diff --git a/app/helpers/epp/domains_helper.rb b/app/helpers/epp/domains_helper.rb index 5958d46a8..f413178c8 100644 --- a/app/helpers/epp/domains_helper.rb +++ b/app/helpers/epp/domains_helper.rb @@ -7,7 +7,9 @@ module Epp::DomainsHelper render '/epp/domains/create' else handle_domain_name_errors + handle_contact_errors render '/epp/error' + raise ActiveRecord::Rollback end end end @@ -56,4 +58,10 @@ module Epp::DomainsHelper end end + def handle_contact_errors + if @domain.errors.added?(:admin_contacts, :blank) + epp_errors << {code: '2306', msg: @domain.errors[:admin_contacts].first} + end + end + end diff --git a/app/models/domain.rb b/app/models/domain.rb index f9dbcdb50..cc5d15d62 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -24,7 +24,7 @@ class Domain < ActiveRecord::Base validates :period, numericality: { only_integer: true, greater_than: 0, less_than: 100 } validates :owner_contact, presence: true # validates :tech_contacts_count - # validates :admin_contacts_count + #validate :admin_contacts_count, on: :update def name=(value) value.strip! @@ -46,7 +46,9 @@ class Domain < ActiveRecord::Base attach_contact(Contact::CONTACT_TYPE_ADMIN, owner_contact) if admin_contacts.empty? end - true + validate_admin_contacts_count + + errors.empty? end def attach_contact(type, contact) @@ -56,6 +58,10 @@ class Domain < ActiveRecord::Base ) end + def validate_admin_contacts_count + errors.add(:admin_contacts, :blank) if admin_contacts.empty? + end + class << self def check_availability(domains) domains = [domains] if domains.is_a?(String) diff --git a/config/locales/en.yml b/config/locales/en.yml index 11d9c1113..192f4e230 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -41,6 +41,8 @@ en: blank: 'Required parameter missing - name' owner_contact: blank: 'Required parameter missing - owner contact' + admin_contacts: + blank: 'Required parameter missing - admin contact' errors: diff --git a/spec/epp/domain_spec.rb b/spec/epp/domain_spec.rb index 6e54b616e..12c49dcd7 100644 --- a/spec/epp/domain_spec.rb +++ b/spec/epp/domain_spec.rb @@ -56,12 +56,13 @@ describe 'EPP Domain', epp: true do end context 'with juridical persion as an owner' do - before(:each) { Fabricate(:contact, code: 'jd1234', ident_type: 'ico')} - - it 'creates a domain with contacts' do + before(:each) { Fabricate(:contact, code: 'sh8013') Fabricate(:contact, code: 'sh801333') + Fabricate(:contact, code: 'jd1234', ident_type: 'ico') + } + it 'creates a domain with contacts' do response = epp_request('domains/create_wo_tech_contact.xml') expect(response[:result_code]).to eq('1000') expect(response[:msg]).to eq('Command completed successfully') @@ -73,6 +74,16 @@ describe 'EPP Domain', epp: true do tech_contact = Domain.first.tech_contacts.first expect(tech_contact.code).to eq('jd1234') end + + it 'does not create a domain without admin contact' do + response = epp_request('domains/create_wo_contacts.xml') + expect(response[:result_code]).to eq('2306') + expect(response[:msg]).to eq('Required parameter missing - admin contact') + expect(response[:clTRID]).to eq('ABC-12345') + + expect(Domain.count).to eq 0 + expect(DomainContact.count).to eq 0 + end end it 'checks a domain' do diff --git a/spec/epp/requests/domains/create_wo_contacts.xml b/spec/epp/requests/domains/create_wo_contacts.xml new file mode 100644 index 000000000..18c2e8dc4 --- /dev/null +++ b/spec/epp/requests/domains/create_wo_contacts.xml @@ -0,0 +1,21 @@ + + + + + + example.ee + 1 + + ns1.example.net + ns2.example.net + + jd1234 + + 2fooBAR + + + + ABC-12345 + +