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

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/spec/lib
diff options
context:
space:
mode:
Diffstat (limited to 'spec/lib')
-rw-r--r--spec/lib/gitlab/import_export/group_object_builder_spec.rb44
-rw-r--r--spec/lib/gitlab/import_export/group_relation_factory_spec.rb120
-rw-r--r--spec/lib/gitlab/import_export/group_tree_restorer_spec.rb80
3 files changed, 244 insertions, 0 deletions
diff --git a/spec/lib/gitlab/import_export/group_object_builder_spec.rb b/spec/lib/gitlab/import_export/group_object_builder_spec.rb
new file mode 100644
index 00000000000..7950a937608
--- /dev/null
+++ b/spec/lib/gitlab/import_export/group_object_builder_spec.rb
@@ -0,0 +1,44 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+describe Gitlab::ImportExport::GroupObjectBuilder do
+ let(:group) { create(:group) }
+ let(:base_attributes) do
+ {
+ 'title' => 'title',
+ 'description' => 'description',
+ 'group' => group
+ }
+ end
+
+ context 'labels' do
+ let(:label_attributes) { base_attributes.merge('type' => 'GroupLabel') }
+
+ it 'finds the existing group label' do
+ group_label = create(:group_label, base_attributes)
+
+ expect(described_class.build(Label, label_attributes)).to eq(group_label)
+ end
+
+ it 'creates a new label' do
+ label = described_class.build(Label, label_attributes)
+
+ expect(label.persisted?).to be true
+ end
+ end
+
+ context 'milestones' do
+ it 'finds the existing group milestone' do
+ milestone = create(:milestone, base_attributes)
+
+ expect(described_class.build(Milestone, base_attributes)).to eq(milestone)
+ end
+
+ it 'creates a new milestone' do
+ milestone = described_class.build(Milestone, base_attributes)
+
+ expect(milestone.persisted?).to be true
+ end
+ end
+end
diff --git a/spec/lib/gitlab/import_export/group_relation_factory_spec.rb b/spec/lib/gitlab/import_export/group_relation_factory_spec.rb
new file mode 100644
index 00000000000..9208b2ad203
--- /dev/null
+++ b/spec/lib/gitlab/import_export/group_relation_factory_spec.rb
@@ -0,0 +1,120 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+describe Gitlab::ImportExport::GroupRelationFactory do
+ let(:group) { create(:group) }
+ let(:members_mapper) { double('members_mapper').as_null_object }
+ let(:user) { create(:admin) }
+ let(:excluded_keys) { [] }
+ let(:created_object) do
+ described_class.create(relation_sym: relation_sym,
+ relation_hash: relation_hash,
+ members_mapper: members_mapper,
+ object_builder: Gitlab::ImportExport::GroupObjectBuilder,
+ user: user,
+ importable: group,
+ excluded_keys: excluded_keys)
+ end
+
+ context 'label object' do
+ let(:relation_sym) { :group_label }
+ let(:id) { random_id }
+ let(:original_group_id) { random_id }
+
+ let(:relation_hash) do
+ {
+ 'id' => 123456,
+ 'title' => 'Bruffefunc',
+ 'color' => '#1d2da4',
+ 'project_id' => nil,
+ 'created_at' => '2019-11-20T17:02:20.546Z',
+ 'updated_at' => '2019-11-20T17:02:20.546Z',
+ 'template' => false,
+ 'description' => 'Description',
+ 'group_id' => original_group_id,
+ 'type' => 'GroupLabel',
+ 'priorities' => [],
+ 'textColor' => '#FFFFFF'
+ }
+ end
+
+ it 'does not have the original ID' do
+ expect(created_object.id).not_to eq(id)
+ end
+
+ it 'does not have the original group_id' do
+ expect(created_object.group_id).not_to eq(original_group_id)
+ end
+
+ it 'has the new group_id' do
+ expect(created_object.group_id).to eq(group.id)
+ end
+
+ context 'excluded attributes' do
+ let(:excluded_keys) { %w[description] }
+
+ it 'are removed from the imported object' do
+ expect(created_object.description).to be_nil
+ end
+ end
+ end
+
+ context 'Notes user references' do
+ let(:relation_sym) { :notes }
+ let(:new_user) { create(:user) }
+ let(:exported_member) do
+ {
+ 'id' => 111,
+ 'access_level' => 30,
+ 'source_id' => 1,
+ 'source_type' => 'Namespace',
+ 'user_id' => 3,
+ 'notification_level' => 3,
+ 'created_at' => '2016-11-18T09:29:42.634Z',
+ 'updated_at' => '2016-11-18T09:29:42.634Z',
+ 'user' => {
+ 'id' => 999,
+ 'email' => new_user.email,
+ 'username' => new_user.username
+ }
+ }
+ end
+
+ let(:relation_hash) do
+ {
+ 'id' => 4947,
+ 'note' => 'note',
+ 'noteable_type' => 'Epic',
+ 'author_id' => 999,
+ 'created_at' => '2016-11-18T09:29:42.634Z',
+ 'updated_at' => '2016-11-18T09:29:42.634Z',
+ 'project_id' => 1,
+ 'attachment' => {
+ 'url' => nil
+ },
+ 'noteable_id' => 377,
+ 'system' => true,
+ 'author' => {
+ 'name' => 'Administrator'
+ },
+ 'events' => []
+ }
+ end
+
+ let(:members_mapper) do
+ Gitlab::ImportExport::MembersMapper.new(
+ exported_members: [exported_member],
+ user: user,
+ importable: group)
+ end
+
+ it 'maps the right author to the imported note' do
+ expect(created_object.author).to eq(new_user)
+ end
+ end
+
+ def random_id
+ rand(1000..10000)
+ end
+end
diff --git a/spec/lib/gitlab/import_export/group_tree_restorer_spec.rb b/spec/lib/gitlab/import_export/group_tree_restorer_spec.rb
new file mode 100644
index 00000000000..0c55ed715cc
--- /dev/null
+++ b/spec/lib/gitlab/import_export/group_tree_restorer_spec.rb
@@ -0,0 +1,80 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+describe Gitlab::ImportExport::GroupTreeRestorer do
+ include ImportExport::CommonUtil
+
+ let(:shared) { Gitlab::ImportExport::Shared.new(group) }
+
+ describe 'restore group tree' do
+ before(:context) do
+ # Using an admin for import, so we can check assignment of existing members
+ user = create(:admin, username: 'root')
+ create(:user, username: 'adriene.mcclure')
+ create(:user, username: 'gwendolyn_robel')
+
+ RSpec::Mocks.with_temporary_scope do
+ @group = create(:group, name: 'group', path: 'group')
+ @shared = Gitlab::ImportExport::Shared.new(@group)
+
+ setup_import_export_config('group_exports/complex')
+
+ group_tree_restorer = described_class.new(user: user, shared: @shared, group: @group, group_hash: nil)
+
+ @restored_group_json = group_tree_restorer.restore
+ end
+ end
+
+ context 'JSON' do
+ it 'restores models based on JSON' do
+ expect(@restored_group_json).to be_truthy
+ end
+
+ it 'has the group description' do
+ expect(Group.find_by_path('group').description).to eq('Group Description')
+ end
+
+ it 'has group labels' do
+ expect(@group.labels.count).to eq(10)
+ end
+
+ it 'has issue boards' do
+ expect(@group.boards.count).to eq(2)
+ end
+
+ it 'has badges' do
+ expect(@group.badges.count).to eq(1)
+ end
+
+ it 'has milestones' do
+ expect(@group.milestones.count).to eq(5)
+ end
+
+ it 'has group children' do
+ expect(@group.children.count).to eq(2)
+ end
+
+ it 'has group members' do
+ expect(@group.members.map(&:user).map(&:username)).to contain_exactly('root', 'adriene.mcclure', 'gwendolyn_robel')
+ end
+ end
+ end
+
+ context 'group.json file access check' do
+ let(:user) { create(:user) }
+ let!(:group) { create(:group, name: 'group2', path: 'group2') }
+ let(:group_tree_restorer) { described_class.new(user: user, shared: shared, group: group, group_hash: nil) }
+ let(:restored_group_json) { group_tree_restorer.restore }
+
+ it 'does not read a symlink' do
+ Dir.mktmpdir do |tmpdir|
+ setup_symlink(tmpdir, 'group.json')
+ allow(shared).to receive(:export_path).and_call_original
+
+ expect(group_tree_restorer.restore).to eq(false)
+ expect(shared.errors).to include('Incorrect JSON format')
+ end
+ end
+ end
+end