summaryrefslogtreecommitdiff
path: root/spec/services/ci/change_variable_service_spec.rb
blob: f86a87132b1bb0455512e8ec14406c04c08ec022 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Ci::ChangeVariableService do
  let(:service) { described_class.new(container: group, current_user: user, params: params) }

  let_it_be(:user) { create(:user) }

  let(:group) { create(:group) }

  describe '#execute' do
    subject(:execute) { service.execute }

    context 'when creating a variable' do
      let(:params) { { variable_params: { key: 'new_variable', value: 'variable_value' }, action: :create } }

      it 'persists a variable' do
        expect { execute }.to change(Ci::GroupVariable, :count).from(0).to(1)
      end
    end

    context 'when updating a variable' do
      let!(:variable) { create(:ci_group_variable, value: 'old_value') }
      let(:params) { { variable_params: { key: variable.key, value: 'new_value' }, action: :update } }

      before do
        group.variables << variable
      end

      it 'updates a variable' do
        expect { execute }.to change { variable.reload.value }.from('old_value').to('new_value')
      end

      context 'when the variable does not exist' do
        before do
          variable.destroy!
        end

        it 'raises a record not found error' do
          expect { execute }.to raise_error(::ActiveRecord::RecordNotFound)
        end
      end
    end

    context 'when destroying a variable' do
      let!(:variable) { create(:ci_group_variable) }
      let(:params) { { variable_params: { key: variable.key }, action: :destroy } }

      before do
        group.variables << variable
      end

      it 'destroys a variable' do
        expect { execute }.to change { Ci::GroupVariable.exists?(variable.id) }.from(true).to(false)
      end

      context 'when the variable does not exist' do
        before do
          variable.destroy!
        end

        it 'raises a record not found error' do
          expect { execute }.to raise_error(::ActiveRecord::RecordNotFound)
        end
      end
    end
  end
end