2019-10-16 05:07:51 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-04 10:23:55 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-16 14:09:01 -04:00
|
|
|
RSpec.describe GroupDescendantsFinder do
|
2020-11-10 16:08:51 -05:00
|
|
|
let_it_be(:user) { create(:user) }
|
2021-12-03 07:10:23 -05:00
|
|
|
|
|
|
|
let_it_be_with_reload(:group) do
|
|
|
|
create(:group).tap do |g|
|
|
|
|
g.add_owner(user)
|
|
|
|
end
|
|
|
|
end
|
2021-06-28 23:07:32 -04:00
|
|
|
|
2017-09-04 10:23:55 -04:00
|
|
|
let(:params) { {} }
|
2019-12-17 13:07:48 -05:00
|
|
|
|
2017-09-19 05:15:57 -04:00
|
|
|
subject(:finder) do
|
|
|
|
described_class.new(current_user: user, parent_group: group, params: params)
|
|
|
|
end
|
2017-09-04 10:23:55 -04:00
|
|
|
|
2022-01-10 16:16:38 -05:00
|
|
|
describe '#has_children?' do
|
|
|
|
it 'is true when there are projects' do
|
|
|
|
create(:project, namespace: group)
|
|
|
|
|
|
|
|
expect(finder.has_children?).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there are subgroups' do
|
2017-10-10 08:11:55 -04:00
|
|
|
it 'is true when there are projects' do
|
2022-01-10 16:16:38 -05:00
|
|
|
create(:group, parent: group)
|
2017-10-10 08:11:55 -04:00
|
|
|
|
|
|
|
expect(finder.has_children?).to be_truthy
|
|
|
|
end
|
2022-01-10 16:16:38 -05:00
|
|
|
end
|
|
|
|
end
|
2017-10-10 08:11:55 -04:00
|
|
|
|
2022-01-10 16:16:38 -05:00
|
|
|
describe '#execute' do
|
|
|
|
it 'includes projects' do
|
|
|
|
project = create(:project, namespace: group)
|
2017-09-04 10:23:55 -04:00
|
|
|
|
2022-01-10 16:16:38 -05:00
|
|
|
expect(finder.execute).to contain_exactly(project)
|
2017-09-04 10:23:55 -04:00
|
|
|
end
|
|
|
|
|
2022-01-10 16:16:38 -05:00
|
|
|
context 'when archived is `true`' do
|
|
|
|
let(:params) { { archived: 'true' } }
|
|
|
|
|
|
|
|
it 'includes archived projects' do
|
|
|
|
archived_project = create(:project, namespace: group, archived: true)
|
2017-10-11 04:17:24 -04:00
|
|
|
project = create(:project, namespace: group)
|
|
|
|
|
2022-01-10 16:16:38 -05:00
|
|
|
expect(finder.execute).to contain_exactly(archived_project, project)
|
2017-10-11 04:17:24 -04:00
|
|
|
end
|
2022-01-10 16:16:38 -05:00
|
|
|
end
|
2017-10-11 04:17:24 -04:00
|
|
|
|
2022-01-10 16:16:38 -05:00
|
|
|
context 'when archived is `only`' do
|
|
|
|
let(:params) { { archived: 'only' } }
|
2017-10-11 04:17:24 -04:00
|
|
|
|
2022-01-10 16:16:38 -05:00
|
|
|
it 'includes only archived projects' do
|
|
|
|
archived_project = create(:project, namespace: group, archived: true)
|
|
|
|
_project = create(:project, namespace: group)
|
2017-10-11 04:17:24 -04:00
|
|
|
|
2022-01-10 16:16:38 -05:00
|
|
|
expect(finder.execute).to contain_exactly(archived_project)
|
2017-10-11 04:17:24 -04:00
|
|
|
end
|
2022-01-10 16:16:38 -05:00
|
|
|
end
|
2017-10-11 04:17:24 -04:00
|
|
|
|
2022-01-10 16:16:38 -05:00
|
|
|
it 'does not include archived projects' do
|
|
|
|
_archived_project = create(:project, :archived, namespace: group)
|
2017-09-21 03:20:37 -04:00
|
|
|
|
2022-01-10 16:16:38 -05:00
|
|
|
expect(finder.execute).to be_empty
|
|
|
|
end
|
2017-09-21 03:20:37 -04:00
|
|
|
|
2022-01-27 22:15:57 -05:00
|
|
|
it 'does not include projects aimed for deletion' do
|
|
|
|
_project_aimed_for_deletion = create(:project, :archived, marked_for_deletion_at: 2.days.ago, pending_delete: false)
|
|
|
|
|
|
|
|
expect(finder.execute).to be_empty
|
|
|
|
end
|
|
|
|
|
2022-01-10 16:16:38 -05:00
|
|
|
context 'with a filter' do
|
|
|
|
let(:params) { { filter: 'test' } }
|
2017-09-04 10:23:55 -04:00
|
|
|
|
2022-01-10 16:16:38 -05:00
|
|
|
it 'includes only projects matching the filter' do
|
|
|
|
_other_project = create(:project, namespace: group)
|
|
|
|
matching_project = create(:project, namespace: group, name: 'testproject')
|
2017-09-04 10:23:55 -04:00
|
|
|
|
2022-01-10 16:16:38 -05:00
|
|
|
expect(finder.execute).to contain_exactly(matching_project)
|
2017-09-04 10:23:55 -04:00
|
|
|
end
|
2022-01-10 16:16:38 -05:00
|
|
|
end
|
2017-12-20 07:17:39 -05:00
|
|
|
|
2022-01-10 16:16:38 -05:00
|
|
|
it 'sorts elements by name as default' do
|
|
|
|
project1 = create(:project, namespace: group, name: 'z')
|
|
|
|
project2 = create(:project, namespace: group, name: 'a')
|
2018-11-14 08:46:31 -05:00
|
|
|
|
2022-01-10 16:16:38 -05:00
|
|
|
expect(subject.execute).to match_array([project2, project1])
|
|
|
|
end
|
2018-11-14 08:46:31 -05:00
|
|
|
|
2022-01-10 16:16:38 -05:00
|
|
|
context 'sorting by name' do
|
|
|
|
let!(:project1) { create(:project, namespace: group, name: 'a', path: 'project-a') }
|
|
|
|
let!(:project2) { create(:project, namespace: group, name: 'z', path: 'project-z') }
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
sort: 'name_asc'
|
|
|
|
}
|
2017-12-20 07:17:39 -05:00
|
|
|
end
|
|
|
|
|
2022-01-10 16:16:38 -05:00
|
|
|
it 'sorts elements by name' do
|
|
|
|
expect(subject.execute).to eq(
|
|
|
|
[
|
|
|
|
project1,
|
|
|
|
project2
|
|
|
|
]
|
|
|
|
)
|
2017-12-20 07:17:39 -05:00
|
|
|
end
|
|
|
|
|
2022-01-10 16:16:38 -05:00
|
|
|
context 'with nested groups' do
|
|
|
|
let!(:subgroup1) { create(:group, parent: group, name: 'a', path: 'sub-a') }
|
|
|
|
let!(:subgroup2) { create(:group, parent: group, name: 'z', path: 'sub-z') }
|
2017-12-20 07:17:39 -05:00
|
|
|
|
|
|
|
it 'sorts elements by name' do
|
|
|
|
expect(subject.execute).to eq(
|
|
|
|
[
|
2022-01-10 16:16:38 -05:00
|
|
|
subgroup1,
|
|
|
|
subgroup2,
|
2017-12-20 07:17:39 -05:00
|
|
|
project1,
|
|
|
|
project2
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
2021-12-03 07:10:23 -05:00
|
|
|
end
|
2022-01-10 16:16:38 -05:00
|
|
|
end
|
2017-09-04 10:23:55 -04:00
|
|
|
|
2022-01-10 16:16:38 -05:00
|
|
|
it 'does not include projects shared with the group' do
|
|
|
|
project = create(:project, namespace: group)
|
|
|
|
other_project = create(:project)
|
|
|
|
other_project.project_group_links.create!(group: group,
|
2022-08-26 02:11:44 -04:00
|
|
|
group_access: Gitlab::Access::MAINTAINER)
|
2020-11-10 16:08:51 -05:00
|
|
|
|
2022-01-10 16:16:38 -05:00
|
|
|
expect(finder.execute).to contain_exactly(project)
|
2020-11-10 16:08:51 -05:00
|
|
|
end
|
2022-01-10 16:16:38 -05:00
|
|
|
end
|
2020-11-10 16:08:51 -05:00
|
|
|
|
2022-01-10 16:16:38 -05:00
|
|
|
context 'with shared groups' do
|
|
|
|
let_it_be(:other_group) { create(:group) }
|
|
|
|
let_it_be(:shared_group_link) do
|
|
|
|
create(:group_group_link,
|
|
|
|
shared_group: group,
|
|
|
|
shared_with_group: other_group)
|
|
|
|
end
|
2020-11-10 16:08:51 -05:00
|
|
|
|
2022-01-10 16:16:38 -05:00
|
|
|
context 'without common ancestor' do
|
|
|
|
it { expect(finder.execute).to be_empty }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with common ancestor' do
|
|
|
|
let_it_be(:common_ancestor) { create(:group) }
|
|
|
|
let_it_be(:other_group) { create(:group, parent: common_ancestor) }
|
|
|
|
let_it_be(:group) { create(:group, parent: common_ancestor) }
|
|
|
|
|
|
|
|
context 'querying under the common ancestor' do
|
2020-11-10 16:08:51 -05:00
|
|
|
it { expect(finder.execute).to be_empty }
|
|
|
|
end
|
|
|
|
|
2022-01-10 16:16:38 -05:00
|
|
|
context 'querying the common ancestor' do
|
|
|
|
subject(:finder) do
|
|
|
|
described_class.new(current_user: user, parent_group: common_ancestor, params: params)
|
2020-11-10 16:08:51 -05:00
|
|
|
end
|
|
|
|
|
2022-01-10 16:16:38 -05:00
|
|
|
it 'contains shared subgroups' do
|
|
|
|
expect(finder.execute).to contain_exactly(group, other_group)
|
2020-11-10 16:08:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-01-10 16:16:38 -05:00
|
|
|
end
|
2020-11-10 16:08:51 -05:00
|
|
|
|
2022-01-10 16:16:38 -05:00
|
|
|
context 'with nested groups' do
|
2022-01-27 10:14:51 -05:00
|
|
|
let_it_be(:project) { create(:project, namespace: group) }
|
|
|
|
let_it_be_with_reload(:subgroup) { create(:group, :private, parent: group) }
|
2017-09-04 10:23:55 -04:00
|
|
|
|
2022-01-10 16:16:38 -05:00
|
|
|
describe '#execute' do
|
|
|
|
it 'contains projects and subgroups' do
|
|
|
|
expect(finder.execute).to contain_exactly(subgroup, project)
|
|
|
|
end
|
2017-09-04 10:23:55 -04:00
|
|
|
|
2022-01-10 16:16:38 -05:00
|
|
|
it 'does not include subgroups the user does not have access to' do
|
|
|
|
subgroup.update!(visibility_level: Gitlab::VisibilityLevel::PRIVATE)
|
2017-09-26 14:06:08 -04:00
|
|
|
|
2022-01-10 16:16:38 -05:00
|
|
|
public_subgroup = create(:group, :public, parent: group, path: 'public-group')
|
|
|
|
other_subgroup = create(:group, :private, parent: group, path: 'visible-private-group')
|
|
|
|
other_user = create(:user)
|
|
|
|
other_subgroup.add_developer(other_user)
|
2017-09-26 14:06:08 -04:00
|
|
|
|
2022-01-10 16:16:38 -05:00
|
|
|
finder = described_class.new(current_user: other_user, parent_group: group)
|
2017-09-26 14:06:08 -04:00
|
|
|
|
2022-01-10 16:16:38 -05:00
|
|
|
expect(finder.execute).to contain_exactly(public_subgroup, other_subgroup)
|
|
|
|
end
|
2017-09-26 14:06:08 -04:00
|
|
|
|
2022-01-10 16:16:38 -05:00
|
|
|
it 'only includes public groups when no user is given' do
|
|
|
|
public_subgroup = create(:group, :public, parent: group)
|
|
|
|
_private_subgroup = create(:group, :private, parent: group)
|
2017-10-10 07:33:02 -04:00
|
|
|
|
2022-01-10 16:16:38 -05:00
|
|
|
finder = described_class.new(current_user: nil, parent_group: group)
|
2017-10-10 07:33:02 -04:00
|
|
|
|
2022-01-10 16:16:38 -05:00
|
|
|
expect(finder.execute).to contain_exactly(public_subgroup)
|
|
|
|
end
|
2017-10-10 07:33:02 -04:00
|
|
|
|
2022-01-10 16:16:38 -05:00
|
|
|
context 'when archived is `true`' do
|
|
|
|
let(:params) { { archived: 'true' } }
|
2017-10-11 04:17:24 -04:00
|
|
|
|
2022-01-10 16:16:38 -05:00
|
|
|
it 'includes archived projects in the count of subgroups' do
|
|
|
|
create(:project, namespace: subgroup, archived: true)
|
2017-10-11 04:17:24 -04:00
|
|
|
|
2022-01-10 16:16:38 -05:00
|
|
|
expect(finder.execute.first.preloaded_project_count).to eq(1)
|
2017-10-11 04:17:24 -04:00
|
|
|
end
|
2022-01-10 16:16:38 -05:00
|
|
|
end
|
2017-10-11 04:17:24 -04:00
|
|
|
|
2022-01-10 16:16:38 -05:00
|
|
|
context 'with a filter' do
|
|
|
|
let(:params) { { filter: 'test' } }
|
2017-09-04 10:23:55 -04:00
|
|
|
|
2022-01-27 10:14:51 -05:00
|
|
|
shared_examples 'filter examples' do
|
|
|
|
it 'contains only matching projects and subgroups' do
|
|
|
|
matching_project = create(:project, namespace: group, name: 'Testproject')
|
|
|
|
matching_subgroup = create(:group, name: 'testgroup', parent: group)
|
2017-09-04 10:23:55 -04:00
|
|
|
|
2022-01-27 10:14:51 -05:00
|
|
|
expect(finder.execute).to contain_exactly(matching_subgroup, matching_project)
|
|
|
|
end
|
2017-09-06 09:28:07 -04:00
|
|
|
|
2022-01-27 10:14:51 -05:00
|
|
|
it 'does not include subgroups the user does not have access to' do
|
|
|
|
_invisible_subgroup = create(:group, :private, parent: group, name: 'test1')
|
|
|
|
other_subgroup = create(:group, :private, parent: group, name: 'test2')
|
|
|
|
public_subgroup = create(:group, :public, parent: group, name: 'test3')
|
|
|
|
other_subsubgroup = create(:group, :private, parent: other_subgroup, name: 'test4')
|
|
|
|
other_user = create(:user)
|
|
|
|
other_subgroup.add_developer(other_user)
|
2017-09-26 14:06:08 -04:00
|
|
|
|
2022-01-27 10:14:51 -05:00
|
|
|
finder = described_class.new(current_user: other_user,
|
2022-08-26 02:11:44 -04:00
|
|
|
parent_group: group,
|
|
|
|
params: params)
|
2017-09-26 14:06:08 -04:00
|
|
|
|
2022-01-27 10:14:51 -05:00
|
|
|
expect(finder.execute).to contain_exactly(other_subgroup, public_subgroup, other_subsubgroup)
|
|
|
|
end
|
2017-09-26 14:06:08 -04:00
|
|
|
|
2022-01-27 10:14:51 -05:00
|
|
|
context 'with matching children' do
|
|
|
|
it 'includes a group that has a subgroup matching the query and its parent' do
|
|
|
|
matching_subgroup = create(:group, :private, name: 'testgroup', parent: subgroup)
|
2017-09-06 09:28:07 -04:00
|
|
|
|
2022-01-27 10:14:51 -05:00
|
|
|
expect(finder.execute).to contain_exactly(subgroup, matching_subgroup)
|
|
|
|
end
|
2017-09-06 09:28:07 -04:00
|
|
|
|
2022-01-27 10:14:51 -05:00
|
|
|
it 'includes the parent of a matching project' do
|
|
|
|
matching_project = create(:project, namespace: subgroup, name: 'Testproject')
|
2017-09-06 09:28:07 -04:00
|
|
|
|
2022-01-27 10:14:51 -05:00
|
|
|
expect(finder.execute).to contain_exactly(subgroup, matching_project)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a small page size' do
|
|
|
|
let(:params) { { filter: 'test', per_page: 1 } }
|
|
|
|
|
|
|
|
it 'contains all the ancestors of a matching subgroup regardless the page size' do
|
|
|
|
subgroup = create(:group, :private, parent: group)
|
|
|
|
matching = create(:group, :private, name: 'testgroup', parent: subgroup)
|
2017-09-10 12:55:52 -04:00
|
|
|
|
2022-01-27 10:14:51 -05:00
|
|
|
expect(finder.execute).to contain_exactly(subgroup, matching)
|
|
|
|
end
|
|
|
|
end
|
2018-01-20 08:35:48 -05:00
|
|
|
|
2022-01-27 10:14:51 -05:00
|
|
|
it 'does not include the parent itself' do
|
|
|
|
group.update!(name: 'test')
|
2018-01-20 08:35:48 -05:00
|
|
|
|
2022-01-27 10:14:51 -05:00
|
|
|
expect(finder.execute).not_to include(group)
|
2018-01-20 08:35:48 -05:00
|
|
|
end
|
2022-01-10 16:16:38 -05:00
|
|
|
end
|
2022-01-27 10:14:51 -05:00
|
|
|
end
|
2018-01-20 08:35:48 -05:00
|
|
|
|
2022-01-27 10:14:51 -05:00
|
|
|
it_behaves_like 'filter examples'
|
2017-09-10 12:55:52 -04:00
|
|
|
|
2022-01-27 10:14:51 -05:00
|
|
|
context 'when feature flag :linear_group_descendants_finder_upto is disabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(linear_group_descendants_finder_upto: false)
|
2017-09-10 12:55:52 -04:00
|
|
|
end
|
2022-01-27 10:14:51 -05:00
|
|
|
|
|
|
|
it_behaves_like 'filter examples'
|
2017-09-06 09:28:07 -04:00
|
|
|
end
|
2017-09-04 10:23:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|