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
|
require 'spec_helper'
describe Projects::ClustersController do
set(:user) { create(:user) }
set(:project) { create(:project) }
let(:role) { :master }
before do
project.team << [user, role]
sign_in(user)
end
describe 'GET index' do
subject do
get :index, namespace_id: project.namespace,
project_id: project
end
context 'when cluster is already created' do
let!(:cluster) { create(:gcp_cluster, :created_on_gke, project: project) }
it 'redirects to show a cluster' do
subject
expect(response).to redirect_to(project_cluster_path(project, cluster))
end
end
context 'when we do not have cluster' do
it 'redirects to create a cluster' do
subject
expect(response).to redirect_to(new_project_cluster_path(project))
end
end
end
describe 'GET login' do
render_views
subject do
get :login, namespace_id: project.namespace,
project_id: project
end
context 'when we do have omniauth configured' do
it 'shows login button' do
subject
expect(response.body).to include('auth_buttons/signin_with_google')
end
end
context 'when we do not have omniauth configured' do
before do
stub_omniauth_setting(providers: [])
end
it 'shows notice message' do
subject
expect(response.body).to include('Ask your GitLab administrator if you want to use this service.')
end
end
end
shared_examples 'requires to login' do
it 'redirects to create a cluster' do
subject
expect(response).to redirect_to(login_project_clusters_path(project))
end
end
describe 'GET new' do
render_views
subject do
get :new, namespace_id: project.namespace,
project_id: project
end
context 'when logged' do
before do
make_logged_in
end
it 'shows a creation form' do
subject
expect(response.body).to include('Create cluster')
end
end
context 'when not logged' do
it_behaves_like 'requires to login'
end
end
describe 'POST create' do
subject do
post :create, params.merge(namespace_id: project.namespace,
project_id: project)
end
context 'when not logged' do
let(:params) { {} }
it_behaves_like 'requires to login'
end
context 'when logged in' do
before do
make_logged_in
end
context 'when all required parameters are set' do
let(:params) do
{
cluster: {
gcp_cluster_name: 'new-cluster',
gcp_project_id: '111'
}
}
end
before do
expect(ClusterProvisionWorker).to receive(:perform_async) { }
end
it 'creates a new cluster' do
expect { subject }.to change { Gcp::Cluster.count }
expect(response).to redirect_to(project_cluster_path(project, project.cluster))
end
end
context 'when not all required parameters are set' do
render_views
let(:params) do
{
cluster: {
project_namespace: 'some namespace'
}
}
end
it 'shows an error message' do
expect { subject }.not_to change { Gcp::Cluster.count }
expect(response).to render_template(:new)
end
end
end
end
describe 'GET status' do
let(:cluster) { create(:gcp_cluster, :created_on_gke, project: project) }
subject do
get :status, namespace_id: project.namespace,
project_id: project,
id: cluster,
format: :json
end
it "responds with matching schema" do
subject
expect(response).to have_http_status(:ok)
expect(response).to match_response_schema('cluster_status')
end
end
describe 'GET show' do
render_views
let(:cluster) { create(:gcp_cluster, :created_on_gke, project: project) }
subject do
get :show, namespace_id: project.namespace,
project_id: project,
id: cluster
end
context 'when logged as master' do
it "allows to update cluster" do
subject
expect(response).to have_http_status(:ok)
expect(response.body).to include("Save changes")
end
it "allows remove integration" do
subject
expect(response).to have_http_status(:ok)
expect(response.body).to include("Remove integration")
end
end
context 'when logged as developer' do
let(:role) { :developer }
it "does not allow to access page" do
subject
expect(response).to have_http_status(:not_found)
end
end
end
describe 'PUT update' do
render_views
let(:service) { project.build_kubernetes_service }
let(:cluster) { create(:gcp_cluster, :created_on_gke, project: project, service: service) }
let(:params) { {} }
subject do
put :update, params.merge(namespace_id: project.namespace,
project_id: project,
id: cluster)
end
context 'when logged as master' do
context 'when valid params are used' do
let(:params) do
{
cluster: { enabled: false }
}
end
it "redirects back to show page" do
subject
expect(response).to redirect_to(project_cluster_path(project, project.cluster))
expect(flash[:notice]).to eq('Cluster was successfully updated.')
end
end
context 'when invalid params are used' do
let(:params) do
{
cluster: { project_namespace: 'my Namespace 321321321 #' }
}
end
it "rejects changes" do
subject
expect(response).to have_http_status(:ok)
expect(response).to render_template(:show)
end
end
end
context 'when logged as developer' do
let(:role) { :developer }
it "does not allow to update cluster" do
subject
expect(response).to have_http_status(:not_found)
end
end
end
describe 'delete update' do
let(:cluster) { create(:gcp_cluster, :created_on_gke, project: project) }
subject do
delete :destroy, namespace_id: project.namespace,
project_id: project,
id: cluster
end
context 'when logged as master' do
it "redirects back to clusters list" do
subject
expect(response).to redirect_to(project_clusters_path(project))
expect(flash[:notice]).to eq('Cluster integration was successfully removed.')
end
end
context 'when logged as developer' do
let(:role) { :developer }
it "does not allow to destroy cluster" do
subject
expect(response).to have_http_status(:not_found)
end
end
end
def make_logged_in
session[GoogleApi::CloudPlatform::Client.session_key_for_token] = '1234'
session[GoogleApi::CloudPlatform::Client.session_key_for_expires_at] = in_hour.to_i.to_s
end
def in_hour
Time.now + 1.hour
end
end
|