2017-04-12 07:36:35 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe Ci::PlayBuildService, '#execute' do
|
2017-04-12 07:36:35 -04:00
|
|
|
let(:user) { create(:user) }
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project) { create(:project) }
|
2017-04-12 07:36:35 -04:00
|
|
|
let(:pipeline) { create(:ci_pipeline, project: project) }
|
|
|
|
let(:build) { create(:ci_build, :manual, pipeline: pipeline) }
|
|
|
|
|
|
|
|
let(:service) do
|
|
|
|
described_class.new(project, user)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when project does not have repository yet' do
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project) { create(:project) }
|
2017-04-12 07:36:35 -04:00
|
|
|
|
2017-05-31 05:15:25 -04:00
|
|
|
it 'allows user to play build if protected branch rules are met' do
|
|
|
|
project.add_developer(user)
|
|
|
|
|
|
|
|
create(:protected_branch, :developers_can_merge,
|
|
|
|
name: build.ref, project: project)
|
2017-04-12 07:36:35 -04:00
|
|
|
|
|
|
|
service.execute(build)
|
|
|
|
|
|
|
|
expect(build.reload).to be_pending
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not allow user with developer role to play build' do
|
|
|
|
project.add_developer(user)
|
|
|
|
|
|
|
|
expect { service.execute(build) }
|
|
|
|
.to raise_error Gitlab::Access::AccessDeniedError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when project has repository' do
|
2017-08-01 14:51:52 -04:00
|
|
|
let(:project) { create(:project, :repository) }
|
2017-04-12 07:36:35 -04:00
|
|
|
|
|
|
|
it 'allows user with developer role to play a build' do
|
|
|
|
project.add_developer(user)
|
|
|
|
|
|
|
|
service.execute(build)
|
|
|
|
|
|
|
|
expect(build.reload).to be_pending
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when build is a playable manual action' do
|
|
|
|
let(:build) { create(:ci_build, :manual, pipeline: pipeline) }
|
|
|
|
|
|
|
|
before do
|
2017-05-31 05:15:25 -04:00
|
|
|
project.add_developer(user)
|
|
|
|
|
|
|
|
create(:protected_branch, :developers_can_merge,
|
|
|
|
name: build.ref, project: project)
|
2017-04-12 07:36:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'enqueues the build' do
|
|
|
|
expect(service.execute(build)).to eq build
|
|
|
|
expect(build.reload).to be_pending
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'reassignes build user correctly' do
|
|
|
|
service.execute(build)
|
|
|
|
|
|
|
|
expect(build.reload.user).to eq user
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when build is not a playable manual action' do
|
|
|
|
let(:build) { create(:ci_build, when: :manual, pipeline: pipeline) }
|
|
|
|
|
|
|
|
before do
|
2017-05-31 05:15:25 -04:00
|
|
|
project.add_developer(user)
|
|
|
|
|
|
|
|
create(:protected_branch, :developers_can_merge,
|
|
|
|
name: build.ref, project: project)
|
2017-04-12 07:36:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'duplicates the build' do
|
|
|
|
duplicate = service.execute(build)
|
|
|
|
|
|
|
|
expect(duplicate).not_to eq build
|
|
|
|
expect(duplicate).to be_pending
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'assigns users correctly' do
|
|
|
|
duplicate = service.execute(build)
|
|
|
|
|
|
|
|
expect(build.user).not_to eq user
|
|
|
|
expect(duplicate.user).to eq user
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when build is not action' do
|
|
|
|
let(:build) { create(:ci_build, :success, pipeline: pipeline) }
|
|
|
|
|
|
|
|
it 'raises an error' do
|
|
|
|
expect { service.execute(build) }
|
|
|
|
.to raise_error Gitlab::Access::AccessDeniedError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user does not have ability to trigger action' do
|
|
|
|
before do
|
|
|
|
create(:protected_branch, :no_one_can_push,
|
|
|
|
name: build.ref, project: project)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error' do
|
|
|
|
expect { service.execute(build) }
|
|
|
|
.to raise_error Gitlab::Access::AccessDeniedError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|