Sha256: ba075f99e18f553b530e727afa2e9141eca088a390c04cddd54b12785fb7b6db

Contents?: true

Size: 1.42 KB

Versions: 5

Compression:

Stored size: 1.42 KB

Contents

require File.expand_path('../test_helper', __FILE__)

DB = Sequel.sqlite

DB.create_table :humans do
  primary_key :id
  column :encrypted_email, :string
  column :password, :string
  column :encrypted_credentials, :string
  column :salt, :string
end

class Human < Sequel::Model(:humans)  
  attr_encrypted :email, :key => 'a secret key'
  attr_encrypted :credentials, :key => Proc.new { |human| Encryptor.encrypt(:value => human.salt, :key => 'some private key') }, :marshal => true

  def after_initialize(attrs = {})
    self.salt ||= Digest::SHA1.hexdigest((Time.now.to_i * rand(5)).to_s)
    self.credentials ||= { :username => 'example', :password => 'test' }
  end
end

class SequelTest < Test::Unit::TestCase

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

  def test_should_encrypt_email
    @human = Human.new :email => 'test@example.com'
    assert @human.save
    assert_not_nil @human.encrypted_email
    assert_not_equal @human.email, @human.encrypted_email
    assert_equal @human.email, Human.first.email
  end

  def test_should_marshal_and_encrypt_credentials
    @human = Human.new
    assert @human.save
    assert_not_nil @human.encrypted_credentials
    assert_not_equal @human.credentials, @human.encrypted_credentials
    assert_equal @human.credentials, Human.first.credentials
    assert Human.first.credentials.is_a?(Hash)
  end

  def test_should_encode_by_default
    assert Human.attr_encrypted_options[:encode]
  end

end

Version data entries

5 entries across 5 versions & 3 rubygems

Version Path
attr_encrypted-1.2.1 test/sequel_test.rb
attr_encryptor-1.0.1 test/sequel_test.rb
attr_encryptor-1.0.0 test/sequel_test.rb
honkster-attr_encrypted-1.2.0 test/sequel_test.rb
attr_encrypted-1.2.0 test/sequel_test.rb