Sha256: eae501a7c34e281fc3c9a277699ef93374e412508c713a0f5e630c131229f83b

Contents?: true

Size: 1.47 KB

Versions: 1

Compression:

Stored size: 1.47 KB

Contents

require 'spec_helper'

require 'cryptonite'

require 'active_record'

describe Cryptonite do
  before do
    ::ActiveRecord::Base.establish_connection(adapter: 'sqlite3',
                                              encoding: 'utf8',
                                              reconnect: false,
                                              database: ':memory:')

    ::ActiveRecord::Schema.define do
      create_table :sensitive_data, :force => true do |t|
        t.column :secret, :text
      end
    end
  end

  subject {
    Class.new(ActiveRecord::Base) do
      def self.table_name
        "sensitive_data"
      end    
    end.tap { |obj| obj.attr_encrypted :secret }
  }

  context "with public key only" do
    before do
      stub_const('Cryptonite::PRIVATE_KEY', nil)
    end

    it 'encrypts field in database' do
      secret = SecureRandom.hex(16)

      subject.new(secret: secret).tap do |instance|
        expect(
          instance.instance_variable_get(:@attributes).send(:fetch, 'secret')
        ).not_to eq(secret)
      end
    end
  end

  context "with private key only" do
    before do
      stub_const('Cryptonite::PUBLIC_KEY', nil)
    end

    it 'decrypts field in database' do
      secret = SecureRandom.hex(16)

      subject.new.tap do |instance|
        instance.instance_variable_get(:@attributes).send(:store, 'secret', Base64.encode64(PUBLIC_FIXTURE_KEY.public_encrypt(secret)))

        expect(instance.secret).to eq(secret)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cryptonite-0.0.2 spec/cryptonite_spec.rb