From a163c371b17f79fa188bbe62a63ea2f8155012c8 Mon Sep 17 00:00:00 2001
From: Sashi <skumar@gitlab.com>
Date: Thu, 1 Jul 2021 21:28:58 +0530
Subject: [PATCH] Fix uninitialized constant in update protected branch

This change fixes the uninitialized constant exception
when a protected branch is updated while creating security
policy project.
---
 .../project_create_service.rb                 |  2 +-
 .../project_create_service_spec.rb            | 42 +++++++++++++++++++
 2 files changed, 43 insertions(+), 1 deletion(-)

diff --git a/ee/app/services/security/security_orchestration_policies/project_create_service.rb b/ee/app/services/security/security_orchestration_policies/project_create_service.rb
index 4ff8f76d9bdd0..248802bd266dc 100644
--- a/ee/app/services/security/security_orchestration_policies/project_create_service.rb
+++ b/ee/app/services/security/security_orchestration_policies/project_create_service.rb
@@ -34,7 +34,7 @@ def create_or_update_protected_branch(policy_project)
         }
 
         if protected_branch.present?
-          ProtectedBranch::UpdateService
+          ProtectedBranches::UpdateService
             .new(policy_project, current_user, params)
             .execute(protected_branch)
           return
diff --git a/ee/spec/services/security/security_orchestration_policies/project_create_service_spec.rb b/ee/spec/services/security/security_orchestration_policies/project_create_service_spec.rb
index f4af182d376ba..006628fb47b0d 100644
--- a/ee/spec/services/security/security_orchestration_policies/project_create_service_spec.rb
+++ b/ee/spec/services/security/security_orchestration_policies/project_create_service_spec.rb
@@ -29,6 +29,48 @@
       end
     end
 
+    context 'when protected branch already exists' do
+      let_it_be(:project) { create(:project) }
+      let_it_be(:current_user) { project.owner }
+      let_it_be(:maintainer) { create(:user) }
+
+      before do
+        project.add_maintainer(maintainer)
+
+        allow_next_instance_of(Project) do |instance|
+          allow(instance).to receive_message_chain(:protected_branches, :find_by_name).and_return([ProtectedBranch.new])
+        end
+
+        protected_branch_service = instance_spy(ProtectedBranches::UpdateService)
+        allow(ProtectedBranches::UpdateService).to receive(:new).and_return(protected_branch_service)
+      end
+
+      it 'updates protected branch' do
+        service.execute
+
+        expect(ProtectedBranches::UpdateService).to have_received(:new)
+      end
+    end
+
+    context 'when protected branch does not exist' do
+      let_it_be(:project) { create(:project) }
+      let_it_be(:current_user) { project.owner }
+      let_it_be(:maintainer) { create(:user) }
+
+      before do
+        project.add_maintainer(maintainer)
+
+        protected_branch_service = instance_spy(ProtectedBranches::CreateService)
+        allow(ProtectedBranches::CreateService).to receive(:new).and_return(protected_branch_service)
+      end
+
+      it 'creates protected branch' do
+        service.execute
+
+        expect(ProtectedBranches::CreateService).to have_received(:new)
+      end
+    end
+
     context 'when adding users to security policy project fails' do
       let_it_be(:project) { create(:project) }
       let_it_be(:current_user) { project.owner }
-- 
GitLab