diff options
Diffstat (limited to 'spec/lib/gitlab/import_export')
37 files changed, 369 insertions, 105 deletions
diff --git a/spec/lib/gitlab/import_export/after_export_strategies/base_after_export_strategy_spec.rb b/spec/lib/gitlab/import_export/after_export_strategies/base_after_export_strategy_spec.rb index a3d2880182d..86ceb97b250 100644 --- a/spec/lib/gitlab/import_export/after_export_strategies/base_after_export_strategy_spec.rb +++ b/spec/lib/gitlab/import_export/after_export_strategies/base_after_export_strategy_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Gitlab::ImportExport::AfterExportStrategies::BaseAfterExportStrategy do diff --git a/spec/lib/gitlab/import_export/after_export_strategies/web_upload_strategy_spec.rb b/spec/lib/gitlab/import_export/after_export_strategies/web_upload_strategy_spec.rb index 21a227335cd..95c47d15f8f 100644 --- a/spec/lib/gitlab/import_export/after_export_strategies/web_upload_strategy_spec.rb +++ b/spec/lib/gitlab/import_export/after_export_strategies/web_upload_strategy_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Gitlab::ImportExport::AfterExportStrategies::WebUploadStrategy do diff --git a/spec/lib/gitlab/import_export/after_export_strategy_builder_spec.rb b/spec/lib/gitlab/import_export/after_export_strategy_builder_spec.rb index bf727285a9f..9fe9e2eb73d 100644 --- a/spec/lib/gitlab/import_export/after_export_strategy_builder_spec.rb +++ b/spec/lib/gitlab/import_export/after_export_strategy_builder_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Gitlab::ImportExport::AfterExportStrategyBuilder do diff --git a/spec/lib/gitlab/import_export/all_models.yml b/spec/lib/gitlab/import_export/all_models.yml index 8f627fcc24d..2ea563c50b6 100644 --- a/spec/lib/gitlab/import_export/all_models.yml +++ b/spec/lib/gitlab/import_export/all_models.yml @@ -8,6 +8,7 @@ issues: - milestone - notes - resource_label_events +- sentry_issue - label_links - labels - last_edited_by @@ -15,6 +16,7 @@ issues: - user_agent_detail - moved_to - duplicated_to +- promoted_to_epic - events - merge_requests_closing_issues - metrics @@ -32,6 +34,7 @@ issues: - zoom_meetings - vulnerability_links - related_vulnerabilities +- user_mentions events: - author - project @@ -80,6 +83,7 @@ snippets: - notes - award_emoji - user_agent_detail +- user_mentions releases: - author - project @@ -138,6 +142,9 @@ merge_requests: - blocking_merge_requests - blocked_merge_requests - description_versions +- deployment_merge_requests +- deployments +- user_mentions external_pull_requests: - project merge_request_diff: @@ -287,6 +294,7 @@ project: - microsoft_teams_service - mattermost_service - hangouts_chat_service +- unify_circuit_service - buildkite_service - bamboo_service - teamcity_service @@ -364,6 +372,7 @@ project: - root_of_fork_network - fork_network_member - fork_network +- fork_network_projects - custom_attributes - lfs_file_locks - project_badges @@ -432,6 +441,9 @@ project: - downstream_projects - upstream_project_subscriptions - downstream_project_subscriptions +- service_desk_setting +- import_failures +- container_expiration_policy award_emoji: - awardable - user @@ -531,14 +543,18 @@ design: &design - actions - versions - notes +- user_mentions designs: *design actions: - design - version versions: &version +- author - issue - designs - actions zoom_meetings: - issue +sentry_issue: +- issue design_versions: *version diff --git a/spec/lib/gitlab/import_export/attribute_cleaner_spec.rb b/spec/lib/gitlab/import_export/attribute_cleaner_spec.rb index 1b28e26a7e8..44192c4639d 100644 --- a/spec/lib/gitlab/import_export/attribute_cleaner_spec.rb +++ b/spec/lib/gitlab/import_export/attribute_cleaner_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Gitlab::ImportExport::AttributeCleaner do diff --git a/spec/lib/gitlab/import_export/attribute_configuration_spec.rb b/spec/lib/gitlab/import_export/attribute_configuration_spec.rb index cc8ca1d87e3..58da25bbedb 100644 --- a/spec/lib/gitlab/import_export/attribute_configuration_spec.rb +++ b/spec/lib/gitlab/import_export/attribute_configuration_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' # Part of the test security suite for the Import/Export feature @@ -10,21 +12,11 @@ require 'spec_helper' describe 'Import/Export attribute configuration' do include ConfigurationHelper - let(:config_hash) { Gitlab::ImportExport::Config.new.to_h.deep_stringify_keys } - let(:relation_names) do - names = names_from_tree(config_hash.dig('tree', 'project')) - - # Remove duplicated or add missing models - # - project is not part of the tree, so it has to be added manually. - # - milestone, labels have both singular and plural versions in the tree, so remove the duplicates. - names.flatten.uniq - %w(milestones labels) + ['project'] - end - let(:safe_attributes_file) { 'spec/lib/gitlab/import_export/safe_model_attributes.yml' } let(:safe_model_attributes) { YAML.load_file(safe_attributes_file) } it 'has no new columns' do - relation_names.each do |relation_name| + relation_names_for(:project).each do |relation_name| relation_class = relation_class_for_name(relation_name) relation_attributes = relation_class.new.attributes.keys - relation_class.encrypted_attributes.keys.map(&:to_s) diff --git a/spec/lib/gitlab/import_export/avatar_restorer_spec.rb b/spec/lib/gitlab/import_export/avatar_restorer_spec.rb index e44ff6bbcbd..662e1a5eaab 100644 --- a/spec/lib/gitlab/import_export/avatar_restorer_spec.rb +++ b/spec/lib/gitlab/import_export/avatar_restorer_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Gitlab::ImportExport::AvatarRestorer do @@ -12,8 +14,9 @@ describe Gitlab::ImportExport::AvatarRestorer do context 'with avatar' do before do - allow_any_instance_of(described_class).to receive(:avatar_export_file) - .and_return(uploaded_image_temp_path) + allow_next_instance_of(described_class) do |instance| + allow(instance).to receive(:avatar_export_file).and_return(uploaded_image_temp_path) + end end it 'restores a project avatar' do @@ -31,8 +34,9 @@ describe Gitlab::ImportExport::AvatarRestorer do Dir.mktmpdir do |tmpdir| FileUtils.mkdir_p("#{tmpdir}/a/b") - allow_any_instance_of(described_class).to receive(:avatar_export_path) - .and_return("#{tmpdir}/a") + allow_next_instance_of(described_class) do |instance| + allow(instance).to receive(:avatar_export_path).and_return("#{tmpdir}/a") + end expect(described_class.new(project: project, shared: shared).restore).to be true end diff --git a/spec/lib/gitlab/import_export/avatar_saver_spec.rb b/spec/lib/gitlab/import_export/avatar_saver_spec.rb index 2bd1b9924c6..d2349e47c0a 100644 --- a/spec/lib/gitlab/import_export/avatar_saver_spec.rb +++ b/spec/lib/gitlab/import_export/avatar_saver_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Gitlab::ImportExport::AvatarSaver do @@ -8,7 +10,9 @@ describe Gitlab::ImportExport::AvatarSaver do before do FileUtils.mkdir_p("#{shared.export_path}/avatar/") - allow_any_instance_of(Gitlab::ImportExport::Shared).to receive(:export_path).and_return(export_path) + allow_next_instance_of(Gitlab::ImportExport::Shared) do |instance| + allow(instance).to receive(:export_path).and_return(export_path) + end end after do diff --git a/spec/lib/gitlab/import_export/fast_hash_serializer_spec.rb b/spec/lib/gitlab/import_export/fast_hash_serializer_spec.rb index b190a1007a0..6f90798f815 100644 --- a/spec/lib/gitlab/import_export/fast_hash_serializer_spec.rb +++ b/spec/lib/gitlab/import_export/fast_hash_serializer_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Gitlab::ImportExport::FastHashSerializer do diff --git a/spec/lib/gitlab/import_export/file_importer_spec.rb b/spec/lib/gitlab/import_export/file_importer_spec.rb index fbc9bcd2df5..7c54c5f2da1 100644 --- a/spec/lib/gitlab/import_export/file_importer_spec.rb +++ b/spec/lib/gitlab/import_export/file_importer_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Gitlab::ImportExport::FileImporter do @@ -16,9 +18,15 @@ describe Gitlab::ImportExport::FileImporter do stub_const('Gitlab::ImportExport::FileImporter::MAX_RETRIES', 0) stub_uploads_object_storage(FileUploader) - allow_any_instance_of(Gitlab::ImportExport).to receive(:storage_path).and_return(storage_path) - allow_any_instance_of(Gitlab::ImportExport::CommandLineUtil).to receive(:untar_zxf).and_return(true) - allow_any_instance_of(Gitlab::ImportExport::Shared).to receive(:relative_archive_path).and_return('test') + allow_next_instance_of(Gitlab::ImportExport) do |instance| + allow(instance).to receive(:storage_path).and_return(storage_path) + end + allow_next_instance_of(Gitlab::ImportExport::CommandLineUtil) do |instance| + allow(instance).to receive(:untar_zxf).and_return(true) + end + allow_next_instance_of(Gitlab::ImportExport::Shared) do |instance| + allow(instance).to receive(:relative_archive_path).and_return('test') + end allow(SecureRandom).to receive(:hex).and_return('abcd') setup_files end @@ -29,7 +37,7 @@ describe Gitlab::ImportExport::FileImporter do context 'normal run' do before do - described_class.import(project: build(:project), archive_file: '', shared: shared) + described_class.import(importable: build(:project), archive_file: '', shared: shared) end it 'removes symlinks in root folder' do @@ -67,8 +75,10 @@ describe Gitlab::ImportExport::FileImporter do context 'error' do before do - allow_any_instance_of(described_class).to receive(:wait_for_archived_file).and_raise(StandardError) - described_class.import(project: build(:project), archive_file: '', shared: shared) + allow_next_instance_of(described_class) do |instance| + allow(instance).to receive(:wait_for_archived_file).and_raise(StandardError) + end + described_class.import(importable: build(:project), archive_file: '', shared: shared) end it 'removes symlinks in root folder' do diff --git a/spec/lib/gitlab/import_export/fork_spec.rb b/spec/lib/gitlab/import_export/fork_spec.rb index 5752fd8fa0d..09e4f62c686 100644 --- a/spec/lib/gitlab/import_export/fork_spec.rb +++ b/spec/lib/gitlab/import_export/fork_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'forked project import' do @@ -30,7 +32,9 @@ describe 'forked project import' do end before do - allow_any_instance_of(Gitlab::ImportExport).to receive(:storage_path).and_return(export_path) + allow_next_instance_of(Gitlab::ImportExport) do |instance| + allow(instance).to receive(:storage_path).and_return(export_path) + end saver.save repo_saver.save diff --git a/spec/lib/gitlab/import_export/group_project_object_builder_spec.rb b/spec/lib/gitlab/import_export/group_project_object_builder_spec.rb index 1a5cb7806a3..0d0a2df4423 100644 --- a/spec/lib/gitlab/import_export/group_project_object_builder_spec.rb +++ b/spec/lib/gitlab/import_export/group_project_object_builder_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Gitlab::ImportExport::GroupProjectObjectBuilder do diff --git a/spec/lib/gitlab/import_export/hash_util_spec.rb b/spec/lib/gitlab/import_export/hash_util_spec.rb index 366582dece3..ddd874ddecf 100644 --- a/spec/lib/gitlab/import_export/hash_util_spec.rb +++ b/spec/lib/gitlab/import_export/hash_util_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Gitlab::ImportExport::HashUtil do diff --git a/spec/lib/gitlab/import_export/import_export_spec.rb b/spec/lib/gitlab/import_export/import_export_spec.rb index a6b0dc758cd..2ece0dd4b56 100644 --- a/spec/lib/gitlab/import_export/import_export_spec.rb +++ b/spec/lib/gitlab/import_export/import_export_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Gitlab::ImportExport do diff --git a/spec/lib/gitlab/import_export/importer_spec.rb b/spec/lib/gitlab/import_export/importer_spec.rb index 898e4d07760..942af4084e5 100644 --- a/spec/lib/gitlab/import_export/importer_spec.rb +++ b/spec/lib/gitlab/import_export/importer_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Gitlab::ImportExport::Importer do diff --git a/spec/lib/gitlab/import_export/lfs_restorer_spec.rb b/spec/lib/gitlab/import_export/lfs_restorer_spec.rb index 2b0bdb909ae..a932dc3ee4e 100644 --- a/spec/lib/gitlab/import_export/lfs_restorer_spec.rb +++ b/spec/lib/gitlab/import_export/lfs_restorer_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Gitlab::ImportExport::LfsRestorer do @@ -7,6 +9,7 @@ describe Gitlab::ImportExport::LfsRestorer do let(:project) { create(:project) } let(:shared) { project.import_export_shared } let(:saver) { Gitlab::ImportExport::LfsSaver.new(project: project, shared: shared) } + subject(:restorer) { described_class.new(project: project, shared: shared) } before do diff --git a/spec/lib/gitlab/import_export/lfs_saver_spec.rb b/spec/lib/gitlab/import_export/lfs_saver_spec.rb index c3c88486e16..a8ff7867410 100644 --- a/spec/lib/gitlab/import_export/lfs_saver_spec.rb +++ b/spec/lib/gitlab/import_export/lfs_saver_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Gitlab::ImportExport::LfsSaver do @@ -8,7 +10,9 @@ describe Gitlab::ImportExport::LfsSaver do subject(:saver) { described_class.new(project: project, shared: shared) } before do - allow_any_instance_of(Gitlab::ImportExport).to receive(:storage_path).and_return(export_path) + allow_next_instance_of(Gitlab::ImportExport) do |instance| + allow(instance).to receive(:storage_path).and_return(export_path) + end FileUtils.mkdir_p(shared.export_path) end diff --git a/spec/lib/gitlab/import_export/members_mapper_spec.rb b/spec/lib/gitlab/import_export/members_mapper_spec.rb index a9e8431acba..01a7901062a 100644 --- a/spec/lib/gitlab/import_export/members_mapper_spec.rb +++ b/spec/lib/gitlab/import_export/members_mapper_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Gitlab::ImportExport::MembersMapper do @@ -25,7 +27,7 @@ describe Gitlab::ImportExport::MembersMapper do "email" => user2.email, "username" => 'test' }, - "user_id" => 19 + "user_id" => 19 }, { "id" => 3, @@ -45,7 +47,7 @@ describe Gitlab::ImportExport::MembersMapper do let(:members_mapper) do described_class.new( - exported_members: exported_members, user: user, project: project) + exported_members: exported_members, user: user, importable: project) end it 'includes the exported user ID in the map' do @@ -81,7 +83,8 @@ describe Gitlab::ImportExport::MembersMapper do end it 'removes old user_id from member_hash to avoid conflict with user key' do - expect(ProjectMember).to receive(:create) + expect(ProjectMember) + .to receive(:create) .twice .with(hash_excluding('user_id')) .and_call_original @@ -115,7 +118,7 @@ describe Gitlab::ImportExport::MembersMapper do let(:project) { create(:project, :public, name: 'searchable_project', namespace: group) } let(:members_mapper) do described_class.new( - exported_members: exported_members, user: user2, project: project) + exported_members: exported_members, user: user2, importable: project) end before do @@ -138,7 +141,7 @@ describe Gitlab::ImportExport::MembersMapper do let(:project) { create(:project, namespace: group) } let(:members_mapper) do described_class.new( - exported_members: exported_members, user: user, project: project) + exported_members: exported_members, user: user, importable: project) end before do @@ -161,7 +164,7 @@ describe Gitlab::ImportExport::MembersMapper do it 'includes importer specific error message' do expect(ProjectMember).to receive(:create!).and_raise(StandardError.new(exception_message)) - expect { members_mapper.map }.to raise_error(StandardError, "Error adding importer user to project members. #{exception_message}") + expect { members_mapper.map }.to raise_error(StandardError, "Error adding importer user to Project members. #{exception_message}") end end end diff --git a/spec/lib/gitlab/import_export/merge_request_parser_spec.rb b/spec/lib/gitlab/import_export/merge_request_parser_spec.rb index 4b234411a44..c437efede4c 100644 --- a/spec/lib/gitlab/import_export/merge_request_parser_spec.rb +++ b/spec/lib/gitlab/import_export/merge_request_parser_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Gitlab::ImportExport::MergeRequestParser do @@ -33,9 +35,11 @@ describe Gitlab::ImportExport::MergeRequestParser do end it 'parses a MR that has no source branch' do - allow_any_instance_of(described_class).to receive(:branch_exists?).and_call_original - allow_any_instance_of(described_class).to receive(:branch_exists?).with(merge_request.source_branch).and_return(false) - allow_any_instance_of(described_class).to receive(:fork_merge_request?).and_return(true) + allow_next_instance_of(described_class) do |instance| + allow(instance).to receive(:branch_exists?).and_call_original + allow(instance).to receive(:branch_exists?).with(merge_request.source_branch).and_return(false) + allow(instance).to receive(:fork_merge_request?).and_return(true) + end allow(Gitlab::GitalyClient).to receive(:migrate).and_call_original allow(Gitlab::GitalyClient).to receive(:migrate).with(:fetch_ref).and_return([nil, 0]) diff --git a/spec/lib/gitlab/import_export/model_configuration_spec.rb b/spec/lib/gitlab/import_export/model_configuration_spec.rb index 4426e68b474..cfbfe244988 100644 --- a/spec/lib/gitlab/import_export/model_configuration_spec.rb +++ b/spec/lib/gitlab/import_export/model_configuration_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' # Part of the test security suite for the Import/Export feature @@ -6,19 +8,10 @@ require 'spec_helper' describe 'Import/Export model configuration' do include ConfigurationHelper - let(:config_hash) { Gitlab::ImportExport::Config.new.to_h.deep_stringify_keys } - let(:model_names) do - names = names_from_tree(config_hash.dig('tree', 'project')) - - # Remove duplicated or add missing models - # - project is not part of the tree, so it has to be added manually. - # - milestone, labels, merge_request have both singular and plural versions in the tree, so remove the duplicates. - # - User, Author... Models we do not care about for checking models - names.flatten.uniq - %w(milestones labels user author merge_request) + ['project'] - end let(:all_models_yml) { 'spec/lib/gitlab/import_export/all_models.yml' } let(:all_models_hash) { YAML.load_file(all_models_yml) } let(:current_models) { setup_models } + let(:model_names) { relation_names_for(:project) } it 'has no new models' do model_names.each do |model_name| diff --git a/spec/lib/gitlab/import_export/project_tree_restorer_spec.rb b/spec/lib/gitlab/import_export/project_tree_restorer_spec.rb index 64a648ca1f8..ec1b935ad63 100644 --- a/spec/lib/gitlab/import_export/project_tree_restorer_spec.rb +++ b/spec/lib/gitlab/import_export/project_tree_restorer_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' include ImportExport::CommonUtil @@ -29,9 +31,6 @@ describe Gitlab::ImportExport::ProjectTreeRestorer do project_tree_restorer = described_class.new(user: @user, shared: @shared, project: @project) - expect(Gitlab::ImportExport::RelationFactory).to receive(:create).with(hash_including(excluded_keys: ['whatever'])).and_call_original.at_least(:once) - allow(project_tree_restorer).to receive(:excluded_keys_for_relation).and_return(['whatever']) - @restored_project_json = project_tree_restorer.restore end end @@ -235,6 +234,22 @@ describe Gitlab::ImportExport::ProjectTreeRestorer do expect(meetings.first.url).to eq('https://zoom.us/j/123456789') end + it 'restores sentry issues' do + sentry_issue = @project.issues.first.sentry_issue + + expect(sentry_issue.sentry_issue_identifier).to eq(1234567891) + end + + it 'restores container_expiration_policy' do + policy = Project.find_by_path('project').container_expiration_policy + + aggregate_failures do + expect(policy).to be_an_instance_of(ContainerExpirationPolicy) + expect(policy).to be_persisted + expect(policy.cadence).to eq('3month') + end + end + context 'Merge requests' do it 'always has the new project as a target' do expect(MergeRequest.find_by_title('MR1').target_project).to eq(@project) @@ -360,7 +375,7 @@ describe Gitlab::ImportExport::ProjectTreeRestorer do expect(restored_project_json).to eq(true) end - it_behaves_like 'restores project correctly', + it_behaves_like 'restores project successfully', issues: 1, labels: 2, label_with_priorities: 'A project label', @@ -373,7 +388,7 @@ describe Gitlab::ImportExport::ProjectTreeRestorer do create(:ci_build, token: 'abcd') end - it_behaves_like 'restores project correctly', + it_behaves_like 'restores project successfully', issues: 1, labels: 2, label_with_priorities: 'A project label', @@ -450,7 +465,7 @@ describe Gitlab::ImportExport::ProjectTreeRestorer do expect(restored_project_json).to eq(true) end - it_behaves_like 'restores project correctly', + it_behaves_like 'restores project successfully', issues: 2, labels: 2, label_with_priorities: 'A project label', @@ -555,8 +570,9 @@ describe Gitlab::ImportExport::ProjectTreeRestorer do context 'Minimal JSON' do let(:project) { create(:project) } + let(:user) { create(:user) } let(:tree_hash) { { 'visibility_level' => visibility } } - let(:restorer) { described_class.new(user: nil, shared: shared, project: project) } + let(:restorer) { described_class.new(user: user, shared: shared, project: project) } before do expect(restorer).to receive(:read_tree_hash) { tree_hash } @@ -631,4 +647,44 @@ describe Gitlab::ImportExport::ProjectTreeRestorer do end end end + + context 'JSON with invalid records' do + subject(:restored_project_json) { project_tree_restorer.restore } + + let(:user) { create(:user) } + let!(:project) { create(:project, :builds_disabled, :issues_disabled, name: 'project', path: 'project') } + let(:project_tree_restorer) { described_class.new(user: user, shared: shared, project: project) } + let(:correlation_id) { 'my-correlation-id' } + + before do + setup_import_export_config('with_invalid_records') + + # Import is running from the rake task, `correlation_id` is not assigned + expect(Labkit::Correlation::CorrelationId).to receive(:new_id).and_return(correlation_id) + subject + end + + context 'when failures occur because a relation fails to be processed' do + it_behaves_like 'restores project successfully', + issues: 0, + labels: 0, + label_with_priorities: nil, + milestones: 1, + first_issue_labels: 0, + services: 0, + import_failures: 1 + + it 'records the failures in the database' do + import_failure = ImportFailure.last + + expect(import_failure.project_id).to eq(project.id) + expect(import_failure.relation_key).to eq('milestones') + expect(import_failure.relation_index).to be_present + expect(import_failure.exception_class).to eq('ActiveRecord::RecordInvalid') + expect(import_failure.exception_message).to be_present + expect(import_failure.correlation_id_value).to eq('my-correlation-id') + expect(import_failure.created_at).to be_present + end + end + end end diff --git a/spec/lib/gitlab/import_export/project_tree_saver_spec.rb b/spec/lib/gitlab/import_export/project_tree_saver_spec.rb index 97d8b155826..29d0099d5c1 100644 --- a/spec/lib/gitlab/import_export/project_tree_saver_spec.rb +++ b/spec/lib/gitlab/import_export/project_tree_saver_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Gitlab::ImportExport::ProjectTreeSaver do diff --git a/spec/lib/gitlab/import_export/reader_spec.rb b/spec/lib/gitlab/import_export/reader_spec.rb index 87f665bd995..e37ad281eb5 100644 --- a/spec/lib/gitlab/import_export/reader_spec.rb +++ b/spec/lib/gitlab/import_export/reader_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Gitlab::ImportExport::Reader do @@ -7,19 +9,18 @@ describe Gitlab::ImportExport::Reader do subject { described_class.new(shared: shared).project_tree } it 'delegates to AttributesFinder#find_root' do - expect_any_instance_of(Gitlab::ImportExport::AttributesFinder) - .to receive(:find_root) - .with(:project) + expect_next_instance_of(Gitlab::ImportExport::AttributesFinder) do |instance| + expect(instance).to receive(:find_root).with(:project) + end subject end context 'when exception raised' do before do - expect_any_instance_of(Gitlab::ImportExport::AttributesFinder) - .to receive(:find_root) - .with(:project) - .and_raise(StandardError) + expect_next_instance_of(Gitlab::ImportExport::AttributesFinder) do |instance| + expect(instance).to receive(:find_root).with(:project).and_raise(StandardError) + end end it { is_expected.to be false } @@ -36,9 +37,9 @@ describe Gitlab::ImportExport::Reader do subject { described_class.new(shared: shared).group_members_tree } it 'delegates to AttributesFinder#find_root' do - expect_any_instance_of(Gitlab::ImportExport::AttributesFinder) - .to receive(:find_root) - .with(:group_members) + expect_next_instance_of(Gitlab::ImportExport::AttributesFinder) do |instance| + expect(instance).to receive(:find_root).with(:group_members) + end subject end diff --git a/spec/lib/gitlab/import_export/references_configuration_spec.rb b/spec/lib/gitlab/import_export/references_configuration_spec.rb new file mode 100644 index 00000000000..91cf9f964c0 --- /dev/null +++ b/spec/lib/gitlab/import_export/references_configuration_spec.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +require 'spec_helper' + +# Part of the test security suite for the Import/Export feature +# Checks whether there are new reference attributes ending with _id in models that are currently being exported as part of the +# project Import/Export feature. +# If there are new references (foreign keys), these will have to either be replaced with actual relation +# or to be blacklisted by using the import_export.yml configuration file. +# Likewise, new models added to import_export.yml, will need to be added with their correspondent relations +# to this spec. +describe 'Import/Export Project configuration' do + include ConfigurationHelper + + where(:relation_path, :relation_name) do + relation_paths_for(:project).map do |relation_names| + next if relation_names.last == :author + + [relation_names.join("."), relation_names.last] + end.compact + end + + with_them do + context "where relation #{params[:relation_path]}" do + it 'does not have prohibited keys' do + relation_class = relation_class_for_name(relation_name) + relation_attributes = relation_class.new.attributes.keys - relation_class.encrypted_attributes.keys.map(&:to_s) + current_attributes = parsed_attributes(relation_name, relation_attributes) + prohibited_keys = current_attributes.select do |attribute| + prohibited_key?(attribute) || !relation_class.attribute_method?(attribute) + end + expect(prohibited_keys).to be_empty, failure_message(relation_class.to_s, prohibited_keys) + end + end + end + + def failure_message(relation_class, prohibited_keys) + <<-MSG + It looks like #{relation_class}, which is exported using the project Import/Export, has references: #{prohibited_keys.join(',')} + + Please replace it with actual relation in IMPORT_EXPORT_CONFIG if you consider this can be exported. + Please blacklist the attribute(s) in IMPORT_EXPORT_CONFIG by adding it to its correspondent + model in the +excluded_attributes+ section. + + IMPORT_EXPORT_CONFIG: #{Gitlab::ImportExport.config_file} + MSG + end +end diff --git a/spec/lib/gitlab/import_export/relation_factory_spec.rb b/spec/lib/gitlab/import_export/relation_factory_spec.rb index a23e68a8f00..41d6e6f24fc 100644 --- a/spec/lib/gitlab/import_export/relation_factory_spec.rb +++ b/spec/lib/gitlab/import_export/relation_factory_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Gitlab::ImportExport::RelationFactory do @@ -201,7 +203,7 @@ describe Gitlab::ImportExport::RelationFactory do Gitlab::ImportExport::MembersMapper.new( exported_members: [exported_member], user: user, - project: project) + importable: project) end it 'maps the right author to the imported note' do diff --git a/spec/lib/gitlab/import_export/relation_tree_restorer_spec.rb b/spec/lib/gitlab/import_export/relation_tree_restorer_spec.rb new file mode 100644 index 00000000000..c761f9652ab --- /dev/null +++ b/spec/lib/gitlab/import_export/relation_tree_restorer_spec.rb @@ -0,0 +1,67 @@ +# frozen_string_literal: true + +# This spec is a lightweight version of: +# * project_tree_restorer_spec.rb +# +# In depth testing is being done in the above specs. +# This spec tests that restore project works +# but does not have 100% relation coverage. + +require 'spec_helper' + +describe Gitlab::ImportExport::RelationTreeRestorer do + include ImportExport::CommonUtil + + let(:user) { create(:user) } + let(:shared) { Gitlab::ImportExport::Shared.new(importable) } + let(:members_mapper) { Gitlab::ImportExport::MembersMapper.new(exported_members: {}, user: user, importable: importable) } + + let(:importable_hash) do + json = IO.read(path) + ActiveSupport::JSON.decode(json) + end + + let(:relation_tree_restorer) do + described_class.new( + user: user, + shared: shared, + tree_hash: tree_hash, + importable: importable, + members_mapper: members_mapper, + relation_factory: relation_factory, + reader: reader + ) + end + + subject { relation_tree_restorer.restore } + + context 'when restoring a project' do + let(:path) { 'spec/fixtures/lib/gitlab/import_export/complex/project.json' } + let(:importable) { create(:project, :builds_enabled, :issues_disabled, name: 'project', path: 'project') } + let(:relation_factory) { Gitlab::ImportExport::RelationFactory } + let(:reader) { Gitlab::ImportExport::Reader.new(shared: shared) } + let(:tree_hash) { importable_hash } + + it 'restores project tree' do + expect(subject).to eq(true) + end + + describe 'imported project' do + let(:project) { Project.find_by_path('project') } + + before do + subject + end + + it 'has the project attributes and relations' do + expect(project.description).to eq('Nisi et repellendus ut enim quo accusamus vel magnam.') + expect(project.labels.count).to eq(3) + expect(project.boards.count).to eq(1) + expect(project.project_feature).not_to be_nil + expect(project.custom_attributes.count).to eq(2) + expect(project.project_badges.count).to eq(2) + expect(project.snippets.count).to eq(1) + end + end + end +end diff --git a/spec/lib/gitlab/import_export/repo_restorer_spec.rb b/spec/lib/gitlab/import_export/repo_restorer_spec.rb index e2ffb2adb9b..a61d966bdfa 100644 --- a/spec/lib/gitlab/import_export/repo_restorer_spec.rb +++ b/spec/lib/gitlab/import_export/repo_restorer_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Gitlab::ImportExport::RepoRestorer do @@ -18,7 +20,9 @@ describe Gitlab::ImportExport::RepoRestorer do end before do - allow_any_instance_of(Gitlab::ImportExport).to receive(:storage_path).and_return(export_path) + allow_next_instance_of(Gitlab::ImportExport) do |instance| + allow(instance).to receive(:storage_path).and_return(export_path) + end bundler.save end diff --git a/spec/lib/gitlab/import_export/repo_saver_spec.rb b/spec/lib/gitlab/import_export/repo_saver_spec.rb index c3df371af43..fc1f782bfdd 100644 --- a/spec/lib/gitlab/import_export/repo_saver_spec.rb +++ b/spec/lib/gitlab/import_export/repo_saver_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Gitlab::ImportExport::RepoSaver do @@ -10,7 +12,9 @@ describe Gitlab::ImportExport::RepoSaver do before do project.add_maintainer(user) - allow_any_instance_of(Gitlab::ImportExport).to receive(:storage_path).and_return(export_path) + allow_next_instance_of(Gitlab::ImportExport) do |instance| + allow(instance).to receive(:storage_path).and_return(export_path) + end end after do diff --git a/spec/lib/gitlab/import_export/safe_model_attributes.yml b/spec/lib/gitlab/import_export/safe_model_attributes.yml index 04fe985cdb5..79442c35797 100644 --- a/spec/lib/gitlab/import_export/safe_model_attributes.yml +++ b/spec/lib/gitlab/import_export/safe_model_attributes.yml @@ -20,6 +20,7 @@ Issue: - due_date - moved_to_id - duplicated_to_id +- promoted_to_epic_id - lock_version - milestone_id - weight @@ -32,7 +33,6 @@ Issue: Event: - id - target_type -- target_id - project_id - group_id - created_at @@ -59,7 +59,6 @@ Note: - attachment - line_code - commit_id -- noteable_id - system - st_diff - updated_by_id @@ -72,11 +71,8 @@ Note: - resolved_by_push - discussion_id - original_discussion_id -- review_id LabelLink: - id -- label_id -- target_id - target_type - created_at - updated_at @@ -129,13 +125,11 @@ Release: - released_at Evidence: - id -- release_id - summary - created_at - updated_at Releases::Link: - id -- release_id - url - name - created_at @@ -143,7 +137,6 @@ Releases::Link: ProjectMember: - id - access_level -- source_id - source_type - user_id - notification_level @@ -442,6 +435,7 @@ Service: - note_events - pipeline_events - job_events +- comment_on_event_enabled - category - default - wiki_page_events @@ -598,7 +592,6 @@ AwardEmoji: LabelPriority: - id - project_id -- label_id - priority - created_at - updated_at @@ -606,7 +599,6 @@ Timelog: - id - time_spent - merge_request_id -- issue_id - user_id - spent_at - created_at @@ -621,7 +613,6 @@ ProjectAutoDevops: - updated_at IssueAssignee: - user_id -- issue_id ProjectCustomAttribute: - id - created_at @@ -650,6 +641,7 @@ PrometheusAlert: - prometheus_metric_id Badge: - id +- name - link_url - image_url - project_id @@ -676,7 +668,6 @@ ProtectedEnvironment::DeployAccessLevel: ResourceLabelEvent: - id - action -- issue_id - merge_request_id - label_id - user_id @@ -686,9 +677,11 @@ ErrorTracking::ProjectErrorTrackingSetting: - project_id - project_name - organization_name +SentryIssue: +- id +- sentry_issue_identifier Suggestion: - id -- note_id - relative_order - applied - commit_id @@ -726,6 +719,7 @@ List: - milestone_id - user_id - max_issue_count +- max_issue_weight ExternalPullRequest: - id - created_at @@ -742,23 +736,31 @@ ExternalPullRequest: DesignManagement::Design: - id - project_id -- issue_id - filename DesignManagement::Action: -- design_id - event -- version_id DesignManagement::Version: - id - created_at - sha -- issue_id - author_id ZoomMeeting: - id -- issue_id - project_id - issue_status - url - created_at - updated_at +ServiceDeskSetting: +- project_id +- issue_template_key +ContainerExpirationPolicy: +- created_at +- updated_at +- next_run_at +- project_id +- name_regex +- cadence +- older_than +- keep_n +- enabled diff --git a/spec/lib/gitlab/import_export/saver_spec.rb b/spec/lib/gitlab/import_export/saver_spec.rb index aca63953677..a59cf7a1260 100644 --- a/spec/lib/gitlab/import_export/saver_spec.rb +++ b/spec/lib/gitlab/import_export/saver_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' require 'fileutils' @@ -5,10 +7,13 @@ 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(exportable: project, shared: shared) } before do - allow_any_instance_of(Gitlab::ImportExport).to receive(:storage_path).and_return(export_path) + allow_next_instance_of(Gitlab::ImportExport) do |instance| + allow(instance).to receive(:storage_path).and_return(export_path) + end FileUtils.mkdir_p(shared.export_path) FileUtils.touch("#{shared.export_path}/tmp.bundle") diff --git a/spec/lib/gitlab/import_export/shared_spec.rb b/spec/lib/gitlab/import_export/shared_spec.rb index fc011f7e1be..8c16243576d 100644 --- a/spec/lib/gitlab/import_export/shared_spec.rb +++ b/spec/lib/gitlab/import_export/shared_spec.rb @@ -1,8 +1,11 @@ +# frozen_string_literal: true + require 'spec_helper' require 'fileutils' describe Gitlab::ImportExport::Shared do let(:project) { build(:project) } + subject { project.import_export_shared } context 'with a repository on disk' do @@ -46,24 +49,9 @@ describe Gitlab::ImportExport::Shared do it 'updates the import JID' do import_state = create(:import_state, project: project, jid: 'jid-test') - expect_next_instance_of(Gitlab::Import::Logger) do |logger| - expect(logger).to receive(:error).with(hash_including(import_jid: import_state.jid)) - end - - subject.error(error) - end - - it 'calls the error logger without a backtrace' do - expect(subject).to receive(:log_error).with(message: error.message) - - subject.error(error) - end - - it 'calls the error logger with the full message' do - backtrace = caller - allow(error).to receive(:backtrace).and_return(caller) - - expect(subject).to receive(:log_error).with(message: error.message, error_backtrace: Gitlab::Profiler.clean_backtrace(backtrace)) + expect(Gitlab::ErrorTracking) + .to receive(:track_exception) + .with(error, hash_including(import_jid: import_state.jid)) subject.error(error) end diff --git a/spec/lib/gitlab/import_export/uploads_manager_spec.rb b/spec/lib/gitlab/import_export/uploads_manager_spec.rb index f13f639d6b7..e6d6ba840be 100644 --- a/spec/lib/gitlab/import_export/uploads_manager_spec.rb +++ b/spec/lib/gitlab/import_export/uploads_manager_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Gitlab::ImportExport::UploadsManager do @@ -10,7 +12,9 @@ describe Gitlab::ImportExport::UploadsManager do subject(:manager) { described_class.new(project: project, shared: shared) } before do - allow_any_instance_of(Gitlab::ImportExport).to receive(:storage_path).and_return(export_path) + allow_next_instance_of(Gitlab::ImportExport) do |instance| + allow(instance).to receive(:storage_path).and_return(export_path) + end FileUtils.mkdir_p(shared.export_path) end diff --git a/spec/lib/gitlab/import_export/uploads_restorer_spec.rb b/spec/lib/gitlab/import_export/uploads_restorer_spec.rb index e2e8204b2fa..077ece87b31 100644 --- a/spec/lib/gitlab/import_export/uploads_restorer_spec.rb +++ b/spec/lib/gitlab/import_export/uploads_restorer_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Gitlab::ImportExport::UploadsRestorer do @@ -6,7 +8,9 @@ describe Gitlab::ImportExport::UploadsRestorer do let(:shared) { project.import_export_shared } before do - allow_any_instance_of(Gitlab::ImportExport).to receive(:storage_path).and_return(export_path) + allow_next_instance_of(Gitlab::ImportExport) do |instance| + allow(instance).to receive(:storage_path).and_return(export_path) + end FileUtils.mkdir_p(File.join(shared.export_path, 'uploads/random')) FileUtils.touch(File.join(shared.export_path, 'uploads/random', 'dummy.txt')) end diff --git a/spec/lib/gitlab/import_export/uploads_saver_spec.rb b/spec/lib/gitlab/import_export/uploads_saver_spec.rb index 24993460e51..8a36caef316 100644 --- a/spec/lib/gitlab/import_export/uploads_saver_spec.rb +++ b/spec/lib/gitlab/import_export/uploads_saver_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Gitlab::ImportExport::UploadsSaver do @@ -7,7 +9,9 @@ describe Gitlab::ImportExport::UploadsSaver do let(:shared) { project.import_export_shared } before do - allow_any_instance_of(Gitlab::ImportExport).to receive(:storage_path).and_return(export_path) + allow_next_instance_of(Gitlab::ImportExport) do |instance| + allow(instance).to receive(:storage_path).and_return(export_path) + end end after do diff --git a/spec/lib/gitlab/import_export/version_checker_spec.rb b/spec/lib/gitlab/import_export/version_checker_spec.rb index 76f8253ec9b..befbd1b4c19 100644 --- a/spec/lib/gitlab/import_export/version_checker_spec.rb +++ b/spec/lib/gitlab/import_export/version_checker_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' include ImportExport::CommonUtil @@ -6,10 +8,20 @@ describe Gitlab::ImportExport::VersionChecker do describe 'bundle a project Git repo' do let(:version) { Gitlab::ImportExport.version } + let(:version_file) { Tempfile.new('VERSION') } before do allow_any_instance_of(Gitlab::ImportExport::Shared).to receive(:relative_archive_path).and_return('') - allow(File).to receive(:open).and_return(version) + + version_file.write(version) + version_file.rewind + + allow_any_instance_of(described_class).to receive(:version_file).and_return(version_file.path) + end + + after do + version_file.close + version_file.unlink end it 'returns true if Import/Export have the same version' do diff --git a/spec/lib/gitlab/import_export/wiki_repo_saver_spec.rb b/spec/lib/gitlab/import_export/wiki_repo_saver_spec.rb index 249afbd23d1..59a59223d8d 100644 --- a/spec/lib/gitlab/import_export/wiki_repo_saver_spec.rb +++ b/spec/lib/gitlab/import_export/wiki_repo_saver_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Gitlab::ImportExport::WikiRepoSaver do @@ -11,7 +13,9 @@ describe Gitlab::ImportExport::WikiRepoSaver do before do project.add_maintainer(user) - allow_any_instance_of(Gitlab::ImportExport).to receive(:storage_path).and_return(export_path) + allow_next_instance_of(Gitlab::ImportExport) do |instance| + allow(instance).to receive(:storage_path).and_return(export_path) + end project_wiki.wiki project_wiki.create_page("index", "test content") end diff --git a/spec/lib/gitlab/import_export/wiki_restorer_spec.rb b/spec/lib/gitlab/import_export/wiki_restorer_spec.rb index f99f198da33..33cd3e55393 100644 --- a/spec/lib/gitlab/import_export/wiki_restorer_spec.rb +++ b/spec/lib/gitlab/import_export/wiki_restorer_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Gitlab::ImportExport::WikiRestorer do |