/
encrypted_attributes_test.rb
277 lines (227 loc) · 8.11 KB
/
encrypted_attributes_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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
require File.dirname(__FILE__) + '/../test_helper'
class EncryptedAttributesTest < ActiveSupport::TestCase
def setup
User.encrypts :password
end
def test_should_use_sha_by_default
user = create_user(:login => 'admin', :password => 'secret')
assert_equal '8152bc582f58c854f580cb101d3182813dec4afe', "#{user.password}"
end
def test_should_encrypt_on_invalid_model
user = new_user(:login => nil, :password => 'secret')
assert !user.valid?
assert_equal '8152bc582f58c854f580cb101d3182813dec4afe', "#{user.password}"
end
def test_should_not_encrypt_if_attribute_is_nil
user = create_user(:login => 'admin', :password => nil)
assert_nil user.password
end
def test_should_not_encrypt_if_attribute_is_blank
user = create_user(:login => 'admin', :password => '')
assert_equal '', user.password
end
def test_should_not_encrypt_if_already_encrypted
user = create_user(:login => 'admin', :password => 'secret'.encrypt)
assert_equal '8152bc582f58c854f580cb101d3182813dec4afe', "#{user.password}"
end
def test_should_return_encrypted_attribute_for_saved_record
user = create_user(:login => 'admin', :password => 'secret')
user = User.find(user.id)
assert user.password.encrypted?
assert_equal '8152bc582f58c854f580cb101d3182813dec4afe', "#{user.password}"
end
def test_should_not_encrypt_attribute_if_updating_without_any_changes
user = create_user(:login => 'admin', :password => 'secret')
user.login = 'Administrator'
user.save!
assert user.password.encrypted?
assert_equal '8152bc582f58c854f580cb101d3182813dec4afe', "#{user.password}"
end
def test_should_encrypt_attribute_if_updating_with_changes
user = create_user(:login => 'admin', :password => 'secret')
user.password = 'shhh'
user.save!
assert user.password.encrypted?
assert_equal '162cf5debf84cbc2af13da848544c3e2c515b4d3', "#{user.password}"
end
def teardown
User.class_eval do
@before_validation_callbacks = nil
end
end
end
class EncryptedAttributesWithDifferentTargetTest < ActiveSupport::TestCase
def setup
User.encrypts :password, :to => :crypted_password
end
def test_should_not_encrypt_if_attribute_is_nil
user = create_user(:login => 'admin', :password => nil)
assert_nil user.password
assert_nil user.crypted_password
end
def test_should_not_encrypt_if_attribute_is_blank
user = create_user(:login => 'admin', :password => '')
assert_equal '', user.password
assert_nil user.crypted_password
end
def test_should_not_encrypt_if_already_encrypted
user = create_user(:login => 'admin', :crypted_password => 'secret'.encrypt)
assert_equal '8152bc582f58c854f580cb101d3182813dec4afe', "#{user.crypted_password}"
end
def test_should_return_encrypted_attribute_for_saved_record
user = create_user(:login => 'admin', :password => 'secret')
user = User.find(user.id)
assert user.crypted_password.encrypted?
assert_equal '8152bc582f58c854f580cb101d3182813dec4afe', "#{user.crypted_password}"
end
def test_should_not_encrypt_attribute_if_updating_without_any_changes
user = create_user(:login => 'admin', :password => 'secret')
user.login = 'Administrator'
user.save!
assert user.crypted_password.encrypted?
assert_equal '8152bc582f58c854f580cb101d3182813dec4afe', "#{user.crypted_password}"
end
def test_should_encrypt_attribute_if_updating_with_changes
user = create_user(:login => 'admin', :password => 'secret')
user.password = 'shhh'
user.save!
assert user.crypted_password.encrypted?
assert_equal '162cf5debf84cbc2af13da848544c3e2c515b4d3', "#{user.crypted_password}"
end
def teardown
User.class_eval do
@before_validation_callbacks = nil
end
end
end
class EncryptedAttributesWithConditionalsTest < ActiveSupport::TestCase
def test_should_not_encrypt_if_if_conditional_is_false
User.encrypts :password, :if => lambda {|user| false}
user = create_user(:login => 'admin', :password => 'secret')
assert_equal 'secret', user.password
end
def test_should_encrypt_if_if_conditional_is_true
User.encrypts :password, :if => lambda {|user| true}
user = create_user(:login => 'admin', :password => 'secret')
assert_equal '8152bc582f58c854f580cb101d3182813dec4afe', "#{user.password}"
end
def test_should_not_encrypt_if_unless_conditional_is_true
User.encrypts :password, :unless => lambda {|user| true}
user = create_user(:login => 'admin', :password => 'secret')
assert_equal 'secret', user.password
end
def test_should_encrypt_if_unless_conditional_is_false
User.encrypts :password, :unless => lambda {|user| false}
user = create_user(:login => 'admin', :password => 'secret')
assert_equal '8152bc582f58c854f580cb101d3182813dec4afe', "#{user.password}"
end
def teardown
User.class_eval do
@before_validation_callbacks = nil
end
end
end
class ShaEncryptionTest < ActiveSupport::TestCase
def setup
User.encrypts :password, :mode => :sha
@user = create_user(:login => 'admin', :password => 'secret')
end
def test_should_encrypt_password
assert_equal '8152bc582f58c854f580cb101d3182813dec4afe', "#{@user.password}"
end
def test_should_be_encrypted
assert @user.password.encrypted?
end
def test_should_use_sha_cipher
assert_instance_of EncryptedAttributes::ShaCipher, @user.password.cipher
end
def test_should_use_default_salt
assert_equal 'salt', @user.password.cipher.salt
end
def test_should_be_able_to_check_password
assert_equal 'secret', @user.password
end
def teardown
User.class_eval do
@before_validation_callbacks = nil
end
end
end
class ShaWithCustomSaltEncryptionTest < ActiveSupport::TestCase
def setup
User.encrypts :password, :mode => :sha, :salt => :login
@user = create_user(:login => 'admin', :password => 'secret')
end
def test_should_encrypt_password
assert_equal 'a55d037f385cad22efe7862e07b805938d150154admin', "#{@user.password}"
end
def test_should_be_encrypted
assert @user.password.encrypted?
end
def test_should_use_sha_cipher
assert_instance_of EncryptedAttributes::ShaCipher, @user.password.cipher
end
def test_should_use_custom_salt
assert_equal 'admin', @user.password.cipher.salt
end
def test_should_be_able_to_check_password
assert_equal 'secret', @user.password
end
def teardown
User.class_eval do
@before_validation_callbacks = nil
end
end
end
class SymmetricEncryptionTest < ActiveSupport::TestCase
def setup
User.encrypts :password, :mode => :symmetric, :password => 'key'
@user = create_user(:login => 'admin', :password => 'secret')
end
def test_should_encrypt_password
assert_equal "zfKtnSa33tc=\n", @user.password
end
def test_should_be_encrypted
assert @user.password.encrypted?
end
def test_should_use_sha_cipher
assert_instance_of EncryptedStrings::SymmetricCipher, @user.password.cipher
end
def test_should_use_custom_password
assert_equal 'key', @user.password.cipher.password
end
def test_should_be_able_to_check_password
assert_equal 'secret', @user.password
end
def teardown
User.class_eval do
@before_validation_callbacks = nil
end
end
end
class AsymmetricEncryptionTest < ActiveSupport::TestCase
def setup
User.encrypts :password, :mode => :asymmetric,
:private_key_file => File.dirname(__FILE__) + '/../keys/private',
:public_key_file => File.dirname(__FILE__) + '/../keys/public'
@user = create_user(:login => 'admin', :password => 'secret')
end
def test_should_encrypt_password
assert_not_equal 'secret', "#{@user.password}"
assert_equal 90, @user.password.length
end
def test_should_be_encrypted
assert @user.password.encrypted?
end
def test_should_use_sha_cipher
assert_instance_of EncryptedStrings::AsymmetricCipher, @user.password.cipher
end
def test_should_be_able_to_check_password
assert_equal 'secret', @user.password
end
def teardown
User.class_eval do
@before_validation_callbacks = nil
end
end
end