debian-mirror-gitlab/spec/features/projects/import_export/export_file_spec.rb

133 lines
4.3 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
2016-09-29 09:46:39 +05:30
require 'spec_helper'
# Integration test that exports a file using the Import/Export feature
# It looks up for any sensitive word inside the JSON, so if a sensitive word is found
2018-03-17 18:26:18 +05:30
# we'll have to either include it adding the model that includes it to the +safe_list+
2016-09-29 09:46:39 +05:30
# or make sure the attribute is blacklisted in the +import_export.yml+ configuration
2020-06-23 00:09:42 +05:30
RSpec.describe 'Import/Export - project export integration test', :js do
2016-09-29 09:46:39 +05:30
include Select2Helper
include ExportFileHelper
let(:user) { create(:admin) }
2017-08-17 22:00:37 +05:30
let(:export_path) { "#{Dir.tmpdir}/import_file_spec" }
2019-06-05 12:25:43 +05:30
let(:sensitive_words) { %w[pass secret token key encrypted html] }
2016-09-29 09:46:39 +05:30
let(:safe_list) do
{
token: [ProjectHook, Ci::Trigger, CommitStatus],
key: [Project, Ci::Variable, :yaml_variables]
}
end
2020-10-24 23:57:45 +05:30
2016-09-29 09:46:39 +05:30
let(:safe_hashes) { { yaml_variables: %w[key value public] } }
let(:project) { setup_project }
2018-11-08 19:23:39 +05:30
before do
2019-12-26 22:10:19 +05:30
allow_next_instance_of(Gitlab::ImportExport) do |instance|
allow(instance).to receive(:storage_path).and_return(export_path)
end
2016-09-29 09:46:39 +05:30
end
after do
FileUtils.rm_rf(export_path, secure: true)
end
context 'admin user' do
before do
2017-09-10 17:25:29 +05:30
sign_in(user)
2016-09-29 09:46:39 +05:30
end
2020-04-22 19:07:51 +05:30
context "with streaming serializer" do
before do
stub_feature_flags(project_export_as_ndjson: false)
end
it 'exports a project successfully', :sidekiq_inline do
export_project_and_download_file(page, project)
in_directory_with_expanded_export(project) do |exit_status, tmpdir|
expect(exit_status).to eq(0)
project_json_path = File.join(tmpdir, 'project.json')
expect(File).to exist(project_json_path)
2016-09-29 09:46:39 +05:30
2020-05-24 23:13:21 +05:30
project_hash = Gitlab::Json.parse(IO.read(project_json_path))
2016-09-29 09:46:39 +05:30
2020-04-22 19:07:51 +05:30
sensitive_words.each do |sensitive_word|
found = find_sensitive_attributes(sensitive_word, project_hash)
2016-09-29 09:46:39 +05:30
2020-04-22 19:07:51 +05:30
expect(found).to be_nil, failure_message(found.try(:key_found), found.try(:parent), sensitive_word)
end
end
end
end
2016-09-29 09:46:39 +05:30
2020-04-22 19:07:51 +05:30
context "with ndjson" do
before do
stub_feature_flags(project_export_as_ndjson: true)
end
2016-09-29 09:46:39 +05:30
2020-04-22 19:07:51 +05:30
it 'exports a project successfully', :sidekiq_inline do
export_project_and_download_file(page, project)
2018-11-20 20:47:30 +05:30
2020-04-22 19:07:51 +05:30
in_directory_with_expanded_export(project) do |exit_status, tmpdir|
expect(exit_status).to eq(0)
2016-09-29 09:46:39 +05:30
2020-04-22 19:07:51 +05:30
project_json_path = File.join(tmpdir, 'tree', 'project.json')
expect(File).to exist(project_json_path)
2016-09-29 09:46:39 +05:30
2020-04-22 19:07:51 +05:30
relations = []
2020-05-24 23:13:21 +05:30
relations << Gitlab::Json.parse(IO.read(project_json_path))
2020-04-22 19:07:51 +05:30
Dir.glob(File.join(tmpdir, 'tree/project', '*.ndjson')) do |rb_filename|
File.foreach(rb_filename) do |line|
json = ActiveSupport::JSON.decode(line)
relations << json
end
end
2016-09-29 09:46:39 +05:30
2020-04-22 19:07:51 +05:30
relations.each do |relation_hash|
sensitive_words.each do |sensitive_word|
found = find_sensitive_attributes(sensitive_word, relation_hash)
2016-09-29 09:46:39 +05:30
2020-04-22 19:07:51 +05:30
expect(found).to be_nil, failure_message(found.try(:key_found), found.try(:parent), sensitive_word)
end
end
2016-09-29 09:46:39 +05:30
end
end
end
2020-04-22 19:07:51 +05:30
end
2016-09-29 09:46:39 +05:30
2020-04-22 19:07:51 +05:30
def export_project_and_download_file(page, project)
visit edit_project_path(project)
2016-09-29 09:46:39 +05:30
2020-04-22 19:07:51 +05:30
expect(page).to have_content('Export project')
2016-09-29 09:46:39 +05:30
2020-04-22 19:07:51 +05:30
find(:link, 'Export project').send_keys(:return)
2016-09-29 09:46:39 +05:30
2020-04-22 19:07:51 +05:30
visit edit_project_path(project)
2017-08-17 22:00:37 +05:30
2020-04-22 19:07:51 +05:30
expect(page).to have_content('Download export')
expect(project.export_status).to eq(:finished)
expect(project.export_file.path).to include('tar.gz')
end
def failure_message(key_found, parent, sensitive_word)
<<-MSG
Found a new sensitive word <#{key_found}>, which is part of the hash #{parent.inspect}
If you think this information shouldn't get exported, please exclude the model or attribute in IMPORT_EXPORT_CONFIG.
Otherwise, please add the exception to +safe_list+ in CURRENT_SPEC using #{sensitive_word} as the key and the
correspondent hash or model as the value.
Also, if the attribute is a generated unique token, please add it to RelationFactory::TOKEN_RESET_MODELS if it needs to be
reset (to prevent duplicate column problems while importing to the same instance).
IMPORT_EXPORT_CONFIG: #{Gitlab::ImportExport.config_file}
CURRENT_SPEC: #{__FILE__}
MSG
2016-09-29 09:46:39 +05:30
end
end