Commit 338068be authored by Kamil Trzciński's avatar Kamil Trzciński

Validate `sha_attribute` type casting with `BulkInsertSafe`

To ensure that data is properly serialised and deserialized
from DB representation
parent 53a9b4cc
...@@ -5,13 +5,16 @@ require 'spec_helper' ...@@ -5,13 +5,16 @@ require 'spec_helper'
describe BulkInsertSafe do describe BulkInsertSafe do
class BulkInsertItem < ApplicationRecord class BulkInsertItem < ApplicationRecord
include BulkInsertSafe include BulkInsertSafe
include ShaAttribute
validates :name, :enum_value, :secret_value, presence: true validates :name, :enum_value, :secret_value, :sha_value, presence: true
ENUM_VALUES = { ENUM_VALUES = {
case_1: 1 case_1: 1
}.freeze }.freeze
sha_attribute :sha_value
enum enum_value: ENUM_VALUES enum enum_value: ENUM_VALUES
attr_encrypted :secret_value, attr_encrypted :secret_value,
...@@ -44,6 +47,7 @@ describe BulkInsertSafe do ...@@ -44,6 +47,7 @@ describe BulkInsertSafe do
t.integer :enum_value, null: false t.integer :enum_value, null: false
t.text :encrypted_secret_value, null: false t.text :encrypted_secret_value, null: false
t.string :encrypted_secret_value_iv, null: false t.string :encrypted_secret_value_iv, null: false
t.binary :sha_value, null: false, limit: 20
end end
end end
...@@ -61,7 +65,8 @@ describe BulkInsertSafe do ...@@ -61,7 +65,8 @@ describe BulkInsertSafe do
BulkInsertItem.new( BulkInsertItem.new(
name: "item-#{n}", name: "item-#{n}",
enum_value: 'case_1', enum_value: 'case_1',
secret_value: "my-secret" secret_value: 'my-secret',
sha_value: '2fd4e1c67a2d28fced849ee1bb76e7391b93eb12'
) )
end end
end end
...@@ -71,7 +76,8 @@ describe BulkInsertSafe do ...@@ -71,7 +76,8 @@ describe BulkInsertSafe do
BulkInsertItem.new( BulkInsertItem.new(
name: nil, # requires `name` to be set name: nil, # requires `name` to be set
enum_value: 'case_1', enum_value: 'case_1',
secret_value: "my-secret" secret_value: 'my-secret',
sha_value: '2fd4e1c67a2d28fced849ee1bb76e7391b93eb12'
) )
end end
end end
...@@ -112,6 +118,16 @@ describe BulkInsertSafe do ...@@ -112,6 +118,16 @@ describe BulkInsertSafe do
BulkInsertItem.bulk_insert!(items, batch_size: 5) BulkInsertItem.bulk_insert!(items, batch_size: 5)
end end
it 'items can be properly fetched from database' do
items = build_valid_items_for_bulk_insertion
BulkInsertItem.bulk_insert!(items)
attribute_names = BulkInsertItem.attribute_names - %w[id]
expect(BulkInsertItem.last(items.size).pluck(*attribute_names)).to eq(
items.pluck(*attribute_names))
end
it 'rolls back the transaction when any item is invalid' do it 'rolls back the transaction when any item is invalid' do
# second batch is bad # second batch is bad
all_items = build_valid_items_for_bulk_insertion + build_invalid_items_for_bulk_insertion all_items = build_valid_items_for_bulk_insertion + build_invalid_items_for_bulk_insertion
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment