Commit 02b1b8b8 authored by nmilojevic1's avatar nmilojevic1

Refactor specs for ndjson

- Make specs faster using let_it_be
- Move shared_examples to spec file
- Apply MR suggestions
parent c5d9f004
...@@ -7,6 +7,7 @@ describe Gitlab::ImportExport::Project::TreeSaver do ...@@ -7,6 +7,7 @@ describe Gitlab::ImportExport::Project::TreeSaver do
let_it_be(:group) { create(:group) } let_it_be(:group) { create(:group) }
let_it_be(:project) { create(:project, group: group) } let_it_be(:project) { create(:project, group: group) }
let_it_be(:issue) { create(:issue, project: project) } let_it_be(:issue) { create(:issue, project: project) }
let_it_be(:shared) { project.import_export_shared }
let_it_be(:design) { create(:design, :with_file, versions_count: 2, issue: issue) } let_it_be(:design) { create(:design, :with_file, versions_count: 2, issue: issue) }
let_it_be(:note) { create(:diff_note_on_design, noteable: design, project: project, author: user) } let_it_be(:note) { create(:diff_note_on_design, noteable: design, project: project, author: user) }
...@@ -15,16 +16,72 @@ describe Gitlab::ImportExport::Project::TreeSaver do ...@@ -15,16 +16,72 @@ describe Gitlab::ImportExport::Project::TreeSaver do
let_it_be(:epic) { create(:epic, group: group) } let_it_be(:epic) { create(:epic, group: group) }
let_it_be(:epic_issue) { create(:epic_issue, issue: issue, epic: epic) } let_it_be(:epic_issue) { create(:epic_issue, issue: issue, epic: epic) }
let(:shared) { project.import_export_shared } let_it_be(:export_path) { "#{Dir.tmpdir}/project_tree_saver_spec_ee" }
let(:export_path) { "#{Dir.tmpdir}/project_tree_saver_spec_ee" }
let(:project_tree_saver) { described_class.new(project: project, current_user: user, shared: shared) }
before do after :all do
FileUtils.rm_rf(export_path)
end
shared_examples 'EE saves project tree successfully' do |ndjson_enabled|
include ::ImportExport::CommonUtil
let_it_be(:project_tree_saver) { described_class.new(project: project, current_user: user, shared: shared) }
let_it_be(:full_path) do
if ndjson_enabled
File.join(shared.export_path, 'tree')
else
File.join(shared.export_path, Gitlab::ImportExport.project_filename)
end
end
let_it_be(:exportable_path) { 'project' }
before_all do
Feature.enable(:project_export_as_ndjson) if ndjson_enabled
project.add_maintainer(user) project.add_maintainer(user)
expect(project_tree_saver.save).to be true
end end
after do let_it_be(:issue_json) { get_json(full_path, exportable_path, :issues, ndjson_enabled).first }
FileUtils.rm_rf(export_path)
describe 'the designs json' do
it 'saves issue.designs correctly' do
expect(issue_json['designs'].size).to eq(1)
end
it 'saves issue.design_versions correctly' do
actions = issue_json['design_versions'].flat_map { |v| v['actions'] }
expect(issue_json['design_versions'].size).to eq(2)
issue_json['design_versions'].each do |version|
expect(version['author_id']).to eq(issue.author_id)
end
expect(actions.size).to eq(2)
actions.each do |action|
expect(action['design']).to be_present
end
end
end
context 'epics' do
it 'has epic_issue' do
expect(issue_json['epic_issue']).not_to be_empty
expect(issue_json['epic_issue']['id']).to eql(epic_issue.id)
end
it 'has epic' do
expect(issue_json['epic_issue']['epic']['title']).to eql(epic.title)
end
it 'does not have epic_id' do
expect(issue_json['epic_issue']['epic_id']).to be_nil
end
it 'does not have issue_id' do
expect(issue_json['epic_issue']['issue_id']).to be_nil
end
end
end end
context 'with JSON' do context 'with JSON' do
......
# frozen_string_literal: true
RSpec.shared_examples 'EE saves project tree successfully' do |ndjson_enabled|
include ::ImportExport::CommonUtil
let(:full_path) do
project_tree_saver.save
if ndjson_enabled == true
File.join(shared.export_path, 'tree')
else
File.join(shared.export_path, Gitlab::ImportExport.project_filename)
end
end
let(:exportable_path) { 'project' }
before do
stub_feature_flags(project_export_as_ndjson: ndjson_enabled)
end
it 'saves successfully' do
expect(project_tree_saver.save).to be true
end
describe 'the designs json' do
let(:issue_json) { saved_relations(full_path, exportable_path, :issues, ndjson_enabled).first }
it 'saves issue.designs correctly' do
expect(issue_json['designs'].size).to eq(1)
end
it 'saves issue.design_versions correctly' do
actions = issue_json['design_versions'].flat_map { |v| v['actions'] }
expect(issue_json['design_versions'].size).to eq(2)
issue_json['design_versions'].each do |version|
expect(version['author_id']).to eq(issue.author_id)
end
expect(actions.size).to eq(2)
actions.each do |action|
expect(action['design']).to be_present
end
end
end
context 'epics' do
it 'has epic_issue' do
expect(saved_relations(full_path, exportable_path, :issues, ndjson_enabled).first['epic_issue']).not_to be_empty
expect(saved_relations(full_path, exportable_path, :issues, ndjson_enabled).first['epic_issue']['id']).to eql(epic_issue.id)
end
it 'has epic' do
expect(saved_relations(full_path, exportable_path, :issues, ndjson_enabled).first['epic_issue']['epic']['title']).to eql(epic.title)
end
it 'does not have epic_id' do
expect(saved_relations(full_path, exportable_path, :issues, ndjson_enabled).first['epic_issue']['epic_id']).to be_nil
end
it 'does not have issue_id' do
expect(saved_relations(full_path, exportable_path, :issues, ndjson_enabled).first['epic_issue']['issue_id']).to be_nil
end
end
end
...@@ -38,6 +38,27 @@ describe 'Import/Export - project export integration test', :js do ...@@ -38,6 +38,27 @@ describe 'Import/Export - project export integration test', :js do
sign_in(user) sign_in(user)
end end
shared_examples 'export file without sensitive words' do
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)
project_hash = JSON.parse(IO.read(project_json_path))
sensitive_words.each do |sensitive_word|
found = find_sensitive_attributes(sensitive_word, project_hash)
expect(found).to be_nil, failure_message(found.try(:key_found), found.try(:parent), sensitive_word)
end
end
end
end
context "with legacy export" do context "with legacy export" do
before do before do
stub_feature_flags(streaming_serializer: false) stub_feature_flags(streaming_serializer: false)
...@@ -52,6 +73,7 @@ describe 'Import/Export - project export integration test', :js do ...@@ -52,6 +73,7 @@ describe 'Import/Export - project export integration test', :js do
stub_feature_flags(streaming_serializer: true) stub_feature_flags(streaming_serializer: true)
stub_feature_flags(project_export_as_ndjson: false) stub_feature_flags(project_export_as_ndjson: false)
end end
it_behaves_like "export file without sensitive words" it_behaves_like "export file without sensitive words"
end end
...@@ -61,19 +83,8 @@ describe 'Import/Export - project export integration test', :js do ...@@ -61,19 +83,8 @@ describe 'Import/Export - project export integration test', :js do
stub_feature_flags(project_export_as_ndjson: true) stub_feature_flags(project_export_as_ndjson: true)
end end
it 'exports a project successfully', :sidekiq_might_not_need_inline do it 'exports a project successfully', :sidekiq_inline do
visit edit_project_path(project) export_project_and_download_file(page, project)
expect(page).to have_content('Export project')
find(:link, 'Export project').send_keys(:return)
visit edit_project_path(project)
expect(page).to have_content('Download export')
expect(project.export_status).to eq(:finished)
expect(project.export_file.path).to include('tar.gz')
in_directory_with_expanded_export(project) do |exit_status, tmpdir| in_directory_with_expanded_export(project) do |exit_status, tmpdir|
expect(exit_status).to eq(0) expect(exit_status).to eq(0)
...@@ -99,6 +110,22 @@ describe 'Import/Export - project export integration test', :js do ...@@ -99,6 +110,22 @@ describe 'Import/Export - project export integration test', :js do
end end
end end
end end
end
end
def export_project_and_download_file(page, project)
visit edit_project_path(project)
expect(page).to have_content('Export project')
find(:link, 'Export project').send_keys(:return)
visit edit_project_path(project)
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) def failure_message(key_found, parent, sensitive_word)
<<-MSG <<-MSG
...@@ -116,6 +143,4 @@ describe 'Import/Export - project export integration test', :js do ...@@ -116,6 +143,4 @@ describe 'Import/Export - project export integration test', :js do
CURRENT_SPEC: #{__FILE__} CURRENT_SPEC: #{__FILE__}
MSG MSG
end end
end
end
end end
...@@ -5,7 +5,7 @@ require "spec_helper" ...@@ -5,7 +5,7 @@ require "spec_helper"
describe Gitlab::ImportExport::JSON::NdjsonWriter do describe Gitlab::ImportExport::JSON::NdjsonWriter do
include ImportExport::CommonUtil include ImportExport::CommonUtil
let(:path) { "#{Dir.tmpdir}/legacy_writer_spec/tree" } let(:path) { "#{Dir.tmpdir}/ndjson_writer_spec/tree" }
let(:exportable_path) { 'projects' } let(:exportable_path) { 'projects' }
subject { described_class.new(path) } subject { described_class.new(path) }
......
...@@ -26,15 +26,13 @@ module ImportExport ...@@ -26,15 +26,13 @@ module ImportExport
"tmp/tests/gitlab-test/import_export" "tmp/tests/gitlab-test/import_export"
end end
def saved_relations(path, exportable_path, key, ndjson_enabled) def get_json(path, exportable_path, key, ndjson_enabled)
if ndjson_enabled == true if ndjson_enabled
json = if key == :projects json = if key == :projects
consume_attributes(path, exportable_path) consume_attributes(path, exportable_path)
else else
consume_relations(path, exportable_path, key) consume_relations(path, exportable_path, key)
end end
json = json.first if key == :project_feature
else else
json = project_json(path) json = project_json(path)
json = json[key.to_s] unless key == :projects json = json[key.to_s] unless key == :projects
...@@ -86,11 +84,11 @@ module ImportExport ...@@ -86,11 +84,11 @@ module ImportExport
relations << json relations << json
end end
relations.flatten key == :project_feature ? relations.first : relations.flatten
end end
def project_json(filename) def project_json(filename)
::JSON.parse(IO.read(filename)) ActiveSupport::JSON.decode(IO.read(filename))
end end
end end
end end
# frozen_string_literal: true
RSpec.shared_examples 'export file without sensitive words' do
it 'exports a project successfully', :sidekiq_might_not_need_inline do
visit edit_project_path(project)
expect(page).to have_content('Export project')
find(:link, 'Export project').send_keys(:return)
visit edit_project_path(project)
expect(page).to have_content('Download export')
expect(project.export_status).to eq(:finished)
expect(project.export_file.path).to include('tar.gz')
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)
project_hash = JSON.parse(IO.read(project_json_path))
sensitive_words.each do |sensitive_word|
found = find_sensitive_attributes(sensitive_word, project_hash)
expect(found).to be_nil, failure_message(found.try(:key_found), found.try(:parent), sensitive_word)
end
end
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
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment