Sha256: 527fe670938d7f68e0bfc40911bdf7cacfd30b2a29c41e9ae887939c6c4f7d31

Contents?: true

Size: 1.5 KB

Versions: 7

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

require_relative 'test_helper'

DB.create_table :humans do
  primary_key :id
  column :encrypted_email, :string
  column :encrypted_email_salt, String
  column :encrypted_email_iv, :string
  column :password, :string
  column :encrypted_credentials, :string
  column :encrypted_credentials_iv, :string
  column :encrypted_credentials_salt, String
end

class Human < Sequel::Model(:humans)
  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 after_initialize(attrs = {})
    self.credentials ||= { :username => 'example', :password => 'test' }
  end
end

class SequelTest < Minitest::Test

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

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

  def test_should_marshal_and_encrypt_credentials

    @human = Human.new :credentials => { :username => 'example', :password => 'test' }
    assert @human.save
    refute_nil @human.encrypted_credentials
    refute_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

7 entries across 7 versions & 2 rubygems

Version Path
attr_encrypted-4.2.0 test/sequel_test.rb
attr_encrypted-4.1.1 test/sequel_test.rb
attr_encrypted-4.1.0 test/sequel_test.rb
attr_encrypted-4.0.0 test/sequel_test.rb
powerhome-attr_encrypted-1.2.0 test/sequel_test.rb
powerhome-attr_encrypted-1.1.0 test/sequel_test.rb
powerhome-attr_encrypted-1.0.1 test/sequel_test.rb