Add coverage for "other domains" scenario in admin/email_domain_blocks

This commit is contained in:
Matt Jankowski 2025-07-22 14:56:29 -04:00
parent f5754f2a36
commit e4611e47b1
2 changed files with 43 additions and 12 deletions

View File

@ -33,17 +33,7 @@ module Admin
@email_domain_block = EmailDomainBlock.new(resource_params) @email_domain_block = EmailDomainBlock.new(resource_params)
if action_from_button == 'save' if action_from_button == 'save'
EmailDomainBlock.transaction do process_email_domain_block
@email_domain_block.save!
log_action :create, @email_domain_block
(@email_domain_block.other_domains || []).uniq.each do |domain|
next if EmailDomainBlock.exists?(domain: domain)
other_email_domain_block = EmailDomainBlock.create!(domain: domain, allow_with_approval: @email_domain_block.allow_with_approval, parent: @email_domain_block)
log_action :create, other_email_domain_block
end
end
redirect_to admin_email_domain_blocks_path, notice: I18n.t('admin.email_domain_blocks.created_msg') redirect_to admin_email_domain_blocks_path, notice: I18n.t('admin.email_domain_blocks.created_msg')
else else
@ -57,6 +47,37 @@ module Admin
private private
def process_email_domain_block
EmailDomainBlock.transaction do
@email_domain_block.save!
log_action :create, @email_domain_block
save_other_domains
end
end
def save_other_domains
other_domains_from_block.each do |domain|
next if EmailDomainBlock.exists?(domain: domain)
log_action :create, block_child_domain(domain)
end
end
def block_child_domain(domain)
EmailDomainBlock.create!(
allow_with_approval: @email_domain_block.allow_with_approval,
domain: domain,
parent: @email_domain_block
)
end
def other_domains_from_block
@email_domain_block
.other_domains
.to_a
.uniq
end
def set_resolved_records def set_resolved_records
@resolved_records = DomainResource.new(@email_domain_block.domain).mx @resolved_records = DomainResource.new(@email_domain_block.domain).mx
end end

View File

@ -8,7 +8,13 @@ RSpec.describe 'Admin::EmailDomainBlocks' do
before { sign_in current_user } before { sign_in current_user }
describe 'Managing email domain blocks' do describe 'Managing email domain blocks' do
before { configure_dns(domain: 'example.com', results: []) } before do
Fabricate :email_domain_block, domain: 'tester.example'
allow(DomainResource)
.to receive(:new)
.with('example.com')
.and_return(instance_double(DomainResource, mx: ['test.example', 'tester.example']))
end
let!(:email_domain_block) { Fabricate :email_domain_block } let!(:email_domain_block) { Fabricate :email_domain_block }
@ -28,8 +34,12 @@ RSpec.describe 'Admin::EmailDomainBlocks' do
expect(page) expect(page)
.to have_content(I18n.t('admin.email_domain_blocks.new.title')) .to have_content(I18n.t('admin.email_domain_blocks.new.title'))
find('input[checked_value="test.example"]').click
find('input[checked_value="tester.example"]').click
expect { submit_create } expect { submit_create }
.to change(EmailDomainBlock.where(domain: 'example.com'), :count).by(1) .to change(EmailDomainBlock.where(domain: 'example.com'), :count).by(1)
.and change(EmailDomainBlock.where.associated(:parent).where(domain: 'test.example'), :count).by(1)
.and not_change(EmailDomainBlock.where.associated(:parent).where(domain: 'tester.example'), :count)
expect(page) expect(page)
.to have_content(I18n.t('admin.email_domain_blocks.title')) .to have_content(I18n.t('admin.email_domain_blocks.title'))
.and have_content(I18n.t('admin.email_domain_blocks.created_msg')) .and have_content(I18n.t('admin.email_domain_blocks.created_msg'))