attribute_cleaner_spec.rb 2.4 KB
Newer Older
1
2
# frozen_string_literal: true

3
4
require 'spec_helper'

5
describe Gitlab::ImportExport::AttributeCleaner do
6
  let(:relation_class) { double('relation_class').as_null_object }
7
8
  let(:unsafe_hash) do
    {
9
      'id' => 101,
10
11
12
13
14
15
16
17
18
19
      '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,
20
21
22
      'notid' => 99,
      'import_source' => 'whatever',
      'import_type' => 'whatever',
23
24
25
26
      'non_existent_attr' => 'whatever',
      'some_html' => '<p>dodgy html</p>',
      'legit_html' => '<p>legit html</p>',
      '_html' => '<p>perfectly ordinary html</p>',
27
      'cached_markdown_version' => 12345,
28
29
      'custom_attributes' => 'whatever',
      'some_attributes_metadata' => 'whatever',
30
      'group_id' => 99,
31
32
33
      'commit_id' => 99,
      'issue_ids' => [1, 2, 3],
      'merge_request_ids' => [1, 2, 3],
34
35
36
37
38
39
40
41
42
      'note_ids' => [1, 2, 3],
      'attributes' => {
        'issue_ids' => [1, 2, 3],
        'merge_request_ids' => [1, 2, 3],
        'note_ids' => [1, 2, 3]
      },
      'variables_attributes' => {
        'id' => 1
      }
43
44
45
46
47
48
49
50
    }
  end

  let(:post_safe_hash) do
    {
      'project_id' => 99,
      'user_id' => 99,
      'random_id_in_the_middle' => 99,
51
52
      'notid' => 99,
      'group_id' => 99,
53
54
      'commit_id' => 99,
      'custom_attributes' => 'whatever'
55
56
57
    }
  end

58
59
60
61
62
63
64
65
66
  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

67
  it 'removes unwanted attributes from the hash' do
68
69
70
71
72
73
74
75
76
77
78
79
    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
80
    parsed_hash = described_class.clean(relation_hash: unsafe_hash, relation_class: relation_class)
81

82
    expect(parsed_hash.keys).to match_array post_safe_hash.keys + excluded_keys
83
84
  end
end