From b12d75ef4fdfdab53cba7ec15027cdd081c7d6fe Mon Sep 17 00:00:00 2001
From: Claire <claire.github-309c@sitedethib.com>
Date: Wed, 9 Aug 2023 09:39:36 +0200
Subject: [PATCH] Fix blocking subdomains of an already-blocked domain (#26392)

---
 .../admin/domain_blocks_controller.rb         |  2 +-
 spec/features/admin/domain_blocks_spec.rb     | 24 +++++++++++++++++++
 2 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/app/controllers/admin/domain_blocks_controller.rb b/app/controllers/admin/domain_blocks_controller.rb
index b9691c5a3..96c31a38f 100644
--- a/app/controllers/admin/domain_blocks_controller.rb
+++ b/app/controllers/admin/domain_blocks_controller.rb
@@ -40,7 +40,7 @@ module Admin
       end
 
       # Allow transparently upgrading a domain block
-      if existing_domain_block.present?
+      if existing_domain_block.present? && existing_domain_block.domain == TagManager.instance.normalize_domain(@domain_block.domain.strip)
         @domain_block = existing_domain_block
         @domain_block.assign_attributes(resource_params)
       end
diff --git a/spec/features/admin/domain_blocks_spec.rb b/spec/features/admin/domain_blocks_spec.rb
index c77d604eb..4672c1e1a 100644
--- a/spec/features/admin/domain_blocks_spec.rb
+++ b/spec/features/admin/domain_blocks_spec.rb
@@ -57,6 +57,30 @@ describe 'blocking domains through the moderation interface' do
     end
   end
 
+  context 'when suspending a subdomain of an already-silenced domain' do
+    it 'presents a confirmation screen before suspending the domain' do
+      domain_block = Fabricate(:domain_block, domain: 'example.com', severity: 'silence')
+
+      visit new_admin_domain_block_path
+
+      fill_in 'domain_block_domain', with: 'subdomain.example.com'
+      select I18n.t('admin.domain_blocks.new.severity.suspend'), from: 'domain_block_severity'
+      click_on I18n.t('admin.domain_blocks.new.create')
+
+      # It presents a confirmation screen
+      expect(page).to have_title(I18n.t('admin.domain_blocks.confirm_suspension.title', domain: 'subdomain.example.com'))
+
+      # Confirming creates the block
+      click_on I18n.t('admin.domain_blocks.confirm_suspension.confirm')
+
+      expect(DomainBlock.where(domain: 'subdomain.example.com', severity: 'suspend')).to exist
+
+      # And leaves the previous block alone
+      expect(domain_block.reload.severity).to eq 'silence'
+      expect(domain_block.reload.domain).to eq 'example.com'
+    end
+  end
+
   context 'when editing a domain block' do
     it 'presents a confirmation screen before suspending the domain' do
       domain_block = Fabricate(:domain_block, domain: 'example.com', severity: 'silence')