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
|
# frozen_string_literal: true
require 'spec_helper'
describe API::Terraform::State do
let_it_be(:project) { create(:project) }
let_it_be(:developer) { create(:user, developer_projects: [project]) }
let_it_be(:maintainer) { create(:user, maintainer_projects: [project]) }
let!(:state) { create(:terraform_state, :with_file, project: project) }
let(:current_user) { maintainer }
let(:auth_header) { basic_auth_header(current_user) }
let(:project_id) { project.id }
let(:state_name) { state.name }
let(:state_path) { "/projects/#{project_id}/terraform/state/#{state_name}" }
before do
stub_terraform_state_object_storage(Terraform::StateUploader)
end
describe 'GET /projects/:id/terraform/state/:name' do
subject(:request) { get api(state_path), headers: auth_header }
context 'without authentication' do
let(:auth_header) { basic_auth_header('failing_token') }
it 'returns 401 if user is not authenticated' do
request
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
context 'with maintainer permissions' do
let(:current_user) { maintainer }
it 'returns terraform state belonging to a project of given state name' do
request
expect(response).to have_gitlab_http_status(:ok)
expect(response.body).to eq(state.file.read)
end
context 'for a project that does not exist' do
let(:project_id) { '0000' }
it 'returns not found' do
request
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
context 'with developer permissions' do
let(:current_user) { developer }
it 'returns forbidden if the user cannot access the state' do
request
expect(response).to have_gitlab_http_status(:forbidden)
end
end
end
describe 'POST /projects/:id/terraform/state/:name' do
let(:params) { { 'instance': 'example-instance' } }
subject(:request) { post api(state_path), headers: auth_header, as: :json, params: params }
context 'when terraform state with a given name is already present' do
context 'with maintainer permissions' do
let(:current_user) { maintainer }
it 'updates the state' do
expect { request }.to change { Terraform::State.count }.by(0)
expect(response).to have_gitlab_http_status(:ok)
end
context 'on Unicorn', :unicorn do
it 'updates the state' do
expect { request }.to change { Terraform::State.count }.by(0)
expect(response).to have_gitlab_http_status(:ok)
end
end
end
context 'without body' do
let(:params) { nil }
it 'returns no content if no body is provided' do
request
expect(response).to have_gitlab_http_status(:no_content)
end
end
context 'with developer permissions' do
let(:current_user) { developer }
it 'returns forbidden' do
request
expect(response).to have_gitlab_http_status(:forbidden)
end
end
end
context 'when there is no terraform state of a given name' do
let(:state_name) { 'example2' }
context 'with maintainer permissions' do
let(:current_user) { maintainer }
it 'creates a new state' do
expect { request }.to change { Terraform::State.count }.by(1)
expect(response).to have_gitlab_http_status(:ok)
end
context 'on Unicorn', :unicorn do
it 'creates a new state' do
expect { request }.to change { Terraform::State.count }.by(1)
expect(response).to have_gitlab_http_status(:ok)
end
end
end
context 'without body' do
let(:params) { nil }
it 'returns no content if no body is provided' do
request
expect(response).to have_gitlab_http_status(:no_content)
end
end
context 'with developer permissions' do
let(:current_user) { developer }
it 'returns forbidden' do
request
expect(response).to have_gitlab_http_status(:forbidden)
end
end
end
end
describe 'DELETE /projects/:id/terraform/state/:name' do
subject(:request) { delete api(state_path), headers: auth_header }
context 'with maintainer permissions' do
let(:current_user) { maintainer }
it 'deletes the state' do
expect { request }.to change { Terraform::State.count }.by(-1)
expect(response).to have_gitlab_http_status(:ok)
end
end
context 'with developer permissions' do
let(:current_user) { developer }
it 'returns forbidden' do
expect { request }.to change { Terraform::State.count }.by(0)
expect(response).to have_gitlab_http_status(:forbidden)
end
end
end
describe 'PUT /projects/:id/terraform/state/:name/lock' do
let(:params) do
{
ID: '123-456',
Version: '0.1',
Operation: 'OperationTypePlan',
Info: '',
Who: "#{current_user.username}",
Created: Time.now.utc.iso8601(6),
Path: ''
}
end
subject(:request) { post api("#{state_path}/lock"), headers: auth_header, params: params }
it 'locks the terraform state' do
request
expect(response).to have_gitlab_http_status(:ok)
end
end
describe 'DELETE /projects/:id/terraform/state/:name/lock' do
before do
state.lock_xid = '123-456'
state.save!
end
subject(:request) { delete api("#{state_path}/lock"), headers: auth_header, params: params }
context 'with the correct lock id' do
let(:params) { { ID: '123-456' } }
it 'removes the terraform state lock' do
request
expect(response).to have_gitlab_http_status(:ok)
end
end
context 'with no lock id (force-unlock)' do
let(:params) { {} }
it 'removes the terraform state lock' do
request
expect(response).to have_gitlab_http_status(:ok)
end
end
context 'with an incorrect lock id' do
let(:params) { { ID: '456-789' } }
it 'returns an error' do
request
expect(response).to have_gitlab_http_status(:conflict)
end
end
context 'with a longer than 255 character lock id' do
let(:params) { { ID: '0' * 256 } }
it 'returns an error' do
request
expect(response).to have_gitlab_http_status(:bad_request)
end
end
end
end
|