Sha256: a52a1101857be5ac481bbfb616b901f9779aa4914c1d2b776566c105b89c44f3

Contents?: true

Size: 1.8 KB

Versions: 2

Compression:

Stored size: 1.8 KB

Contents

require 'spec_helper'

db_config_file = File.expand_path('../../config/database.yml', __FILE__)
encryption_config_file = File.expand_path('../../config/symmetric-encryption.yml', __FILE__)

ActiveRecord::Base.configurations = YAML::load(ERB.new(IO.read(db_config_file)).result)
ActiveRecord::Base.establish_connection(:test)

ActiveRecord::Schema.define(version: 0) do
  create_table :widgets, force: true do |t|
    t.string :encrypted_name
    t.string :redacted_name
  end
end

class Widget < ActiveRecord::Base
  attr_encrypted :name
  attr_redacted  :name
end

SymmetricEncryption.load!(encryption_config_file, 'test')

# Initialize the database connection
config = YAML.load(ERB.new(File.new(db_config_file).read).result)['test']

Widget.establish_connection(config)

describe 'attr_redacted' do
  it 'sets redacted name on initialization' do
    widget = Widget.new(name: 'Everything')
    expect(widget.redacted_name).to eq('Eve')

    widget.save!

    widget.reload
    expect(widget.redacted_name).to eq('Eve')
  end

  it 'sets redacted name on creation' do
    widget = Widget.create!(name: 'Testing')
    expect(widget.redacted_name).to eq('Tes')

    widget.reload
    expect(widget.redacted_name).to eq('Tes')
  end

  it 'sets redacted name via assignment' do
    widget = Widget.create!

    widget.name = 'Value'
    expect(widget.redacted_name).to eq('Val')

    widget.save!
    expect(widget.redacted_name).to eq('Val')

    widget.reload
    expect(widget.redacted_name).to eq('Val')
  end

  it 'preserves attr_encrypted functionality' do
    widget = Widget.create!(name: 'Anything')
    expect(widget.name).to eq('Anything')
    expect(widget.encrypted_name).to eq(::SymmetricEncryption.encrypt('Anything'))

    widget.reload
    expect(widget.encrypted_name).to eq(::SymmetricEncryption.encrypt('Anything'))
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
redacted_attributes-1.1.1 spec/acceptance/attr_redacted_spec.rb
redacted_attributes-1.1.0 spec/acceptance/attr_redacted_spec.rb