pipeline_policy_spec.rb 2.2 KB
Newer Older
1 2 3 4 5 6
require 'spec_helper'

describe Ci::PipelinePolicy, :models do
  let(:user) { create(:user) }
  let(:pipeline) { create(:ci_empty_pipeline, project: project) }

L
Lin Jen-Shin 已提交
7 8
  let(:policy) do
    described_class.new(user, pipeline)
9 10 11
  end

  describe 'rules' do
12
    describe 'rules for protected ref' do
13
      let(:project) { create(:project, :repository) }
14 15 16 17 18 19

      before do
        project.add_developer(user)
      end

      context 'when no one can push or merge to the branch' do
20 21 22 23
        before do
          create(:protected_branch, :no_one_can_push,
                 name: pipeline.ref, project: project)
        end
24 25

        it 'does not include ability to update pipeline' do
L
Lin Jen-Shin 已提交
26
          expect(policy).to be_disallowed :update_pipeline
27 28 29 30
        end
      end

      context 'when developers can push to the branch' do
31 32 33 34
        before do
          create(:protected_branch, :developers_can_merge,
                 name: pipeline.ref, project: project)
        end
35 36

        it 'includes ability to update pipeline' do
L
Lin Jen-Shin 已提交
37
          expect(policy).to be_allowed :update_pipeline
38 39 40
        end
      end

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
      context 'when no one can create the tag' do
        before do
          create(:protected_tag, :no_one_can_create,
                 name: pipeline.ref, project: project)

          pipeline.update(tag: true)
        end

        it 'does not include ability to update pipeline' do
          expect(policy).to be_disallowed :update_pipeline
        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,
                 name: pipeline.ref, project: project)
        end
59 60

        it 'includes ability to update pipeline' do
L
Lin Jen-Shin 已提交
61
          expect(policy).to be_allowed :update_pipeline
62 63 64
        end
      end
    end
65 66 67 68 69 70 71

    context 'when maintainer is allowed to push to pipeline branch' do
      let(:project) { create(:project, :public) }
      let(:owner) { user }

      it 'enables update_pipeline if user is maintainer' do
        allow_any_instance_of(Project).to receive(:empty_repo?).and_return(false)
72
        allow_any_instance_of(Project).to receive(:branch_allows_collaboration?).and_return(true)
73 74 75 76

        expect(policy).to be_allowed :update_pipeline
      end
    end
77 78
  end
end