2019-10-24 20:06:14 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-01 07:38:57 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe EnvironmentPolicy do
|
2018-07-10 04:11:04 -04:00
|
|
|
using RSpec::Parameterized::TableSyntax
|
2017-05-01 07:38:57 -04:00
|
|
|
|
2018-07-10 04:11:04 -04:00
|
|
|
let(:user) { create(:user) }
|
2017-05-01 07:38:57 -04:00
|
|
|
|
2017-04-06 17:09:58 -04:00
|
|
|
let(:policy) do
|
|
|
|
described_class.new(user, environment)
|
2017-05-01 07:38:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#rules' do
|
2018-07-10 04:11:04 -04:00
|
|
|
shared_examples 'project permissions' do
|
|
|
|
context 'with stop action' do
|
|
|
|
let(:environment) do
|
|
|
|
create(:environment, :with_review_app, project: project)
|
|
|
|
end
|
2017-05-01 07:38:57 -04:00
|
|
|
|
2018-07-10 04:11:04 -04:00
|
|
|
where(:access_level, :allowed?) do
|
2018-07-11 10:36:08 -04:00
|
|
|
nil | false
|
|
|
|
:guest | false
|
|
|
|
:reporter | false
|
|
|
|
:developer | true
|
|
|
|
:maintainer | true
|
2018-07-10 04:11:04 -04:00
|
|
|
end
|
2017-05-01 07:38:57 -04:00
|
|
|
|
2018-07-10 04:11:04 -04:00
|
|
|
with_them do
|
|
|
|
before do
|
2022-07-05 14:08:43 -04:00
|
|
|
project.add_member(user, access_level) unless access_level.nil?
|
2018-07-10 04:11:04 -04:00
|
|
|
end
|
2017-05-01 07:38:57 -04:00
|
|
|
|
2018-07-10 04:11:04 -04:00
|
|
|
it { expect(policy.allowed?(:stop_environment)).to be allowed? }
|
|
|
|
end
|
2017-05-01 07:38:57 -04:00
|
|
|
|
2018-07-10 04:11:04 -04:00
|
|
|
context 'when an admin user' do
|
|
|
|
let(:user) { create(:user, :admin) }
|
2017-05-01 07:38:57 -04:00
|
|
|
|
2020-05-15 11:08:04 -04:00
|
|
|
context 'when admin mode is enabled', :enable_admin_mode do
|
|
|
|
it { expect(policy).to be_allowed :stop_environment }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when admin mode is disabled' do
|
|
|
|
it { expect(policy).to be_disallowed :stop_environment }
|
|
|
|
end
|
2018-07-10 04:11:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with protected branch' do
|
|
|
|
with_them do
|
|
|
|
before do
|
2022-07-05 14:08:43 -04:00
|
|
|
project.add_member(user, access_level) unless access_level.nil?
|
2018-07-10 04:11:04 -04:00
|
|
|
create(:protected_branch, :no_one_can_push,
|
|
|
|
name: 'master', project: project)
|
|
|
|
end
|
2017-05-01 07:38:57 -04:00
|
|
|
|
2018-07-10 04:11:04 -04:00
|
|
|
it { expect(policy).to be_disallowed :stop_environment }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when an admin user' do
|
|
|
|
let(:user) { create(:user, :admin) }
|
|
|
|
|
2020-05-15 11:08:04 -04:00
|
|
|
context 'when admin mode is enabled', :enable_admin_mode do
|
|
|
|
it { expect(policy).to be_allowed :stop_environment }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when admin mode is disabled' do
|
|
|
|
it { expect(policy).to be_disallowed :stop_environment }
|
|
|
|
end
|
2018-07-10 04:11:04 -04:00
|
|
|
end
|
2017-05-01 07:38:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-10 04:11:04 -04:00
|
|
|
context 'without stop action' do
|
|
|
|
let(:environment) do
|
|
|
|
create(:environment, project: project)
|
|
|
|
end
|
|
|
|
|
|
|
|
where(:access_level, :allowed?) do
|
2018-07-11 10:36:08 -04:00
|
|
|
nil | false
|
|
|
|
:guest | false
|
|
|
|
:reporter | false
|
2019-12-10 02:53:40 -05:00
|
|
|
:developer | true
|
2018-07-11 10:36:08 -04:00
|
|
|
:maintainer | true
|
2017-05-01 07:38:57 -04:00
|
|
|
end
|
|
|
|
|
2018-07-10 04:11:04 -04:00
|
|
|
with_them do
|
|
|
|
before do
|
2022-07-05 14:08:43 -04:00
|
|
|
project.add_member(user, access_level) unless access_level.nil?
|
2018-07-10 04:11:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(policy.allowed?(:stop_environment)).to be allowed? }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when an admin user' do
|
|
|
|
let(:user) { create(:user, :admin) }
|
|
|
|
|
2020-05-15 11:08:04 -04:00
|
|
|
context 'when admin mode is enabled', :enable_admin_mode do
|
|
|
|
it { expect(policy).to be_allowed :stop_environment }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when admin mode is disabled' do
|
|
|
|
it { expect(policy).to be_disallowed :stop_environment }
|
|
|
|
end
|
2017-05-01 07:38:57 -04:00
|
|
|
end
|
|
|
|
end
|
2020-03-25 05:08:11 -04:00
|
|
|
|
|
|
|
describe '#destroy_environment' do
|
|
|
|
let(:environment) do
|
|
|
|
create(:environment, project: project)
|
|
|
|
end
|
|
|
|
|
|
|
|
where(:access_level, :allowed?) do
|
|
|
|
nil | false
|
|
|
|
:guest | false
|
|
|
|
:reporter | false
|
|
|
|
:developer | true
|
|
|
|
:maintainer | true
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
before do
|
2022-07-05 14:08:43 -04:00
|
|
|
project.add_member(user, access_level) unless access_level.nil?
|
2020-03-25 05:08:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(policy).to be_disallowed :destroy_environment }
|
|
|
|
|
|
|
|
context 'when environment is stopped' do
|
|
|
|
before do
|
|
|
|
environment.stop!
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(policy.allowed?(:destroy_environment)).to be allowed? }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when an admin user' do
|
|
|
|
let(:user) { create(:user, :admin) }
|
|
|
|
|
|
|
|
it { expect(policy).to be_disallowed :destroy_environment }
|
|
|
|
|
|
|
|
context 'when environment is stopped' do
|
|
|
|
before do
|
|
|
|
environment.stop!
|
|
|
|
end
|
|
|
|
|
2020-05-15 11:08:04 -04:00
|
|
|
context 'when admin mode is enabled', :enable_admin_mode do
|
|
|
|
it { expect(policy).to be_allowed :destroy_environment }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when admin mode is disabled' do
|
|
|
|
it { expect(policy).to be_disallowed :destroy_environment }
|
|
|
|
end
|
2020-03-25 05:08:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-05-01 07:38:57 -04:00
|
|
|
end
|
2018-07-10 04:11:04 -04:00
|
|
|
|
|
|
|
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
|
2017-05-01 07:38:57 -04:00
|
|
|
end
|
|
|
|
end
|