debian-mirror-gitlab/spec/lib/gitlab/import_export/attribute_cleaner_spec.rb

88 lines
2.6 KiB
Ruby
Raw Normal View History

2020-01-01 13:55:28 +05:30
# frozen_string_literal: true
2016-11-03 12:29:30 +05:30
require 'spec_helper'
2017-09-10 17:25:29 +05:30
describe Gitlab::ImportExport::AttributeCleaner do
2018-03-17 18:26:18 +05:30
let(:relation_class) { double('relation_class').as_null_object }
2016-11-03 12:29:30 +05:30
let(:unsafe_hash) do
{
'id' => 101,
'service_id' => 99,
'moved_to_id' => 99,
'namespace_id' => 99,
'ci_id' => 99,
'random_project_id' => 99,
'random_id' => 99,
'milestone_id' => 99,
'project_id' => 99,
'user_id' => 99,
'random_id_in_the_middle' => 99,
2018-06-03 19:52:53 +05:30
'notid' => 99,
'import_source' => 'whatever',
'import_type' => 'whatever',
2019-06-05 12:25:43 +05:30
'non_existent_attr' => 'whatever',
'some_html' => '<p>dodgy html</p>',
'legit_html' => '<p>legit html</p>',
'_html' => '<p>perfectly ordinary html</p>',
2019-09-30 21:07:59 +05:30
'cached_markdown_version' => 12345,
2020-01-14 00:54:23 +05:30
'custom_attributes' => 'whatever',
'some_attributes_metadata' => 'whatever',
2019-09-30 21:07:59 +05:30
'group_id' => 99,
2019-12-04 20:38:33 +05:30
'commit_id' => 99,
'issue_ids' => [1, 2, 3],
'merge_request_ids' => [1, 2, 3],
2020-01-14 00:54:23 +05:30
'note_ids' => [1, 2, 3],
2020-03-28 13:19:24 +05:30
'remote_attachment_url' => 'http://something.dodgy',
'remote_attachment_request_header' => 'bad value',
'remote_attachment_urls' => %w(http://something.dodgy http://something.okay),
2020-01-14 00:54:23 +05:30
'attributes' => {
'issue_ids' => [1, 2, 3],
'merge_request_ids' => [1, 2, 3],
'note_ids' => [1, 2, 3]
},
'variables_attributes' => {
'id' => 1
}
2016-11-03 12:29:30 +05:30
}
end
let(:post_safe_hash) do
{
'project_id' => 99,
'user_id' => 99,
'random_id_in_the_middle' => 99,
2019-09-30 21:07:59 +05:30
'notid' => 99,
'group_id' => 99,
2020-01-14 00:54:23 +05:30
'commit_id' => 99,
'custom_attributes' => 'whatever'
2016-11-03 12:29:30 +05:30
}
end
2018-06-03 19:52:53 +05:30
let(:excluded_keys) { %w[import_source import_type] }
subject { described_class.clean(relation_hash: unsafe_hash, relation_class: relation_class, excluded_keys: excluded_keys) }
before do
allow(relation_class).to receive(:attribute_method?).and_return(true)
allow(relation_class).to receive(:attribute_method?).with('non_existent_attr').and_return(false)
end
2016-11-03 12:29:30 +05:30
it 'removes unwanted attributes from the hash' do
2018-06-03 19:52:53 +05:30
expect(subject).to eq(post_safe_hash)
end
it 'removes attributes not present in relation_class' do
expect(subject.keys).not_to include 'non_existent_attr'
end
it 'removes excluded keys from the hash' do
expect(subject.keys).not_to include excluded_keys
end
it 'does not remove excluded key if not listed' do
2016-11-03 12:29:30 +05:30
parsed_hash = described_class.clean(relation_hash: unsafe_hash, relation_class: relation_class)
2019-09-30 21:07:59 +05:30
expect(parsed_hash.keys).to match_array post_safe_hash.keys + excluded_keys
2016-11-03 12:29:30 +05:30
end
end