summaryrefslogtreecommitdiff
path: root/spec/services/groups/create_service_spec.rb
blob: 235fa5c51883fdf3dfe89928c79678510f26fdc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
require 'spec_helper'

describe Groups::CreateService, '#execute', services: true do
  let!(:user) { create(:user) }
  let!(:group_params) { { path: "group_path", visibility_level: Gitlab::VisibilityLevel::PUBLIC } }

  subject { service.execute }

  describe 'visibility level restrictions' do
    let!(:service) { described_class.new(user, group_params) }

    context "create groups without restricted visibility level" do
      it { is_expected.to be_persisted }
    end

    context "cannot create group with restricted visibility level" do
      before { allow_any_instance_of(ApplicationSetting).to receive(:restricted_visibility_levels).and_return([Gitlab::VisibilityLevel::PUBLIC]) }

      it { is_expected.not_to be_persisted }
    end
  end

  describe 'creating subgroup' do
    let!(:group) { create(:group) }
    let!(:service) { described_class.new(user, group_params.merge(parent_id: group.id)) }

    context 'as group owner' do
      before { group.add_owner(user) }

      it { is_expected.to be_persisted }
    end

    context 'as guest' do
      it 'does not save group and returns an error' do
        is_expected.not_to be_persisted
        expect(subject.errors[:parent_id].first).to eq('manage access required to create subgroup')
        expect(subject.parent_id).to be_nil
      end
    end
  end

  describe 'creating a mattermost team' do
    let!(:params) { group_params.merge(create_chat_team: true) }
    let!(:service) { described_class.new(user, params) }

    it 'queues a background job' do
      expect(Mattermost::CreateTeamWorker).to receive(:perform_async)

      subject
    end
  end
end