email_mx_validator_spec.rb 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 no error if there are 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([Resolv::DNS::Resource::IN::A.new('192.0.2.42')])
  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).not_to have_received(:add)
  33. end
  34. it 'adds an error if the email domain name contains empty labels' do
  35. resolver = double
  36. allow(resolver).to receive(:getresources).with('example..com', Resolv::DNS::Resource::IN::MX).and_return([])
  37. allow(resolver).to receive(:getresources).with('example..com', Resolv::DNS::Resource::IN::A).and_return([Resolv::DNS::Resource::IN::A.new('192.0.2.42')])
  38. allow(resolver).to receive(:getresources).with('example..com', Resolv::DNS::Resource::IN::AAAA).and_return([])
  39. allow(resolver).to receive(:timeouts=).and_return(nil)
  40. allow(Resolv::DNS).to receive(:open).and_yield(resolver)
  41. user = double(email: 'foo@example..com', sign_up_ip: '1.2.3.4', errors: double(add: nil))
  42. subject.validate(user)
  43. expect(user.errors).to have_received(:add)
  44. end
  45. it 'adds an error if there are no DNS records for the e-mail domain' do
  46. resolver = double
  47. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::MX).and_return([])
  48. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::A).and_return([])
  49. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::AAAA).and_return([])
  50. allow(resolver).to receive(:timeouts=).and_return(nil)
  51. allow(Resolv::DNS).to receive(:open).and_yield(resolver)
  52. subject.validate(user)
  53. expect(user.errors).to have_received(:add)
  54. end
  55. it 'adds an error if a MX record does not lead to an IP' do
  56. resolver = double
  57. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::MX).and_return([double(exchange: 'mail.example.com')])
  58. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::A).and_return([])
  59. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::AAAA).and_return([])
  60. allow(resolver).to receive(:getresources).with('mail.example.com', Resolv::DNS::Resource::IN::A).and_return([])
  61. allow(resolver).to receive(:getresources).with('mail.example.com', Resolv::DNS::Resource::IN::AAAA).and_return([])
  62. allow(resolver).to receive(:timeouts=).and_return(nil)
  63. allow(Resolv::DNS).to receive(:open).and_yield(resolver)
  64. subject.validate(user)
  65. expect(user.errors).to have_received(:add)
  66. end
  67. it 'adds an error if the MX record is blacklisted' do
  68. EmailDomainBlock.create!(domain: 'mail.example.com')
  69. resolver = double
  70. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::MX).and_return([double(exchange: 'mail.example.com')])
  71. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::A).and_return([])
  72. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::AAAA).and_return([])
  73. allow(resolver).to receive(:getresources).with('mail.example.com', Resolv::DNS::Resource::IN::A).and_return([double(address: '2.3.4.5')])
  74. allow(resolver).to receive(:getresources).with('mail.example.com', Resolv::DNS::Resource::IN::AAAA).and_return([double(address: 'fd00::2')])
  75. allow(resolver).to receive(:timeouts=).and_return(nil)
  76. allow(Resolv::DNS).to receive(:open).and_yield(resolver)
  77. subject.validate(user)
  78. expect(user.errors).to have_received(:add)
  79. end
  80. end
  81. end