Commit ff82c583 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Add some specs for cross project pipeline creation service

parent bf9c09d7
......@@ -9,7 +9,7 @@ module Ci
belongs_to :pipeline, class_name: Ci::Pipeline
belongs_to :source_project, class_name: Project, foreign_key: :source_project_id
belongs_to :source_job, class_name: Ci::Build, foreign_key: :source_job_id
belongs_to :source_job, class_name: CommitStatus, foreign_key: :source_job_id
belongs_to :source_pipeline, class_name: Ci::Pipeline, foreign_key: :source_pipeline_id
validates :project, presence: true
......
......@@ -9,7 +9,8 @@ module EE
override :failure_reasons
def failure_reasons
super.merge(protected_environment_failure: 1_000)
super.merge(protected_environment_failure: 1_000,
insufficient_permissions: 1_001)
end
end
end
......
......@@ -25,6 +25,8 @@ module Ci
private
def can_create_cross_pipeline?
# TODO should we check update_pipeline in the first condition?
#
can?(current_user, :create_pipeline, project) &&
can?(current_user, :create_pipeline, target_project) &&
can?(@bridge.target_user, :create_pipeline, target_project)
......
require 'spec_helper'
describe Ci::CreateCrossProjectPipelineService, '#execute' do
set(:upstream_project) { create(:project, :repository) }
set(:downstream_project) { create(:project, :repository) }
set(:upstream_pipeline) { create(:ci_pipeline, project: upstream_project) }
set(:user) { create(:user) }
let(:trigger) do
{ trigger: {
project: downstream_project.full_path,
branch: 'feature'
}
}
end
let(:bridge) do
create(:ci_bridge, user: user,
options: trigger,
pipeline: upstream_pipeline)
end
let(:service) { described_class.new(upstream_project, user) }
before do
stub_ci_pipeline_to_return_yaml_file
upstream_project.add_developer(user)
end
context 'when user does not have ability to create a pipeline' do
it 'changes status of the bridge build' do
expect { service.execute(bridge) }
.not_to change { Ci::Pipeline.count }
expect(bridge).to be_failed
expect(bridge.failure_reason).to eq 'insufficient_permissions'
end
end
context 'when user can create pipeline in a downstream project' do
before do
downstream_project.add_developer(user)
end
it 'creates a new pipeline' do
expect { service.execute(bridge) }
.to change { Ci::Pipeline.count }.by(1)
end
it 'creates a new pipeline in a downstream project' do
pipeline = service.execute(bridge)
expect(pipeline.project).to eq downstream_project
expect(bridge.sourced_pipelines.first.pipeline).to eq pipeline
expect(pipeline.source_pipeline.source_pipeline).to eq upstream_pipeline
expect(pipeline.source_pipeline.source_job).to eq bridge
end
it 'delegates permissions to newly created pipelines' do
pipeline = service.execute(bridge)
expect(pipeline.user).to eq bridge.user
end
end
end
......@@ -6,7 +6,7 @@ FactoryBot.define do
ref 'master'
tag false
created_at 'Di 29. Okt 09:50:00 CET 2013'
status :success
status :created
pipeline factory: :ci_pipeline
......
......@@ -5,7 +5,7 @@ describe Ci::Bridge do
set(:pipeline) { create(:ci_pipeline, project: project) }
let(:bridge) do
create(:ci_bridge, pipeline: pipeline)
create(:ci_bridge, status: :success, pipeline: pipeline)
end
describe '#tags' do
......
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