diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2019-11-12 00:06:20 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2019-11-12 00:06:20 +0300 |
commit | 0c3f12149372a79b825d265a6c28dc547e4a1afc (patch) | |
tree | 43bdaa20afb0b061d09c56d8507efd51d28601be /spec/lib | |
parent | ff67e3ed08355fb2d6f6e69d4ed06cd09052e573 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/lib')
6 files changed, 215 insertions, 6 deletions
diff --git a/spec/lib/gitlab/import_export/group_tree_saver_spec.rb b/spec/lib/gitlab/import_export/group_tree_saver_spec.rb new file mode 100644 index 00000000000..c752c557d99 --- /dev/null +++ b/spec/lib/gitlab/import_export/group_tree_saver_spec.rb @@ -0,0 +1,162 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Gitlab::ImportExport::GroupTreeSaver do + describe 'saves the group tree into a json object' do + let(:shared) { Gitlab::ImportExport::Shared.new(group) } + let(:group_tree_saver) { described_class.new(group: group, current_user: user, shared: shared) } + let(:export_path) { "#{Dir.tmpdir}/group_tree_saver_spec" } + let(:user) { create(:user) } + let!(:group) { setup_group } + + before do + group.add_maintainer(user) + allow_any_instance_of(Gitlab::ImportExport).to receive(:storage_path).and_return(export_path) + end + + after do + FileUtils.rm_rf(export_path) + end + + it 'saves group successfully' do + expect(group_tree_saver.save).to be true + end + + context ':export_fast_serialize feature flag checks' do + before do + expect(Gitlab::ImportExport::Reader).to receive(:new).with(shared: shared, config: group_config).and_return(reader) + expect(reader).to receive(:group_tree).and_return(group_tree) + end + + let(:reader) { instance_double('Gitlab::ImportExport::Reader') } + let(:group_config) { Gitlab::ImportExport::Config.new(config: Gitlab::ImportExport.group_config_file).to_h } + let(:group_tree) do + { + include: [{ milestones: { include: [] } }], + preload: { milestones: nil } + } + end + + context 'when :export_fast_serialize feature is enabled' do + before do + stub_feature_flags(export_fast_serialize: true) + end + + it 'uses FastHashSerializer' do + expect_any_instance_of(Gitlab::ImportExport::FastHashSerializer).to receive(:execute).and_call_original + + group_tree_saver.save + end + end + + context 'when :export_fast_serialize feature is disabled' do + before do + stub_feature_flags(export_fast_serialize: false) + end + + it 'is serialized via built-in `as_json`' do + expect(group).to receive(:as_json).with(group_tree).and_call_original + + group_tree_saver.save + end + end + end + + # It is mostly duplicated in + # `spec/lib/gitlab/import_export/fast_hash_serializer_spec.rb` + # except: + # context 'with description override' do + # context 'group members' do + # ^ These are specific for the groupTreeSaver + context 'JSON' do + let(:saved_group_json) do + group_tree_saver.save + group_json(group_tree_saver.full_path) + end + + it 'saves the correct json' do + expect(saved_group_json).to include({ 'description' => 'description', 'visibility_level' => 20 }) + end + + it 'has milestones' do + expect(saved_group_json['milestones']).not_to be_empty + end + + it 'has labels' do + expect(saved_group_json['labels']).not_to be_empty + end + + it 'has boards' do + expect(saved_group_json['boards']).not_to be_empty + end + + it 'has group members' do + expect(saved_group_json['members']).not_to be_empty + end + + it 'has priorities associated to labels' do + expect(saved_group_json['labels'].first['priorities']).not_to be_empty + end + + it 'has badges' do + expect(saved_group_json['badges']).not_to be_empty + end + + context 'group members' do + let(:user2) { create(:user, email: 'group@member.com') } + let(:member_emails) do + saved_group_json['members'].map do |pm| + pm['user']['email'] + end + end + + before do + group.add_developer(user2) + end + + it 'exports group members as group owner' do + group.add_owner(user) + + expect(member_emails).to include('group@member.com') + end + + context 'as admin' do + let(:user) { create(:admin) } + + it 'exports group members as admin' do + expect(member_emails).to include('group@member.com') + end + + it 'exports group members' do + member_types = saved_group_json['members'].map { |pm| pm['source_type'] } + + expect(member_types).to all(eq('Namespace')) + end + end + end + + context 'group attributes' do + it 'does not contain the runners token' do + expect(saved_group_json).not_to include("runners_token" => 'token') + end + end + end + end + + def setup_group + group = create(:group, description: 'description') + create(:milestone, group: group) + create(:group_badge, group: group) + group_label = create(:group_label, group: group) + create(:label_priority, label: group_label, priority: 1) + create(:board, group: group) + create(:group_badge, group: group) + + group + end + + def group_json(filename) + JSON.parse(IO.read(filename)) + end +end diff --git a/spec/lib/gitlab/import_export/import_export_spec.rb b/spec/lib/gitlab/import_export/import_export_spec.rb index 40a5f2294a2..a6b0dc758cd 100644 --- a/spec/lib/gitlab/import_export/import_export_spec.rb +++ b/spec/lib/gitlab/import_export/import_export_spec.rb @@ -6,17 +6,17 @@ describe Gitlab::ImportExport do let(:project) { create(:project, :public, path: 'project-path', namespace: group) } it 'contains the project path' do - expect(described_class.export_filename(project: project)).to include(project.path) + expect(described_class.export_filename(exportable: project)).to include(project.path) end it 'contains the namespace path' do - expect(described_class.export_filename(project: project)).to include(project.namespace.full_path.tr('/', '_')) + expect(described_class.export_filename(exportable: project)).to include(project.namespace.full_path.tr('/', '_')) end it 'does not go over a certain length' do project.path = 'a' * 100 - expect(described_class.export_filename(project: project).length).to be < 70 + expect(described_class.export_filename(exportable: project).length).to be < 70 end end end diff --git a/spec/lib/gitlab/import_export/relation_rename_service_spec.rb b/spec/lib/gitlab/import_export/relation_rename_service_spec.rb index 843de27df1a..d62f5725f9e 100644 --- a/spec/lib/gitlab/import_export/relation_rename_service_spec.rb +++ b/spec/lib/gitlab/import_export/relation_rename_service_spec.rb @@ -96,15 +96,20 @@ describe Gitlab::ImportExport::RelationRenameService do let(:export_content_path) { project_tree_saver.full_path } let(:export_content_hash) { ActiveSupport::JSON.decode(File.read(export_content_path)) } let(:injected_hash) { renames.values.product([{}]).to_h } + let(:relation_tree_saver) { Gitlab::ImportExport::RelationTreeSaver.new } let(:project_tree_saver) do Gitlab::ImportExport::ProjectTreeSaver.new( project: project, current_user: user, shared: shared) end + before do + allow(project_tree_saver).to receive(:tree_saver).and_return(relation_tree_saver) + end + it 'adds old relationships to the exported file' do # we inject relations with new names that should be rewritten - expect(project_tree_saver).to receive(:serialize_project_tree).and_wrap_original do |method, *args| + expect(relation_tree_saver).to receive(:serialize).and_wrap_original do |method, *args| method.call(*args).merge(injected_hash) end diff --git a/spec/lib/gitlab/import_export/relation_tree_saver_spec.rb b/spec/lib/gitlab/import_export/relation_tree_saver_spec.rb new file mode 100644 index 00000000000..2fc26c0e3d4 --- /dev/null +++ b/spec/lib/gitlab/import_export/relation_tree_saver_spec.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Gitlab::ImportExport::RelationTreeSaver do + let(:exportable) { create(:group) } + let(:relation_tree_saver) { described_class.new } + let(:tree) { {} } + + describe '#serialize' do + context 'when :export_fast_serialize feature is enabled' do + let(:serializer) { instance_double(Gitlab::ImportExport::FastHashSerializer) } + + before do + stub_feature_flags(export_fast_serialize: true) + end + + it 'uses FastHashSerializer' do + expect(Gitlab::ImportExport::FastHashSerializer) + .to receive(:new) + .with(exportable, tree) + .and_return(serializer) + + expect(serializer).to receive(:execute) + + relation_tree_saver.serialize(exportable, tree) + end + end + + context 'when :export_fast_serialize feature is disabled' do + before do + stub_feature_flags(export_fast_serialize: false) + end + + it 'is serialized via built-in `as_json`' do + expect(exportable).to receive(:as_json).with(tree) + + relation_tree_saver.serialize(exportable, tree) + end + end + end +end diff --git a/spec/lib/gitlab/import_export/saver_spec.rb b/spec/lib/gitlab/import_export/saver_spec.rb index d185ff2dfcc..aca63953677 100644 --- a/spec/lib/gitlab/import_export/saver_spec.rb +++ b/spec/lib/gitlab/import_export/saver_spec.rb @@ -5,7 +5,7 @@ describe Gitlab::ImportExport::Saver do let!(:project) { create(:project, :public, name: 'project') } let(:export_path) { "#{Dir.tmpdir}/project_tree_saver_spec" } let(:shared) { project.import_export_shared } - subject { described_class.new(project: project, shared: shared) } + subject { described_class.new(exportable: project, shared: shared) } before do allow_any_instance_of(Gitlab::ImportExport).to receive(:storage_path).and_return(export_path) diff --git a/spec/lib/gitlab/import_export/shared_spec.rb b/spec/lib/gitlab/import_export/shared_spec.rb index 62669836973..fc011f7e1be 100644 --- a/spec/lib/gitlab/import_export/shared_spec.rb +++ b/spec/lib/gitlab/import_export/shared_spec.rb @@ -7,7 +7,7 @@ describe Gitlab::ImportExport::Shared do context 'with a repository on disk' do let(:project) { create(:project, :repository) } - let(:base_path) { %(/tmp/project_exports/#{project.disk_path}/) } + let(:base_path) { %(/tmp/gitlab_exports/#{project.disk_path}/) } describe '#archive_path' do it 'uses a random hash to avoid conflicts' do |