summaryrefslogtreecommitdiff
path: root/spec/finders/feature_flags_finder_spec.rb
blob: 8744a1862120e1c94e81999ae247817587c72c2a (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe FeatureFlagsFinder do
  include FeatureFlagHelpers

  let(:finder) { described_class.new(project, user, params) }
  let(:project) { create(:project) }
  let(:user) { developer }
  let(:developer) { create(:user) }
  let(:reporter) { create(:user) }
  let(:params) { {} }

  before do
    project.add_developer(developer)
    project.add_reporter(reporter)
  end

  describe '#execute' do
    subject { finder.execute(**args) }

    let!(:feature_flag_1) { create(:operations_feature_flag, name: 'flag-a', project: project) }
    let!(:feature_flag_2) { create(:operations_feature_flag, name: 'flag-b', project: project) }
    let(:args) { {} }

    it 'returns feature flags ordered by name' do
      is_expected.to eq([feature_flag_1, feature_flag_2])
    end

    it 'preloads relations by default' do
      expect(Operations::FeatureFlag).to receive(:preload_relations).and_call_original

      subject
    end

    context 'when user is a reporter' do
      let(:user) { reporter }

      it 'returns an empty list' do
        is_expected.to be_empty
      end
    end

    context 'when scope is given' do
      let!(:feature_flag_1) { create(:operations_feature_flag, project: project, active: true) }
      let!(:feature_flag_2) { create(:operations_feature_flag, project: project, active: false) }

      context 'when scope is enabled' do
        let(:params) { { scope: 'enabled' } }

        it 'returns active feature flag' do
          is_expected.to eq([feature_flag_1])
        end
      end

      context 'when scope is disabled' do
        let(:params) { { scope: 'disabled' } }

        it 'returns inactive feature flag' do
          is_expected.to eq([feature_flag_2])
        end
      end
    end

    context 'when preload option is false' do
      let(:args) { { preload: false } }

      it 'does not preload relations' do
        expect(Operations::FeatureFlag).not_to receive(:preload_relations)

        subject
      end
    end

    context 'when new version flags are enabled' do
      let!(:feature_flag_3) { create(:operations_feature_flag, :new_version_flag, name: 'flag-c', project: project) }

      it 'returns new and legacy flags' do
        is_expected.to eq([feature_flag_1, feature_flag_2, feature_flag_3])
      end
    end
  end
end