summaryrefslogtreecommitdiff
path: root/spec/services/feature_flags/create_service_spec.rb
blob: e80a24f9760590a0bdeb6ffab69606dab35ccd9c (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe FeatureFlags::CreateService do
  let(:project) { create(:project) }
  let(:developer) { create(:user) }
  let(:reporter) { create(:user) }
  let(:user) { developer }

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

  describe '#execute' do
    subject do
      described_class.new(project, user, params).execute
    end

    let(:feature_flag) { subject[:feature_flag] }

    context 'when feature flag can not be created' do
      let(:params) { {} }

      it 'returns status error' do
        expect(subject[:status]).to eq(:error)
      end

      it 'returns validation errors' do
        expect(subject[:message]).to include("Name can't be blank")
      end

      it 'does not create audit log' do
        expect { subject }.not_to change { AuditEvent.count }
      end
    end

    context 'when feature flag is saved correctly' do
      let(:params) do
        {
          name: 'feature_flag',
          description: 'description',
          scopes_attributes: [{ environment_scope: '*', active: true },
                              { environment_scope: 'production', active: false }]
        }
      end

      it 'returns status success' do
        expect(subject[:status]).to eq(:success)
      end

      it 'creates feature flag' do
        expect { subject }.to change { Operations::FeatureFlag.count }.by(1)
      end

      it 'creates audit event' do
        expected_message = 'Created feature flag <strong>feature_flag</strong> '\
                           'with description <strong>"description"</strong>. '\
                           'Created rule <strong>*</strong> and set it as <strong>active</strong> '\
                           'with strategies <strong>[{"name"=>"default", "parameters"=>{}}]</strong>. '\
                           'Created rule <strong>production</strong> and set it as <strong>inactive</strong> '\
                           'with strategies <strong>[{"name"=>"default", "parameters"=>{}}]</strong>.'

        expect { subject }.to change { AuditEvent.count }.by(1)
        expect(AuditEvent.last.details[:custom_message]).to eq(expected_message)
      end

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

        it 'returns error status' do
          expect(subject[:status]).to eq(:error)
          expect(subject[:message]).to eq('Access Denied')
        end
      end
    end
  end
end