summaryrefslogtreecommitdiff
path: root/spec/models/operations/feature_flag_spec.rb
blob: 55682e12642d847669a8302871ebdc16a023552d (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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Operations::FeatureFlag do
  include FeatureFlagHelpers

  subject { create(:operations_feature_flag) }

  it_behaves_like 'includes Limitable concern' do
    subject { build(:operations_feature_flag, project: create(:project)) }
  end

  describe 'associations' do
    it { is_expected.to belong_to(:project) }
    it { is_expected.to have_many(:scopes) }
  end

  describe '.reference_pattern' do
    subject { described_class.reference_pattern }

    it { is_expected.to match('[feature_flag:123]') }
    it { is_expected.to match('[feature_flag:gitlab-org/gitlab/123]') }
  end

  describe '.link_reference_pattern' do
    subject { described_class.link_reference_pattern }

    it { is_expected.to match("#{Gitlab.config.gitlab.url}/gitlab-org/gitlab/-/feature_flags/123/edit") }
    it { is_expected.not_to match("#{Gitlab.config.gitlab.url}/gitlab-org/gitlab/issues/123/edit") }
    it { is_expected.not_to match("gitlab-org/gitlab/-/feature_flags/123/edit") }
  end

  describe '#to_reference' do
    let(:namespace) { build(:namespace, path: 'sample-namespace') }
    let(:project) { build(:project, name: 'sample-project', namespace: namespace) }
    let(:feature_flag) { build(:operations_feature_flag, iid: 1, project: project) }

    it 'returns feature flag id' do
      expect(feature_flag.to_reference).to eq '[feature_flag:1]'
    end

    it 'returns complete path to the feature flag with full: true' do
      expect(feature_flag.to_reference(full: true)).to eq '[feature_flag:sample-namespace/sample-project/1]'
    end
  end

  describe 'validations' do
    it { is_expected.to validate_presence_of(:project) }
    it { is_expected.to validate_presence_of(:name) }
    it { is_expected.to validate_uniqueness_of(:name).scoped_to(:project_id) }
    it { is_expected.to define_enum_for(:version).with_values(legacy_flag: 1, new_version_flag: 2) }

    context 'a version 1 feature flag' do
      it 'is valid if associated with Operations::FeatureFlagScope models' do
        project = create(:project)
        feature_flag = described_class.create!({ name: 'test', project: project, version: 1,
                                 scopes_attributes: [{ environment_scope: '*', active: false }] })

        expect(feature_flag).to be_valid
      end

      it 'is invalid if associated with Operations::FeatureFlags::Strategy models' do
        project = create(:project)
        feature_flag = described_class.new({ name: 'test', project: project, version: 1,
                                 strategies_attributes: [{ name: 'default', parameters: {} }] })

        expect(feature_flag.valid?).to eq(false)
        expect(feature_flag.errors.messages).to eq({
          version_associations: ["version 1 feature flags may not have strategies"]
        })
      end
    end

    context 'a version 2 feature flag' do
      it 'is invalid if associated with Operations::FeatureFlagScope models' do
        project = create(:project)
        feature_flag = described_class.new({ name: 'test', project: project, version: 2,
                                 scopes_attributes: [{ environment_scope: '*', active: false }] })

        expect(feature_flag.valid?).to eq(false)
        expect(feature_flag.errors.messages).to eq({
          version_associations: ["version 2 feature flags may not have scopes"]
        })
      end

      it 'is valid if associated with Operations::FeatureFlags::Strategy models' do
        project = create(:project)
        feature_flag = described_class.create!({ name: 'test', project: project, version: 2,
                                                strategies_attributes: [{ name: 'default', parameters: {} }] })

        expect(feature_flag).to be_valid
      end
    end

    it_behaves_like 'AtomicInternalId', validate_presence: true do
      let(:internal_id_attribute) { :iid }
      let(:instance) { build(:operations_feature_flag) }
      let(:scope) { :project }
      let(:scope_attrs) { { project: instance.project } }
      let(:usage) { :operations_feature_flags }
    end
  end

  describe 'feature flag version' do
    it 'defaults to 1 if unspecified' do
      project = create(:project)

      feature_flag = described_class.create!(name: 'my_flag', project: project, active: true)

      expect(feature_flag).to be_valid
      expect(feature_flag.version_before_type_cast).to eq(1)
    end
  end

  describe 'Scope creation' do
    subject { described_class.new(**params) }

    let(:project) { create(:project) }

    let(:params) do
      { name: 'test', project: project, scopes_attributes: scopes_attributes }
    end

    let(:scopes_attributes) do
      [{ environment_scope: '*', active: false },
       { environment_scope: 'review/*', active: true }]
    end

    it { is_expected.to be_valid }

    context 'when the first scope is not wildcard' do
      let(:scopes_attributes) do
        [{ environment_scope: 'review/*', active: true },
         { environment_scope: '*', active: false }]
      end

      it { is_expected.not_to be_valid }
    end
  end

  describe 'the default scope' do
    let_it_be(:project) { create(:project) }

    context 'with a version 1 feature flag' do
      it 'creates a default scope' do
        feature_flag = described_class.create!({ name: 'test', project: project, scopes_attributes: [], version: 1 })

        expect(feature_flag.scopes.count).to eq(1)
        expect(feature_flag.scopes.first.environment_scope).to eq('*')
      end

      it 'allows specifying the default scope in the parameters' do
        feature_flag = described_class.create!({ name: 'test', project: project,
                                                scopes_attributes: [{ environment_scope: '*', active: false },
                                                                    { environment_scope: 'review/*', active: true }], version: 1 })

        expect(feature_flag.scopes.count).to eq(2)
        expect(feature_flag.scopes.first.environment_scope).to eq('*')
      end
    end

    context 'with a version 2 feature flag' do
      it 'does not create a default scope' do
        feature_flag = described_class.create!({ name: 'test', project: project, scopes_attributes: [], version: 2 })

        expect(feature_flag.scopes).to eq([])
      end
    end
  end

  describe '.enabled' do
    subject { described_class.enabled }

    context 'when the feature flag is active' do
      let!(:feature_flag) { create(:operations_feature_flag, active: true) }

      it 'returns the flag' do
        is_expected.to eq([feature_flag])
      end
    end

    context 'when the feature flag is active and all scopes are inactive' do
      let!(:feature_flag) { create(:operations_feature_flag, :legacy_flag, active: true) }

      it 'returns the flag' do
        feature_flag.default_scope.update!(active: false)

        is_expected.to eq([feature_flag])
      end
    end

    context 'when the feature flag is inactive' do
      let!(:feature_flag) { create(:operations_feature_flag, active: false) }

      it 'does not return the flag' do
        is_expected.to be_empty
      end
    end

    context 'when the feature flag is inactive and all scopes are active' do
      let!(:feature_flag) { create(:operations_feature_flag, :legacy_flag, active: false) }

      it 'does not return the flag' do
        feature_flag.default_scope.update!(active: true)

        is_expected.to be_empty
      end
    end
  end

  describe '.disabled' do
    subject { described_class.disabled }

    context 'when the feature flag is active' do
      let!(:feature_flag) { create(:operations_feature_flag, active: true) }

      it 'does not return the flag' do
        is_expected.to be_empty
      end
    end

    context 'when the feature flag is active and all scopes are inactive' do
      let!(:feature_flag) { create(:operations_feature_flag, :legacy_flag, active: true) }

      it 'does not return the flag' do
        feature_flag.default_scope.update!(active: false)

        is_expected.to be_empty
      end
    end

    context 'when the feature flag is inactive' do
      let!(:feature_flag) { create(:operations_feature_flag, active: false) }

      it 'returns the flag' do
        is_expected.to eq([feature_flag])
      end
    end

    context 'when the feature flag is inactive and all scopes are active' do
      let!(:feature_flag) { create(:operations_feature_flag, :legacy_flag, active: false) }

      it 'returns the flag' do
        feature_flag.default_scope.update!(active: true)

        is_expected.to eq([feature_flag])
      end
    end
  end

  describe '.for_unleash_client' do
    let_it_be(:project) { create(:project) }
    let!(:feature_flag) do
      create(:operations_feature_flag, project: project,
             name: 'feature1', active: true, version: 2)
    end

    let!(:strategy) do
      create(:operations_strategy, feature_flag: feature_flag,
             name: 'default', parameters: {})
    end

    it 'matches wild cards in the scope' do
      create(:operations_scope, strategy: strategy, environment_scope: 'review/*')

      flags = described_class.for_unleash_client(project, 'review/feature-branch')

      expect(flags).to eq([feature_flag])
    end

    it 'matches wild cards case sensitively' do
      create(:operations_scope, strategy: strategy, environment_scope: 'Staging/*')

      flags = described_class.for_unleash_client(project, 'staging/feature')

      expect(flags).to eq([])
    end

    it 'returns feature flags ordered by id' do
      create(:operations_scope, strategy: strategy, environment_scope: 'production')
      feature_flag_b = create(:operations_feature_flag, project: project,
                              name: 'feature2', active: true, version: 2)
      strategy_b = create(:operations_strategy, feature_flag: feature_flag_b,
                          name: 'default', parameters: {})
      create(:operations_scope, strategy: strategy_b, environment_scope: '*')

      flags = described_class.for_unleash_client(project, 'production')

      expect(flags.map(&:id)).to eq([feature_flag.id, feature_flag_b.id])
    end
  end

  describe '#hook_attrs' do
    it 'includes expected attributes' do
      hook_attrs = {
        id: subject.id,
        name: subject.name,
        description: subject.description,
        active: subject.active
      }
      expect(subject.hook_attrs).to eq(hook_attrs)
    end
  end

  describe "#execute_hooks" do
    let_it_be(:user) { create(:user) }
    let_it_be(:project) { create(:project) }
    let_it_be(:feature_flag) { create(:operations_feature_flag, project: project) }

    it 'does not execute the hook when feature_flag event is disabled' do
      create(:project_hook, project: project, feature_flag_events: false)
      expect(WebHookWorker).not_to receive(:perform_async)

      feature_flag.execute_hooks(user)
      feature_flag.touch
    end

    it 'executes hook when feature_flag event is enabled' do
      hook = create(:project_hook, project: project, feature_flag_events: true)
      expect(WebHookWorker).to receive(:perform_async).with(hook.id, an_instance_of(Hash), 'feature_flag_hooks')

      feature_flag.execute_hooks(user)
      feature_flag.touch
    end
  end
end