Read about our upcoming Code of Conduct on this issue

project_feature_spec.rb 8.09 KB
Newer Older
1
2
# frozen_string_literal: true

Felipe Artur's avatar
Felipe Artur committed
3
4
5
require 'spec_helper'

describe ProjectFeature do
6
  let(:project) { create(:project) }
Felipe Artur's avatar
Felipe Artur committed
7
8
  let(:user) { create(:user) }

9
10
  describe '.quoted_access_level_column' do
    it 'returns the table name and quoted column name for a feature' do
11
      expected = '"project_features"."issues_access_level"'
12
13
14
15
16

      expect(described_class.quoted_access_level_column(:issues)).to eq(expected)
    end
  end

Felipe Artur's avatar
Felipe Artur committed
17
  describe '#feature_available?' do
18
    let(:features) { %w(issues wiki builds merge_requests snippets repository pages) }
Felipe Artur's avatar
Felipe Artur committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

    context 'when features are disabled' do
      it "returns false" do
        features.each do |feature|
          project.project_feature.update_attribute("#{feature}_access_level".to_sym, ProjectFeature::DISABLED)
          expect(project.feature_available?(:issues, user)).to eq(false)
        end
      end
    end

    context 'when features are enabled only for team members' do
      it "returns false when user is not a team member" do
        features.each do |feature|
          project.project_feature.update_attribute("#{feature}_access_level".to_sym, ProjectFeature::PRIVATE)
          expect(project.feature_available?(:issues, user)).to eq(false)
        end
      end

      it "returns true when user is a team member" do
38
        project.add_developer(user)
Felipe Artur's avatar
Felipe Artur committed
39
40
41
42
43
44
45
46
47

        features.each do |feature|
          project.project_feature.update_attribute("#{feature}_access_level".to_sym, ProjectFeature::PRIVATE)
          expect(project.feature_available?(:issues, user)).to eq(true)
        end
      end

      it "returns true when user is a member of project group" do
        group = create(:group)
48
        project = create(:project, namespace: group)
Felipe Artur's avatar
Felipe Artur committed
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
        group.add_developer(user)

        features.each do |feature|
          project.project_feature.update_attribute("#{feature}_access_level".to_sym, ProjectFeature::PRIVATE)
          expect(project.feature_available?(:issues, user)).to eq(true)
        end
      end

      it "returns true if user is an admin" do
        user.update_attribute(:admin, true)

        features.each do |feature|
          project.project_feature.update_attribute("#{feature}_access_level".to_sym, ProjectFeature::PRIVATE)
          expect(project.feature_available?(:issues, user)).to eq(true)
        end
      end
    end

    context 'when feature is enabled for everyone' do
      it "returns true" do
        features.each do |feature|
          expect(project.feature_available?(:issues, user)).to eq(true)
        end
      end
    end
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

    context 'when feature is disabled by a feature flag' do
      it 'returns false' do
        stub_feature_flags(issues: false)

        expect(project.feature_available?(:issues, user)).to eq(false)
      end
    end

    context 'when feature is enabled by a feature flag' do
      it 'returns true' do
        stub_feature_flags(issues: true)

        expect(project.feature_available?(:issues, user)).to eq(true)
      end
    end
Felipe Artur's avatar
Felipe Artur committed
90
91
  end

92
93
  context 'repository related features' do
    before do
Lin Jen-Shin's avatar
Lin Jen-Shin committed
94
      project.project_feature.update(
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
        merge_requests_access_level: ProjectFeature::DISABLED,
        builds_access_level: ProjectFeature::DISABLED,
        repository_access_level: ProjectFeature::PRIVATE
      )
    end

    it "does not allow repository related features have higher level" do
      features = %w(builds merge_requests)
      project_feature = project.project_feature

      features.each do |feature|
        field = "#{feature}_access_level".to_sym
        project_feature.update_attribute(field, ProjectFeature::ENABLED)
        expect(project_feature.valid?).to be_falsy
      end
    end
  end

113
114
115
116
117
118
119
120
121
122
123
124
125
  context 'public features' do
    it "does not allow public for other than pages" do
      features = %w(issues wiki builds merge_requests snippets repository)
      project_feature = project.project_feature

      features.each do |feature|
        field = "#{feature}_access_level".to_sym
        project_feature.update_attribute(field, ProjectFeature::PUBLIC)
        expect(project_feature.valid?).to be_falsy
      end
    end
  end

Felipe Artur's avatar
Felipe Artur committed
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
  describe '#*_enabled?' do
    let(:features) { %w(wiki builds merge_requests) }

    it "returns false when feature is disabled" do
      features.each do |feature|
        project.project_feature.update_attribute("#{feature}_access_level".to_sym, ProjectFeature::DISABLED)
        expect(project.public_send("#{feature}_enabled?")).to eq(false)
      end
    end

    it "returns true when feature is enabled only for team members" do
      features.each do |feature|
        project.project_feature.update_attribute("#{feature}_access_level".to_sym, ProjectFeature::PRIVATE)
        expect(project.public_send("#{feature}_enabled?")).to eq(true)
      end
    end

    it "returns true when feature is enabled for everyone" do
      features.each do |feature|
        expect(project.public_send("#{feature}_enabled?")).to eq(true)
      end
    end
  end
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176

  describe 'default pages access level' do
    subject { project.project_feature.pages_access_level }

    before do
      # project factory overrides all values in project_feature after creation
      project.project_feature.destroy!
      project.build_project_feature.save!
    end

    context 'when new project is private' do
      let(:project) { create(:project, :private) }

      it { is_expected.to eq(ProjectFeature::PRIVATE) }
    end

    context 'when new project is internal' do
      let(:project) { create(:project, :internal) }

      it { is_expected.to eq(ProjectFeature::PRIVATE) }
    end

    context 'when new project is public' do
      let(:project) { create(:project, :public) }

      it { is_expected.to eq(ProjectFeature::ENABLED) }
    end
  end
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230

  describe '#public_pages?' do
    it 'returns true if Pages access controll is not enabled' do
      stub_config(pages: { access_control: false })

      project_feature = described_class.new

      expect(project_feature.public_pages?).to eq(true)
    end

    context 'Pages access control is enabled' do
      before do
        stub_config(pages: { access_control: true })
      end

      it 'returns true if Pages access level is public' do
        project_feature = described_class.new(pages_access_level: described_class::PUBLIC)

        expect(project_feature.public_pages?).to eq(true)
      end

      it 'returns true if Pages access level is enabled and the project is public' do
        project = build(:project, :public)

        project_feature = described_class.new(project: project, pages_access_level: described_class::ENABLED)

        expect(project_feature.public_pages?).to eq(true)
      end

      it 'returns false if pages or the project are not public' do
        project = build(:project, :private)

        project_feature = described_class.new(project: project, pages_access_level: described_class::ENABLED)

        expect(project_feature.public_pages?).to eq(false)
      end
    end

    describe '#private_pages?' do
      subject(:project_feature) { described_class.new }

      it 'returns false if public_pages? is true' do
        expect(project_feature).to receive(:public_pages?).and_return(true)

        expect(project_feature.private_pages?).to eq(false)
      end

      it 'returns true if public_pages? is false' do
        expect(project_feature).to receive(:public_pages?).and_return(false)

        expect(project_feature.private_pages?).to eq(true)
      end
    end
  end
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254

  describe '.required_minimum_access_level' do
    it 'handles reporter level' do
      expect(described_class.required_minimum_access_level(:merge_requests)).to eq(Gitlab::Access::REPORTER)
    end

    it 'handles guest level' do
      expect(described_class.required_minimum_access_level(:issues)).to eq(Gitlab::Access::GUEST)
    end

    it 'accepts ActiveModel' do
      expect(described_class.required_minimum_access_level(MergeRequest)).to eq(Gitlab::Access::REPORTER)
    end

    it 'accepts string' do
      expect(described_class.required_minimum_access_level('merge_requests')).to eq(Gitlab::Access::REPORTER)
    end

    it 'raises error if feature is invalid' do
      expect do
        described_class.required_minimum_access_level(:foos)
      end.to raise_error
    end
  end
Felipe Artur's avatar
Felipe Artur committed
255
end