summaryrefslogtreecommitdiff
path: root/spec/finders/projects/groups_finder_spec.rb
blob: 89d4edaec7c165db5cfa0faa5641282aff6b4212 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Projects::GroupsFinder do
  describe '#execute' do
    let_it_be(:user) { create(:user) }
    let_it_be(:root_group) { create(:group, :public) }
    let_it_be(:project_group) { create(:group, :public, parent: root_group) }
    let_it_be(:shared_group_with_dev_access) { create(:group, :private, parent: root_group) }
    let_it_be(:shared_group_with_reporter_access) { create(:group, :private) }

    let_it_be(:public_project) { create(:project, :public, group: project_group) }
    let_it_be(:private_project) { create(:project, :private, group: project_group) }

    before_all do
      [public_project, private_project].each do |project|
        create(:project_group_link, :developer, group: shared_group_with_dev_access, project: project)
        create(:project_group_link, :reporter, group: shared_group_with_reporter_access, project: project)
      end
    end

    let(:params) { {} }
    let(:current_user) { user }
    let(:finder) { described_class.new(project: project, current_user: current_user, params: params) }

    subject { finder.execute }

    shared_examples 'finding related groups' do
      it 'returns ancestor groups for this project' do
        is_expected.to match_array([project_group, root_group])
      end

      context 'when the project does not belong to any group' do
        before do
          allow(project).to receive(:group) { nil }
        end

        it { is_expected.to eq([]) }
      end

      context 'when shared groups option is on' do
        let(:params) { { with_shared: true } }

        it 'returns ancestor and all shared groups' do
          is_expected.to match_array([project_group, root_group, shared_group_with_dev_access, shared_group_with_reporter_access])
        end

        context 'when shared_min_access_level is developer' do
          let(:params) { super().merge(shared_min_access_level: Gitlab::Access::DEVELOPER) }

          it 'returns ancestor and shared groups with at least developer access' do
            is_expected.to match_array([project_group, root_group, shared_group_with_dev_access])
          end
        end
      end

      context 'when skip group option is on' do
        let(:params) { { skip_groups: [project_group.id] } }

        it 'excludes provided groups' do
          is_expected.to match_array([root_group])
        end
      end
    end

    context 'Public project' do
      it_behaves_like 'finding related groups' do
        let(:project) { public_project }

        context 'when user is not authorized' do
          let(:current_user) { nil }

          it 'returns ancestor groups for this project' do
            is_expected.to match_array([project_group, root_group])
          end
        end
      end
    end

    context 'Private project' do
      it_behaves_like 'finding related groups' do
        let(:project) { private_project }

        before do
          project.add_developer(user)
        end

        context 'when user is not authorized' do
          let(:current_user) { nil }

          it { is_expected.to eq([]) }
        end
      end
    end

    context 'Missing project' do
      let(:project) { nil }

      it { is_expected.to eq([]) }
    end
  end
end