email_mx_validator_spec.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe EmailMxValidator do
  4. describe '#validate' do
  5. let(:user) { double(email: 'foo@example.com', sign_up_ip: '1.2.3.4', errors: double(add: nil)) }
  6. context 'for an e-mail domain that is explicitly allowed' do
  7. around do |block|
  8. tmp = Rails.configuration.x.email_domains_whitelist
  9. Rails.configuration.x.email_domains_whitelist = 'example.com'
  10. block.call
  11. Rails.configuration.x.email_domains_whitelist = tmp
  12. end
  13. it 'does not add errors if there are no DNS records' do
  14. resolver = double
  15. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::MX).and_return([])
  16. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::A).and_return([])
  17. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::AAAA).and_return([])
  18. allow(resolver).to receive(:timeouts=).and_return(nil)
  19. allow(Resolv::DNS).to receive(:open).and_yield(resolver)
  20. subject.validate(user)
  21. expect(user.errors).to_not have_received(:add)
  22. end
  23. end
  24. it 'adds an error if there are no DNS records for the e-mail domain' do
  25. resolver = double
  26. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::MX).and_return([])
  27. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::A).and_return([])
  28. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::AAAA).and_return([])
  29. allow(resolver).to receive(:timeouts=).and_return(nil)
  30. allow(Resolv::DNS).to receive(:open).and_yield(resolver)
  31. subject.validate(user)
  32. expect(user.errors).to have_received(:add)
  33. end
  34. it 'adds an error if a MX record does not lead to an IP' do
  35. resolver = double
  36. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::MX).and_return([double(exchange: 'mail.example.com')])
  37. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::A).and_return([])
  38. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::AAAA).and_return([])
  39. allow(resolver).to receive(:getresources).with('mail.example.com', Resolv::DNS::Resource::IN::A).and_return([])
  40. allow(resolver).to receive(:getresources).with('mail.example.com', Resolv::DNS::Resource::IN::AAAA).and_return([])
  41. allow(resolver).to receive(:timeouts=).and_return(nil)
  42. allow(Resolv::DNS).to receive(:open).and_yield(resolver)
  43. subject.validate(user)
  44. expect(user.errors).to have_received(:add)
  45. end
  46. it 'adds an error if the MX record is blacklisted' do
  47. EmailDomainBlock.create!(domain: 'mail.example.com')
  48. resolver = double
  49. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::MX).and_return([double(exchange: 'mail.example.com')])
  50. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::A).and_return([])
  51. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::AAAA).and_return([])
  52. allow(resolver).to receive(:getresources).with('mail.example.com', Resolv::DNS::Resource::IN::A).and_return([double(address: '2.3.4.5')])
  53. allow(resolver).to receive(:getresources).with('mail.example.com', Resolv::DNS::Resource::IN::AAAA).and_return([double(address: 'fd00::2')])
  54. allow(resolver).to receive(:timeouts=).and_return(nil)
  55. allow(Resolv::DNS).to receive(:open).and_yield(resolver)
  56. subject.validate(user)
  57. expect(user.errors).to have_received(:add)
  58. end
  59. end
  60. end