forked from attr-encrypted/attr_encrypted
/
data_mapper_test.rb
52 lines (40 loc) · 1.46 KB
/
data_mapper_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
require File.expand_path('../test_helper', __FILE__)
DataMapper.setup(:default, 'sqlite3::memory:')
class Client
include DataMapper::Resource
property :id, Serial
property :encrypted_email, String
property :encrypted_credentials, Text
property :salt, String
attr_encrypted :email, :key => 'a secret key'
attr_encrypted :credentials, :key => Proc.new { |client| Encryptor.encrypt(:value => client.salt, :key => 'some private key') }, :marshal => true
def initialize(attrs = {})
super attrs
self.salt ||= Digest::SHA1.hexdigest((Time.now.to_i * rand(5)).to_s)
self.credentials ||= { :username => 'example', :password => 'test' }
end
end
DataMapper.auto_migrate!
class DataMapperTest < Test::Unit::TestCase
def setup
Client.all.each(&:destroy)
end
def test_should_encrypt_email
@client = Client.new :email => 'test@example.com'
assert @client.save
assert_not_nil @client.encrypted_email
assert_not_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
assert_not_nil @client.encrypted_credentials
assert_not_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