debian-mirror-gitlab/spec/policies/ci/build_policy_spec.rb

232 lines
6.4 KiB
Ruby
Raw Normal View History

2017-08-17 22:00:37 +05:30
require 'spec_helper'
2017-09-10 17:25:29 +05:30
describe Ci::BuildPolicy do
2017-08-17 22:00:37 +05:30
let(:user) { create(:user) }
let(:build) { create(:ci_build, pipeline: pipeline) }
let(:pipeline) { create(:ci_empty_pipeline, project: project) }
2017-09-10 17:25:29 +05:30
let(:policy) do
described_class.new(user, build)
2017-08-17 22:00:37 +05:30
end
shared_context 'public pipelines disabled' do
2017-09-10 17:25:29 +05:30
before do
project.update_attribute(:public_builds, false)
end
2017-08-17 22:00:37 +05:30
end
describe '#rules' do
context 'when user does not have access to the project' do
2017-09-10 17:25:29 +05:30
let(:project) { create(:project, :private) }
2017-08-17 22:00:37 +05:30
context 'when public builds are enabled' do
it 'does not include ability to read build' do
2017-09-10 17:25:29 +05:30
expect(policy).not_to be_allowed :read_build
2017-08-17 22:00:37 +05:30
end
end
context 'when public builds are disabled' do
include_context 'public pipelines disabled'
it 'does not include ability to read build' do
2017-09-10 17:25:29 +05:30
expect(policy).not_to be_allowed :read_build
2017-08-17 22:00:37 +05:30
end
end
end
context 'when anonymous user has access to the project' do
2017-09-10 17:25:29 +05:30
let(:project) { create(:project, :public) }
2017-08-17 22:00:37 +05:30
context 'when public builds are enabled' do
it 'includes ability to read build' do
2017-09-10 17:25:29 +05:30
expect(policy).to be_allowed :read_build
2017-08-17 22:00:37 +05:30
end
end
context 'when public builds are disabled' do
include_context 'public pipelines disabled'
it 'does not include ability to read build' do
2017-09-10 17:25:29 +05:30
expect(policy).not_to be_allowed :read_build
2017-08-17 22:00:37 +05:30
end
end
end
context 'when team member has access to the project' do
2017-09-10 17:25:29 +05:30
let(:project) { create(:project, :public) }
2017-08-17 22:00:37 +05:30
context 'team member is a guest' do
2017-09-10 17:25:29 +05:30
before do
2018-03-17 18:26:18 +05:30
project.add_guest(user)
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
context 'when public builds are enabled' do
it 'includes ability to read build' do
2017-09-10 17:25:29 +05:30
expect(policy).to be_allowed :read_build
2017-08-17 22:00:37 +05:30
end
end
context 'when public builds are disabled' do
include_context 'public pipelines disabled'
it 'does not include ability to read build' do
2017-09-10 17:25:29 +05:30
expect(policy).not_to be_allowed :read_build
2017-08-17 22:00:37 +05:30
end
end
end
context 'team member is a reporter' do
2017-09-10 17:25:29 +05:30
before do
2018-03-17 18:26:18 +05:30
project.add_reporter(user)
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
context 'when public builds are enabled' do
it 'includes ability to read build' do
2017-09-10 17:25:29 +05:30
expect(policy).to be_allowed :read_build
2017-08-17 22:00:37 +05:30
end
end
context 'when public builds are disabled' do
include_context 'public pipelines disabled'
it 'does not include ability to read build' do
2017-09-10 17:25:29 +05:30
expect(policy).to be_allowed :read_build
2017-08-17 22:00:37 +05:30
end
end
end
end
2017-09-10 17:25:29 +05:30
describe 'rules for protected ref' do
let(:project) { create(:project, :repository) }
let(:build) { create(:ci_build, ref: 'some-ref', pipeline: pipeline) }
2017-08-17 22:00:37 +05:30
before do
project.add_developer(user)
end
2017-09-10 17:25:29 +05:30
context 'when no one can push or merge to the branch' do
2017-08-17 22:00:37 +05:30
before do
create(:protected_branch, :no_one_can_push,
2017-09-10 17:25:29 +05:30
name: build.ref, project: project)
2017-08-17 22:00:37 +05:30
end
2017-09-10 17:25:29 +05:30
it 'does not include ability to update build' do
expect(policy).to be_disallowed :update_build
2017-08-17 22:00:37 +05:30
end
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
context 'when developers can push to the branch' do
before do
create(:protected_branch, :developers_can_merge,
name: build.ref, project: project)
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
it 'includes ability to update build' do
expect(policy).to be_allowed :update_build
2017-08-17 22:00:37 +05:30
end
end
2017-09-10 17:25:29 +05:30
context 'when no one can create the tag' do
before do
create(:protected_tag, :no_one_can_create,
name: build.ref, project: project)
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
build.update(tag: true)
end
it 'does not include ability to update build' do
expect(policy).to be_disallowed :update_build
2017-08-17 22:00:37 +05:30
end
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
context 'when no one can create the tag but it is not a tag' do
before do
create(:protected_tag, :no_one_can_create,
name: build.ref, project: project)
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
it 'includes ability to update build' do
expect(policy).to be_allowed :update_build
2017-08-17 22:00:37 +05:30
end
end
end
2018-03-17 18:26:18 +05:30
describe 'rules for erase build' do
let(:project) { create(:project, :repository) }
let(:build) { create(:ci_build, pipeline: pipeline, ref: 'some-ref', user: owner) }
context 'when a developer erases a build' do
before do
project.add_developer(user)
end
context 'when developers can push to the branch' do
before do
create(:protected_branch, :developers_can_push,
name: build.ref, project: project)
end
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
end
context 'when no one can push or merge to the branch' do
let(:owner) { user }
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 }
end
end
context 'when a master erases a build' do
before do
project.add_master(user)
end
context 'when masters can push to the branch' do
before do
create(:protected_branch, :masters_can_push,
name: build.ref, project: project)
end
context 'when the build was created by the master' 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_allowed :erase_build }
end
end
context 'when no one can push or merge to the branch' do
let(:owner) { user }
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 }
end
end
end
2017-08-17 22:00:37 +05:30
end
end