Added model test and created helper for E.164 phone format testing

This commit is contained in:
Sergei Tsõganov 2022-01-07 16:05:13 +02:00 committed by Sergei Tsõganov
parent 8c8e9c8619
commit d361288329
3 changed files with 55 additions and 31 deletions

View file

@ -0,0 +1,37 @@
module PhoneFormatHelperTest
# https://en.wikipedia.org/wiki/E.164
def assert_phone_format(contact)
contact.phone = '+.1'
assert contact.invalid?
contact.phone = '+123.'
assert contact.invalid?
contact.phone = '+1.123456789123456'
assert contact.invalid?
contact.phone = '+134.1234567891234'
assert contact.invalid?
contact.phone = '+000.1'
assert contact.invalid?
contact.phone = '+123.0'
assert contact.invalid?
contact.phone = '+1.2'
assert contact.valid?
contact.phone = '+123.4'
assert contact.valid?
contact.phone = '+1.12345678912345'
assert contact.valid?
contact.phone = '+134.123456789123'
assert contact.valid?
contact.phone = '+134.00000000'
assert contact.invalid?
end
end

View file

@ -1,6 +1,9 @@
require 'test_helper'
require 'helpers/phone_format_helper_test'
class ContactTest < ActiveJob::TestCase
include PhoneFormatHelperTest
setup do
@contact = contacts(:john)
@old_validation_type = Truemail.configure.default_validation_type
@ -102,39 +105,9 @@ class ContactTest < ActiveJob::TestCase
assert contact.invalid?
end
# https://en.wikipedia.org/wiki/E.164
def test_validates_phone_format
contact = valid_contact
contact.phone = '+.1'
assert contact.invalid?
contact.phone = '+123.'
assert contact.invalid?
contact.phone = '+1.123456789123456'
assert contact.invalid?
contact.phone = '+134.1234567891234'
assert contact.invalid?
contact.phone = '+000.1'
assert contact.invalid?
contact.phone = '+123.0'
assert contact.invalid?
contact.phone = '+1.2'
assert contact.valid?
contact.phone = '+123.4'
assert contact.valid?
contact.phone = '+1.12345678912345'
assert contact.valid?
contact.phone = '+134.123456789123'
assert contact.valid?
assert_phone_format(contact)
end
def test_valid_without_address_when_address_processing_id_disabled

View file

@ -0,0 +1,14 @@
require 'test_helper'
require 'helpers/phone_format_helper_test'
class DeppContactTest < ActiveSupport::TestCase
include PhoneFormatHelperTest
setup do
@depp_contact = Depp::Contact.new
end
def test_validates_phone_format
assert_phone_format(@depp_contact)
end
end