1
0

domain_helpers.rb 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # frozen_string_literal: true
  2. module DomainHelpers
  3. def configure_mx(domain:, exchange:, ip_v4_addr: '2.3.4.5', ip_v6_addr: 'fd00::2')
  4. resolver = instance_double(Resolv::DNS, :timeouts= => nil)
  5. allow(resolver).to receive(:getresources)
  6. .with(domain, Resolv::DNS::Resource::IN::MX)
  7. .and_return([double_mx(exchange)])
  8. allow(resolver)
  9. .to receive(:getresources)
  10. .with(domain, Resolv::DNS::Resource::IN::A)
  11. .and_return([])
  12. allow(resolver)
  13. .to receive(:getresources)
  14. .with(domain, Resolv::DNS::Resource::IN::AAAA)
  15. .and_return([])
  16. allow(resolver)
  17. .to receive(:getresources)
  18. .with(exchange, Resolv::DNS::Resource::IN::A)
  19. .and_return([double_resource_v4(ip_v4_addr)])
  20. allow(resolver)
  21. .to receive(:getresources)
  22. .with(exchange, Resolv::DNS::Resource::IN::AAAA)
  23. .and_return([double_resource_v6(ip_v6_addr)])
  24. allow(Resolv::DNS)
  25. .to receive(:open)
  26. .and_yield(resolver)
  27. end
  28. private
  29. def double_mx(exchange)
  30. instance_double(Resolv::DNS::Resource::MX, exchange: exchange)
  31. end
  32. def double_resource_v4(addr)
  33. instance_double(Resolv::DNS::Resource::IN::A, address: addr)
  34. end
  35. def double_resource_v6(addr)
  36. instance_double(Resolv::DNS::Resource::IN::AAAA, address: addr)
  37. end
  38. end