email_mx_validator_spec.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe EmailMxValidator do
  4. describe '#validate' do
  5. let(:user) { instance_double(User, email: 'foo@example.com', sign_up_ip: '1.2.3.4', errors: instance_double(ActiveModel::Errors, add: nil)) }
  6. let(:resolv_dns_double) { instance_double(Resolv::DNS) }
  7. context 'with an e-mail domain that is explicitly allowed' do
  8. around do |block|
  9. tmp = Rails.configuration.x.email_domains_allowlist
  10. Rails.configuration.x.email_domains_allowlist = 'example.com'
  11. block.call
  12. Rails.configuration.x.email_domains_allowlist = tmp
  13. end
  14. it 'does not add errors if there are no DNS records' do
  15. configure_resolver('example.com')
  16. subject.validate(user)
  17. expect(user.errors).to_not have_received(:add)
  18. end
  19. end
  20. it 'adds no error if there are DNS records for the e-mail domain' do
  21. configure_resolver('example.com', a: resolv_double_a('192.0.2.42'))
  22. subject.validate(user)
  23. expect(user.errors).to_not have_received(:add)
  24. end
  25. it 'adds an error if the TagManager fails to normalize domain' do
  26. double = instance_double(TagManager)
  27. allow(TagManager).to receive(:instance).and_return(double)
  28. allow(double).to receive(:normalize_domain).with('example.com').and_raise(Addressable::URI::InvalidURIError)
  29. user = instance_double(User, email: 'foo@example.com', errors: instance_double(ActiveModel::Errors, add: nil))
  30. subject.validate(user)
  31. expect(user.errors).to have_received(:add)
  32. end
  33. it 'adds an error if the domain email portion is blank' do
  34. user = instance_double(User, email: 'foo@', errors: instance_double(ActiveModel::Errors, add: nil))
  35. subject.validate(user)
  36. expect(user.errors).to have_received(:add)
  37. end
  38. it 'adds an error if the email domain name contains empty labels' do
  39. configure_resolver('example..com', a: resolv_double_a('192.0.2.42'))
  40. user = instance_double(User, email: 'foo@example..com', sign_up_ip: '1.2.3.4', errors: instance_double(ActiveModel::Errors, add: nil))
  41. subject.validate(user)
  42. expect(user.errors).to have_received(:add)
  43. end
  44. it 'adds an error if there are no DNS records for the e-mail domain' do
  45. configure_resolver('example.com')
  46. subject.validate(user)
  47. expect(user.errors).to have_received(:add)
  48. end
  49. it 'adds an error if a MX record does not lead to an IP' do
  50. configure_resolver('example.com', mx: resolv_double_mx('mail.example.com'))
  51. configure_resolver('mail.example.com')
  52. subject.validate(user)
  53. expect(user.errors).to have_received(:add)
  54. end
  55. it 'adds an error if the MX record has an email domain block' do
  56. EmailDomainBlock.create!(domain: 'mail.example.com')
  57. configure_resolver(
  58. 'example.com',
  59. mx: resolv_double_mx('mail.example.com')
  60. )
  61. configure_resolver(
  62. 'mail.example.com',
  63. a: instance_double(Resolv::DNS::Resource::IN::A, address: '2.3.4.5'),
  64. aaaa: instance_double(Resolv::DNS::Resource::IN::AAAA, address: 'fd00::2')
  65. )
  66. subject.validate(user)
  67. expect(user.errors).to have_received(:add)
  68. end
  69. end
  70. def configure_resolver(domain, options = {})
  71. allow(resolv_dns_double)
  72. .to receive(:getresources)
  73. .with(domain, Resolv::DNS::Resource::IN::MX)
  74. .and_return(Array(options[:mx]))
  75. allow(resolv_dns_double)
  76. .to receive(:getresources)
  77. .with(domain, Resolv::DNS::Resource::IN::A)
  78. .and_return(Array(options[:a]))
  79. allow(resolv_dns_double)
  80. .to receive(:getresources)
  81. .with(domain, Resolv::DNS::Resource::IN::AAAA)
  82. .and_return(Array(options[:aaaa]))
  83. allow(resolv_dns_double)
  84. .to receive(:timeouts=)
  85. .and_return(nil)
  86. allow(Resolv::DNS)
  87. .to receive(:open)
  88. .and_yield(resolv_dns_double)
  89. end
  90. def resolv_double_mx(domain)
  91. instance_double(Resolv::DNS::Resource::MX, exchange: domain)
  92. end
  93. def resolv_double_a(domain)
  94. Resolv::DNS::Resource::IN::A.new(domain)
  95. end
  96. end