gitlab-org--gitlab-foss/spec/finders/pipelines_finder_spec.rb

259 lines
8.1 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2016-08-29 12:02:08 -04:00
require 'spec_helper'
describe PipelinesFinder do
let(:project) { create(:project, :public, :repository) }
let(:current_user) { nil }
let(:params) { {} }
subject { described_class.new(project, current_user, params).execute }
2016-08-29 12:02:08 -04:00
describe "#execute" do
2017-03-27 06:12:09 -04:00
context 'when params is empty' do
2017-02-28 12:34:48 -05:00
let(:params) { {} }
2017-03-27 06:12:09 -04:00
let!(:pipelines) { create_list(:ci_pipeline, 2, project: project) }
2017-02-28 12:34:48 -05:00
2017-03-14 12:42:31 -04:00
it 'returns all pipelines' do
2017-04-27 09:23:06 -04:00
is_expected.to match_array(pipelines)
2017-02-28 12:34:48 -05:00
end
end
2017-03-27 06:12:09 -04:00
%w[running pending].each do |target|
context "when scope is #{target}" do
let(:params) { { scope: target } }
let!(:pipeline) { create(:ci_pipeline, project: project, status: target) }
2017-02-28 12:34:48 -05:00
2017-03-14 12:42:31 -04:00
it 'returns matched pipelines' do
2017-03-27 06:12:09 -04:00
is_expected.to eq([pipeline])
2017-02-28 12:34:48 -05:00
end
end
2017-03-27 06:12:09 -04:00
end
2017-02-28 12:34:48 -05:00
2017-03-27 06:12:09 -04:00
context 'when scope is finished' do
let(:params) { { scope: 'finished' } }
2017-03-28 14:07:11 -04:00
let!(:pipelines) do
[create(:ci_pipeline, project: project, status: 'success'),
create(:ci_pipeline, project: project, status: 'failed'),
create(:ci_pipeline, project: project, status: 'canceled')]
end
2017-02-28 12:34:48 -05:00
2017-03-27 06:12:09 -04:00
it 'returns matched pipelines' do
2017-04-27 09:23:06 -04:00
is_expected.to match_array(pipelines)
2017-02-28 12:34:48 -05:00
end
2017-03-27 06:12:09 -04:00
end
2017-02-28 12:34:48 -05:00
2017-03-27 06:12:09 -04:00
context 'when scope is branches or tags' do
let!(:pipeline_branch) { create(:ci_pipeline, project: project) }
let!(:pipeline_tag) { create(:ci_pipeline, project: project, ref: 'v1.0.0', tag: true) }
2016-08-29 12:02:08 -04:00
2017-03-14 12:42:31 -04:00
context 'when scope is branches' do
2016-08-29 12:02:08 -04:00
let(:params) { { scope: 'branches' } }
2017-03-14 12:42:31 -04:00
it 'returns matched pipelines' do
2017-03-27 06:12:09 -04:00
is_expected.to eq([pipeline_branch])
2016-08-29 12:02:08 -04:00
end
end
2017-03-14 12:42:31 -04:00
context 'when scope is tags' do
2016-08-29 12:02:08 -04:00
let(:params) { { scope: 'tags' } }
2017-03-14 12:42:31 -04:00
it 'returns matched pipelines' do
2017-03-27 06:12:09 -04:00
is_expected.to eq([pipeline_tag])
2016-08-29 12:02:08 -04:00
end
end
end
HasStatus::AVAILABLE_STATUSES.each do |target|
2017-03-27 06:12:09 -04:00
context "when status is #{target}" do
let(:params) { { status: target } }
let!(:pipeline) { create(:ci_pipeline, project: project, status: target) }
2017-02-28 12:34:48 -05:00
2017-03-28 12:02:17 -04:00
before do
exception_status = HasStatus::AVAILABLE_STATUSES - [target]
2017-04-05 07:52:02 -04:00
create(:ci_pipeline, project: project, status: exception_status.first)
2017-03-28 12:02:17 -04:00
end
2017-03-14 12:42:31 -04:00
it 'returns matched pipelines' do
2017-03-27 06:12:09 -04:00
is_expected.to eq([pipeline])
2017-02-28 12:34:48 -05:00
end
end
2017-03-27 06:12:09 -04:00
end
2016-08-29 12:02:08 -04:00
2017-03-27 06:12:09 -04:00
context 'when ref is specified' do
let!(:pipeline) { create(:ci_pipeline, project: project) }
2017-02-28 12:34:48 -05:00
2017-03-08 12:20:00 -05:00
context 'when ref exists' do
2017-02-28 12:34:48 -05:00
let(:params) { { ref: 'master' } }
2017-03-14 12:42:31 -04:00
it 'returns matched pipelines' do
2017-03-27 06:12:09 -04:00
is_expected.to eq([pipeline])
2017-02-28 12:34:48 -05:00
end
end
2017-03-08 12:20:00 -05:00
context 'when ref does not exist' do
2017-03-08 06:24:00 -05:00
let(:params) { { ref: 'invalid-ref' } }
2017-02-28 12:34:48 -05:00
2017-03-14 12:42:31 -04:00
it 'returns empty' do
2017-03-27 06:12:09 -04:00
is_expected.to be_empty
2017-02-28 12:34:48 -05:00
end
end
2017-03-08 06:24:00 -05:00
end
2017-03-27 06:12:09 -04:00
context 'when name is specified' do
let(:user) { create(:user) }
let!(:pipeline) { create(:ci_pipeline, project: project, user: user) }
2017-03-08 12:20:00 -05:00
context 'when name exists' do
2017-03-27 06:12:09 -04:00
let(:params) { { name: user.name } }
2017-03-08 06:24:00 -05:00
2017-03-14 12:42:31 -04:00
it 'returns matched pipelines' do
2017-03-27 06:12:09 -04:00
is_expected.to eq([pipeline])
2017-03-08 06:24:00 -05:00
end
end
2017-03-08 12:20:00 -05:00
context 'when name does not exist' do
2017-03-08 06:24:00 -05:00
let(:params) { { name: 'invalid-name' } }
2017-03-14 12:42:31 -04:00
it 'returns empty' do
2017-03-27 06:12:09 -04:00
is_expected.to be_empty
2017-03-08 06:24:00 -05:00
end
end
end
2017-02-28 12:34:48 -05:00
2017-03-27 06:12:09 -04:00
context 'when username is specified' do
let(:user) { create(:user) }
let!(:pipeline) { create(:ci_pipeline, project: project, user: user) }
2017-03-08 12:20:00 -05:00
context 'when username exists' do
2017-03-27 06:12:09 -04:00
let(:params) { { username: user.username } }
2017-02-28 12:34:48 -05:00
2017-03-14 12:42:31 -04:00
it 'returns matched pipelines' do
2017-03-27 06:12:09 -04:00
is_expected.to eq([pipeline])
2017-02-28 12:34:48 -05:00
end
end
2017-03-08 12:20:00 -05:00
context 'when username does not exist' do
2017-03-08 06:24:00 -05:00
let(:params) { { username: 'invalid-username' } }
2017-02-28 12:34:48 -05:00
2017-03-14 12:42:31 -04:00
it 'returns empty' do
2017-03-27 06:12:09 -04:00
is_expected.to be_empty
2017-02-28 12:34:48 -05:00
end
end
2017-03-08 06:24:00 -05:00
end
2017-02-28 12:34:48 -05:00
2017-03-27 06:12:09 -04:00
context 'when yaml_errors is specified' do
let!(:pipeline1) { create(:ci_pipeline, project: project, yaml_errors: 'Syntax error') }
let!(:pipeline2) { create(:ci_pipeline, project: project) }
2017-02-28 12:34:48 -05:00
context 'when yaml_errors is true' do
let(:params) { { yaml_errors: true } }
2017-03-14 12:42:31 -04:00
it 'returns matched pipelines' do
2017-03-27 06:12:09 -04:00
is_expected.to eq([pipeline1])
2017-02-28 12:34:48 -05:00
end
end
context 'when yaml_errors is false' do
let(:params) { { yaml_errors: false } }
2017-03-14 12:42:31 -04:00
it 'returns matched pipelines' do
2017-03-27 06:12:09 -04:00
is_expected.to eq([pipeline2])
2017-02-28 12:34:48 -05:00
end
end
2017-03-14 12:42:31 -04:00
context 'when yaml_errors is invalid' do
2017-03-27 06:12:09 -04:00
let(:params) { { yaml_errors: "invalid-yaml_errors" } }
2017-03-14 12:42:31 -04:00
it 'returns all pipelines' do
2017-04-27 09:23:06 -04:00
is_expected.to match_array([pipeline1, pipeline2])
end
end
2017-02-28 12:34:48 -05:00
end
context 'when updated_at filters are specified' do
let(:params) { { updated_before: 1.day.ago, updated_after: 3.days.ago } }
let!(:pipeline1) { create(:ci_pipeline, project: project, updated_at: 2.days.ago) }
let!(:pipeline2) { create(:ci_pipeline, project: project, updated_at: 4.days.ago) }
let!(:pipeline3) { create(:ci_pipeline, project: project, updated_at: 1.hour.ago) }
it 'returns deployments with matched updated_at' do
is_expected.to match_array([pipeline1])
end
2016-08-29 12:02:08 -04:00
end
context 'when sha is specified' do
let!(:pipeline) { create(:ci_pipeline, project: project, sha: '97de212e80737a608d939f648d959671fb0a0142') }
context 'when sha exists' do
let(:params) { { sha: '97de212e80737a608d939f648d959671fb0a0142' } }
it 'returns matched pipelines' do
is_expected.to eq([pipeline])
end
end
context 'when sha does not exist' do
let(:params) { { sha: 'invalid-sha' } }
it 'returns empty' do
is_expected.to be_empty
end
end
end
2018-11-15 16:29:53 -05:00
context 'when the project has limited access to pipelines' do
let(:project) { create(:project, :private, :repository) }
let(:current_user) { create(:user) }
let!(:pipelines) { create_list(:ci_pipeline, 2, project: project) }
context 'when the user has access' do
before do
project.add_developer(current_user)
end
it 'is expected to return pipelines' do
is_expected.to contain_exactly(*pipelines)
end
end
context 'the user is not allowed to read pipelines' do
it 'returns empty' do
is_expected.to be_empty
end
end
end
describe 'ordering' do
using RSpec::Parameterized::TableSyntax
let(:params) { { order_by: order_by, sort: sort } }
let!(:pipeline_1) { create(:ci_pipeline, :scheduled, project: project, iid: 11, ref: 'master', created_at: Time.now, updated_at: Time.now, user: create(:user)) }
let!(:pipeline_2) { create(:ci_pipeline, :created, project: project, iid: 12, ref: 'feature', created_at: 1.day.ago, updated_at: 2.hours.ago, user: create(:user)) }
let!(:pipeline_3) { create(:ci_pipeline, :success, project: project, iid: 8, ref: 'patch', created_at: 2.days.ago, updated_at: 1.hour.ago, user: create(:user)) }
where(:order_by, :sort, :ordered_pipelines) do
'id' | 'asc' | [:pipeline_1, :pipeline_2, :pipeline_3]
'id' | 'desc' | [:pipeline_3, :pipeline_2, :pipeline_1]
'ref' | 'asc' | [:pipeline_2, :pipeline_1, :pipeline_3]
'ref' | 'desc' | [:pipeline_3, :pipeline_1, :pipeline_2]
'status' | 'asc' | [:pipeline_2, :pipeline_1, :pipeline_3]
'status' | 'desc' | [:pipeline_3, :pipeline_1, :pipeline_2]
'updated_at' | 'asc' | [:pipeline_2, :pipeline_3, :pipeline_1]
'updated_at' | 'desc' | [:pipeline_1, :pipeline_3, :pipeline_2]
'user_id' | 'asc' | [:pipeline_1, :pipeline_2, :pipeline_3]
'user_id' | 'desc' | [:pipeline_3, :pipeline_2, :pipeline_1]
'invalid' | 'asc' | [:pipeline_1, :pipeline_2, :pipeline_3]
'id' | 'err' | [:pipeline_3, :pipeline_2, :pipeline_1]
end
with_them do
it 'returns the pipelines ordered' do
expect(subject).to eq(ordered_pipelines.map { |name| public_send(name) })
end
end
end
2016-08-29 12:02:08 -04:00
end
end