environment_policy_spec.rb 2.57 KB
Newer Older
1 2
require 'spec_helper'

3
describe EnvironmentPolicy do
4
  using RSpec::Parameterized::TableSyntax
5

6
  let(:user) { create(:user) }
7

8 9
  let(:policy) do
    described_class.new(user, environment)
10 11 12
  end

  describe '#rules' do
13 14 15 16 17
    shared_examples 'project permissions' do
      context 'with stop action' do
        let(:environment) do
          create(:environment, :with_review_app, project: project)
        end
18

19
        where(:access_level, :allowed?) do
20 21 22 23 24
          nil         | false
          :guest      | false
          :reporter   | false
          :developer  | true
          :maintainer | true
25
        end
26

27 28 29 30
        with_them do
          before do
            project.add_user(user, access_level) unless access_level.nil?
          end
31

32 33
          it { expect(policy.allowed?(:stop_environment)).to be allowed? }
        end
34

35 36
        context 'when an admin user' do
          let(:user) { create(:user, :admin) }
37

38 39 40 41 42 43 44 45 46 47
          it { expect(policy).to be_allowed :stop_environment }
        end

        context 'with protected branch' do
          with_them do
            before do
              project.add_user(user, access_level) unless access_level.nil?
              create(:protected_branch, :no_one_can_push,
                     name: 'master', project: project)
            end
48

49 50 51 52 53 54 55 56
            it { expect(policy).to be_disallowed :stop_environment }
          end

          context 'when an admin user' do
            let(:user) { create(:user, :admin) }

            it { expect(policy).to be_allowed :stop_environment }
          end
57 58 59
        end
      end

60 61 62 63 64 65
      context 'without stop action' do
        let(:environment) do
          create(:environment, project: project)
        end

        where(:access_level, :allowed?) do
66 67 68 69 70
          nil         | false
          :guest      | false
          :reporter   | false
          :developer  | false
          :maintainer | true
71 72
        end

73 74 75 76 77 78 79 80 81 82 83 84
        with_them do
          before do
            project.add_user(user, access_level) unless access_level.nil?
          end

          it { expect(policy.allowed?(:stop_environment)).to be allowed? }
        end

        context 'when an admin user' do
          let(:user) { create(:user, :admin) }

          it { expect(policy).to be_allowed :stop_environment }
85 86 87
        end
      end
    end
88 89 90 91 92 93 94 95 96 97 98 99

    context 'when project is public' do
      let(:project) { create(:project, :public, :repository) }

      include_examples 'project permissions'
    end

    context 'when project is private' do
      let(:project) { create(:project, :private, :repository) }

      include_examples 'project permissions'
    end
100 101
  end
end