summaryrefslogtreecommitdiff
path: root/spec/lib/bulk_imports/groups/loaders/group_loader_spec.rb
blob: 7d1f9ae5da085b5463dc87782928a01c8ce5c88a (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe BulkImports::Groups::Loaders::GroupLoader, feature_category: :importers do
  describe '#load' do
    let_it_be(:user) { create(:user) }
    let_it_be(:bulk_import) { create(:bulk_import, user: user) }
    let_it_be(:entity) { create(:bulk_import_entity, bulk_import: bulk_import) }
    let_it_be(:tracker) { create(:bulk_import_tracker, entity: entity) }
    let_it_be(:context) { BulkImports::Pipeline::Context.new(tracker) }

    let(:service_double) { instance_double(::Groups::CreateService) }
    let(:data) { { 'path' => 'test' } }

    subject { described_class.new }

    context 'when path is missing' do
      it 'raises an error' do
        expect { subject.load(context, {}) }.to raise_error(described_class::GroupCreationError, 'Path is missing')
      end
    end

    context 'when destination namespace is not a group' do
      it 'raises an error' do
        entity.update!(destination_namespace: user.namespace.path)

        expect { subject.load(context, data) }.to raise_error(described_class::GroupCreationError, 'Destination is not a group')
      end
    end

    context 'when there are other group errors' do
      it 'raises an error with those errors' do
        entity.update!(destination_namespace: '')
        group = ::Group.new
        group.validate
        expected_errors = group.errors.full_messages.to_sentence

        expect(::Groups::CreateService)
          .to receive(:new)
          .with(context.current_user, data)
          .and_return(service_double)

        expect(service_double).to receive(:execute).and_return(group)
        expect(entity).not_to receive(:update!)

        expect { subject.load(context, data) }.to raise_error(described_class::GroupCreationError, expected_errors)
      end
    end

    context 'when user can create group' do
      shared_examples 'calls Group Create Service to create a new group' do
        it 'calls Group Create Service to create a new group' do
          group_double = instance_double(::Group)

          expect(::Groups::CreateService)
            .to receive(:new)
            .with(context.current_user, data)
            .and_return(service_double)

          expect(service_double).to receive(:execute).and_return(group_double)
          expect(group_double).to receive(:errors).and_return([])
          expect(entity).to receive(:update!).with(group: group_double)

          subject.load(context, data)
        end
      end

      context 'when there is no parent group' do
        before do
          allow(Ability).to receive(:allowed?).with(user, :create_group).and_return(true)
        end

        include_examples 'calls Group Create Service to create a new group'
      end

      context 'when there is parent group' do
        let(:parent) { create(:group) }
        let(:data) { { 'parent_id' => parent.id, 'path' => 'test' } }

        before do
          allow(Ability).to receive(:allowed?).with(user, :create_subgroup, parent).and_return(true)
        end

        include_examples 'calls Group Create Service to create a new group'
      end
    end

    context 'when user cannot create group' do
      shared_examples 'does not create new group' do
        it 'does not create new group' do
          expect(::Groups::CreateService).not_to receive(:new)

          expect { subject.load(context, data) }.to raise_error(described_class::GroupCreationError, 'User not allowed to create group')
        end
      end

      context 'when there is no parent group' do
        before do
          allow(Ability).to receive(:allowed?).with(user, :create_group).and_return(false)
        end

        include_examples 'does not create new group'
      end

      context 'when there is parent group' do
        let(:parent) { create(:group) }
        let(:data) { { 'parent_id' => parent.id, 'path' => 'test' } }

        before do
          allow(Ability).to receive(:allowed?).with(user, :create_subgroup, parent).and_return(false)
        end

        include_examples 'does not create new group'
      end
    end
  end
end