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
|
# frozen_string_literal: true
require 'spec_helper'
describe Ci::UpdateInstanceVariablesService do
let(:params) { { variables_attributes: variables_attributes } }
subject { described_class.new(params) }
describe '#execute' do
context 'without variables' do
let(:variables_attributes) { [] }
it { expect(subject.execute).to be_truthy }
end
context 'with insert only variables' do
let(:variables_attributes) do
[
{ key: 'var_a', secret_value: 'dummy_value_for_a', protected: true },
{ key: 'var_b', secret_value: 'dummy_value_for_b', protected: false }
]
end
it { expect(subject.execute).to be_truthy }
it 'persists all the records' do
expect { subject.execute }
.to change { Ci::InstanceVariable.count }
.by variables_attributes.size
end
it 'persists attributes' do
subject.execute
expect(Ci::InstanceVariable.all).to contain_exactly(
have_attributes(key: 'var_a', secret_value: 'dummy_value_for_a', protected: true),
have_attributes(key: 'var_b', secret_value: 'dummy_value_for_b', protected: false)
)
end
end
context 'with update only variables' do
let!(:var_a) { create(:ci_instance_variable) }
let!(:var_b) { create(:ci_instance_variable, protected: false) }
let(:variables_attributes) do
[
{
id: var_a.id,
key: var_a.key,
secret_value: 'new_dummy_value_for_a',
protected: var_a.protected?.to_s
},
{
id: var_b.id,
key: 'var_b_key',
secret_value: 'new_dummy_value_for_b',
protected: 'true'
}
]
end
it { expect(subject.execute).to be_truthy }
it 'does not change the count' do
expect { subject.execute }
.not_to change { Ci::InstanceVariable.count }
end
it 'updates the records in place', :aggregate_failures do
subject.execute
expect(var_a.reload).to have_attributes(secret_value: 'new_dummy_value_for_a')
expect(var_b.reload).to have_attributes(
key: 'var_b_key', secret_value: 'new_dummy_value_for_b', protected: true)
end
end
context 'with insert and update variables' do
let!(:var_a) { create(:ci_instance_variable) }
let(:variables_attributes) do
[
{
id: var_a.id,
key: var_a.key,
secret_value: 'new_dummy_value_for_a',
protected: var_a.protected?.to_s
},
{
key: 'var_b',
secret_value: 'dummy_value_for_b',
protected: true
}
]
end
it { expect(subject.execute).to be_truthy }
it 'inserts only one record' do
expect { subject.execute }
.to change { Ci::InstanceVariable.count }.by 1
end
it 'persists all the records', :aggregate_failures do
subject.execute
var_b = Ci::InstanceVariable.find_by(key: 'var_b')
expect(var_a.reload.secret_value).to eq('new_dummy_value_for_a')
expect(var_b.secret_value).to eq('dummy_value_for_b')
end
end
context 'with insert, update, and destroy variables' do
let!(:var_a) { create(:ci_instance_variable) }
let!(:var_b) { create(:ci_instance_variable) }
let(:variables_attributes) do
[
{
id: var_a.id,
key: var_a.key,
secret_value: 'new_dummy_value_for_a',
protected: var_a.protected?.to_s
},
{
id: var_b.id,
key: var_b.key,
secret_value: 'dummy_value_for_b',
protected: var_b.protected?.to_s,
'_destroy' => 'true'
},
{
key: 'var_c',
secret_value: 'dummy_value_for_c',
protected: true
}
]
end
it { expect(subject.execute).to be_truthy }
it 'persists all the records', :aggregate_failures do
subject.execute
var_c = Ci::InstanceVariable.find_by(key: 'var_c')
expect(var_a.reload.secret_value).to eq('new_dummy_value_for_a')
expect { var_b.reload }.to raise_error(ActiveRecord::RecordNotFound)
expect(var_c.secret_value).to eq('dummy_value_for_c')
end
end
context 'with invalid variables' do
let!(:var_a) { create(:ci_instance_variable, secret_value: 'dummy_value_for_a') }
let(:variables_attributes) do
[
{
key: '...?',
secret_value: 'nice_value'
},
{
id: var_a.id,
key: var_a.key,
secret_value: 'new_dummy_value_for_a',
protected: var_a.protected?.to_s
},
{
key: var_a.key,
secret_value: 'other_value'
}
]
end
it { expect(subject.execute).to be_falsey }
it 'does not insert any records' do
expect { subject.execute }
.not_to change { Ci::InstanceVariable.count }
end
it 'does not update existing records' do
subject.execute
expect(var_a.reload.secret_value).to eq('dummy_value_for_a')
end
it 'returns errors' do
subject.execute
expect(subject.errors).to match_array(
[
"Key (#{var_a.key}) has already been taken",
"Key can contain only letters, digits and '_'."
])
end
end
context 'when deleting non existing variables' do
let(:variables_attributes) do
[
{
id: 'some-id',
key: 'some_key',
secret_value: 'other_value',
'_destroy' => 'true'
}
]
end
it { expect { subject.execute }.to raise_error(ActiveRecord::RecordNotFound) }
end
context 'when updating non existing variables' do
let(:variables_attributes) do
[
{
id: 'some-id',
key: 'some_key',
secret_value: 'other_value'
}
]
end
it { expect { subject.execute }.to raise_error(ActiveRecord::RecordNotFound) }
end
end
end
|