Read about our upcoming Code of Conduct on this issue

export_file_spec.rb 2.92 KB
Newer Older
1
2
require 'spec_helper'

James Lopez's avatar
James Lopez committed
3
# Integration test that exports a file using the Import/Export feature
4
# It looks up for any sensitive word inside the JSON, so if a sensitive word is found
5
# we'll have to either include it adding the model that includes it to the +safe_list+
6
# or make sure the attribute is blacklisted in the +import_export.yml+ configuration
7
describe 'Import/Export - project export integration test', :js do
8
  include Select2Helper
9
  include ExportFileHelper
10
11

  let(:user) { create(:admin) }
Douwe Maan's avatar
Douwe Maan committed
12
  let(:export_path) { "#{Dir.tmpdir}/import_file_spec" }
13
  let(:config_hash) { YAML.load_file(Gitlab::ImportExport.config_file).deep_stringify_keys }
14
15

  let(:sensitive_words) { %w[pass secret token key] }
16
  let(:safe_list) do
17
    {
18
      token: [ProjectHook, Ci::Trigger, CommitStatus],
19
      key: [Project, Ci::Variable, :yaml_variables]
20
21
    }
  end
22
  let(:safe_hashes) { { yaml_variables: %w[key value public] } }
23
24
25

  let(:project) { setup_project }

26
  before do
27
    allow_any_instance_of(Gitlab::ImportExport).to receive(:storage_path).and_return(export_path)
28
    stub_feature_flags(import_export_object_storage: false)
29
30
31
32
33
34
35
36
  end

  after do
    FileUtils.rm_rf(export_path, secure: true)
  end

  context 'admin user' do
    before do
37
      sign_in(user)
38
39
    end

40
    it 'exports a project successfully' do
41
      visit edit_project_path(project)
42
43
44

      expect(page).to have_content('Export project')

Jose Ivan Vargas's avatar
Jose Ivan Vargas committed
45
      find(:link, 'Export project').send_keys(:return)
46

47
      visit edit_project_path(project)
48
49
50

      expect(page).to have_content('Download export')

51
52
      expect(file_permissions(project.export_path)).to eq(0700)

53
54
55
56
57
58
59
60
61
      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|
62
63
          found = find_sensitive_attributes(sensitive_word, project_hash)

64
          expect(found).to be_nil, failure_message(found.try(:key_found), found.try(:parent), sensitive_word)
65
66
67
        end
      end
    end
68
69
70
71
72
73
74
75
76
77

    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.

78
79
        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).
80

81
82
83
84
        IMPORT_EXPORT_CONFIG: #{Gitlab::ImportExport.config_file}
        CURRENT_SPEC: #{__FILE__}
      MSG
    end
85
86
  end
end