group_descendants_finder_spec.rb 6.9 KB
Newer Older
1 2
require 'spec_helper'

3
describe GroupDescendantsFinder do
4 5 6
  let(:user) { create(:user) }
  let(:group) { create(:group) }
  let(:params) { {} }
7 8 9
  subject(:finder) do
    described_class.new(current_user: user, parent_group: group, params: params)
  end
10 11 12 13 14

  before do
    group.add_owner(user)
  end

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
  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', :nested_groups do
      it 'is true when there are projects' do
        create(:group, parent: group)

        expect(finder.has_children?).to be_truthy
      end
    end
  end

31 32 33 34 35 36 37
  describe '#execute' do
    it 'includes projects' do
      project = create(:project, namespace: group)

      expect(finder.execute).to contain_exactly(project)
    end

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    context 'when archived is `true`' do
      let(:params) { { archived: 'true' } }

      it 'includes archived projects' do
        archived_project = create(:project, namespace: group, archived: true)
        project = create(:project, namespace: group)

        expect(finder.execute).to contain_exactly(archived_project, project)
      end
    end

    context 'when archived is `only`' do
      let(:params) { { archived: 'only' } }

      it 'includes only archived projects' do
        archived_project = create(:project, namespace: group, archived: true)
        _project = create(:project, namespace: group)

        expect(finder.execute).to contain_exactly(archived_project)
      end
    end

60 61 62 63 64 65
    it 'does not include archived projects' do
      _archived_project = create(:project, :archived, namespace: group)

      expect(finder.execute).to be_empty
    end

66
    context 'with a filter' do
67
      let(:params) { { filter: 'test' } }
68 69 70 71 72 73 74 75

      it 'includes only projects matching the filter' do
        _other_project = create(:project, namespace: group)
        matching_project = create(:project, namespace: group, name: 'testproject')

        expect(finder.execute).to contain_exactly(matching_project)
      end
    end
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

    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'
        }
      end

      it 'sorts elements by name' do
        expect(subject.execute).to eq(
          [
            project1,
            project2
          ]
        )
      end

      context 'with nested groups', :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') }

        it 'sorts elements by name' do
          expect(subject.execute).to eq(
            [
              subgroup1,
              subgroup2,
              project1,
              project2
            ]
          )
        end
      end
    end
111 112 113 114
  end

  context 'with nested groups', :nested_groups do
    let!(:project) { create(:project, namespace: group) }
115
    let!(:subgroup) { create(:group, :private, parent: group) }
116 117 118 119 120 121

    describe '#execute' do
      it 'contains projects and subgroups' do
        expect(finder.execute).to contain_exactly(subgroup, project)
      end

122 123 124 125 126 127 128 129 130 131 132 133 134
      it 'does not include subgroups the user does not have access to' do
        subgroup.update!(visibility_level: Gitlab::VisibilityLevel::PRIVATE)

        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)

        finder = described_class.new(current_user: other_user, parent_group: group)

        expect(finder.execute).to contain_exactly(public_subgroup, other_subgroup)
      end

135 136 137 138 139 140 141 142 143
      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)

        finder = described_class.new(current_user: nil, parent_group: group)

        expect(finder.execute).to contain_exactly(public_subgroup)
      end

144 145 146 147 148 149 150 151 152 153
      context 'when archived is `true`' do
        let(:params) { { archived: 'true' } }

        it 'includes archived projects in the count of subgroups' do
          create(:project, namespace: subgroup, archived: true)

          expect(finder.execute.first.preloaded_project_count).to eq(1)
        end
      end

154 155 156 157 158 159 160 161 162
      context 'with a filter' do
        let(:params) { { filter: 'test' } }

        it 'contains only matching projects and subgroups' do
          matching_project = create(:project, namespace: group, name: 'Testproject')
          matching_subgroup = create(:group, name: 'testgroup', parent: group)

          expect(finder.execute).to contain_exactly(matching_subgroup, matching_project)
        end
163

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
        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)

          finder = described_class.new(current_user: other_user,
                                       parent_group: group,
                                       params: params)

          expect(finder.execute).to contain_exactly(other_subgroup, public_subgroup, other_subsubgroup)
        end

179
        context 'with matching children' do
180
          it 'includes a group that has a subgroup matching the query and its parent' do
181
            matching_subgroup = create(:group, :private, name: 'testgroup', parent: subgroup)
182

183
            expect(finder.execute).to contain_exactly(subgroup, matching_subgroup)
184 185
          end

186
          it 'includes the parent of a matching project' do
187 188
            matching_project = create(:project, namespace: subgroup, name: 'Testproject')

189
            expect(finder.execute).to contain_exactly(subgroup, matching_project)
190
          end
191

192 193 194 195 196 197 198 199 200 201 202
          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)

              expect(finder.execute).to contain_exactly(subgroup, matching)
            end
          end

203 204 205 206 207
          it 'does not include the parent itself' do
            group.update!(name: 'test')

            expect(finder.execute).not_to include(group)
          end
208
        end
209 210 211 212
      end
    end
  end
end