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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Ci::JobsFinder, '#execute', feature_category: :continuous_integration do
let_it_be(:user) { create(:user) }
let_it_be(:admin) { create(:user, :admin) }
let_it_be(:project) { create(:project, :private, public_builds: false) }
let_it_be(:pipeline) { create(:ci_pipeline, project: project) }
let_it_be(:pending_job) { create(:ci_build, :pending) }
let_it_be(:running_job) { create(:ci_build, :running) }
let_it_be(:successful_job) { create(:ci_build, :success, pipeline: pipeline, name: 'build') }
let(:params) { {} }
context 'when project, pipeline, and runner are blank' do
subject(:finder_execute) { described_class.new(current_user: current_user, params: params).execute }
context 'with admin' do
let(:current_user) { admin }
context 'when admin mode setting is disabled', :do_not_mock_admin_mode_setting do
it { is_expected.to match_array([pending_job, running_job, successful_job]) }
end
context 'when admin mode setting is enabled' do
context 'when in admin mode', :enable_admin_mode do
it { is_expected.to match_array([pending_job, running_job, successful_job]) }
end
context 'when not in admin mode' do
it { is_expected.to be_empty }
end
end
end
context 'with admin and admin mode enabled', :enable_admin_mode do
let(:current_user) { admin }
context 'with param `scope`' do
using RSpec::Parameterized::TableSyntax
where(:scope, :expected_jobs) do
'pending' | lazy { [pending_job] }
'running' | lazy { [running_job] }
'finished' | lazy { [successful_job] }
%w[running success] | lazy { [running_job, successful_job] }
end
with_them do
let(:params) { { scope: scope } }
it { is_expected.to match_array(expected_jobs) }
end
end
context 'with param `runner_type`' do
let_it_be(:job_with_group_runner) { create(:ci_build, :success, runner: create(:ci_runner, :group)) }
let_it_be(:job_with_instance_runner) { create(:ci_build, :success, runner: create(:ci_runner, :instance)) }
let_it_be(:job_with_project_runner) { create(:ci_build, :success, runner: create(:ci_runner, :project)) }
context 'with feature flag :admin_jobs_filter_runner_type enabled' do
using RSpec::Parameterized::TableSyntax
where(:runner_type, :expected_jobs) do
'group_type' | lazy { [job_with_group_runner] }
'instance_type' | lazy { [job_with_instance_runner] }
'project_type' | lazy { [job_with_project_runner] }
%w[instance_type project_type] | lazy { [job_with_instance_runner, job_with_project_runner] }
end
with_them do
let(:params) { { runner_type: runner_type } }
it { is_expected.to match_array(expected_jobs) }
end
end
context 'with feature flag :admin_jobs_filter_runner_type disabled' do
let(:params) { { runner_type: 'instance_type' } }
let(:expected_jobs) do
[
job_with_group_runner,
job_with_instance_runner,
job_with_project_runner,
pending_job,
running_job,
successful_job
]
end
before do
stub_feature_flags(admin_jobs_filter_runner_type: false)
end
it { is_expected.to match_array(expected_jobs) }
end
end
context "with params" do
let_it_be(:job_with_running_status_and_group_runner) do
create(:ci_build, :running, runner: create(:ci_runner, :group))
end
let_it_be(:job_with_instance_runner) { create(:ci_build, :success, runner: create(:ci_runner, :instance)) }
let_it_be(:job_with_project_runner) { create(:ci_build, :success, runner: create(:ci_runner, :project)) }
context 'with feature flag :admin_jobs_filter_runner_type enabled' do
using RSpec::Parameterized::TableSyntax
where(:param_runner_type, :param_scope, :expected_jobs) do
'group_type' | 'running' | lazy { [job_with_running_status_and_group_runner] }
%w[instance_type project_type] | 'finished' | lazy { [job_with_instance_runner, job_with_project_runner] }
%w[instance_type project_type] | 'pending' | lazy { [] }
end
with_them do
let(:params) { { runner_type: param_runner_type, scope: param_scope } }
it { is_expected.to match_array(expected_jobs) }
end
end
context 'with feature flag :admin_jobs_filter_runner_type disabled' do
before do
stub_feature_flags(admin_jobs_filter_runner_type: false)
end
using RSpec::Parameterized::TableSyntax
where(:param_runner_type, :param_scope, :expected_jobs) do
'group_type' | 'running' | lazy do
[job_with_running_status_and_group_runner, running_job]
end
%w[instance_type project_type] | 'finished' | lazy do
[
job_with_instance_runner,
job_with_project_runner,
successful_job
]
end
%w[instance_type project_type] | 'pending' | lazy { [pending_job] }
end
with_them do
let(:params) { { runner_type: param_runner_type, scope: param_scope } }
it { is_expected.to match_array(expected_jobs) }
end
end
end
end
context 'with user not being project member' do
let(:current_user) { user }
it { is_expected.to be_empty }
end
context 'without user' do
let(:current_user) { nil }
it { is_expected.to be_empty }
end
end
context 'when project is present' do
subject { described_class.new(current_user: user, project: project, params: params).execute }
context 'with user being project maintainer' do
before do
project.add_maintainer(user)
end
it 'returns jobs for the specified project' do
expect(subject).to match_array([successful_job])
end
context 'when artifacts are present for some jobs' do
let_it_be(:job_with_artifacts) { create(:ci_build, :success, pipeline: pipeline, name: 'test') }
let_it_be(:artifact) { create(:ci_job_artifact, job: job_with_artifacts) }
context 'when with_artifacts is true' do
let(:params) { { with_artifacts: true } }
it 'returns only jobs with artifacts' do
expect(subject).to match_array([job_with_artifacts])
end
end
context 'when with_artifacts is false' do
let(:params) { { with_artifacts: false } }
it 'returns all jobs' do
expect(subject).to match_array([successful_job, job_with_artifacts])
end
end
context "with param `scope" do
using RSpec::Parameterized::TableSyntax
where(:param_scope, :expected_jobs) do
'success' | lazy { [successful_job, job_with_artifacts] }
'[success pending]' | lazy { [successful_job, job_with_artifacts] }
'pending' | lazy { [] }
nil | lazy { [successful_job, job_with_artifacts] }
end
with_them do
let(:params) { { with_artifacts: false, scope: param_scope } }
it { is_expected.to match_array(expected_jobs) }
end
end
end
end
context 'with user being project guest' do
before do
project.add_guest(user)
end
it 'returns no jobs' do
expect(subject).to be_empty
end
end
context 'without user' do
let(:user) { nil }
it 'returns no jobs' do
expect(subject).to be_empty
end
end
end
context 'when pipeline is present' do
subject { described_class.new(current_user: user, pipeline: pipeline, params: params).execute }
context 'with user being project maintainer' do
before_all do
project.add_maintainer(user)
successful_job.update!(retried: true)
end
let_it_be(:job_4) { create(:ci_build, :success, pipeline: pipeline, name: 'build') }
it 'does not return retried jobs by default' do
expect(subject).to match_array([job_4])
end
context 'when include_retried is false' do
let(:params) { { include_retried: false } }
it 'does not return retried jobs' do
expect(subject).to match_array([job_4])
end
end
context 'when include_retried is true' do
let(:params) { { include_retried: true } }
it 'returns retried jobs' do
expect(subject).to match_array([successful_job, job_4])
end
end
end
context 'without user' do
let(:user) { nil }
it 'returns no jobs' do
expect(subject).to be_empty
end
end
end
context 'when runner is present' do
let_it_be(:runner) { create(:ci_runner, :project, projects: [project]) }
let_it_be(:job_4) { create(:ci_build, :success, runner: runner) }
subject(:execute) { described_class.new(current_user: user, runner: runner, params: params).execute }
context 'when current user is an admin' do
let(:user) { admin }
context 'when admin mode is enabled', :enable_admin_mode do
it 'returns jobs for the specified project' do
expect(subject).to contain_exactly job_4
end
context 'with params' do
using RSpec::Parameterized::TableSyntax
where(:param_runner_type, :param_scope, :expected_jobs) do
'project_type' | 'success' | lazy { [job_4] }
'instance_type' | nil | lazy { [] }
nil | 'pending' | lazy { [] }
end
with_them do
let(:params) { { runner_type: param_runner_type, scope: param_scope } }
it { is_expected.to match_array(expected_jobs) }
end
end
end
end
context 'with user being project guest' do
let_it_be(:guest) { create(:user) }
let(:user) { guest }
before do
project.add_guest(guest)
end
it 'returns no jobs' do
expect(subject).to be_empty
end
end
context 'without user' do
let(:user) { nil }
it 'returns no jobs' do
expect(subject).to be_empty
end
end
end
end
|