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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'RunnerCreate', feature_category: :runner_fleet do
include GraphqlHelpers
let_it_be(:user) { create(:user) }
let_it_be(:group_owner) { create(:user) }
let_it_be(:admin) { create(:admin) }
let_it_be(:group) { create(:group) }
let_it_be(:other_group) { create(:group) }
let(:mutation_params) do
{
description: 'create description',
maintenance_note: 'create maintenance note',
maximum_timeout: 900,
access_level: 'REF_PROTECTED',
paused: true,
run_untagged: false,
tag_list: %w[tag1 tag2]
}.deep_merge(mutation_scope_params)
end
let(:mutation) do
variables = {
**mutation_params
}
graphql_mutation(
:runner_create,
variables,
<<-QL
runner {
ephemeralAuthenticationToken
runnerType
description
maintenanceNote
paused
tagList
accessLevel
locked
maximumTimeout
runUntagged
}
errors
QL
)
end
let(:mutation_response) { graphql_mutation_response(:runner_create) }
before do
group.add_owner(group_owner)
end
shared_context 'when model is invalid returns error' do
let(:mutation_params) do
{
description: '',
maintenanceNote: '',
paused: true,
accessLevel: 'NOT_PROTECTED',
runUntagged: false,
tagList: [],
maximumTimeout: 1
}.deep_merge(mutation_scope_params)
end
it do
post_graphql_mutation(mutation, current_user: current_user)
expect(response).to have_gitlab_http_status(:success)
expect(mutation_response['errors']).to contain_exactly(
'Tags list can not be empty when runner is not allowed to pick untagged jobs',
'Maximum timeout needs to be at least 10 minutes'
)
end
end
shared_context 'when user does not have permissions' do
let(:current_user) { user }
it 'returns an error' do
post_graphql_mutation(mutation, current_user: current_user)
expect_graphql_errors_to_include(
'The resource that you are attempting to access does not exist ' \
"or you don't have permission to perform this action"
)
end
end
shared_examples 'when runner is created successfully' do
it do
expected_args = { user: current_user, params: anything }
expect_next_instance_of(::Ci::Runners::CreateRunnerService, expected_args) do |service|
expect(service).to receive(:execute).and_call_original
end
post_graphql_mutation(mutation, current_user: current_user)
expect(response).to have_gitlab_http_status(:success)
expect(mutation_response['errors']).to eq([])
expect(mutation_response['runner']).not_to be_nil
mutation_params.except(:group_id, :project_id).each_key do |key|
expect(mutation_response['runner'][key.to_s.camelize(:lower)]).to eq mutation_params[key]
end
expect(mutation_response['runner']['ephemeralAuthenticationToken'])
.to start_with Ci::Runner::CREATED_RUNNER_TOKEN_PREFIX
end
end
context 'when runnerType is INSTANCE_TYPE' do
let(:mutation_scope_params) do
{ runner_type: 'INSTANCE_TYPE' }
end
it_behaves_like 'when user does not have permissions'
context 'when user has permissions', :enable_admin_mode do
let(:current_user) { admin }
it_behaves_like 'when runner is created successfully'
it_behaves_like 'when model is invalid returns error'
end
end
context 'when runnerType is GROUP_TYPE' do
let(:mutation_scope_params) do
{
runner_type: 'GROUP_TYPE',
group_id: group.to_global_id
}
end
it_behaves_like 'when user does not have permissions'
context 'when user has permissions' do
context 'when user is group owner' do
let(:current_user) { group_owner }
it_behaves_like 'when runner is created successfully'
it_behaves_like 'when model is invalid returns error'
context 'when group_id is missing' do
let(:mutation_scope_params) do
{ runner_type: 'GROUP_TYPE' }
end
it 'returns an error' do
post_graphql_mutation(mutation, current_user: current_user)
expect_graphql_errors_to_include('`group_id` is missing')
end
end
context 'when group_id is malformed' do
let(:mutation_scope_params) do
{
runner_type: 'GROUP_TYPE',
group_id: ''
}
end
it 'returns an error' do
post_graphql_mutation(mutation, current_user: current_user)
expect_graphql_errors_to_include(
"RunnerCreateInput! was provided invalid value for groupId"
)
end
end
context 'when group_id does not exist' do
let(:mutation_scope_params) do
{
runner_type: 'GROUP_TYPE',
group_id: "gid://gitlab/Group/#{non_existing_record_id}"
}
end
it 'returns an error' do
post_graphql_mutation(mutation, current_user: current_user)
expect(flattened_errors).not_to be_empty
end
end
end
context 'when user is admin in admin mode', :enable_admin_mode do
let(:current_user) { admin }
it_behaves_like 'when runner is created successfully'
it_behaves_like 'when model is invalid returns error'
end
end
end
context 'when runnerType is PROJECT_TYPE' do
let_it_be(:project) { create(:project, namespace: group) }
let(:mutation_scope_params) do
{
runner_type: 'PROJECT_TYPE',
project_id: project.to_global_id
}
end
it_behaves_like 'when user does not have permissions'
context 'when user has permissions' do
context 'when user is group owner' do
let(:current_user) { group_owner }
it_behaves_like 'when runner is created successfully'
it_behaves_like 'when model is invalid returns error'
context 'when project_id is missing' do
let(:mutation_scope_params) do
{ runner_type: 'PROJECT_TYPE' }
end
it 'returns an error' do
post_graphql_mutation(mutation, current_user: current_user)
expect_graphql_errors_to_include('`project_id` is missing')
end
end
context 'when project_id is malformed' do
let(:mutation_scope_params) do
{
runner_type: 'PROJECT_TYPE',
project_id: ''
}
end
it 'returns an error' do
post_graphql_mutation(mutation, current_user: current_user)
expect_graphql_errors_to_include(
"RunnerCreateInput! was provided invalid value for projectId"
)
end
end
context 'when project_id does not exist' do
let(:mutation_scope_params) do
{
runner_type: 'PROJECT_TYPE',
project_id: "gid://gitlab/Project/#{non_existing_record_id}"
}
end
it 'returns an error' do
post_graphql_mutation(mutation, current_user: current_user)
expect_graphql_errors_to_include(
'The resource that you are attempting to access does not exist ' \
"or you don't have permission to perform this action"
)
end
end
end
context 'when user is admin in admin mode', :enable_admin_mode do
let(:current_user) { admin }
it_behaves_like 'when runner is created successfully'
it_behaves_like 'when model is invalid returns error'
end
end
end
end
|