Compare commits

...

2 Commits

Author SHA1 Message Date
Matt Jankowski
2a82def331
Merge e4611e47b1 into e7c30cd072 2025-09-04 14:05:17 +00:00
Matt Jankowski
e4611e47b1 Add coverage for "other domains" scenario in admin/email_domain_blocks 2025-08-18 10:51:14 -04:00
2 changed files with 43 additions and 12 deletions

View File

@ -33,17 +33,7 @@ module Admin
@email_domain_block = EmailDomainBlock.new(resource_params)
if action_from_button == 'save'
EmailDomainBlock.transaction do
@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
process_email_domain_block
redirect_to admin_email_domain_blocks_path, notice: I18n.t('admin.email_domain_blocks.created_msg')
else
@ -57,6 +47,37 @@ module Admin
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
@resolved_records = DomainResource.new(@email_domain_block.domain).mx
end

View File

@ -8,7 +8,13 @@ RSpec.describe 'Admin::EmailDomainBlocks' do
before { sign_in current_user }
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 }
@ -28,8 +34,12 @@ RSpec.describe 'Admin::EmailDomainBlocks' do
expect(page)
.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 }
.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)
.to have_content(I18n.t('admin.email_domain_blocks.title'))
.and have_content(I18n.t('admin.email_domain_blocks.created_msg'))