2019-10-24 20:06:14 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-01-23 08:49:13 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-06-29 00:13:10 -04:00
|
|
|
describe Ci::BuildPolicy do
|
2017-01-23 08:49:13 -05:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:build) { create(:ci_build, pipeline: pipeline) }
|
|
|
|
let(:pipeline) { create(:ci_empty_pipeline, project: project) }
|
|
|
|
|
2017-04-06 17:09:58 -04:00
|
|
|
let(:policy) do
|
|
|
|
described_class.new(user, build)
|
2017-01-23 08:49:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
shared_context 'public pipelines disabled' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
project.update_attribute(:public_builds, false)
|
|
|
|
end
|
2017-01-23 08:49:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#rules' do
|
|
|
|
context 'when user does not have access to the project' do
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project) { create(:project, :private) }
|
2017-01-23 08:49:13 -05:00
|
|
|
|
|
|
|
context 'when public builds are enabled' do
|
|
|
|
it 'does not include ability to read build' do
|
2017-04-06 17:09:58 -04:00
|
|
|
expect(policy).not_to be_allowed :read_build
|
2017-01-23 08:49:13 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when public builds are disabled' do
|
|
|
|
include_context 'public pipelines disabled'
|
|
|
|
|
|
|
|
it 'does not include ability to read build' do
|
2017-04-06 17:09:58 -04:00
|
|
|
expect(policy).not_to be_allowed :read_build
|
2017-01-23 08:49:13 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when anonymous user has access to the project' do
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project) { create(:project, :public) }
|
2017-01-23 08:49:13 -05:00
|
|
|
|
|
|
|
context 'when public builds are enabled' do
|
|
|
|
it 'includes ability to read build' do
|
2017-04-06 17:09:58 -04:00
|
|
|
expect(policy).to be_allowed :read_build
|
2017-01-23 08:49:13 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when public builds are disabled' do
|
|
|
|
include_context 'public pipelines disabled'
|
|
|
|
|
|
|
|
it 'does not include ability to read build' do
|
2017-04-06 17:09:58 -04:00
|
|
|
expect(policy).not_to be_allowed :read_build
|
2017-01-23 08:49:13 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when team member has access to the project' do
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project) { create(:project, :public) }
|
2017-01-23 08:49:13 -05:00
|
|
|
|
|
|
|
context 'team member is a guest' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
2017-12-22 03:18:28 -05:00
|
|
|
project.add_guest(user)
|
2017-06-14 14:18:56 -04:00
|
|
|
end
|
2017-01-23 08:49:13 -05:00
|
|
|
|
|
|
|
context 'when public builds are enabled' do
|
|
|
|
it 'includes ability to read build' do
|
2017-04-06 17:09:58 -04:00
|
|
|
expect(policy).to be_allowed :read_build
|
2017-01-23 08:49:13 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when public builds are disabled' do
|
|
|
|
include_context 'public pipelines disabled'
|
|
|
|
|
|
|
|
it 'does not include ability to read build' do
|
2017-04-06 17:09:58 -04:00
|
|
|
expect(policy).not_to be_allowed :read_build
|
2017-01-23 08:49:13 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'team member is a reporter' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
2017-12-22 03:18:28 -05:00
|
|
|
project.add_reporter(user)
|
2017-06-14 14:18:56 -04:00
|
|
|
end
|
2017-01-23 08:49:13 -05:00
|
|
|
|
|
|
|
context 'when public builds are enabled' do
|
|
|
|
it 'includes ability to read build' do
|
2017-04-06 17:09:58 -04:00
|
|
|
expect(policy).to be_allowed :read_build
|
2017-01-23 08:49:13 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when public builds are disabled' do
|
|
|
|
include_context 'public pipelines disabled'
|
|
|
|
|
|
|
|
it 'does not include ability to read build' do
|
2017-04-06 17:09:58 -04:00
|
|
|
expect(policy).to be_allowed :read_build
|
2017-01-23 08:49:13 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-05-15 04:18:22 -04:00
|
|
|
|
|
|
|
context 'when maintainer is allowed to push to pipeline branch' do
|
|
|
|
let(:project) { create(:project, :public) }
|
|
|
|
let(:owner) { user }
|
|
|
|
|
|
|
|
it 'enables update_build if user is maintainer' do
|
|
|
|
allow_any_instance_of(Project).to receive(:empty_repo?).and_return(false)
|
2018-05-22 21:54:57 -04:00
|
|
|
allow_any_instance_of(Project).to receive(:branch_allows_collaboration?).and_return(true)
|
2018-05-15 04:18:22 -04:00
|
|
|
|
|
|
|
expect(policy).to be_allowed :update_build
|
|
|
|
expect(policy).to be_allowed :update_commit_status
|
|
|
|
end
|
|
|
|
end
|
2017-01-23 08:49:13 -05:00
|
|
|
end
|
2017-04-12 05:26:18 -04:00
|
|
|
|
2017-07-18 10:04:22 -04:00
|
|
|
describe 'rules for protected ref' do
|
2017-08-01 14:51:52 -04:00
|
|
|
let(:project) { create(:project, :repository) }
|
2017-07-18 09:08:48 -04:00
|
|
|
let(:build) { create(:ci_build, ref: 'some-ref', pipeline: pipeline) }
|
2017-04-12 05:26:18 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_developer(user)
|
2017-07-03 17:01:05 -04:00
|
|
|
end
|
2017-04-12 05:26:18 -04:00
|
|
|
|
2017-07-03 17:01:05 -04:00
|
|
|
context 'when no one can push or merge to the branch' do
|
2017-07-18 09:08:48 -04:00
|
|
|
before do
|
|
|
|
create(:protected_branch, :no_one_can_push,
|
2017-07-18 10:04:22 -04:00
|
|
|
name: build.ref, project: project)
|
2017-07-18 09:08:48 -04:00
|
|
|
end
|
2017-04-12 05:26:18 -04:00
|
|
|
|
2017-07-03 17:01:05 -04:00
|
|
|
it 'does not include ability to update build' do
|
2017-07-03 17:20:44 -04:00
|
|
|
expect(policy).to be_disallowed :update_build
|
2017-04-12 05:26:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-03 17:01:05 -04:00
|
|
|
context 'when developers can push to the branch' do
|
2017-07-18 09:08:48 -04:00
|
|
|
before do
|
|
|
|
create(:protected_branch, :developers_can_merge,
|
2017-07-18 10:04:22 -04:00
|
|
|
name: build.ref, project: project)
|
2017-07-18 09:08:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes ability to update build' do
|
|
|
|
expect(policy).to be_allowed :update_build
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when no one can create the tag' do
|
|
|
|
before do
|
|
|
|
create(:protected_tag, :no_one_can_create,
|
2017-07-18 10:04:22 -04:00
|
|
|
name: build.ref, project: project)
|
2017-07-18 09:08:48 -04:00
|
|
|
|
|
|
|
build.update(tag: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not include ability to update build' do
|
|
|
|
expect(policy).to be_disallowed :update_build
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when no one can create the tag but it is not a tag' do
|
|
|
|
before do
|
|
|
|
create(:protected_tag, :no_one_can_create,
|
2017-07-18 10:04:22 -04:00
|
|
|
name: build.ref, project: project)
|
2017-07-18 09:08:48 -04:00
|
|
|
end
|
2017-04-12 05:26:18 -04:00
|
|
|
|
2017-07-03 17:01:05 -04:00
|
|
|
it 'includes ability to update build' do
|
2017-07-03 17:20:44 -04:00
|
|
|
expect(policy).to be_allowed :update_build
|
2017-04-12 05:26:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-11-06 08:20:44 -05:00
|
|
|
|
|
|
|
describe 'rules for erase build' do
|
2017-11-07 08:45:55 -05:00
|
|
|
let(:project) { create(:project, :repository) }
|
2017-11-06 12:47:05 -05:00
|
|
|
let(:build) { create(:ci_build, pipeline: pipeline, ref: 'some-ref', user: owner) }
|
2017-11-06 08:20:44 -05:00
|
|
|
|
2017-11-06 12:47:05 -05:00
|
|
|
context 'when a developer erases a build' do
|
2017-11-06 08:20:44 -05:00
|
|
|
before do
|
|
|
|
project.add_developer(user)
|
|
|
|
end
|
|
|
|
|
2017-11-06 12:47:05 -05:00
|
|
|
context 'when developers can push to the branch' do
|
|
|
|
before do
|
2017-11-07 08:45:55 -05:00
|
|
|
create(:protected_branch, :developers_can_push,
|
2017-11-06 12:47:05 -05:00
|
|
|
name: build.ref, project: project)
|
|
|
|
end
|
2017-11-06 08:20:44 -05:00
|
|
|
|
2017-11-06 12:47:05 -05:00
|
|
|
context 'when the build was created by the developer' do
|
|
|
|
let(:owner) { user }
|
|
|
|
|
|
|
|
it { expect(policy).to be_allowed :erase_build }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the build was created by the other' do
|
|
|
|
let(:owner) { create(:user) }
|
|
|
|
|
|
|
|
it { expect(policy).to be_disallowed :erase_build }
|
|
|
|
end
|
2017-11-06 08:20:44 -05:00
|
|
|
end
|
|
|
|
|
2017-11-06 12:47:05 -05:00
|
|
|
context 'when no one can push or merge to the branch' do
|
|
|
|
let(:owner) { user }
|
|
|
|
|
|
|
|
before do
|
2017-11-07 08:45:55 -05:00
|
|
|
create(:protected_branch, :no_one_can_push, :no_one_can_merge,
|
2017-11-06 12:47:05 -05:00
|
|
|
name: build.ref, project: project)
|
|
|
|
end
|
2017-11-06 08:20:44 -05:00
|
|
|
|
|
|
|
it { expect(policy).to be_disallowed :erase_build }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-11 10:36:08 -04:00
|
|
|
context 'when a maintainer erases a build' do
|
2017-11-06 08:20:44 -05:00
|
|
|
before do
|
2018-07-11 10:36:08 -04:00
|
|
|
project.add_maintainer(user)
|
2017-11-06 08:20:44 -05:00
|
|
|
end
|
|
|
|
|
2018-07-11 10:36:08 -04:00
|
|
|
context 'when maintainers can push to the branch' do
|
2017-11-07 08:45:55 -05:00
|
|
|
before do
|
2018-07-11 10:36:08 -04:00
|
|
|
create(:protected_branch, :maintainers_can_push,
|
2017-11-07 08:45:55 -05:00
|
|
|
name: build.ref, project: project)
|
|
|
|
end
|
|
|
|
|
2018-07-11 10:36:08 -04:00
|
|
|
context 'when the build was created by the maintainer' do
|
2017-11-07 08:45:55 -05:00
|
|
|
let(:owner) { user }
|
|
|
|
|
|
|
|
it { expect(policy).to be_allowed :erase_build }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the build was created by the other' do
|
|
|
|
let(:owner) { create(:user) }
|
2017-11-06 08:20:44 -05:00
|
|
|
|
2017-11-07 08:45:55 -05:00
|
|
|
it { expect(policy).to be_allowed :erase_build }
|
|
|
|
end
|
2017-11-06 08:20:44 -05:00
|
|
|
end
|
|
|
|
|
2017-11-07 08:45:55 -05:00
|
|
|
context 'when no one can push or merge to the branch' do
|
|
|
|
let(:owner) { user }
|
2017-11-06 08:20:44 -05:00
|
|
|
|
2017-11-07 08:45:55 -05:00
|
|
|
before do
|
|
|
|
create(:protected_branch, :no_one_can_push, :no_one_can_merge,
|
|
|
|
name: build.ref, project: project)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(policy).to be_disallowed :erase_build }
|
2017-11-06 08:20:44 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-01-23 08:49:13 -05:00
|
|
|
end
|
|
|
|
end
|