summaryrefslogtreecommitdiff
path: root/spec/services/groups/nested_create_service_spec.rb
blob: c1526456bac2aac9bb75b0028901b7668912f33a (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
53
require 'spec_helper'

describe Groups::NestedCreateService do
  let(:user) { create(:user) }
  let(:params) { { group_path: 'a-group/a-sub-group' } }

  subject(:service) { described_class.new(user, params) }

  describe "#execute" do
    it 'returns the group if it already existed' do
      parent = create(:group, path: 'a-group', owner: user)
      child = create(:group, path: 'a-sub-group', parent: parent, owner: user)

      expect(service.execute).to eq(child)
    end

    it 'reuses a parent if it already existed' do
      parent = create(:group, path: 'a-group')
      parent.add_owner(user)

      expect(service.execute.parent).to eq(parent)
    end

    it 'creates group and subgroup in the database' do
      service.execute

      parent = Group.find_by_full_path('a-group')
      child = parent.children.find_by(path: 'a-sub-group')

      expect(parent).not_to be_nil
      expect(child).not_to be_nil
    end

    it 'creates the group with correct visibility level' do
      allow(Gitlab::CurrentSettings.current_application_settings)
        .to receive(:default_group_visibility) { Gitlab::VisibilityLevel::INTERNAL }

      group = service.execute

      expect(group.visibility_level).to eq(Gitlab::VisibilityLevel::INTERNAL)
    end

    context 'adding a visibility level ' do
      let(:params) { { group_path: 'a-group/a-sub-group', visibility_level: Gitlab::VisibilityLevel::PRIVATE } }

      it 'overwrites the visibility level' do
        group = service.execute

        expect(group.visibility_level).to eq(Gitlab::VisibilityLevel::PRIVATE)
      end
    end
  end
end