From 1645b3e96fa99c1fe942758b448be86a4222a63f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andres=20Keskk=C3=BCla?= Date: Fri, 25 Jul 2014 14:19:21 +0300 Subject: [PATCH] Basic ident and phone validations --- Gemfile | 3 +++ Gemfile.lock | 2 ++ app/models/contact.rb | 11 ++++++++++- spec/epp/requests/contacts/create.xml | 4 ++-- spec/fabricators/contact_fabricator.rb | 6 ++++++ spec/models/contact_spec.rb | 22 ++++++++++++++++++++++ 6 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 spec/fabricators/contact_fabricator.rb diff --git a/Gemfile b/Gemfile index 72ff366d7..8961de1f1 100644 --- a/Gemfile +++ b/Gemfile @@ -37,6 +37,9 @@ gem 'nokogiri', '~> 1.6.2.1' # For punycode gem 'simpleidn', '~> 0.0.5' +#for EE-id validation +gem 'isikukood' + group :assets do # See https://github.com/sstephenson/execjs#readme for more supported runtimes gem 'therubyracer', platforms: :ruby diff --git a/Gemfile.lock b/Gemfile.lock index c2a741f8b..a428b85db 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -62,6 +62,7 @@ GEM hike (1.2.3) hpricot (0.8.6) i18n (0.6.9) + isikukood (0.1.2) jbuilder (2.1.1) activesupport (>= 3.0.0, < 5) multi_json (~> 1.2) @@ -185,6 +186,7 @@ DEPENDENCIES fabrication (~> 2.11.3) faker (~> 1.3.0) haml-rails (~> 0.5.3) + isikukood jbuilder (~> 2.0) jquery-rails nokogiri (~> 1.6.2.1) diff --git a/app/models/contact.rb b/app/models/contact.rb index 0beb316be..b77f5309f 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -1,7 +1,16 @@ class Contact < ActiveRecord::Base - #TODO Estonian id validation #TODO Foreign contact will get email with activation link/username/temp password #TODO Phone number validation, in first phase very minimam in order to support current registries has_many :addresses + validate :ident_must_be_valid + validates :phone, format: { with: /\+\d{3}\.\d+/, message: "bad format" } + + def ident_must_be_valid + #TODO Ident can also be passport number or company registry code. + #so have to make changes to validations (and doc/schema) accordingly + return true unless ident.present? + code = Isikukood.new(ident) + errors.add(:ident, 'bad format') unless code.valid? + end end diff --git a/spec/epp/requests/contacts/create.xml b/spec/epp/requests/contacts/create.xml index c19bfe953..3774a2706 100644 --- a/spec/epp/requests/contacts/create.xml +++ b/spec/epp/requests/contacts/create.xml @@ -17,10 +17,10 @@ EE - +1.7035555555 + +123.7035555555 +1.7035555556 jdoe@example.com - 12345678901 + 37605030299 2fooBAR diff --git a/spec/fabricators/contact_fabricator.rb b/spec/fabricators/contact_fabricator.rb new file mode 100644 index 000000000..31715f979 --- /dev/null +++ b/spec/fabricators/contact_fabricator.rb @@ -0,0 +1,6 @@ +Fabricator(:contact) do + name Faker::Name.name + phone '+372.12345678' + email Faker::Internet.email + ident '37605030299' +end diff --git a/spec/models/contact_spec.rb b/spec/models/contact_spec.rb index 567c21c99..b5bb81f41 100644 --- a/spec/models/contact_spec.rb +++ b/spec/models/contact_spec.rb @@ -2,4 +2,26 @@ require "rails_helper" describe Contact do it { should have_many(:addresses) } + + context 'with invalid attribute' do + before(:each) { @contact = Fabricate(:contact) } + + it 'phone should return false' do + @contact.phone = "32341" + expect(@contact.valid?).to be false + end + + it 'ident should return false' do + @contact.ident = "123abc" + expect(@contact.valid?).to be false + end + end + + context 'with valid attributes' do + before(:each) { @contact = Fabricate(:contact) } + + it 'should return true' do + expect(@contact.valid?).to be true + end + end end