diff options
author | James Lopez <james@jameslopez.es> | 2016-09-01 12:49:55 +0300 |
---|---|---|
committer | James Lopez <james@jameslopez.es> | 2016-09-09 15:56:10 +0300 |
commit | 8ca963bd96a47411f390360fbe3e85766dce00db (patch) | |
tree | 0735d00827bdb3e65ee0356de97765d40dddbade /spec/support/import_export | |
parent | 7323d30f44eaabbd8aad66a18ded233261a74718 (diff) |
more refactoring of the specs
Diffstat (limited to 'spec/support/import_export')
-rw-r--r-- | spec/support/import_export/export_file_helper.rb | 36 |
1 files changed, 29 insertions, 7 deletions
diff --git a/spec/support/import_export/export_file_helper.rb b/spec/support/import_export/export_file_helper.rb index a2ff15c7bc5..791863b8d16 100644 --- a/spec/support/import_export/export_file_helper.rb +++ b/spec/support/import_export/export_file_helper.rb @@ -1,4 +1,6 @@ module ExportFileHelper + IGNORED_ATTRIBUTES = %w[created_at updated_at url group_id] + def setup_project project = create(:project, :public) @@ -53,22 +55,28 @@ module ExportFileHelper end # Recursively finds key/values including +key+ as part of the key, inside a nested hash - def deep_find_with_parent(key, object, found = nil) - if object.respond_to?(:key?) && object.keys.any? { |k| k.include?(key) } - [object[key], object] if object[key] - elsif object.is_a? Enumerable - object.find { |*a| found, object = deep_find_with_parent(key, a.last, found) } + def deep_find_with_parent(sensitive_key_word, object, found = nil) + # Returns the parent object and the object found containing a sensitive word as part of the key + if object_contains_key?(object, sensitive_key_word) + [object[sensitive_key_word], object] if object[sensitive_key_word] + elsif object.is_a?(Enumerable) + # Recursively lookup for keys containing sensitive words in a Hash or Array + object.find { |*hash_or_array| found, object = deep_find_with_parent(sensitive_key_word, hash_or_array.last, found) } [found, object] if found end end + #Return true if the hash has a key containing a sensitive word + def object_contains_key?(object, sensitive_key_word) + object.is_a?(Hash) && object.keys.any? { |key| key.include?(sensitive_key_word) } + end + # Returns true if a sensitive word is found inside a hash, excluding safe hashes def has_sensitive_attributes?(sensitive_word, project_hash) loop do object, parent = deep_find_with_parent(sensitive_word, project_hash) - parent.except!('created_at', 'updated_at', 'url', 'group_id') if parent - if object && safe_hashes[sensitive_word.to_sym].include?(parent) + if object && is_safe_hash?(parent, sensitive_word) # It's in the safe list, remove hash and keep looking parent.delete(object) elsif object @@ -78,4 +86,18 @@ module ExportFileHelper end end end + + # Returns true if it's one of the excluded models in +safe_models+ + def is_safe_hash?(parent, sensitive_word) + return false unless parent + + # Extra attributes that appear in a model but not in the exported hash. + excluded_attributes = ['type'] + + safe_models[sensitive_word.to_sym].each do |safe_model| + return true if (safe_model.attribute_names - parent.keys - excluded_attributes).empty? + end + + false + end end |