You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
3.5 KiB

  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', errors: double(add: nil)) }
  6. it 'adds an error if there are no DNS records for the e-mail domain' do
  7. resolver = double
  8. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::MX).and_return([])
  9. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::A).and_return([])
  10. allow(resolver).to receive(:timeouts=).and_return(nil)
  11. allow(Resolv::DNS).to receive(:open).and_yield(resolver)
  12. subject.validate(user)
  13. expect(user.errors).to have_received(:add)
  14. end
  15. it 'adds an error if a MX record exists but does not lead to an IP' do
  16. resolver = double
  17. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::MX).and_return([double(exchange: 'mail.example.com')])
  18. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::A).and_return([])
  19. allow(resolver).to receive(:getresources).with('mail.example.com', Resolv::DNS::Resource::IN::A).and_return([])
  20. allow(resolver).to receive(:timeouts=).and_return(nil)
  21. allow(Resolv::DNS).to receive(:open).and_yield(resolver)
  22. subject.validate(user)
  23. expect(user.errors).to have_received(:add)
  24. end
  25. it 'adds an error if the A record is blacklisted' do
  26. EmailDomainBlock.create!(domain: '1.2.3.4')
  27. resolver = double
  28. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::MX).and_return([])
  29. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::A).and_return([double(address: '1.2.3.4')])
  30. allow(resolver).to receive(:timeouts=).and_return(nil)
  31. allow(Resolv::DNS).to receive(:open).and_yield(resolver)
  32. subject.validate(user)
  33. expect(user.errors).to have_received(:add)
  34. end
  35. it 'adds an error if the MX record is blacklisted' do
  36. EmailDomainBlock.create!(domain: '2.3.4.5')
  37. resolver = double
  38. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::MX).and_return([double(exchange: 'mail.example.com')])
  39. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::A).and_return([])
  40. allow(resolver).to receive(:getresources).with('mail.example.com', Resolv::DNS::Resource::IN::A).and_return([double(address: '2.3.4.5')])
  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 hostname 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('mail.example.com', Resolv::DNS::Resource::IN::A).and_return([double(address: '2.3.4.5')])
  52. allow(resolver).to receive(:timeouts=).and_return(nil)
  53. allow(Resolv::DNS).to receive(:open).and_yield(resolver)
  54. subject.validate(user)
  55. expect(user.errors).to have_received(:add)
  56. end
  57. end
  58. end