From 414d88e171224d65b7da130526d16aee9323d680 Mon Sep 17 00:00:00 2001
From: Sashi Kumar Kumaresan <skumar@gitlab.com>
Date: Mon, 15 Apr 2024 20:45:34 +0000
Subject: [PATCH] Fix FetchPolicyApproversService for ComplianceFramework

This change fixes a bug in FetchPolicyApproversService
where if a compliance framework is queried, it throws
an error.

EE: true
Changelog: fixed
---
 .../fetch_policy_approvers_service.rb         |  2 +-
 .../fetch_policy_approvers_service_spec.rb    | 26 +++++++++++++++++++
 2 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/ee/app/services/security/security_orchestration_policies/fetch_policy_approvers_service.rb b/ee/app/services/security/security_orchestration_policies/fetch_policy_approvers_service.rb
index 40aa13b6596a5..6f81cfc42d6d3 100644
--- a/ee/app/services/security/security_orchestration_policies/fetch_policy_approvers_service.rb
+++ b/ee/app/services/security/security_orchestration_policies/fetch_policy_approvers_service.rb
@@ -7,8 +7,8 @@ class FetchPolicyApproversService
 
       def initialize(policy:, container:, current_user:)
         @policy = policy
-        @container = container
         @current_user = current_user
+        @container = container.is_a?(ComplianceManagement::Framework) ? container.namespace : container
       end
 
       def execute
diff --git a/ee/spec/services/security/security_orchestration_policies/fetch_policy_approvers_service_spec.rb b/ee/spec/services/security/security_orchestration_policies/fetch_policy_approvers_service_spec.rb
index fa10e152f1f2c..9e3a4aa3b125e 100644
--- a/ee/spec/services/security/security_orchestration_policies/fetch_policy_approvers_service_spec.rb
+++ b/ee/spec/services/security/security_orchestration_policies/fetch_policy_approvers_service_spec.rb
@@ -44,6 +44,19 @@
         expect(response[:all_groups]).to be_empty
       end
 
+      context 'with container of compliance framework type' do
+        let(:container) { create(:compliance_framework, namespace: group) }
+
+        it 'returns user approvers' do
+          response = service.execute
+
+          expect(response[:status]).to eq(:success)
+          expect(response[:users]).to match_array([user])
+          expect(response[:groups]).to be_empty
+          expect(response[:all_groups]).to be_empty
+        end
+      end
+
       context 'with container of a group type' do
         let(:container) { group }
 
@@ -99,6 +112,19 @@
         expect(response[:users]).to be_empty
       end
 
+      context 'with container of compliance framework type' do
+        let(:container) { create(:compliance_framework, namespace: group) }
+
+        it 'returns group approvers' do
+          response = service.execute
+
+          expect(response[:status]).to eq(:success)
+          expect(response[:groups]).to match_array([group])
+          expect(response[:all_groups]).to match_array([group])
+          expect(response[:users]).to be_empty
+        end
+      end
+
       context 'when groups with same name exist in and outside of container' do
         let_it_be(:other_container) { create(:group) }
         let_it_be(:other_group) { create(:group, name: group.name, parent: other_container) }
-- 
GitLab