From e8bc429e6217650fde33562ca8867c2562b22db5 Mon Sep 17 00:00:00 2001
From: Rutger Wessels <rwessels@gitlab.com>
Date: Fri, 19 Jul 2024 15:31:35 +0200
Subject: [PATCH] Improve tests for require_organization logic

---
 spec/models/namespace_spec.rb | 38 ++++++++++++++++++++++++++++-------
 1 file changed, 31 insertions(+), 7 deletions(-)

diff --git a/spec/models/namespace_spec.rb b/spec/models/namespace_spec.rb
index be3ae850ec77..3922e07140b5 100644
--- a/spec/models/namespace_spec.rb
+++ b/spec/models/namespace_spec.rb
@@ -703,7 +703,7 @@
     it { is_expected.to include_module(Namespaces::Traversal::LinearScopes) }
   end
 
-  context 'when feature flag require_organization is disabled' do
+  context 'when feature flag require_organization is disabled', :request_store do
     before do
       stub_feature_flags(require_organization: false)
     end
@@ -713,18 +713,42 @@
 
       expect(namespace.valid?).to eq(true)
     end
+
+    describe '.with_disabled_organization_validation' do
+      it 'does not require organization' do
+        namespace.organization = nil
+
+        Namespace.with_disabled_organization_validation do
+          expect(namespace.valid?).to eq(true)
+        end
+      end
+
+      context 'with nested calls' do
+        it 'validation will not be re-enabled' do
+          result = []
+          Namespace.with_disabled_organization_validation do
+            result << described_class.new.require_organization?
+            Namespace.with_disabled_organization_validation do
+              result << described_class.new.require_organization?
+            end
+            result << described_class.new.require_organization?
+          end
+
+          expect(result.any?(true)).to be false
+          expect(described_class.new.require_organization?).to be false
+        end
+      end
+    end
   end
 
-  context 'when feature flag require_organization is enabled' do
+  context 'when feature flag require_organization is enabled', :request_store do
     it 'does require organization' do
       namespace.organization = nil
 
-      Namespace.with_disabled_organization_validation do
-        expect(namespace.valid?).to eq(false)
-      end
+      expect(namespace.valid?).to eq(false)
     end
 
-    describe '.with_disabled_organization_validation', :request_store do
+    describe '.with_disabled_organization_validation' do
       it 'does not require organization' do
         namespace.organization = nil
 
@@ -734,7 +758,7 @@
       end
 
       context 'with nested calls' do
-        it 'only last call will enable the validation' do
+        it 'only last call will re-enable the validation' do
           result = []
           Namespace.with_disabled_organization_validation do
             result << described_class.new.require_organization?
-- 
GitLab