summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/controllers/variables_shared_examples.rb
blob: d979683cce798ced34dad8ccfcae1abb5d12c791 (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
# frozen_string_literal: true

RSpec.shared_examples 'GET #show lists all variables' do
  it 'renders the variables as json' do
    subject

    expect(response).to match_response_schema('variables')
  end

  it 'has only one variable' do
    subject

    expect(json_response['variables'].count).to eq(1)
  end
end

RSpec.shared_examples 'PATCH #update updates variables' do
  let(:variable_attributes) do
    { id: variable.id,
      key: variable.key,
      secret_value: variable.value,
      protected: variable.protected?.to_s,
      raw: (!variable.raw?).to_s }
  end

  let(:new_variable_attributes) do
    { key: 'new_key',
      secret_value: 'dummy_value',
      protected: 'false',
      raw: 'true' }
  end

  let(:variables_scope) { owner.variables }
  let(:file_variables_scope) { owner.variables.file }

  context 'with invalid new variable parameters' do
    let(:variables_attributes) do
      [
        variable_attributes.merge(secret_value: 'other_value'),
        new_variable_attributes.merge(key: '...?')
      ]
    end

    it 'does not update the existing variable' do
      expect { subject }.not_to change { variable.reload.value }
    end

    it 'does not create the new variable' do
      expect { subject }.not_to change { variables_scope.count }
    end

    it 'returns a bad request response' do
      subject

      expect(response).to have_gitlab_http_status(:bad_request)
    end
  end

  context 'with duplicate new variable parameters' do
    let(:variables_attributes) do
      [
        new_variable_attributes,
        new_variable_attributes.merge(secret_value: 'other_value')
      ]
    end

    it 'does not update the existing variable' do
      expect { subject }.not_to change { variable.reload.value }
    end

    it 'does not create the new variable' do
      expect { subject }.not_to change { variables_scope.count }
    end

    it 'returns a bad request response' do
      subject

      expect(response).to have_gitlab_http_status(:bad_request)
    end
  end

  context 'with valid new variable parameters' do
    let(:variables_attributes) do
      [
        variable_attributes.merge(secret_value: 'other_value'),
        new_variable_attributes
      ]
    end

    it 'updates the existing variable' do
      old_raw = variable.raw

      subject

      variable.reload
      expect(variable.value).to eq('other_value')
      expect(variable.raw?).not_to be(old_raw)
    end

    it 'creates the new variable' do
      expect { subject }.to change { variables_scope.count }.by(1)
    end

    it 'returns a successful response' do
      subject

      expect(response).to have_gitlab_http_status(:ok)
    end

    it 'has all variables in response' do
      subject

      expect(response).to match_response_schema('variables')
    end
  end

  context 'with a deleted variable' do
    let(:variables_attributes) { [variable_attributes.merge(_destroy: 'true')] }

    it 'destroys the variable' do
      expect { subject }.to change { variables_scope.count }.by(-1)
      expect { variable.reload }.to raise_error ActiveRecord::RecordNotFound
    end

    it 'returns a successful response' do
      subject

      expect(response).to have_gitlab_http_status(:ok)
    end

    it 'has all variables in response' do
      subject

      expect(response).to match_response_schema('variables')
    end
  end

  context 'with missing variable' do
    let(:variables_attributes) do
      [variable_attributes.merge(_destroy: 'true', id: 'some-id')]
    end

    it 'returns not found response' do
      subject

      expect(response).to have_gitlab_http_status(:not_found)
    end
  end

  context 'for variables of type file' do
    let(:variables_attributes) do
      [
        new_variable_attributes.merge(variable_type: 'file')
      ]
    end

    it 'creates new variable of type file' do
      expect { subject }.to change { file_variables_scope.count }.by(1)
    end
  end
end