Sha256: 30bc3d668ae93c72f6caa09299653d9ceac291d01555e10b9d6d3632c4054cce

Contents?: true

Size: 1.47 KB

Versions: 6

Compression:

Stored size: 1.47 KB

Contents

require_relative 'test_helper'

DataMapper.setup(:default, 'sqlite3::memory:')

class Client
  include DataMapper::Resource

  property :id, Serial
  property :encrypted_email, String
  property :encrypted_email_iv, String
  property :encrypted_email_salt, String

  property :encrypted_credentials, Text
  property :encrypted_credentials_iv, Text
  property :encrypted_credentials_salt, Text

  self.attr_encrypted_options[:mode] = :per_attribute_iv_and_salt

  attr_encrypted :email, :key => SECRET_KEY
  attr_encrypted :credentials, :key => SECRET_KEY, :marshal => true

  def initialize(attrs = {})
    super attrs
    self.credentials ||= { :username => 'example', :password => 'test' }
  end
end

DataMapper.auto_migrate!

class DataMapperTest < Minitest::Test

  def setup
    Client.all.each(&:destroy)
  end

  def test_should_encrypt_email
    @client = Client.new :email => 'test@example.com'
    assert @client.save
    refute_nil @client.encrypted_email
    refute_equal @client.email, @client.encrypted_email
    assert_equal @client.email, Client.first.email
  end

  def test_should_marshal_and_encrypt_credentials
    @client = Client.new
    assert @client.save
    refute_nil @client.encrypted_credentials
    refute_equal @client.credentials, @client.encrypted_credentials
    assert_equal @client.credentials, Client.first.credentials
    assert Client.first.credentials.is_a?(Hash)
  end

  def test_should_encode_by_default
    assert Client.attr_encrypted_options[:encode]
  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
attr_encrypted-3.1.0 test/data_mapper_test.rb
attr_encrypted-3.0.3 test/data_mapper_test.rb
attr_encrypted-3.0.2 test/data_mapper_test.rb
attr_encrypted-3.0.1 test/data_mapper_test.rb
attr_encrypted-3.0.0 test/data_mapper_test.rb
attr_encrypted-2.0.0 test/data_mapper_test.rb