闭社主体 forked from https://github.com/tootsuite/mastodon
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.

113 lines
6.1 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(:getresources).with('example.com', Resolv::DNS::Resource::IN::AAAA).and_return([])
  11. allow(resolver).to receive(:timeouts=).and_return(nil)
  12. allow(Resolv::DNS).to receive(:open).and_yield(resolver)
  13. subject.validate(user)
  14. expect(user.errors).to have_received(:add)
  15. end
  16. it 'adds an error if a MX record exists but does not lead to an IP' do
  17. resolver = double
  18. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::MX).and_return([double(exchange: 'mail.example.com')])
  19. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::A).and_return([])
  20. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::AAAA).and_return([])
  21. allow(resolver).to receive(:getresources).with('mail.example.com', Resolv::DNS::Resource::IN::A).and_return([])
  22. allow(resolver).to receive(:getresources).with('mail.example.com', Resolv::DNS::Resource::IN::AAAA).and_return([])
  23. allow(resolver).to receive(:timeouts=).and_return(nil)
  24. allow(Resolv::DNS).to receive(:open).and_yield(resolver)
  25. subject.validate(user)
  26. expect(user.errors).to have_received(:add)
  27. end
  28. it 'adds an error if the A record is blacklisted' do
  29. EmailDomainBlock.create!(domain: '1.2.3.4')
  30. resolver = double
  31. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::MX).and_return([])
  32. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::A).and_return([double(address: '1.2.3.4')])
  33. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::AAAA).and_return([])
  34. allow(resolver).to receive(:timeouts=).and_return(nil)
  35. allow(Resolv::DNS).to receive(:open).and_yield(resolver)
  36. subject.validate(user)
  37. expect(user.errors).to have_received(:add)
  38. end
  39. it 'adds an error if the AAAA record is blacklisted' do
  40. EmailDomainBlock.create!(domain: 'fd00::1')
  41. resolver = double
  42. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::MX).and_return([])
  43. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::A).and_return([])
  44. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::AAAA).and_return([double(address: 'fd00::1')])
  45. allow(resolver).to receive(:timeouts=).and_return(nil)
  46. allow(Resolv::DNS).to receive(:open).and_yield(resolver)
  47. subject.validate(user)
  48. expect(user.errors).to have_received(:add)
  49. end
  50. it 'adds an error if the MX record is blacklisted' do
  51. EmailDomainBlock.create!(domain: '2.3.4.5')
  52. resolver = double
  53. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::MX).and_return([double(exchange: 'mail.example.com')])
  54. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::A).and_return([])
  55. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::AAAA).and_return([])
  56. allow(resolver).to receive(:getresources).with('mail.example.com', Resolv::DNS::Resource::IN::A).and_return([double(address: '2.3.4.5')])
  57. allow(resolver).to receive(:getresources).with('mail.example.com', Resolv::DNS::Resource::IN::AAAA).and_return([])
  58. allow(resolver).to receive(:timeouts=).and_return(nil)
  59. allow(Resolv::DNS).to receive(:open).and_yield(resolver)
  60. subject.validate(user)
  61. expect(user.errors).to have_received(:add)
  62. end
  63. it 'adds an error if the MX IPv6 record is blacklisted' do
  64. EmailDomainBlock.create!(domain: 'fd00::2')
  65. resolver = double
  66. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::MX).and_return([double(exchange: 'mail.example.com')])
  67. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::A).and_return([])
  68. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::AAAA).and_return([])
  69. allow(resolver).to receive(:getresources).with('mail.example.com', Resolv::DNS::Resource::IN::A).and_return([])
  70. allow(resolver).to receive(:getresources).with('mail.example.com', Resolv::DNS::Resource::IN::AAAA).and_return([double(address: 'fd00::2')])
  71. allow(resolver).to receive(:timeouts=).and_return(nil)
  72. allow(Resolv::DNS).to receive(:open).and_yield(resolver)
  73. subject.validate(user)
  74. expect(user.errors).to have_received(:add)
  75. end
  76. it 'adds an error if the MX hostname is blacklisted' do
  77. EmailDomainBlock.create!(domain: 'mail.example.com')
  78. resolver = double
  79. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::MX).and_return([double(exchange: 'mail.example.com')])
  80. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::A).and_return([])
  81. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::AAAA).and_return([])
  82. allow(resolver).to receive(:getresources).with('mail.example.com', Resolv::DNS::Resource::IN::A).and_return([double(address: '2.3.4.5')])
  83. allow(resolver).to receive(:getresources).with('mail.example.com', Resolv::DNS::Resource::IN::AAAA).and_return([double(address: 'fd00::2')])
  84. allow(resolver).to receive(:timeouts=).and_return(nil)
  85. allow(Resolv::DNS).to receive(:open).and_yield(resolver)
  86. subject.validate(user)
  87. expect(user.errors).to have_received(:add)
  88. end
  89. end
  90. end