2019-11-25 19:06:28 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-16 14:09:01 -04:00
|
|
|
RSpec.describe Ci::JobsFinder, '#execute' do
|
2020-02-16 22:09:00 -05:00
|
|
|
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) }
|
2022-03-21 20:07:23 -04:00
|
|
|
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') }
|
2019-11-25 19:06:28 -05:00
|
|
|
|
|
|
|
let(:params) { {} }
|
|
|
|
|
|
|
|
context 'no project' do
|
|
|
|
subject { described_class.new(current_user: admin, params: params).execute }
|
|
|
|
|
|
|
|
it 'returns all jobs' do
|
2022-03-21 20:07:23 -04:00
|
|
|
expect(subject).to match_array([pending_job, running_job, successful_job])
|
2019-11-25 19:06:28 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'non admin user' do
|
|
|
|
let(:admin) { user }
|
|
|
|
|
|
|
|
it 'returns no jobs' do
|
|
|
|
expect(subject).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without user' do
|
|
|
|
let(:admin) { nil }
|
|
|
|
|
|
|
|
it 'returns no jobs' do
|
|
|
|
expect(subject).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-21 05:08:50 -04:00
|
|
|
context 'scope is present' do
|
2022-03-21 20:07:23 -04:00
|
|
|
let(:jobs) { [pending_job, running_job, successful_job] }
|
2020-10-21 05:08:50 -04:00
|
|
|
|
|
|
|
where(:scope, :index) do
|
|
|
|
[
|
|
|
|
['pending', 0],
|
|
|
|
['running', 1],
|
|
|
|
['finished', 2]
|
|
|
|
]
|
2019-11-25 19:06:28 -05:00
|
|
|
end
|
|
|
|
|
2020-10-21 05:08:50 -04:00
|
|
|
with_them do
|
|
|
|
let(:params) { { scope: scope } }
|
2019-11-25 19:06:28 -05:00
|
|
|
|
2020-10-21 05:08:50 -04:00
|
|
|
it { expect(subject).to match_array([jobs[index]]) }
|
2020-09-11 08:08:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-21 05:08:50 -04:00
|
|
|
context 'scope is an array' do
|
2022-03-21 20:07:23 -04:00
|
|
|
let(:jobs) { [pending_job, running_job, successful_job, canceled_job] }
|
|
|
|
let(:params) {{ scope: %w'running success' }}
|
2020-09-11 08:08:50 -04:00
|
|
|
|
2020-10-21 05:08:50 -04:00
|
|
|
it 'filters by the job statuses in the scope' do
|
2022-03-21 20:07:23 -04:00
|
|
|
expect(subject).to contain_exactly(running_job, successful_job)
|
2019-11-25 19:06:28 -05:00
|
|
|
end
|
|
|
|
end
|
2020-09-11 08:08:50 -04:00
|
|
|
end
|
2019-11-25 19:06:28 -05:00
|
|
|
|
2020-10-21 05:08:50 -04:00
|
|
|
context 'a project is present' do
|
|
|
|
subject { described_class.new(current_user: user, project: project, params: params).execute }
|
2019-11-25 19:06:28 -05:00
|
|
|
|
2020-10-21 05:08:50 -04:00
|
|
|
context 'user has access to the project' do
|
|
|
|
before do
|
|
|
|
project.add_maintainer(user)
|
2020-09-11 08:08:50 -04:00
|
|
|
end
|
|
|
|
|
2020-10-21 05:08:50 -04:00
|
|
|
it 'returns jobs for the specified project' do
|
2022-03-21 20:07:23 -04:00
|
|
|
expect(subject).to match_array([successful_job])
|
2019-11-25 19:06:28 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-21 05:08:50 -04:00
|
|
|
context 'user has no access to project builds' do
|
|
|
|
before do
|
|
|
|
project.add_guest(user)
|
2020-09-11 08:08:50 -04:00
|
|
|
end
|
|
|
|
|
2020-10-21 05:08:50 -04:00
|
|
|
it 'returns no jobs' do
|
|
|
|
expect(subject).to be_empty
|
2020-09-11 08:08:50 -04:00
|
|
|
end
|
2020-10-21 05:08:50 -04:00
|
|
|
end
|
2020-09-11 08:08:50 -04:00
|
|
|
|
2020-10-21 05:08:50 -04:00
|
|
|
context 'without user' do
|
|
|
|
let(:user) { nil }
|
2020-09-11 08:08:50 -04:00
|
|
|
|
2020-10-21 05:08:50 -04:00
|
|
|
it 'returns no jobs' do
|
|
|
|
expect(subject).to be_empty
|
2019-11-25 19:06:28 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-02-16 13:09:24 -05:00
|
|
|
|
|
|
|
context 'when pipeline is present' do
|
|
|
|
before_all do
|
|
|
|
project.add_maintainer(user)
|
2022-03-21 20:07:23 -04:00
|
|
|
successful_job.update!(retried: true)
|
2021-02-16 13:09:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
let_it_be(:job_4) { create(:ci_build, :success, pipeline: pipeline, name: 'build') }
|
|
|
|
|
|
|
|
subject { described_class.new(current_user: user, pipeline: pipeline, params: params).execute }
|
|
|
|
|
|
|
|
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
|
2022-03-21 20:07:23 -04:00
|
|
|
expect(subject).to match_array([successful_job, job_4])
|
2021-02-16 13:09:24 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-01-28 22:15:22 -05:00
|
|
|
|
|
|
|
context 'a 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 { described_class.new(current_user: user, runner: runner, params: params).execute }
|
|
|
|
|
|
|
|
context 'user has access to the runner', :enable_admin_mode do
|
|
|
|
let(:user) { admin }
|
|
|
|
|
|
|
|
it 'returns jobs for the specified project' do
|
|
|
|
expect(subject).to match_array([job_4])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'user has no access to project builds' 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
|
2019-11-25 19:06:28 -05:00
|
|
|
end
|