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.

118 lines
5.8 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', 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).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 = double(email: 'foo@example.com', errors: double(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 = double(email: 'foo@', errors: double(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 = double
  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 = double(email: 'foo@example..com', sign_up_ip: '1.2.3.4', errors: double(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 = double
  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 = 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([])
  74. allow(resolver).to receive(:getresources).with('mail.example.com', Resolv::DNS::Resource::IN::AAAA).and_return([])
  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. it 'adds an error if the MX record is blacklisted' do
  81. EmailDomainBlock.create!(domain: 'mail.example.com')
  82. resolver = double
  83. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::MX).and_return([double(exchange: 'mail.example.com')])
  84. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::A).and_return([])
  85. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::AAAA).and_return([])
  86. allow(resolver).to receive(:getresources).with('mail.example.com', Resolv::DNS::Resource::IN::A).and_return([double(address: '2.3.4.5')])
  87. allow(resolver).to receive(:getresources).with('mail.example.com', Resolv::DNS::Resource::IN::AAAA).and_return([double(address: 'fd00::2')])
  88. allow(resolver).to receive(:timeouts=).and_return(nil)
  89. allow(Resolv::DNS).to receive(:open).and_yield(resolver)
  90. subject.validate(user)
  91. expect(user.errors).to have_received(:add)
  92. end
  93. end
  94. end