summaryrefslogtreecommitdiff
path: root/spec/support/features/variable_list_shared_examples.rb
blob: 693b796fbdc9b0fa08cd692d28b24d0fa6196741 (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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
shared_examples 'variable list' do
  it 'shows list of variables' do
    page.within('.js-ci-variable-list-section') do
      expect(first('.js-ci-variable-input-key').value).to eq(variable.key)
    end
  end

  it 'adds new CI variable' do
    page.within('.js-ci-variable-list-section .js-row:last-child') do
      find('.js-ci-variable-input-key').set('key')
      find('.js-ci-variable-input-value').set('key_value')
    end

    click_button('Save variables')
    wait_for_requests

    visit page_path

    # We check the first row because it re-sorts to alphabetical order on refresh
    page.within('.js-ci-variable-list-section .js-row:nth-child(1)') do
      expect(find('.js-ci-variable-input-key').value).to eq('key')
      expect(find('.js-ci-variable-input-value', visible: false).value).to eq('key_value')
    end
  end

  it 'adds a new protected variable' do
    page.within('.js-ci-variable-list-section .js-row:last-child') do
      find('.js-ci-variable-input-key').set('key')
      find('.js-ci-variable-input-value').set('key_value')
      find('.ci-variable-protected-item .js-project-feature-toggle').click

      expect(find('.js-ci-variable-input-protected', visible: false).value).to eq('true')
    end

    click_button('Save variables')
    wait_for_requests

    visit page_path

    # We check the first row because it re-sorts to alphabetical order on refresh
    page.within('.js-ci-variable-list-section .js-row:nth-child(1)') do
      expect(find('.js-ci-variable-input-key').value).to eq('key')
      expect(find('.js-ci-variable-input-value', visible: false).value).to eq('key_value')
      expect(find('.js-ci-variable-input-protected', visible: false).value).to eq('true')
    end
  end

  it 'defaults to masked' do
    page.within('.js-ci-variable-list-section .js-row:last-child') do
      find('.js-ci-variable-input-key').set('key')
      find('.js-ci-variable-input-value').set('key_value')

      expect(find('.js-ci-variable-input-masked', visible: false).value).to eq('true')
    end

    click_button('Save variables')
    wait_for_requests

    visit page_path

    # We check the first row because it re-sorts to alphabetical order on refresh
    page.within('.js-ci-variable-list-section .js-row:nth-child(1)') do
      expect(find('.js-ci-variable-input-key').value).to eq('key')
      expect(find('.js-ci-variable-input-value', visible: false).value).to eq('key_value')
      expect(find('.js-ci-variable-input-masked', visible: false).value).to eq('true')
    end
  end

  context 'defaults to the application setting' do
    context 'application setting is true' do
      before do
        stub_application_setting(protected_ci_variables: true)

        visit page_path
      end

      it 'defaults to protected' do
        page.within('.js-ci-variable-list-section .js-row:last-child') do
          find('.js-ci-variable-input-key').set('key')
        end

        values = all('.js-ci-variable-input-protected', visible: false).map(&:value)

        expect(values).to eq %w(false true true)
      end

      it 'shows a message regarding the changed default' do
        expect(page).to have_content 'Environment variables are configured by your administrator to be protected by default'
      end
    end

    context 'application setting is false' do
      before do
        stub_application_setting(protected_ci_variables: false)

        visit page_path
      end

      it 'defaults to unprotected' do
        page.within('.js-ci-variable-list-section .js-row:last-child') do
          find('.js-ci-variable-input-key').set('key')
        end

        values = all('.js-ci-variable-input-protected', visible: false).map(&:value)

        expect(values).to eq %w(false false false)
      end

      it 'does not show a message regarding the default' do
        expect(page).not_to have_content 'Environment variables are configured by your administrator to be protected by default'
      end
    end
  end

  it 'reveals and hides variables' do
    page.within('.js-ci-variable-list-section') do
      expect(first('.js-ci-variable-input-key').value).to eq(variable.key)
      expect(first('.js-ci-variable-input-value', visible: false).value).to eq(variable.value)
      expect(page).to have_content('*' * 20)

      click_button('Reveal value')

      expect(first('.js-ci-variable-input-key').value).to eq(variable.key)
      expect(first('.js-ci-variable-input-value').value).to eq(variable.value)
      expect(page).not_to have_content('*' * 20)

      click_button('Hide value')

      expect(first('.js-ci-variable-input-key').value).to eq(variable.key)
      expect(first('.js-ci-variable-input-value', visible: false).value).to eq(variable.value)
      expect(page).to have_content('*' * 20)
    end
  end

  it 'deletes variable' do
    page.within('.js-ci-variable-list-section') do
      expect(page).to have_selector('.js-row', count: 2)

      first('.js-row-remove-button').click

      click_button('Save variables')
      wait_for_requests

      expect(page).to have_selector('.js-row', count: 1)
    end
  end

  it 'edits variable' do
    page.within('.js-ci-variable-list-section') do
      click_button('Reveal value')

      page.within('.js-row:nth-child(1)') do
        find('.js-ci-variable-input-key').set('new_key')
        find('.js-ci-variable-input-value').set('new_value')
      end

      click_button('Save variables')
      wait_for_requests

      visit page_path

      page.within('.js-row:nth-child(1)') do
        expect(find('.js-ci-variable-input-key').value).to eq('new_key')
        expect(find('.js-ci-variable-input-value', visible: false).value).to eq('new_value')
      end
    end
  end

  it 'edits variable to be protected' do
    # Create the unprotected variable
    page.within('.js-ci-variable-list-section .js-row:last-child') do
      find('.js-ci-variable-input-key').set('unprotected_key')
      find('.js-ci-variable-input-value').set('unprotected_value')

      expect(find('.js-ci-variable-input-protected', visible: false).value).to eq('false')
    end

    click_button('Save variables')
    wait_for_requests

    visit page_path

    # We check the first row because it re-sorts to alphabetical order on refresh
    page.within('.js-ci-variable-list-section .js-row:nth-child(2)') do
      find('.ci-variable-protected-item .js-project-feature-toggle').click

      expect(find('.js-ci-variable-input-protected', visible: false).value).to eq('true')
    end

    click_button('Save variables')
    wait_for_requests

    visit page_path

    # We check the first row because it re-sorts to alphabetical order on refresh
    page.within('.js-ci-variable-list-section .js-row:nth-child(2)') do
      expect(find('.js-ci-variable-input-key').value).to eq('unprotected_key')
      expect(find('.js-ci-variable-input-value', visible: false).value).to eq('unprotected_value')
      expect(find('.js-ci-variable-input-protected', visible: false).value).to eq('true')
    end
  end

  it 'edits variable to be unprotected' do
    # Create the protected variable
    page.within('.js-ci-variable-list-section .js-row:last-child') do
      find('.js-ci-variable-input-key').set('protected_key')
      find('.js-ci-variable-input-value').set('protected_value')
      find('.ci-variable-protected-item .js-project-feature-toggle').click

      expect(find('.js-ci-variable-input-protected', visible: false).value).to eq('true')
    end

    click_button('Save variables')
    wait_for_requests

    visit page_path

    page.within('.js-ci-variable-list-section .js-row:nth-child(1)') do
      find('.ci-variable-protected-item .js-project-feature-toggle').click

      expect(find('.js-ci-variable-input-protected', visible: false).value).to eq('false')
    end

    click_button('Save variables')
    wait_for_requests

    visit page_path

    page.within('.js-ci-variable-list-section .js-row:nth-child(1)') do
      expect(find('.js-ci-variable-input-key').value).to eq('protected_key')
      expect(find('.js-ci-variable-input-value', visible: false).value).to eq('protected_value')
      expect(find('.js-ci-variable-input-protected', visible: false).value).to eq('false')
    end
  end

  it 'edits variable to be unmasked' do
    page.within('.js-ci-variable-list-section .js-row:nth-child(1)') do
      expect(find('.js-ci-variable-input-masked', visible: false).value).to eq('true')

      find('.ci-variable-masked-item .js-project-feature-toggle').click

      expect(find('.js-ci-variable-input-masked', visible: false).value).to eq('false')
    end

    click_button('Save variables')
    wait_for_requests

    visit page_path

    page.within('.js-ci-variable-list-section .js-row:nth-child(1)') do
      expect(find('.js-ci-variable-input-masked', visible: false).value).to eq('false')
    end
  end

  it 'edits variable to be masked' do
    page.within('.js-ci-variable-list-section .js-row:nth-child(1)') do
      expect(find('.js-ci-variable-input-masked', visible: false).value).to eq('true')

      find('.ci-variable-masked-item .js-project-feature-toggle').click

      expect(find('.js-ci-variable-input-masked', visible: false).value).to eq('false')
    end

    click_button('Save variables')
    wait_for_requests

    visit page_path

    page.within('.js-ci-variable-list-section .js-row:nth-child(1)') do
      expect(find('.js-ci-variable-input-masked', visible: false).value).to eq('false')

      find('.ci-variable-masked-item .js-project-feature-toggle').click

      expect(find('.js-ci-variable-input-masked', visible: false).value).to eq('true')
    end

    click_button('Save variables')
    wait_for_requests

    visit page_path

    page.within('.js-ci-variable-list-section .js-row:nth-child(1)') do
      expect(find('.js-ci-variable-input-masked', visible: false).value).to eq('true')
    end
  end

  it 'handles multiple edits and deletion in the middle' do
    page.within('.js-ci-variable-list-section') do
      # Create 2 variables
      page.within('.js-row:last-child') do
        find('.js-ci-variable-input-key').set('akey')
        find('.js-ci-variable-input-value').set('akeyvalue')
      end
      page.within('.js-row:last-child') do
        find('.js-ci-variable-input-key').set('zkey')
        find('.js-ci-variable-input-value').set('zkeyvalue')
      end

      click_button('Save variables')
      wait_for_requests

      expect(page).to have_selector('.js-row', count: 4)

      # Remove the `akey` variable
      page.within('.js-row:nth-child(2)') do
        first('.js-row-remove-button').click
      end

      # Add another variable
      page.within('.js-row:last-child') do
        find('.js-ci-variable-input-key').set('ckey')
        find('.js-ci-variable-input-value').set('ckeyvalue')
      end

      click_button('Save variables')
      wait_for_requests

      visit page_path

      # Expect to find 3 variables(4 rows) in alphbetical order
      expect(page).to have_selector('.js-row', count: 4)
      row_keys = all('.js-ci-variable-input-key')
      expect(row_keys[0].value).to eq('ckey')
      expect(row_keys[1].value).to eq('test_key')
      expect(row_keys[2].value).to eq('zkey')
      expect(row_keys[3].value).to eq('')
    end
  end

  it 'shows validation error box about duplicate keys' do
    page.within('.js-ci-variable-list-section .js-row:last-child') do
      find('.js-ci-variable-input-key').set('samekey')
      find('.js-ci-variable-input-value').set('value123')
    end
    page.within('.js-ci-variable-list-section .js-row:last-child') do
      find('.js-ci-variable-input-key').set('samekey')
      find('.js-ci-variable-input-value').set('value456')
    end

    click_button('Save variables')
    wait_for_requests

    expect(all('.js-ci-variable-list-section .js-ci-variable-error-box ul li').count).to eq(1)

    # We check the first row because it re-sorts to alphabetical order on refresh
    page.within('.js-ci-variable-list-section') do
      expect(find('.js-ci-variable-error-box')).to have_content(/Validation failed Variables have duplicate values \(.+\)/)
    end
  end

  it 'shows validation error box about empty values' do
    page.within('.js-ci-variable-list-section .js-row:last-child') do
      find('.js-ci-variable-input-key').set('empty_value')
      find('.js-ci-variable-input-value').set('')
    end

    click_button('Save variables')
    wait_for_requests

    page.within('.js-ci-variable-list-section') do
      expect(all('.js-ci-variable-error-box ul li').count).to eq(1)
      expect(find('.js-ci-variable-error-box')).to have_content(/Validation failed Variables value is invalid/)
    end
  end

  it 'shows validation error box about unmaskable values' do
    page.within('.js-ci-variable-list-section .js-row:last-child') do
      find('.js-ci-variable-input-key').set('unmaskable_value')
      find('.js-ci-variable-input-value').set('???')
    end

    click_button('Save variables')
    wait_for_requests

    page.within('.js-ci-variable-list-section') do
      expect(all('.js-ci-variable-error-box ul li').count).to eq(1)
      expect(find('.js-ci-variable-error-box')).to have_content(/Validation failed Variables value is invalid/)
    end
  end
end