Commit d45da5a9 authored by Allison Browne's avatar Allison Browne

Optimize pipeline entity spec

Make database creation more efficent and
only add setup code where it is needed
parent 3e5374cc
...@@ -7,18 +7,8 @@ RSpec.describe PipelineEntity do ...@@ -7,18 +7,8 @@ RSpec.describe PipelineEntity do
let_it_be(:project) { create(:project) } let_it_be(:project) { create(:project) }
let_it_be(:user) { create(:user) } let_it_be(:user) { create(:user) }
let(:request) { double('request') } let(:request) { double('request', current_user: user) }
let(:entity) { described_class.represent(pipeline, request: request) }
before do
stub_not_protect_default_branch
allow(request).to receive(:current_user).and_return(user)
allow(request).to receive(:project).and_return(project)
end
let(:entity) do
described_class.represent(pipeline, request: request)
end
describe '#as_json' do describe '#as_json' do
subject { entity.as_json } subject { entity.as_json }
...@@ -54,70 +44,72 @@ RSpec.describe PipelineEntity do ...@@ -54,70 +44,72 @@ RSpec.describe PipelineEntity do
end end
end end
context 'when pipeline is retryable' do context 'when default branch not protected' do
let(:project) { create(:project) }
let(:pipeline) do
create(:ci_pipeline, status: :success, project: project)
end
before do before do
create(:ci_build, :failed, pipeline: pipeline) stub_not_protect_default_branch
end end
it 'does not serialize stage builds' do context 'when pipeline is retryable' do
subject.with_indifferent_access.dig(:details, :stages, 0).tap do |stage| let_it_be(:pipeline) do
expect(stage).not_to include(:groups, :latest_statuses, :retries) create(:ci_pipeline, status: :success, project: project)
end end
end
context 'user has ability to retry pipeline' do
before do before do
project.add_developer(user) create(:ci_build, :failed, pipeline: pipeline)
end
it 'contains retry path' do
expect(subject[:retry_path]).to be_present
end end
end
context 'user does not have ability to retry pipeline' do it 'does not serialize stage builds' do
it 'does not contain retry path' do subject.with_indifferent_access.dig(:details, :stages, 0).tap do |stage|
expect(subject).not_to have_key(:retry_path) expect(stage).not_to include(:groups, :latest_statuses, :retries)
end
end end
end
end
context 'when pipeline is cancelable' do context 'user has ability to retry pipeline' do
let(:project) { create(:project) } before do
project.add_developer(user)
end
let(:pipeline) do it 'contains retry path' do
create(:ci_pipeline, status: :running, project: project) expect(subject[:retry_path]).to be_present
end end
end
before do context 'user does not have ability to retry pipeline' do
create(:ci_build, :pending, pipeline: pipeline) it 'does not contain retry path' do
expect(subject).not_to have_key(:retry_path)
end
end
end end
it 'does not serialize stage builds' do context 'when pipeline is cancelable' do
subject.with_indifferent_access.dig(:details, :stages, 0).tap do |stage| let_it_be(:pipeline) do
expect(stage).not_to include(:groups, :latest_statuses, :retries) create(:ci_pipeline, status: :running, project: project)
end end
end
context 'user has ability to cancel pipeline' do
before do before do
project.add_developer(user) create(:ci_build, :pending, pipeline: pipeline)
end end
it 'contains cancel path' do it 'does not serialize stage builds' do
expect(subject[:cancel_path]).to be_present subject.with_indifferent_access.dig(:details, :stages, 0).tap do |stage|
expect(stage).not_to include(:groups, :latest_statuses, :retries)
end
end end
end
context 'user does not have ability to cancel pipeline' do context 'user has ability to cancel pipeline' do
it 'does not contain cancel path' do before do
expect(subject).not_to have_key(:cancel_path) project.add_developer(user)
end
it 'contains cancel path' do
expect(subject[:cancel_path]).to be_present
end
end
context 'user does not have ability to cancel pipeline' do
it 'does not contain cancel path' do
expect(subject).not_to have_key(:cancel_path)
end
end end
end end
end end
...@@ -133,7 +125,6 @@ RSpec.describe PipelineEntity do ...@@ -133,7 +125,6 @@ RSpec.describe PipelineEntity do
end end
context 'user does not have ability to delete pipeline' do context 'user does not have ability to delete pipeline' do
let(:project) { create(:project) }
let(:pipeline) { create(:ci_pipeline, project: project) } let(:pipeline) { create(:ci_pipeline, project: project) }
it 'does not contain delete path' do it 'does not contain delete path' do
...@@ -167,79 +158,85 @@ RSpec.describe PipelineEntity do ...@@ -167,79 +158,85 @@ RSpec.describe PipelineEntity do
end end
end end
context 'when pipeline is detached merge request pipeline' do context 'when request has a project' do
let(:merge_request) { create(:merge_request, :with_detached_merge_request_pipeline) } before do
let(:project) { merge_request.target_project } allow(request).to receive(:project).and_return(project)
let(:pipeline) { merge_request.pipelines_for_merge_request.first }
it 'makes detached flag true' do
expect(subject[:flags][:detached_merge_request_pipeline]).to be_truthy
end end
it 'does not expose source sha and target sha' do context 'when pipeline is detached merge request pipeline' do
expect(subject[:source_sha]).to be_nil let_it_be(:merge_request) { create(:merge_request, :with_detached_merge_request_pipeline) }
expect(subject[:target_sha]).to be_nil let(:project) { merge_request.target_project }
end let(:pipeline) { merge_request.pipelines_for_merge_request.first }
context 'when user is a developer' do it 'makes detached flag true' do
before do expect(subject[:flags][:detached_merge_request_pipeline]).to be_truthy
project.add_developer(user)
end end
it 'has merge request information' do it 'does not expose source sha and target sha' do
expect(subject[:merge_request][:iid]).to eq(merge_request.iid) expect(subject[:source_sha]).to be_nil
expect(subject[:target_sha]).to be_nil
end
context 'when user is a developer' do
before do
project.add_developer(user)
end
it 'has merge request information' do
expect(subject[:merge_request][:iid]).to eq(merge_request.iid)
expect(project_merge_request_path(project, merge_request)) expect(project_merge_request_path(project, merge_request))
.to include(subject[:merge_request][:path]) .to include(subject[:merge_request][:path])
expect(subject[:merge_request][:title]).to eq(merge_request.title) expect(subject[:merge_request][:title]).to eq(merge_request.title)
expect(subject[:merge_request][:source_branch]) expect(subject[:merge_request][:source_branch])
.to eq(merge_request.source_branch) .to eq(merge_request.source_branch)
expect(project_commits_path(project, merge_request.source_branch)) expect(project_commits_path(project, merge_request.source_branch))
.to include(subject[:merge_request][:source_branch_path]) .to include(subject[:merge_request][:source_branch_path])
expect(subject[:merge_request][:target_branch]) expect(subject[:merge_request][:target_branch])
.to eq(merge_request.target_branch) .to eq(merge_request.target_branch)
expect(project_commits_path(project, merge_request.target_branch)) expect(project_commits_path(project, merge_request.target_branch))
.to include(subject[:merge_request][:target_branch_path]) .to include(subject[:merge_request][:target_branch_path])
end
end end
end
context 'when user is an external user' do context 'when user is an external user' do
it 'has no merge request information' do it 'has no merge request information' do
expect(subject[:merge_request]).to be_nil expect(subject[:merge_request]).to be_nil
end
end end
end end
end
context 'when pipeline is merge request pipeline' do context 'when pipeline is merge request pipeline' do
let(:merge_request) { create(:merge_request, :with_merge_request_pipeline, merge_sha: 'abc') } let_it_be(:merge_request) { create(:merge_request, :with_merge_request_pipeline, merge_sha: 'abc') }
let(:project) { merge_request.target_project } let(:project) { merge_request.target_project }
let(:pipeline) { merge_request.pipelines_for_merge_request.first } let(:pipeline) { merge_request.pipelines_for_merge_request.first }
it 'makes detached flag false' do it 'makes detached flag false' do
expect(subject[:flags][:detached_merge_request_pipeline]).to be_falsy expect(subject[:flags][:detached_merge_request_pipeline]).to be_falsy
end end
it 'makes atached flag true' do it 'makes atached flag true' do
expect(subject[:flags][:merge_request_pipeline]).to be_truthy expect(subject[:flags][:merge_request_pipeline]).to be_truthy
end end
it 'exposes source sha and target sha' do it 'exposes source sha and target sha' do
expect(subject[:source_sha]).to be_present expect(subject[:source_sha]).to be_present
expect(subject[:target_sha]).to be_present expect(subject[:target_sha]).to be_present
end end
it 'exposes merge request event type' do it 'exposes merge request event type' do
expect(subject[:merge_request_event_type]).to be_present expect(subject[:merge_request_event_type]).to be_present
end
end end
end end
context 'when pipeline has failed builds' do context 'when pipeline has failed builds' do
let_it_be(:pipeline) { create(:ci_pipeline, project: project, user: user) } let_it_be(:pipeline) { create(:ci_pipeline, user: user) }
let_it_be(:build) { create(:ci_build, :success, pipeline: pipeline) } let_it_be(:build) { create(:ci_build, :success, pipeline: pipeline) }
let_it_be(:failed_1) { create(:ci_build, :failed, pipeline: pipeline) } let_it_be(:failed_1) { create(:ci_build, :failed, pipeline: pipeline) }
let_it_be(:failed_2) { create(:ci_build, :failed, pipeline: pipeline) } let_it_be(:failed_2) { create(:ci_build, :failed, pipeline: pipeline) }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment