email_address_validation_spec.rb 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe EmailAddressValidation do
  4. shared_examples 'email address validity' do |valid:, check_mx:|
  5. let(:email_address_validation) { described_class.new(email_address) }
  6. it 'reports given email address' do
  7. expect(email_address_validation.email_address).to eq(email_address)
  8. expect(email_address_validation.to_s).to eq(email_address)
  9. end
  10. it "reports email address as #{valid ? 'valid' : 'invalid'}" do
  11. expect(email_address_validation.valid?(check_mx: check_mx)).to be(valid)
  12. end
  13. end
  14. describe 'Email address' do
  15. describe 'with MX record' do
  16. let(:email_address) { 'greetings@zammad.org' }
  17. include_examples 'email address validity', valid: true, check_mx: true
  18. end
  19. describe 'without MX record' do
  20. let(:email_address) { 'someone@this-is-probably-a-non-existent-domain.com.example' }
  21. include_examples 'email address validity', valid: true, check_mx: false
  22. end
  23. describe 'with unicode characters' do
  24. let(:email_address) { 'ąžuolas@paštas.com' }
  25. include_examples 'email address validity', valid: true, check_mx: false
  26. end
  27. describe 'with special characters' do
  28. let(:email_address) { '"a+ddress="@example.com' }
  29. include_examples 'email address validity', valid: true, check_mx: false
  30. end
  31. describe 'with short legacy gmail address' do
  32. let(:email_address) { 'you@gmail.com' }
  33. include_examples 'email address validity', valid: true, check_mx: false
  34. end
  35. describe 'when very long local part' do
  36. let(:email_address) { 'd+aorgprcb5rboeldeti12rh-4fs92usdgsdzkw1xfppexrzd_efcxum1rxchmhjlplidc99k0141zduz2rmsvi2boiep-8b3-xh1dtbdvqjqn7lhguq6zh4nza8jmc2pv3thujtjatgtk@docs.google.com' }
  37. include_examples 'email address validity', valid: true, check_mx: false
  38. end
  39. describe 'when max length' do
  40. let(:email_address) { 'trulyverylongpastasdomainnamehere.trulyverylongpastasdomain@trulyverylongpastasdomainnameheredoublethatloremipsumnamecodena.trulyverylongpastasdomainnameheredoublethatloremipsumnamecodena.trulyverylongpastasdomainnameheredoublethatloremipsumname.com' }
  41. include_examples 'email address validity', valid: true, check_mx: false
  42. end
  43. describe 'when over max length' do
  44. let(:email_address) { 'trulyverylongpastasdomainnamehere.trulyverylongpastasdomainnamee@trulyverylongpastasdomainnameheredoublethatloremipsumnamecodena.trulyverylongpastasdomainnameheredoublethatloremipsumnamecodena.trulyverylongpastasdomainnameheredoublethatloremipsumname.com' }
  45. include_examples 'email address validity', valid: false, check_mx: false
  46. end
  47. describe 'when max length with unicode characters' do
  48. let(:email_address) { 'trulyverylongpaštasdomainnamehere.trulyverylongpaštasdomain@trulyverylongpaštasdomainnameheredoublethatloremipsumnamecodena.trulyverylongpaštasdomainnameheredoublethatloremipsumnamecodena.trulyverylongpaštasdomainnameheredob.com' }
  49. include_examples 'email address validity', valid: true, check_mx: false
  50. end
  51. describe 'when local non-localhost email' do
  52. let(:email_address) { 'test@localhost' }
  53. include_examples 'email address validity', valid: true, check_mx: false
  54. end
  55. describe 'without dot in domain' do
  56. let(:email_address) { 'greetings@localhost' }
  57. include_examples 'email address validity', valid: true, check_mx: false
  58. end
  59. describe 'without domain' do
  60. let(:email_address) { 'zammad' }
  61. include_examples 'email address validity', valid: false, check_mx: false
  62. end
  63. describe 'when too long' do
  64. let(:email_address) { 'trulyverylongpastasdomainnamehere.trulyverylongpastasdomainnamee@trulyverylongpastasdomainnameheredoublethatloremipsumnamecodena.trulyverylongpastasdomainnameheredoublethatloremipsumnamecodena.trulyverylongpastasdomainnameheredoublethatloremipsumnametoolong.com' }
  65. include_examples 'email address validity', valid: false, check_mx: false
  66. end
  67. describe 'with invalid domain format' do
  68. let(:email_address) { 'greetings@example..com' }
  69. include_examples 'email address validity', valid: false, check_mx: false
  70. end
  71. describe 'which is empty' do
  72. let(:email_address) { '' }
  73. include_examples 'email address validity', valid: false, check_mx: false
  74. end
  75. end
  76. end