BigW Consortium Gitlab

model_configuration_spec.rb 2.21 KB
Newer Older
1 2
require 'spec_helper'

James Lopez committed
3
# Part of the test security suite for the Import/Export feature
4 5
# Finds if a new model has been added that can potentially be part of the Import/Export
# If it finds a new model, it will show a +failure_message+ with the options available.
6
describe 'Import/Export model configuration', lib: true do
7 8 9
  include ConfigurationHelper

  let(:config_hash) { YAML.load_file(Gitlab::ImportExport.config_file).deep_stringify_keys }
10
  let(:model_names) do
11 12 13 14 15
    names = names_from_tree(config_hash['project_tree'])

    # 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.
16
    # - User, Author... Models we do not care about for checking models
17 18 19
    names.flatten.uniq - ['milestones', 'labels', 'user', 'author'] + ['project']
  end

20 21
  let(:all_models_yml) { 'spec/lib/gitlab/import_export/all_models.yml' }
  let(:all_models) { YAML.load_file(all_models_yml) }
22 23 24
  let(:current_models) { setup_models }

  it 'has no new models' do
25
    model_names.each do |model_name|
26
      new_models = Array(current_models[model_name]) - Array(all_models[model_name])
27
      expect(new_models).to be_empty, failure_message(model_name.classify, new_models)
28 29 30
    end
  end

31
  # List of current models between models, in the format of
32 33 34 35
  # {model: [model_2, model3], ...}
  def setup_models
    all_models_hash = {}

36 37
    model_names.each do |model_name|
      model_class = relation_class_for_name(model_name)
38

39
      all_models_hash[model_name] = associations_for(model_class) - ['project']
40 41 42 43 44
    end

    all_models_hash
  end

45
  def failure_message(parent_model_name, new_models)
46
    <<-MSG
47
      New model(s) <#{new_models.join(',')}> have been added, related to #{parent_model_name}, which is exported by
48 49
      the Import/Export feature.

50 51
      If you think this model should be included in the export, please add it to IMPORT_EXPORT_CONFIG.
      Definitely add it to MODELS_JSON to signal that you've handled this error and to prevent it from showing up in the future.
52

53
      MODELS_JSON: #{File.expand_path(all_models_yml)}
54 55 56 57
      IMPORT_EXPORT_CONFIG: #{Gitlab::ImportExport.config_file}
    MSG
  end
end