From df3b9547202c07ec7ec7f6af8295deb272c6f5ca Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Tue, 29 Oct 2024 11:35:25 -0400 Subject: [PATCH] Add `DomainHelpers` spec support module for DNS/MX stub (#32690) --- .../auth/registrations_controller_spec.rb | 12 +---- spec/rails_helper.rb | 1 + spec/services/app_sign_up_service_spec.rb | 12 +---- spec/support/domain_helpers.rb | 44 +++++++++++++++++++ 4 files changed, 47 insertions(+), 22 deletions(-) create mode 100644 spec/support/domain_helpers.rb diff --git a/spec/controllers/auth/registrations_controller_spec.rb b/spec/controllers/auth/registrations_controller_spec.rb index 6118edf4e3..d1542128e7 100644 --- a/spec/controllers/auth/registrations_controller_spec.rb +++ b/spec/controllers/auth/registrations_controller_spec.rb @@ -233,17 +233,7 @@ RSpec.describe Auth::RegistrationsController do Setting.registrations_mode = 'open' Fabricate(:email_domain_block, allow_with_approval: true, domain: 'mail.example.com') allow(User).to receive(:skip_mx_check?).and_return(false) - - resolver = instance_double(Resolv::DNS, :timeouts= => nil) - - allow(resolver).to receive(:getresources) - .with('example.com', Resolv::DNS::Resource::IN::MX) - .and_return([instance_double(Resolv::DNS::Resource::MX, exchange: 'mail.example.com')]) - allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::A).and_return([]) - allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::AAAA).and_return([]) - 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')]) - allow(resolver).to receive(:getresources).with('mail.example.com', Resolv::DNS::Resource::IN::AAAA).and_return([instance_double(Resolv::DNS::Resource::IN::AAAA, address: 'fd00::2')]) - allow(Resolv::DNS).to receive(:open).and_yield(resolver) + configure_mx(domain: 'example.com', exchange: 'mail.example.com') end it 'creates unapproved user and redirects to setup' do diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 91a2e21bbb..d2ad40be73 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -113,6 +113,7 @@ RSpec.configure do |config| config.include ActiveSupport::Testing::TimeHelpers config.include Chewy::Rspec::Helpers config.include Redisable + config.include DomainHelpers config.include ThreadingHelpers config.include SignedRequestHelpers, type: :request config.include CommandLineHelpers, type: :cli diff --git a/spec/services/app_sign_up_service_spec.rb b/spec/services/app_sign_up_service_spec.rb index ec7b7516f9..b78868db49 100644 --- a/spec/services/app_sign_up_service_spec.rb +++ b/spec/services/app_sign_up_service_spec.rb @@ -53,17 +53,7 @@ RSpec.describe AppSignUpService do Setting.registrations_mode = 'open' Fabricate(:email_domain_block, allow_with_approval: true, domain: 'smtp.email.com') allow(User).to receive(:skip_mx_check?).and_return(false) - - resolver = instance_double(Resolv::DNS, :timeouts= => nil) - - allow(resolver).to receive(:getresources) - .with('email.com', Resolv::DNS::Resource::IN::MX) - .and_return([instance_double(Resolv::DNS::Resource::MX, exchange: 'smtp.email.com')]) - allow(resolver).to receive(:getresources).with('email.com', Resolv::DNS::Resource::IN::A).and_return([]) - allow(resolver).to receive(:getresources).with('email.com', Resolv::DNS::Resource::IN::AAAA).and_return([]) - allow(resolver).to receive(:getresources).with('smtp.email.com', Resolv::DNS::Resource::IN::A).and_return([instance_double(Resolv::DNS::Resource::IN::A, address: '2.3.4.5')]) - allow(resolver).to receive(:getresources).with('smtp.email.com', Resolv::DNS::Resource::IN::AAAA).and_return([instance_double(Resolv::DNS::Resource::IN::AAAA, address: 'fd00::2')]) - allow(Resolv::DNS).to receive(:open).and_yield(resolver) + configure_mx(domain: 'email.com', exchange: 'smtp.email.com') end it 'creates an unapproved user', :aggregate_failures do diff --git a/spec/support/domain_helpers.rb b/spec/support/domain_helpers.rb new file mode 100644 index 0000000000..9977702099 --- /dev/null +++ b/spec/support/domain_helpers.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +module DomainHelpers + def configure_mx(domain:, exchange:, ip_v4_addr: '2.3.4.5', ip_v6_addr: 'fd00::2') + resolver = instance_double(Resolv::DNS, :timeouts= => nil) + + allow(resolver).to receive(:getresources) + .with(domain, Resolv::DNS::Resource::IN::MX) + .and_return([double_mx(exchange)]) + allow(resolver) + .to receive(:getresources) + .with(domain, Resolv::DNS::Resource::IN::A) + .and_return([]) + allow(resolver) + .to receive(:getresources) + .with(domain, Resolv::DNS::Resource::IN::AAAA) + .and_return([]) + allow(resolver) + .to receive(:getresources) + .with(exchange, Resolv::DNS::Resource::IN::A) + .and_return([double_resource_v4(ip_v4_addr)]) + allow(resolver) + .to receive(:getresources) + .with(exchange, Resolv::DNS::Resource::IN::AAAA) + .and_return([double_resource_v6(ip_v6_addr)]) + allow(Resolv::DNS) + .to receive(:open) + .and_yield(resolver) + end + + private + + def double_mx(exchange) + instance_double(Resolv::DNS::Resource::MX, exchange: exchange) + end + + def double_resource_v4(addr) + instance_double(Resolv::DNS::Resource::IN::A, address: addr) + end + + def double_resource_v6(addr) + instance_double(Resolv::DNS::Resource::IN::AAAA, address: addr) + end +end