Commit 61eddb69 authored by Gosia Ksionek's avatar Gosia Ksionek Committed by James Lopez

Make sure groups with templates finder returns subgroups

When subgroup is a descendant of a paid group

Fix errors
parent aebcb96b
......@@ -9,17 +9,27 @@ class GroupsWithTemplatesFinder
end
def execute
groups = @group_id ? ::Group.find(group_id).self_and_ancestors : ::Group.all
groups = groups.with_project_templates
if ::Gitlab::CurrentSettings.should_check_namespace_plan? && Time.zone.now > CUT_OFF_DATE
groups = groups.with_feature_available_in_plan(:group_project_templates)
groups = extended_group_search
simple_group_search(groups)
else
simple_group_search(Group.all)
end
groups
end
private
attr_reader :group_id
def extended_group_search
groups = Group.with_feature_available_in_plan(:group_project_templates)
Gitlab::ObjectHierarchy.new(groups).base_and_descendants
end
def simple_group_search(groups)
groups = group_id ? groups.find_by(id: group_id)&.self_and_ancestors : groups # rubocop: disable CodeReuse/ActiveRecord
return Group.none unless groups
groups.with_project_templates
end
end
---
title: Make sure groups with templates finder returns subgroups
merge_request: 15631
author:
type: fixed
......@@ -3,14 +3,17 @@
require 'spec_helper'
describe GroupsWithTemplatesFinder do
let(:group_1) { create(:group, name: 'group-1') }
let(:group_2) { create(:group, name: 'group-2') }
let(:group_3) { create(:group, name: 'group-3') }
let!(:group_4) { create(:group, name: 'group-4') }
set(:group_1) { create(:group, name: 'group-1') }
set(:group_2) { create(:group, name: 'group-2') }
set(:group_3) { create(:group, name: 'group-3') }
set(:group_4) { create(:group, name: 'group-4') }
let!(:subgroup_1) { create(:group, parent: group_1, name: 'subgroup-1') }
let!(:subgroup_2) { create(:group, parent: group_2, name: 'subgroup-2') }
let!(:subgroup_3) { create(:group, parent: group_3, name: 'subgroup-3') }
set(:subgroup_1) { create(:group, parent: group_1, name: 'subgroup-1') }
set(:subgroup_2) { create(:group, parent: group_2, name: 'subgroup-2') }
set(:subgroup_3) { create(:group, parent: group_3, name: 'subgroup-3') }
set(:subgroup_4) { create(:group, parent: group_1, name: 'subgroup-4') }
set(:subgroup_5) { create(:group, parent: subgroup_4, name: 'subgroup-5') }
before do
group_1.update!(custom_project_templates_group_id: subgroup_1.id)
......@@ -44,6 +47,19 @@ describe GroupsWithTemplatesFinder do
expect(described_class.new.execute).to contain_exactly(group_1, group_2)
end
end
context 'with subgroup with template' do
before do
subgroup_4.update!(custom_project_templates_group_id: subgroup_5.id)
create(:project, namespace: subgroup_5)
end
it 'returns groups on gold/silver plan after cut-off date' do
Timecop.freeze(described_class::CUT_OFF_DATE + 1.day) do
expect(described_class.new.execute).to contain_exactly(group_1, group_2, subgroup_4)
end
end
end
end
end
......@@ -52,6 +68,17 @@ describe GroupsWithTemplatesFinder do
expect(described_class.new(group_1.id).execute).to contain_exactly(group_1)
end
context 'with subgroup with template' do
before do
subgroup_4.update!(custom_project_templates_group_id: subgroup_5.id)
create(:project, namespace: subgroup_5)
end
it 'returns only chosen group' do
expect(described_class.new(group_1.id).execute).to contain_exactly(group_1)
end
end
context 'when namespace checked' do
before do
allow(Gitlab::CurrentSettings).to receive(:should_check_namespace_plan?) { true }
......@@ -68,6 +95,25 @@ describe GroupsWithTemplatesFinder do
expect(described_class.new(group_3.id).execute).to be_empty
end
end
context 'with subgroup with template' do
before do
subgroup_4.update!(custom_project_templates_group_id: subgroup_5.id)
create(:project, namespace: subgroup_5)
end
it 'returns only chosen group' do
Timecop.freeze(described_class::CUT_OFF_DATE + 1.day) do
expect(described_class.new(group_1.id).execute).to contain_exactly(group_1)
end
end
it 'returns only chosen subgroup' do
Timecop.freeze(described_class::CUT_OFF_DATE + 1.day) do
expect(described_class.new(subgroup_4.id).execute).to contain_exactly(group_1, subgroup_4)
end
end
end
end
end
end
......@@ -70,6 +70,7 @@ describe Namespace do
it 'returns namespaces with plan' do
create(:gitlab_subscription, :bronze, namespace: namespace)
create(:gitlab_subscription, :free, namespace: create(:namespace))
expect(described_class.with_feature_available_in_plan(:audit_events)).to eq([namespace])
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment