diff --git a/test/helpers/phone_format_helper_test.rb b/test/helpers/phone_format_helper_test.rb new file mode 100644 index 000000000..45ee80300 --- /dev/null +++ b/test/helpers/phone_format_helper_test.rb @@ -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 \ No newline at end of file diff --git a/test/models/contact_test.rb b/test/models/contact_test.rb index bd4e1c0c1..d258d7fb4 100644 --- a/test/models/contact_test.rb +++ b/test/models/contact_test.rb @@ -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 diff --git a/test/models/depp_contact_test.rb b/test/models/depp_contact_test.rb new file mode 100644 index 000000000..12f7c3b4f --- /dev/null +++ b/test/models/depp_contact_test.rb @@ -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 \ No newline at end of file