email_mx_validator_spec.rb 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. 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. context 'with 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 = instance_double(Resolv::DNS)
  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 = instance_double(Resolv::DNS)
  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).to_not have_received(:add)
  33. end
  34. it 'adds an error if the TagManager fails to normalize domain' do
  35. double = instance_double(TagManager)
  36. allow(TagManager).to receive(:instance).and_return(double)
  37. allow(double).to receive(:normalize_domain).with('example.com').and_raise(Addressable::URI::InvalidURIError)
  38. user = instance_double(User, email: 'foo@example.com', errors: instance_double(ActiveModel::Errors, add: nil))
  39. subject.validate(user)
  40. expect(user.errors).to have_received(:add)
  41. end
  42. it 'adds an error if the domain email portion is blank' do
  43. user = instance_double(User, email: 'foo@', errors: instance_double(ActiveModel::Errors, add: nil))
  44. subject.validate(user)
  45. expect(user.errors).to have_received(:add)
  46. end
  47. it 'adds an error if the email domain name contains empty labels' do
  48. resolver = instance_double(Resolv::DNS)
  49. allow(resolver).to receive(:getresources).with('example..com', Resolv::DNS::Resource::IN::MX).and_return([])
  50. 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')])
  51. allow(resolver).to receive(:getresources).with('example..com', Resolv::DNS::Resource::IN::AAAA).and_return([])
  52. allow(resolver).to receive(:timeouts=).and_return(nil)
  53. allow(Resolv::DNS).to receive(:open).and_yield(resolver)
  54. user = instance_double(User, email: 'foo@example..com', sign_up_ip: '1.2.3.4', errors: instance_double(ActiveModel::Errors, add: nil))
  55. subject.validate(user)
  56. expect(user.errors).to have_received(:add)
  57. end
  58. it 'adds an error if there are no DNS records for the e-mail domain' do
  59. resolver = instance_double(Resolv::DNS)
  60. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::MX).and_return([])
  61. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::A).and_return([])
  62. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::AAAA).and_return([])
  63. allow(resolver).to receive(:timeouts=).and_return(nil)
  64. allow(Resolv::DNS).to receive(:open).and_yield(resolver)
  65. subject.validate(user)
  66. expect(user.errors).to have_received(:add)
  67. end
  68. it 'adds an error if a MX record does not lead to an IP' do
  69. resolver = instance_double(Resolv::DNS)
  70. allow(resolver).to receive(:getresources)
  71. .with('example.com', Resolv::DNS::Resource::IN::MX)
  72. .and_return([instance_double(Resolv::DNS::Resource::MX, exchange: 'mail.example.com')])
  73. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::A).and_return([])
  74. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::AAAA).and_return([])
  75. allow(resolver).to receive(:getresources).with('mail.example.com', Resolv::DNS::Resource::IN::A).and_return([])
  76. allow(resolver).to receive(:getresources).with('mail.example.com', Resolv::DNS::Resource::IN::AAAA).and_return([])
  77. allow(resolver).to receive(:timeouts=).and_return(nil)
  78. allow(Resolv::DNS).to receive(:open).and_yield(resolver)
  79. subject.validate(user)
  80. expect(user.errors).to have_received(:add)
  81. end
  82. it 'adds an error if the MX record is blacklisted' do
  83. EmailDomainBlock.create!(domain: 'mail.example.com')
  84. resolver = instance_double(Resolv::DNS)
  85. allow(resolver).to receive(:getresources)
  86. .with('example.com', Resolv::DNS::Resource::IN::MX)
  87. .and_return([instance_double(Resolv::DNS::Resource::MX, exchange: 'mail.example.com')])
  88. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::A).and_return([])
  89. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::AAAA).and_return([])
  90. allow(resolver).to receive(:getresources).with('mail.example.com', Resolv::DNS::Resource::IN::A).and_return([instance_double(Resolv::DNS::Resource::IN::A, address: '2.3.4.5')])
  91. allow(resolver).to receive(:getresources).with('mail.example.com', Resolv::DNS::Resource::IN::AAAA).and_return([instance_double(Resolv::DNS::Resource::IN::A, address: 'fd00::2')])
  92. allow(resolver).to receive(:timeouts=).and_return(nil)
  93. allow(Resolv::DNS).to receive(:open).and_yield(resolver)
  94. subject.validate(user)
  95. expect(user.errors).to have_received(:add)
  96. end
  97. end
  98. end