|
1 | 1 | # frozen_string_literal: true
|
2 | 2 |
|
3 | 3 | RSpec.shared_examples "active_record_encryption" do |model|
|
4 |
| - if PaperTrail.active_record_gte_7_0? |
5 |
| - context "when ActiveRecord Encryption is enabled", versioning: true do |
6 |
| - let(:record) { model.create(supplier: "ABC", name: "Tomato") } |
7 |
| - |
8 |
| - before do |
9 |
| - ActiveRecord::Encryption.configure( |
10 |
| - primary_key: "test", |
11 |
| - deterministic_key: "test", |
12 |
| - key_derivation_salt: "test" |
13 |
| - ) |
14 |
| - end |
| 4 | + context "when ActiveRecord Encryption is enabled", versioning: true do |
| 5 | + let(:record) { model.create(supplier: "ABC", name: "Tomato") } |
| 6 | + |
| 7 | + before do |
| 8 | + ActiveRecord::Encryption.configure( |
| 9 | + primary_key: "test", |
| 10 | + deterministic_key: "test", |
| 11 | + key_derivation_salt: "test" |
| 12 | + ) |
| 13 | + end |
15 | 14 |
|
16 |
| - it "is versioned with encrypted values" do |
17 |
| - original_supplier, original_name = record.values_at(:supplier, :name) |
18 |
| - |
19 |
| - # supplier is encrypted, name is not |
20 |
| - record.update!(supplier: "XYZ", name: "Avocado") |
21 |
| - |
22 |
| - expect(record.versions.count).to be 2 |
23 |
| - expect(record.versions.pluck(:event)).to include("create", "update") |
24 |
| - |
25 |
| - # versioned encrypted value should be something like |
26 |
| - # "{\"p\":\"zDQU\",\"h\":{\"iv\":\"h2OADmJT3DfK1EZc\",\"at\":\"Urcd0mGSwyu9rGT1vrE5cg==\"}}" |
27 |
| - |
28 |
| - # check paper trail object |
29 |
| - object = record.versions.last.object |
30 |
| - expect(object.to_s).not_to include("XYZ") |
31 |
| - versioned_supplier, versioned_name = object.values_at("supplier", "name") |
32 |
| - # encrypted column should be versioned with encrypted value |
33 |
| - expect(versioned_supplier).not_to eq(original_supplier) |
34 |
| - # non-encrypted column should be versioned with the original value |
35 |
| - expect(versioned_name).to eq(original_name) |
36 |
| - parsed_versioned_supplier = JSON.parse(versioned_supplier) |
37 |
| - expect(parsed_versioned_supplier) |
38 |
| - .to match(hash_including("p", "h" => hash_including("iv", "at"))) |
39 |
| - |
40 |
| - # check paper trail object_changes |
41 |
| - object_changes = record.versions.last.object_changes |
42 |
| - expect(object_changes.to_s).not_to include("XYZ") |
43 |
| - supplier_changes, name_changes = object_changes.values_at("supplier", "name") |
44 |
| - expect(supplier_changes).not_to eq([original_supplier, "XYZ"]) |
45 |
| - expect(name_changes).to eq([original_name, "Avocado"]) |
46 |
| - supplier_changes.each do |supplier| |
47 |
| - parsed_supplier = JSON.parse(supplier) |
48 |
| - expect(parsed_supplier).to match(hash_including("p", "h" => hash_including("iv", "at"))) |
49 |
| - end |
| 15 | + it "is versioned with encrypted values" do |
| 16 | + original_supplier, original_name = record.values_at(:supplier, :name) |
| 17 | + |
| 18 | + # supplier is encrypted, name is not |
| 19 | + record.update!(supplier: "XYZ", name: "Avocado") |
| 20 | + |
| 21 | + expect(record.versions.count).to be 2 |
| 22 | + expect(record.versions.pluck(:event)).to include("create", "update") |
| 23 | + |
| 24 | + # versioned encrypted value should be something like |
| 25 | + # "{\"p\":\"zDQU\",\"h\":{\"iv\":\"h2OADmJT3DfK1EZc\",\"at\":\"Urcd0mGSwyu9rGT1vrE5cg==\"}}" |
| 26 | + |
| 27 | + # check paper trail object |
| 28 | + object = record.versions.last.object |
| 29 | + expect(object.to_s).not_to include("XYZ") |
| 30 | + versioned_supplier, versioned_name = object.values_at("supplier", "name") |
| 31 | + # encrypted column should be versioned with encrypted value |
| 32 | + expect(versioned_supplier).not_to eq(original_supplier) |
| 33 | + # non-encrypted column should be versioned with the original value |
| 34 | + expect(versioned_name).to eq(original_name) |
| 35 | + parsed_versioned_supplier = JSON.parse(versioned_supplier) |
| 36 | + expect(parsed_versioned_supplier) |
| 37 | + .to match(hash_including("p", "h" => hash_including("iv", "at"))) |
| 38 | + |
| 39 | + # check paper trail object_changes |
| 40 | + object_changes = record.versions.last.object_changes |
| 41 | + expect(object_changes.to_s).not_to include("XYZ") |
| 42 | + supplier_changes, name_changes = object_changes.values_at("supplier", "name") |
| 43 | + expect(supplier_changes).not_to eq([original_supplier, "XYZ"]) |
| 44 | + expect(name_changes).to eq([original_name, "Avocado"]) |
| 45 | + supplier_changes.each do |supplier| |
| 46 | + parsed_supplier = JSON.parse(supplier) |
| 47 | + expect(parsed_supplier).to match(hash_including("p", "h" => hash_including("iv", "at"))) |
50 | 48 | end
|
| 49 | + end |
51 | 50 |
|
52 |
| - it "reifies encrypted values to decrypted values" do |
53 |
| - record.update!(supplier: "XYZ", name: "Avocado") |
54 |
| - expect(record.versions.last.reify.supplier).to eq "ABC" |
55 |
| - end |
| 51 | + it "reifies encrypted values to decrypted values" do |
| 52 | + record.update!(supplier: "XYZ", name: "Avocado") |
| 53 | + expect(record.versions.last.reify.supplier).to eq "ABC" |
56 | 54 | end
|
57 | 55 | end
|
58 | 56 | end
|
0 commit comments