diff --git a/app/controllers/admin/domain_blocks_controller.rb b/app/controllers/admin/domain_blocks_controller.rb index 74764640b8..1fd244d34a 100644 --- a/app/controllers/admin/domain_blocks_controller.rb +++ b/app/controllers/admin/domain_blocks_controller.rb @@ -37,7 +37,7 @@ module Admin @domain_block.errors.delete(:domain) render :new else - if existing_domain_block.present? + if existing_domain_block.present? && existing_domain_block.domain == resource_params[:domain] @domain_block = existing_domain_block @domain_block.update(resource_params) end diff --git a/spec/controllers/admin/domain_blocks_controller_spec.rb b/spec/controllers/admin/domain_blocks_controller_spec.rb index f432060d98..ab8ece9c5f 100644 --- a/spec/controllers/admin/domain_blocks_controller_spec.rb +++ b/spec/controllers/admin/domain_blocks_controller_spec.rb @@ -68,6 +68,26 @@ RSpec.describe Admin::DomainBlocksController, type: :controller do expect(flash[:notice]).to eq I18n.t('admin.domain_blocks.created_msg') expect(response).to redirect_to(admin_instances_path(limited: '1')) end + + context 'when a block for a parent domain already exists' do + subject { post :create, params: { domain_block: { domain: 'subdomain.example.com', severity: child_severity } } } + + let(:parent_severity) { 'silence' } + let(:child_severity) { 'suspend' } + + before do + Fabricate(:domain_block, domain: 'example.com', severity: parent_severity) + end + + it 'does not change the existing block' do + expect { subject }.to_not change { DomainBlock.find_by(domain: 'example.com') } + end + + it 'creates a domain block with expected severity' do + subject + expect(DomainBlock.where(domain: 'subdomain.example.com', severity: child_severity)).to exist + end + end end describe 'PUT #update' do