Welcome to mirror list, hosted at ThFree Co, Russian Federation.

group_attributes_transformer_spec.rb « transformers « groups « bulk_imports « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 896af865c56c79bf5e4fb8178e42ce950bc861dd (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe BulkImports::Groups::Transformers::GroupAttributesTransformer do
  describe '#transform' do
    let_it_be(:parent) { create(:group) }

    let(:bulk_import) { build_stubbed(:bulk_import) }

    let(:entity) do
      build_stubbed(
        :bulk_import_entity,
        bulk_import: bulk_import,
        source_full_path: 'source/full/path',
        destination_slug: 'destination-slug-path',
        destination_namespace: parent.full_path
      )
    end

    let(:tracker) { build_stubbed(:bulk_import_tracker, entity: entity) }
    let(:context) { BulkImports::Pipeline::Context.new(tracker) }

    let(:data) do
      {
        'name' => 'Source Group Name',
        'path' => 'source-group-path',
        'full_path' => 'source/full/path',
        'visibility' => 'private',
        'project_creation_level' => 'developer',
        'subgroup_creation_level' => 'maintainer'
      }
    end

    subject { described_class.new }

    it 'returns original data with some keys transformed' do
      transformed_data = subject.transform(context, { 'name' => 'Name', 'description' => 'Description' })

      expect(transformed_data).to eq({
        'name' => 'Name',
        'description' => 'Description',
        'parent_id' => parent.id,
        'path' => 'destination-slug-path'
      })
    end

    it 'transforms path from destination_slug' do
      transformed_data = subject.transform(context, data)

      expect(transformed_data['path']).to eq(entity.destination_slug)
    end

    it 'removes full path' do
      transformed_data = subject.transform(context, data)

      expect(transformed_data).not_to have_key('full_path')
    end

    it 'transforms visibility level' do
      visibility = data['visibility']
      transformed_data = subject.transform(context, data)

      expect(transformed_data).not_to have_key('visibility')
      expect(transformed_data['visibility_level']).to eq(Gitlab::VisibilityLevel.string_options[visibility])
    end

    it 'transforms project creation level' do
      level = data['project_creation_level']
      transformed_data = subject.transform(context, data)

      expect(transformed_data['project_creation_level']).to eq(Gitlab::Access.project_creation_string_options[level])
    end

    it 'transforms subgroup creation level' do
      level = data['subgroup_creation_level']
      transformed_data = subject.transform(context, data)

      expect(transformed_data['subgroup_creation_level']).to eq(Gitlab::Access.subgroup_creation_string_options[level])
    end

    describe 'parent group transformation' do
      it 'sets parent id' do
        transformed_data = subject.transform(context, data)

        expect(transformed_data['parent_id']).to eq(parent.id)
      end

      context 'when destination namespace is empty' do
        before do
          entity.destination_namespace = ''
        end

        it 'does not set parent id' do
          transformed_data = subject.transform(context, data)

          expect(transformed_data).not_to have_key('parent_id')
        end
      end
    end

    describe 'group name transformation' do
      context 'when destination namespace is empty' do
        before do
          entity.destination_namespace = ''
        end

        it 'does not transform name' do
          transformed_data = subject.transform(context, data)

          expect(transformed_data['name']).to eq('Source Group Name')
        end
      end

      context 'when destination namespace is present' do
        context 'when destination namespace does not have a group with same name' do
          it 'does not transform name' do
            transformed_data = subject.transform(context, data)

            expect(transformed_data['name']).to eq('Source Group Name')
          end
        end

        context 'when destination namespace already have a group with the same name' do
          before do
            create(:group, parent: parent, name: 'Source Group Name', path: 'group_1')
            create(:group, parent: parent, name: 'Source Group Name(1)', path: 'group_2')
            create(:group, parent: parent, name: 'Source Group Name(2)', path: 'group_3')
            create(:group, parent: parent, name: 'Source Group Name(1)(1)', path: 'group_4')
          end

          it 'makes the name unique by appeding a counter', :aggregate_failures do
            transformed_data = subject.transform(context, data.merge('name' => 'Source Group Name'))
            expect(transformed_data['name']).to eq('Source Group Name(3)')

            transformed_data = subject.transform(context, data.merge('name' => 'Source Group Name(2)'))
            expect(transformed_data['name']).to eq('Source Group Name(2)(1)')

            transformed_data = subject.transform(context, data.merge('name' => 'Source Group Name(1)'))
            expect(transformed_data['name']).to eq('Source Group Name(1)(2)')

            transformed_data = subject.transform(context, data.merge('name' => 'Source Group Name(1)(1)'))
            expect(transformed_data['name']).to eq('Source Group Name(1)(1)(1)')
          end
        end
      end
    end
  end
end