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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Projects::GoogleCloud::ServiceAccountsController, feature_category: :kubernetes_management do
let_it_be(:project) { create(:project, :public) }
describe 'GET index', :snowplow do
let_it_be(:url) { project_google_cloud_service_accounts_path(project).to_s }
let_it_be(:user_guest) { create(:user) }
let_it_be(:user_developer) { create(:user) }
let_it_be(:user_maintainer) { create(:user) }
let_it_be(:user_creator) { project.creator }
let_it_be(:unauthorized_members) { [user_guest, user_developer] }
let_it_be(:authorized_members) { [user_maintainer, user_creator] }
let_it_be(:google_client_error) { Google::Apis::ClientError.new('client-error') }
before do
project.add_guest(user_guest)
project.add_developer(user_developer)
project.add_maintainer(user_maintainer)
end
context 'when a public request is made' do
it 'returns not found on GET request' do
get url
expect(response).to have_gitlab_http_status(:not_found)
expect_snowplow_event(
category: 'Projects::GoogleCloud::ServiceAccountsController',
action: 'error_invalid_user',
label: nil,
project: project,
user: nil
)
end
it 'returns not found on POST request' do
post url
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'when unauthorized members make requests' do
it 'returns not found on GET request' do
unauthorized_members.each do |unauthorized_member|
sign_in(unauthorized_member)
get url
expect_snowplow_event(
category: 'Projects::GoogleCloud::ServiceAccountsController',
action: 'error_invalid_user',
label: nil,
project: project,
user: unauthorized_member
)
expect(response).to have_gitlab_http_status(:not_found)
end
end
it 'returns not found on POST request' do
unauthorized_members.each do |unauthorized_member|
sign_in(unauthorized_member)
post url
expect_snowplow_event(
category: 'Projects::GoogleCloud::ServiceAccountsController',
action: 'error_invalid_user',
label: nil,
project: project,
user: unauthorized_member
)
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
context 'when authorized members make requests' do
it 'redirects on GET request' do
authorized_members.each do |authorized_member|
sign_in(authorized_member)
get url
expect(response).to redirect_to(assigns(:authorize_url))
end
end
it 'redirects on POST request' do
authorized_members.each do |authorized_member|
sign_in(authorized_member)
post url
expect(response).to redirect_to(assigns(:authorize_url))
end
end
context 'and user has successfully completed the google oauth2 flow' do
context 'but the user does not have gcp projects' do
before do
allow_next_instance_of(GoogleApi::CloudPlatform::Client) do |client|
mock_service_account = Struct.new(:project_id, :unique_id, :email).new(123, 456, 'em@ai.l')
allow(client).to receive(:list_projects).and_return([])
allow(client).to receive(:validate_token).and_return(true)
allow(client).to receive(:create_service_account).and_return(mock_service_account)
allow(client).to receive(:create_service_account_key).and_return({})
allow(client).to receive(:grant_service_account_roles)
end
end
it 'flashes error and redirects to google cloud configurations' do
authorized_members.each do |authorized_member|
allow_next_instance_of(BranchesFinder) do |branches_finder|
allow(branches_finder).to receive(:execute).and_return([])
end
allow_next_instance_of(TagsFinder) do |branches_finder|
allow(branches_finder).to receive(:execute).and_return([])
end
sign_in(authorized_member)
get url
expect(response).to redirect_to(project_google_cloud_configuration_path(project))
expect(flash[:warning]).to eq('No Google Cloud projects - You need at least one Google Cloud project')
expect_snowplow_event(
category: 'Projects::GoogleCloud::ServiceAccountsController',
action: 'error_no_gcp_projects',
label: nil,
project: project,
user: authorized_member
)
end
end
end
context 'user has three gcp_projects' do
before do
allow_next_instance_of(GoogleApi::CloudPlatform::Client) do |client|
mock_service_account = Struct.new(:project_id, :unique_id, :email).new(123, 456, 'em@ai.l')
allow(client).to receive(:list_projects).and_return([{}, {}, {}])
allow(client).to receive(:validate_token).and_return(true)
allow(client).to receive(:create_service_account).and_return(mock_service_account)
allow(client).to receive(:create_service_account_key).and_return({})
allow(client).to receive(:grant_service_account_roles)
end
end
it 'returns success on GET' do
authorized_members.each do |authorized_member|
allow_next_instance_of(BranchesFinder) do |branches_finder|
allow(branches_finder).to receive(:execute).and_return([])
end
allow_next_instance_of(TagsFinder) do |branches_finder|
allow(branches_finder).to receive(:execute).and_return([])
end
sign_in(authorized_member)
get url
expect(response).to have_gitlab_http_status(:ok)
end
end
it 'returns success on POST' do
authorized_members.each do |authorized_member|
sign_in(authorized_member)
post url, params: { gcp_project: 'prj1', ref: 'env1' }
expect(response).to redirect_to(project_google_cloud_configuration_path(project))
end
end
end
end
context 'but google returns client error' do
before do
allow_next_instance_of(GoogleApi::CloudPlatform::Client) do |client|
allow(client).to receive(:validate_token).and_return(true)
allow(client).to receive(:list_projects).and_raise(google_client_error)
allow(client).to receive(:create_service_account).and_raise(google_client_error)
allow(client).to receive(:create_service_account_key).and_raise(google_client_error)
end
end
it 'GET flashes error and redirects to -/google_cloud/configurations' do
authorized_members.each do |authorized_member|
sign_in(authorized_member)
get url
expect(response).to redirect_to(project_google_cloud_configuration_path(project))
expect(flash[:warning]).to eq('Google Cloud Error - client-error')
expect_snowplow_event(
category: 'Projects::GoogleCloud::ServiceAccountsController',
action: 'error_google_api',
project: project,
label: nil,
user: authorized_member
)
end
end
it 'POST flashes error and redirects to -/google_cloud/configurations' do
authorized_members.each do |authorized_member|
sign_in(authorized_member)
post url, params: { gcp_project: 'prj1', environment: 'env1' }
expect(response).to redirect_to(project_google_cloud_configuration_path(project))
expect(flash[:warning]).to eq('Google Cloud Error - client-error')
expect_snowplow_event(
category: 'Projects::GoogleCloud::ServiceAccountsController',
action: 'error_google_api',
label: nil,
project: project,
user: authorized_member
)
end
end
end
context 'but gitlab instance is not configured for google oauth2' do
before do
unconfigured_google_oauth2 = Struct.new(:app_id, :app_secret)
.new('', '')
allow(Gitlab::Auth::OAuth::Provider).to receive(:config_for)
.with('google_oauth2')
.and_return(unconfigured_google_oauth2)
end
it 'returns forbidden' do
authorized_members.each do |authorized_member|
sign_in(authorized_member)
get url
expect(response).to have_gitlab_http_status(:forbidden)
end
end
end
context 'but feature flag is disabled' do
before do
stub_feature_flags(incubation_5mp_google_cloud: false)
end
it 'returns not found' do
authorized_members.each do |authorized_member|
sign_in(authorized_member)
get url
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
end
end
end
|