2019-07-31 22:56:46 +05:30
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
|
RSpec.describe Groups::CreateService, '#execute' do
|
2017-08-17 22:00:37 +05:30
|
|
|
|
let!(:user) { create(:user) }
|
2016-06-02 11:05:42 +05:30
|
|
|
|
let!(:group_params) { { path: "group_path", visibility_level: Gitlab::VisibilityLevel::PUBLIC } }
|
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
|
subject { service.execute }
|
|
|
|
|
|
|
|
|
|
describe 'visibility level restrictions' do
|
|
|
|
|
let!(:service) { described_class.new(user, group_params) }
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
|
|
context "create groups without restricted visibility level" do
|
|
|
|
|
it { is_expected.to be_persisted }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context "cannot create group with restricted visibility level" do
|
2017-09-10 17:25:29 +05:30
|
|
|
|
before do
|
|
|
|
|
allow_any_instance_of(ApplicationSetting).to receive(:restricted_visibility_levels).and_return([Gitlab::VisibilityLevel::PUBLIC])
|
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
|
it { is_expected.not_to be_persisted }
|
2016-06-02 11:05:42 +05:30
|
|
|
|
end
|
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
|
context 'creating a group with `default_branch_protection` attribute' do
|
|
|
|
|
let(:params) { group_params.merge(default_branch_protection: Gitlab::Access::PROTECTION_NONE) }
|
|
|
|
|
let(:service) { described_class.new(user, params) }
|
|
|
|
|
let(:created_group) { service.execute }
|
|
|
|
|
|
|
|
|
|
context 'for users who have the ability to create a group with `default_branch_protection`' do
|
|
|
|
|
it 'creates group with the specified branch protection level' do
|
|
|
|
|
expect(created_group.default_branch_protection).to eq(Gitlab::Access::PROTECTION_NONE)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'for users who do not have the ability to create a group with `default_branch_protection`' do
|
|
|
|
|
it 'does not create the group with the specified branch protection level' do
|
|
|
|
|
allow(Ability).to receive(:allowed?).and_call_original
|
|
|
|
|
allow(Ability).to receive(:allowed?).with(user, :create_group_with_default_branch_protection) { false }
|
|
|
|
|
|
|
|
|
|
expect(created_group.default_branch_protection).not_to eq(Gitlab::Access::PROTECTION_NONE)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
|
describe 'creating a top level group' do
|
|
|
|
|
let(:service) { described_class.new(user, group_params) }
|
|
|
|
|
|
|
|
|
|
context 'when user can create a group' do
|
|
|
|
|
before do
|
|
|
|
|
user.update_attribute(:can_create_group, true)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it { is_expected.to be_persisted }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when user can not create a group' do
|
|
|
|
|
before do
|
|
|
|
|
user.update_attribute(:can_create_group, false)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it { is_expected.not_to be_persisted }
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
|
describe 'creating subgroup' do
|
2017-08-17 22:00:37 +05:30
|
|
|
|
let!(:group) { create(:group) }
|
|
|
|
|
let!(:service) { described_class.new(user, group_params.merge(parent_id: group.id)) }
|
|
|
|
|
|
|
|
|
|
context 'as group owner' do
|
2017-09-10 17:25:29 +05:30
|
|
|
|
before do
|
|
|
|
|
group.add_owner(user)
|
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
|
|
it { is_expected.to be_persisted }
|
2019-10-12 21:52:04 +05:30
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
|
context 'as guest' do
|
|
|
|
|
it 'does not save group and returns an error' do
|
|
|
|
|
is_expected.not_to be_persisted
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
|
expect(subject.errors[:parent_id].first).to eq(s_('CreateGroup|You don’t have permission to create a subgroup in this group.'))
|
2019-10-12 21:52:04 +05:30
|
|
|
|
expect(subject.parent_id).to be_nil
|
2018-03-17 18:26:18 +05:30
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
end
|
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
|
context 'as owner' do
|
2018-03-17 18:26:18 +05:30
|
|
|
|
before do
|
2019-10-12 21:52:04 +05:30
|
|
|
|
group.add_owner(user)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
end
|
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
|
it { is_expected.to be_persisted }
|
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
|
context 'as maintainer' do
|
|
|
|
|
before do
|
|
|
|
|
group.add_maintainer(user)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
end
|
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
|
it { is_expected.to be_persisted }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
|
describe "when visibility level is passed as a string" do
|
|
|
|
|
let(:service) { described_class.new(user, group_params) }
|
|
|
|
|
let(:group_params) { { path: 'group_path', visibility: 'public' } }
|
|
|
|
|
|
|
|
|
|
it "assigns the correct visibility level" do
|
|
|
|
|
group = service.execute
|
|
|
|
|
|
|
|
|
|
expect(group.visibility_level).to eq(Gitlab::VisibilityLevel::PUBLIC)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
|
describe 'creating a mattermost team' do
|
|
|
|
|
let!(:params) { group_params.merge(create_chat_team: "true") }
|
|
|
|
|
let!(:service) { described_class.new(user, params) }
|
|
|
|
|
|
|
|
|
|
before do
|
|
|
|
|
stub_mattermost_setting(enabled: true)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'create the chat team with the group' do
|
|
|
|
|
allow_any_instance_of(Mattermost::Team).to receive(:create)
|
|
|
|
|
.and_return({ 'name' => 'tanuki', 'id' => 'lskdjfwlekfjsdifjj' })
|
|
|
|
|
|
|
|
|
|
expect { subject }.to change { ChatTeam.count }.from(0).to(1)
|
|
|
|
|
end
|
|
|
|
|
end
|
2020-07-28 23:09:34 +05:30
|
|
|
|
|
|
|
|
|
describe 'creating a setting record' do
|
|
|
|
|
let(:service) { described_class.new(user, group_params) }
|
|
|
|
|
|
|
|
|
|
it 'create the settings record connected to the group' do
|
|
|
|
|
group = subject
|
|
|
|
|
expect(group.namespace_settings).to be_persisted
|
|
|
|
|
end
|
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
|
end
|