summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/features/variable_list_shared_examples.rb
blob: 997500415a90e6fc0cd93bb7b35ba935dce14c5c (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
# frozen_string_literal: true

RSpec.shared_examples 'variable list' do
  it 'shows a list of variables' do
    page.within('[data-testid="ci-variable-table"]') do
      expect(find('.js-ci-variable-row:nth-child(1) td[data-label="Key"]').text).to eq(variable.key)
    end
  end

  it 'adds a new CI variable' do
    click_button('Add variable')

    fill_variable('key', 'key_value') do
      click_button('Add variable')
    end

    wait_for_requests

    page.within('[data-testid="ci-variable-table"]') do
      expect(find('.js-ci-variable-row:nth-child(1) td[data-label="Key"]').text).to eq('key')
    end
  end

  it 'adds a new protected variable' do
    click_button('Add variable')

    fill_variable('key', 'key_value') do
      click_button('Add variable')
    end

    wait_for_requests

    page.within('[data-testid="ci-variable-table"]') do
      expect(find('.js-ci-variable-row:nth-child(1) td[data-label="Key"]').text).to eq('key')
      expect(find('.js-ci-variable-row:nth-child(1) td[data-label="Protected"] svg[data-testid="mobile-issue-close-icon"]')).to be_present
    end
  end

  it 'defaults to unmasked' do
    click_button('Add variable')

    fill_variable('key', 'key_value') do
      click_button('Add variable')
    end

    wait_for_requests

    page.within('[data-testid="ci-variable-table"]') do
      expect(find('.js-ci-variable-row:nth-child(1) td[data-label="Key"]').text).to eq('key')
      expect(find('.js-ci-variable-row:nth-child(1) td[data-label="Masked"] svg[data-testid="close-icon"]')).to be_present
    end
  end

  it 'reveals and hides variables' do
    page.within('[data-testid="ci-variable-table"]') do
      expect(first('.js-ci-variable-row td[data-label="Key"]').text).to eq(variable.key)
      expect(page).to have_content('*' * 17)

      click_button('Reveal value')

      expect(first('.js-ci-variable-row td[data-label="Key"]').text).to eq(variable.key)
      expect(first('.js-ci-variable-row td[data-label="Value"]').text).to eq(variable.value)
      expect(page).not_to have_content('*' * 17)

      click_button('Hide value')

      expect(first('.js-ci-variable-row td[data-label="Key"]').text).to eq(variable.key)
      expect(page).to have_content('*' * 17)
    end
  end

  it 'deletes a variable' do
    expect(page).to have_selector('.js-ci-variable-row', count: 1)

    page.within('[data-testid="ci-variable-table"]') do
      click_button('Edit')
    end

    page.within('#add-ci-variable') do
      click_button('Delete variable')
    end

    wait_for_requests

    expect(first('.js-ci-variable-row').text).to eq('There are no variables yet.')
  end

  it 'edits a variable' do
    page.within('[data-testid="ci-variable-table"]') do
      click_button('Edit')
    end

    page.within('#add-ci-variable') do
      find('[data-qa-selector="ci_variable_key_field"] input').set('new_key')

      click_button('Update variable')
    end

    wait_for_requests

    expect(first('.js-ci-variable-row td[data-label="Key"]').text).to eq('new_key')
  end

  it 'edits a variable to be unmasked' do
    page.within('[data-testid="ci-variable-table"]') do
      click_button('Edit')
    end

    page.within('#add-ci-variable') do
      find('[data-testid="ci-variable-protected-checkbox"]').click
      find('[data-testid="ci-variable-masked-checkbox"]').click

      click_button('Update variable')
    end

    wait_for_requests

    page.within('[data-testid="ci-variable-table"]') do
      expect(find('.js-ci-variable-row:nth-child(1) td[data-label="Masked"] svg[data-testid="close-icon"]')).to be_present
    end
  end

  it 'edits a variable to be masked' do
    page.within('[data-testid="ci-variable-table"]') do
      click_button('Edit')
    end

    page.within('#add-ci-variable') do
      find('[data-testid="ci-variable-masked-checkbox"]').click

      click_button('Update variable')
    end

    wait_for_requests

    page.within('[data-testid="ci-variable-table"]') do
      click_button('Edit')
    end

    page.within('#add-ci-variable') do
      find('[data-testid="ci-variable-masked-checkbox"]').click

      click_button('Update variable')
    end

    page.within('[data-testid="ci-variable-table"]') do
      expect(find('.js-ci-variable-row:nth-child(1) td[data-label="Masked"] svg[data-testid="mobile-issue-close-icon"]')).to be_present
    end
  end

  it 'shows a validation error box about duplicate keys' do
    click_button('Add variable')

    fill_variable('key', 'key_value') do
      click_button('Add variable')
    end

    wait_for_requests

    click_button('Add variable')

    fill_variable('key', 'key_value') do
      click_button('Add variable')
    end

    wait_for_requests

    expect(find('.flash-container')).to be_present
    expect(find('.flash-text').text).to have_content('Variables key (key) has already been taken')
  end

  it 'prevents a variable to be added if no values are provided when a variable is set to masked' do
    click_button('Add variable')

    page.within('#add-ci-variable') do
      find('[data-qa-selector="ci_variable_key_field"] input').set('empty_mask_key')
      find('[data-testid="ci-variable-protected-checkbox"]').click
      find('[data-testid="ci-variable-masked-checkbox"]').click

      expect(find_button('Add variable', disabled: true)).to be_present
    end
  end

  it 'shows validation error box about unmaskable values' do
    click_button('Add variable')

    fill_variable('empty_mask_key', '???', protected: true, masked: true) do
      expect(page).to have_content('This variable can not be masked')
      expect(find_button('Add variable', disabled: true)).to be_present
    end
  end

  it 'handles multiple edits and a deletion' do
    # Create two variables
    click_button('Add variable')

    fill_variable('akey', 'akeyvalue') do
      click_button('Add variable')
    end

    wait_for_requests

    click_button('Add variable')

    fill_variable('zkey', 'zkeyvalue') do
      click_button('Add variable')
    end

    wait_for_requests

    expect(page).to have_selector('.js-ci-variable-row', count: 3)

    # Remove the `akey` variable
    page.within('[data-testid="ci-variable-table"]') do
      page.within('.js-ci-variable-row:first-child') do
        click_button('Edit')
      end
    end

    page.within('#add-ci-variable') do
      click_button('Delete variable')
    end

    wait_for_requests

    # Add another variable
    click_button('Add variable')

    fill_variable('ckey', 'ckeyvalue') do
      click_button('Add variable')
    end

    wait_for_requests

    # expect to find 3 rows of variables in alphabetical order
    expect(page).to have_selector('.js-ci-variable-row', count: 3)
    rows = all('.js-ci-variable-row')
    expect(rows[0].find('td[data-label="Key"]').text).to eq('ckey')
    expect(rows[1].find('td[data-label="Key"]').text).to eq('test_key')
    expect(rows[2].find('td[data-label="Key"]').text).to eq('zkey')
  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
        click_button('Add variable')

        page.within('#add-ci-variable') do
          expect(find('[data-testid="ci-variable-protected-checkbox"]')).to be_checked
        end
      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
        click_button('Add variable')

        page.within('#add-ci-variable') do
          expect(find('[data-testid="ci-variable-protected-checkbox"]')).not_to be_checked
        end
      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

  def fill_variable(key, value, protected: false, masked: false)
    wait_for_requests

    page.within('#add-ci-variable') do
      find('[data-qa-selector="ci_variable_key_field"] input').set(key)
      find('[data-qa-selector="ci_variable_value_field"]').set(value) if value.present?
      find('[data-testid="ci-variable-protected-checkbox"]').click if protected
      find('[data-testid="ci-variable-masked-checkbox"]').click if masked

      yield
    end
  end
end