summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/background_migration/encrypt_integration_properties_spec.rb
blob: 38e8b159e63a5b7b837f1c256488a43c7cd29366 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# frozen_string_literal: true
require 'spec_helper'

RSpec.describe Gitlab::BackgroundMigration::EncryptIntegrationProperties, schema: 20220415124804 do
  let(:integrations) do
    table(:integrations) do |integrations|
      integrations.send :attr_encrypted, :encrypted_properties_tmp,
                        attribute: :encrypted_properties,
                        mode: :per_attribute_iv,
                        key: ::Settings.attr_encrypted_db_key_base_32,
                        algorithm: 'aes-256-gcm',
                        marshal: true,
                        marshaler: ::Gitlab::Json,
                        encode: false,
                        encode_iv: false
    end
  end

  let!(:no_properties) { integrations.create! }
  let!(:with_plaintext_1) { integrations.create!(properties: json_props(1)) }
  let!(:with_plaintext_2) { integrations.create!(properties: json_props(2)) }
  let!(:with_encrypted) do
    x = integrations.new
    x.properties = nil
    x.encrypted_properties_tmp = some_props(3)
    x.save!
    x
  end

  let(:start_id) { integrations.minimum(:id) }
  let(:end_id) { integrations.maximum(:id) }

  it 'ensures all properties are encrypted', :aggregate_failures do
    described_class.new.perform(start_id, end_id)

    props = integrations.all.to_h do |record|
      [record.id, [Gitlab::Json.parse(record.properties), record.encrypted_properties_tmp]]
    end

    expect(integrations.count).to eq(4)

    expect(props).to match(
      no_properties.id    => both(be_nil),
      with_plaintext_1.id => both(eq some_props(1)),
      with_plaintext_2.id => both(eq some_props(2)),
      with_encrypted.id   => match([be_nil, eq(some_props(3))])
    )
  end

  private

  def both(obj)
    match [obj, obj]
  end

  def some_props(id)
    HashWithIndifferentAccess.new({ id: id, foo: 1, bar: true, baz: %w[a string array] })
  end

  def json_props(id)
    some_props(id).to_json
  end
end