summaryrefslogtreecommitdiff
path: root/spec/models/concerns/featurable_spec.rb
blob: 89720e3652c6a82706cb66ba1b8d5e917f8c7c5f (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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
177
178
179
180
181
182
183
184
# frozen_string_literal: true

require 'spec_helper'

describe Featurable do
  let_it_be(:user) { create(:user) }
  let(:project) { create(:project) }
  let(:feature_class) { subject.class }
  let(:features) { feature_class::FEATURES }

  subject { project.project_feature }

  describe '.quoted_access_level_column' do
    it 'returns the table name and quoted column name for a feature' do
      expected = '"project_features"."issues_access_level"'

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

  describe '.access_level_attribute' do
    it { expect(feature_class.access_level_attribute(:wiki)).to eq :wiki_access_level }

    it 'raises error for unspecified feature' do
      expect { feature_class.access_level_attribute(:unknown) }
        .to raise_error(ArgumentError, /invalid feature: unknown/)
    end
  end

  describe '.set_available_features' do
    let!(:klass) do
      Class.new do
        include Featurable
        set_available_features %i(feature1 feature2)

        def feature1_access_level
          Featurable::DISABLED
        end

        def feature2_access_level
          Featurable::ENABLED
        end
      end
    end
    let!(:instance) { klass.new }

    it { expect(klass.available_features).to eq [:feature1, :feature2] }
    it { expect(instance.feature1_enabled?).to be_falsey }
    it { expect(instance.feature2_enabled?).to be_truthy }
  end

  describe '.available_features' do
    it { expect(feature_class.available_features).to include(*features) }
  end

  describe '#access_level' do
    it 'returns access level' do
      expect(subject.access_level(:wiki)).to eq(subject.wiki_access_level)
    end
  end

  describe '#feature_available?' do
    let(:features) { %w(issues wiki builds merge_requests snippets repository pages metrics_dashboard) }

    context 'when features are disabled' do
      it "returns false" do
        update_all_project_features(project, features, ProjectFeature::DISABLED)

        features.each do |feature|
          expect(project.feature_available?(feature.to_sym, user)).to eq(false), "#{feature} failed"
        end
      end
    end

    context 'when features are enabled only for team members' do
      it "returns false when user is not a team member" do
        update_all_project_features(project, features, ProjectFeature::PRIVATE)

        features.each do |feature|
          expect(project.feature_available?(feature.to_sym, user)).to eq(false), "#{feature} failed"
        end
      end

      it "returns true when user is a team member" do
        project.add_developer(user)

        update_all_project_features(project, features, ProjectFeature::PRIVATE)

        features.each do |feature|
          expect(project.feature_available?(feature.to_sym, user)).to eq(true), "#{feature} failed"
        end
      end

      it "returns true when user is a member of project group" do
        group = create(:group)
        project = create(:project, namespace: group)
        group.add_developer(user)

        update_all_project_features(project, features, ProjectFeature::PRIVATE)

        features.each do |feature|
          expect(project.feature_available?(feature.to_sym, user)).to eq(true), "#{feature} failed"
        end
      end

      context 'when admin mode is enabled', :enable_admin_mode do
        it "returns true if user is an admin" do
          user.update_attribute(:admin, true)

          update_all_project_features(project, features, ProjectFeature::PRIVATE)

          features.each do |feature|
            expect(project.feature_available?(feature.to_sym, user)).to eq(true), "#{feature} failed"
          end
        end
      end

      context 'when admin mode is disabled' do
        it "returns false when user is an admin" do
          user.update_attribute(:admin, true)

          update_all_project_features(project, features, ProjectFeature::PRIVATE)

          features.each do |feature|
            expect(project.feature_available?(feature.to_sym, user)).to eq(false), "#{feature} failed"
          end
        end
      end
    end

    context 'when feature is enabled for everyone' do
      it "returns true" do
        expect(project.feature_available?(:issues, user)).to eq(true)
      end
    end

    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
  end

  describe '#*_enabled?' do
    let(:features) { %w(wiki builds merge_requests) }

    it "returns false when feature is disabled" do
      update_all_project_features(project, features, ProjectFeature::DISABLED)

      features.each do |feature|
        expect(project.public_send("#{feature}_enabled?")).to eq(false), "#{feature} failed"
      end
    end

    it "returns true when feature is enabled only for team members" do
      update_all_project_features(project, features, ProjectFeature::PRIVATE)

      features.each do |feature|
        expect(project.public_send("#{feature}_enabled?")).to eq(true), "#{feature} failed"
      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), "#{feature} failed"
      end
    end
  end

  def update_all_project_features(project, features, value)
    project_feature_attributes = features.map { |f| ["#{f}_access_level", value] }.to_h
    project.project_feature.update(project_feature_attributes)
  end
end