symmetric-encryption 3.7.2 → 3.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +65 -83
- data/Rakefile +4 -4
- data/lib/rails/generators/symmetric_encryption/config/config_generator.rb +3 -3
- data/lib/rails/generators/symmetric_encryption/heroku_config/heroku_config_generator.rb +3 -3
- data/lib/rails/generators/symmetric_encryption/new_keys/new_keys_generator.rb +2 -2
- data/lib/symmetric_encryption.rb +7 -1
- data/lib/symmetric_encryption/cipher.rb +180 -50
- data/lib/symmetric_encryption/coerce.rb +75 -0
- data/lib/symmetric_encryption/config.rb +88 -0
- data/lib/symmetric_encryption/extensions/active_record/base.rb +2 -2
- data/lib/symmetric_encryption/extensions/mongoid/encrypted.rb +2 -2
- data/lib/symmetric_encryption/generator.rb +5 -1
- data/lib/symmetric_encryption/railtie.rb +3 -3
- data/lib/symmetric_encryption/railties/symmetric_encryption.rake +6 -6
- data/lib/symmetric_encryption/railties/symmetric_encryption_validator.rb +1 -1
- data/lib/symmetric_encryption/reader.rb +16 -14
- data/lib/symmetric_encryption/symmetric_encryption.rb +30 -285
- data/lib/symmetric_encryption/version.rb +1 -1
- data/lib/symmetric_encryption/writer.rb +13 -13
- data/test/active_record_test.rb +126 -73
- data/test/cipher_test.rb +42 -42
- data/test/mongo_mapper_test.rb +171 -114
- data/test/mongoid_test.rb +173 -115
- data/test/reader_test.rb +63 -63
- data/test/symmetric_encryption_test.rb +81 -80
- data/test/test_db.sqlite3 +0 -0
- data/test/test_helper.rb +1 -2
- data/test/writer_test.rb +20 -20
- metadata +13 -13
- data/lib/_test_empty +0 -0
data/test/mongoid_test.rb
CHANGED
@@ -5,9 +5,10 @@ begin
|
|
5
5
|
ENV['RACK_ENV'] = 'test'
|
6
6
|
|
7
7
|
Mongoid.logger = SemanticLogger[Mongoid]
|
8
|
-
filename
|
8
|
+
filename = defined?(Mongoid::VERSION) ? "test/config/mongoid_v3.yml" : "test/config/mongoid_v2.yml"
|
9
9
|
Mongoid.load!(filename)
|
10
10
|
|
11
|
+
#@formatter:off
|
11
12
|
class MongoidUser
|
12
13
|
include Mongoid::Document
|
13
14
|
|
@@ -33,58 +34,74 @@ begin
|
|
33
34
|
validates :encrypted_social_security_number, symmetric_encryption: true
|
34
35
|
end
|
35
36
|
|
37
|
+
class MongoidUniqueUser
|
38
|
+
include Mongoid::Document
|
39
|
+
|
40
|
+
field :encrypted_email, type: String, encrypted: true
|
41
|
+
field :encrypted_username, type: String, encrypted: true
|
42
|
+
|
43
|
+
validates_uniqueness_of :encrypted_email, allow_blank: true, if: :encrypted_email_changed?
|
44
|
+
validates_uniqueness_of :encrypted_username, allow_blank: true, if: :encrypted_username_changed?
|
45
|
+
|
46
|
+
validates :username,
|
47
|
+
length: {in: 3..20},
|
48
|
+
format: {with: /\A[\w\d\-[[:alnum:]]]+\z/},
|
49
|
+
allow_blank: true
|
50
|
+
end
|
51
|
+
#@formatter:on
|
52
|
+
|
36
53
|
#
|
37
54
|
# Unit Tests for field encrypted and validation aspects of SymmetricEncryption
|
38
55
|
#
|
39
56
|
class MongoidTest < Minitest::Test
|
40
|
-
|
41
|
-
|
42
|
-
@bank_account_number
|
57
|
+
describe 'Mongoid' do
|
58
|
+
before do
|
59
|
+
@bank_account_number = "1234567890"
|
43
60
|
@bank_account_number_encrypted = "QEVuQwIAL94ArJeFlJrZp6SYsvoOGA=="
|
44
61
|
|
45
|
-
@social_security_number
|
62
|
+
@social_security_number = "987654321"
|
46
63
|
@social_security_number_encrypted = "QEVuQwIAS+8X1NRrqdfEIQyFHVPuVA=="
|
47
64
|
|
48
|
-
@integer
|
65
|
+
@integer = 32768
|
49
66
|
@integer_encrypted = "FA3smFQEKqB/ITv+A0xACg=="
|
50
67
|
|
51
|
-
@float
|
68
|
+
@float = 0.9867
|
52
69
|
@float_encrypted = "z7Pwt2JDp74d+u0IXFAdrQ=="
|
53
70
|
|
54
|
-
@date
|
71
|
+
@date = Date.parse('20120320')
|
55
72
|
@date_encrypted = "WTkSPHo5ApSSHBJMxxWt2A=="
|
56
73
|
|
57
|
-
@string
|
74
|
+
@string = "A string containing some data to be encrypted with a random initialization vector"
|
58
75
|
@long_string = "A string containing some data to be encrypted with a random initialization vector and compressed since it takes up so much space in plain text form"
|
59
76
|
|
60
|
-
@integer_value
|
61
|
-
@float_value
|
62
|
-
@decimal_value
|
77
|
+
@integer_value = 12
|
78
|
+
@float_value = 88.12345
|
79
|
+
@decimal_value = BigDecimal.new("22.51")
|
63
80
|
@datetime_value = DateTime.new(2001, 11, 26, 20, 55, 54, "-5")
|
64
|
-
@time_value
|
65
|
-
@date_value
|
66
|
-
@h
|
81
|
+
@time_value = Time.new(2013, 01, 01, 22, 30, 00, "-04:00")
|
82
|
+
@date_value = Date.new(1927, 04, 02)
|
83
|
+
@h = {a: 'A', b: 'B'}
|
67
84
|
|
68
85
|
@user = MongoidUser.new(
|
69
|
-
encrypted_bank_account_number:
|
86
|
+
encrypted_bank_account_number: @bank_account_number_encrypted,
|
70
87
|
encrypted_social_security_number: @social_security_number_encrypted,
|
71
|
-
name:
|
88
|
+
name: "Joe Bloggs",
|
72
89
|
# data type specific fields
|
73
|
-
integer_value:
|
74
|
-
aliased_integer_value:
|
75
|
-
float_value:
|
76
|
-
decimal_value:
|
77
|
-
datetime_value:
|
78
|
-
time_value:
|
79
|
-
date_value:
|
80
|
-
true_value:
|
81
|
-
false_value:
|
82
|
-
data_yaml:
|
83
|
-
data_json:
|
90
|
+
integer_value: @integer_value,
|
91
|
+
aliased_integer_value: @integer_value,
|
92
|
+
float_value: @float_value,
|
93
|
+
decimal_value: @decimal_value,
|
94
|
+
datetime_value: @datetime_value,
|
95
|
+
time_value: @time_value,
|
96
|
+
date_value: @date_value,
|
97
|
+
true_value: true,
|
98
|
+
false_value: false,
|
99
|
+
data_yaml: @h.dup,
|
100
|
+
data_json: @h.dup
|
84
101
|
)
|
85
102
|
end
|
86
103
|
|
87
|
-
|
104
|
+
it "have encrypted methods" do
|
88
105
|
assert_equal true, @user.respond_to?(:encrypted_bank_account_number)
|
89
106
|
assert_equal true, @user.respond_to?(:encrypted_social_security_number)
|
90
107
|
assert_equal true, @user.respond_to?(:encrypted_string)
|
@@ -98,7 +115,7 @@ begin
|
|
98
115
|
assert_equal false, @user.respond_to?(:encrypted_name=)
|
99
116
|
end
|
100
117
|
|
101
|
-
|
118
|
+
it "have unencrypted methods" do
|
102
119
|
assert_equal true, @user.respond_to?(:bank_account_number)
|
103
120
|
assert_equal true, @user.respond_to?(:social_security_number)
|
104
121
|
assert_equal true, @user.respond_to?(:string)
|
@@ -112,22 +129,22 @@ begin
|
|
112
129
|
assert_equal true, @user.respond_to?(:name=)
|
113
130
|
end
|
114
131
|
|
115
|
-
|
132
|
+
it "support aliased fields" do
|
116
133
|
assert_equal true, @user.respond_to?(:aliased_integer_value=)
|
117
134
|
assert_equal true, @user.respond_to?(:aliased_integer_value)
|
118
135
|
end
|
119
136
|
|
120
|
-
|
137
|
+
it "have unencrypted values" do
|
121
138
|
assert_equal @bank_account_number, @user.bank_account_number
|
122
139
|
assert_equal @social_security_number, @user.social_security_number
|
123
140
|
end
|
124
141
|
|
125
|
-
|
142
|
+
it "have encrypted values" do
|
126
143
|
assert_equal @bank_account_number_encrypted, @user.encrypted_bank_account_number
|
127
144
|
assert_equal @social_security_number_encrypted, @user.encrypted_social_security_number
|
128
145
|
end
|
129
146
|
|
130
|
-
|
147
|
+
it "support same iv" do
|
131
148
|
@user.social_security_number = @social_security_number
|
132
149
|
assert first_value = @user.social_security_number
|
133
150
|
# Assign the same value
|
@@ -135,7 +152,7 @@ begin
|
|
135
152
|
assert_equal first_value, @user.social_security_number
|
136
153
|
end
|
137
154
|
|
138
|
-
|
155
|
+
it "support a random iv" do
|
139
156
|
@user.string = @string
|
140
157
|
assert first_value = @user.encrypted_string
|
141
158
|
# Assign the same value
|
@@ -143,31 +160,31 @@ begin
|
|
143
160
|
assert_equal true, first_value != @user.encrypted_string
|
144
161
|
end
|
145
162
|
|
146
|
-
|
147
|
-
@user.string
|
163
|
+
it "support a random iv and compress" do
|
164
|
+
@user.string = @long_string
|
148
165
|
@user.long_string = @long_string
|
149
166
|
|
150
167
|
assert_equal true, (@user.encrypted_long_string.length.to_f / @user.encrypted_string.length) < 0.8
|
151
168
|
end
|
152
169
|
|
153
|
-
|
154
|
-
user
|
170
|
+
it "encrypt" do
|
171
|
+
user = MongoidUser.new
|
155
172
|
user.bank_account_number = @bank_account_number
|
156
173
|
assert_equal @bank_account_number, user.bank_account_number
|
157
174
|
assert_equal @bank_account_number_encrypted, user.encrypted_bank_account_number
|
158
175
|
end
|
159
176
|
|
160
|
-
|
177
|
+
it "all paths it lead to the same result" do
|
161
178
|
assert_equal @bank_account_number_encrypted, (@user.encrypted_social_security_number = @bank_account_number_encrypted)
|
162
179
|
assert_equal @bank_account_number, @user.social_security_number
|
163
180
|
end
|
164
181
|
|
165
|
-
|
182
|
+
it "all paths it lead to the same result 2" do
|
166
183
|
assert_equal @bank_account_number, (@user.social_security_number = @bank_account_number)
|
167
184
|
assert_equal @bank_account_number_encrypted, @user.encrypted_social_security_number
|
168
185
|
end
|
169
186
|
|
170
|
-
|
187
|
+
it "all paths it lead to the same result, check uninitialized" do
|
171
188
|
user = MongoidUser.new
|
172
189
|
assert_equal nil, user.social_security_number
|
173
190
|
assert_equal @bank_account_number, (user.social_security_number = @bank_account_number)
|
@@ -179,7 +196,7 @@ begin
|
|
179
196
|
assert_equal nil, user.encrypted_social_security_number
|
180
197
|
end
|
181
198
|
|
182
|
-
|
199
|
+
it "allow unencrypted values to be passed to the constructor" do
|
183
200
|
user = MongoidUser.new(bank_account_number: @bank_account_number, social_security_number: @social_security_number)
|
184
201
|
assert_equal @bank_account_number, user.bank_account_number
|
185
202
|
assert_equal @social_security_number, user.social_security_number
|
@@ -187,7 +204,7 @@ begin
|
|
187
204
|
assert_equal @social_security_number_encrypted, user.encrypted_social_security_number
|
188
205
|
end
|
189
206
|
|
190
|
-
|
207
|
+
it "allow both encrypted and unencrypted values to be passed to the constructor" do
|
191
208
|
user = MongoidUser.new(encrypted_bank_account_number: @bank_account_number_encrypted, social_security_number: @social_security_number)
|
192
209
|
assert_equal @bank_account_number, user.bank_account_number
|
193
210
|
assert_equal @social_security_number, user.social_security_number
|
@@ -195,32 +212,57 @@ begin
|
|
195
212
|
assert_equal @social_security_number_encrypted, user.encrypted_social_security_number
|
196
213
|
end
|
197
214
|
|
198
|
-
|
199
|
-
|
215
|
+
describe 'changed?' do
|
216
|
+
before do
|
217
|
+
@user.save!
|
218
|
+
end
|
219
|
+
|
220
|
+
after do
|
221
|
+
@user.destroy if @user
|
222
|
+
end
|
223
|
+
|
224
|
+
it 'return false if it was not changed' do
|
225
|
+
assert_equal false, @user.encrypted_bank_account_number_changed?
|
226
|
+
assert_equal false, @user.bank_account_number_changed?
|
227
|
+
end
|
228
|
+
|
229
|
+
it 'return true if it was changed' do
|
230
|
+
@user.bank_account_number = '15424623'
|
231
|
+
assert_equal true, @user.encrypted_bank_account_number_changed?
|
232
|
+
assert_equal true, @user.bank_account_number_changed?
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
describe "data types" do
|
237
|
+
before do
|
200
238
|
@user.save!
|
201
239
|
@user_clone = MongoidUser.find(@user.id)
|
202
240
|
end
|
203
241
|
|
204
|
-
|
205
|
-
|
242
|
+
after do
|
243
|
+
@user.destroy if @user
|
244
|
+
end
|
245
|
+
|
246
|
+
describe "aliased fields" do
|
247
|
+
it "return correct data type" do
|
206
248
|
@user_clone.aliased_integer_value = "5"
|
207
249
|
assert_equal 5, @user_clone.aliased_integer_value
|
208
250
|
end
|
209
251
|
end
|
210
252
|
|
211
|
-
|
212
|
-
|
253
|
+
describe "integer values" do
|
254
|
+
it "return correct data type" do
|
213
255
|
assert_equal @integer_value, @user_clone.integer_value
|
214
256
|
assert @user.clone.integer_value.kind_of?(Integer)
|
215
257
|
end
|
216
258
|
|
217
|
-
|
259
|
+
it "coerce data type before save" do
|
218
260
|
u = MongoidUser.new(integer_value: "5")
|
219
261
|
assert_equal 5, u.integer_value
|
220
262
|
assert u.integer_value.kind_of?(Integer)
|
221
263
|
end
|
222
264
|
|
223
|
-
|
265
|
+
it "permit replacing value with nil" do
|
224
266
|
@user_clone.integer_value = nil
|
225
267
|
@user_clone.save!
|
226
268
|
|
@@ -229,8 +271,8 @@ begin
|
|
229
271
|
assert_nil @user.encrypted_integer_value
|
230
272
|
end
|
231
273
|
|
232
|
-
|
233
|
-
new_integer_value
|
274
|
+
it "permit replacing value" do
|
275
|
+
new_integer_value = 98
|
234
276
|
@user_clone.integer_value = new_integer_value
|
235
277
|
@user_clone.save!
|
236
278
|
|
@@ -239,19 +281,19 @@ begin
|
|
239
281
|
end
|
240
282
|
end
|
241
283
|
|
242
|
-
|
243
|
-
|
284
|
+
describe "float values" do
|
285
|
+
it "return correct data type" do
|
244
286
|
assert_equal @float_value, @user_clone.float_value
|
245
287
|
assert @user.clone.float_value.kind_of?(Float)
|
246
288
|
end
|
247
289
|
|
248
|
-
|
290
|
+
it "coerce data type before save" do
|
249
291
|
u = MongoidUser.new(float_value: "5.6")
|
250
292
|
assert_equal 5.6, u.float_value
|
251
293
|
assert u.float_value.kind_of?(Float)
|
252
294
|
end
|
253
295
|
|
254
|
-
|
296
|
+
it "permit replacing value with nil" do
|
255
297
|
@user_clone.float_value = nil
|
256
298
|
@user_clone.save!
|
257
299
|
|
@@ -260,8 +302,8 @@ begin
|
|
260
302
|
assert_nil @user.encrypted_float_value
|
261
303
|
end
|
262
304
|
|
263
|
-
|
264
|
-
new_float_value
|
305
|
+
it "permit replacing value" do
|
306
|
+
new_float_value = 45.4321
|
265
307
|
@user_clone.float_value = new_float_value
|
266
308
|
@user_clone.save!
|
267
309
|
|
@@ -270,19 +312,19 @@ begin
|
|
270
312
|
end
|
271
313
|
end
|
272
314
|
|
273
|
-
|
274
|
-
|
315
|
+
describe "decimal values" do
|
316
|
+
it "return correct data type" do
|
275
317
|
assert_equal @decimal_value, @user_clone.decimal_value
|
276
318
|
assert @user.clone.decimal_value.kind_of?(BigDecimal)
|
277
319
|
end
|
278
320
|
|
279
|
-
|
321
|
+
it "coerce data type before save" do
|
280
322
|
u = MongoidUser.new(decimal_value: "99.95")
|
281
323
|
assert_equal BigDecimal.new("99.95"), u.decimal_value
|
282
324
|
assert u.decimal_value.kind_of?(BigDecimal)
|
283
325
|
end
|
284
326
|
|
285
|
-
|
327
|
+
it "permit replacing value with nil" do
|
286
328
|
@user_clone.decimal_value = nil
|
287
329
|
@user_clone.save!
|
288
330
|
|
@@ -291,8 +333,8 @@ begin
|
|
291
333
|
assert_nil @user.encrypted_decimal_value
|
292
334
|
end
|
293
335
|
|
294
|
-
|
295
|
-
new_decimal_value
|
336
|
+
it "permit replacing value" do
|
337
|
+
new_decimal_value = BigDecimal.new("99.95")
|
296
338
|
@user_clone.decimal_value = new_decimal_value
|
297
339
|
@user_clone.save!
|
298
340
|
|
@@ -301,20 +343,20 @@ begin
|
|
301
343
|
end
|
302
344
|
end
|
303
345
|
|
304
|
-
|
305
|
-
|
346
|
+
describe "datetime values" do
|
347
|
+
it "return correct data type" do
|
306
348
|
assert_equal @datetime_value, @user_clone.datetime_value
|
307
349
|
assert @user.clone.datetime_value.kind_of?(DateTime)
|
308
350
|
end
|
309
351
|
|
310
|
-
|
352
|
+
it "coerce data type before save" do
|
311
353
|
now = Time.now
|
312
|
-
u
|
354
|
+
u = MongoidUser.new(datetime_value: now)
|
313
355
|
assert_equal now, u.datetime_value
|
314
356
|
assert u.datetime_value.kind_of?(DateTime)
|
315
357
|
end
|
316
358
|
|
317
|
-
|
359
|
+
it "permit replacing value with nil" do
|
318
360
|
@user_clone.datetime_value = nil
|
319
361
|
@user_clone.save!
|
320
362
|
|
@@ -323,8 +365,8 @@ begin
|
|
323
365
|
assert_nil @user.encrypted_datetime_value
|
324
366
|
end
|
325
367
|
|
326
|
-
|
327
|
-
new_datetime_value
|
368
|
+
it "permit replacing value" do
|
369
|
+
new_datetime_value = DateTime.new(1998, 10, 21, 8, 33, 28, "+5")
|
328
370
|
@user_clone.datetime_value = new_datetime_value
|
329
371
|
@user_clone.save!
|
330
372
|
|
@@ -333,20 +375,20 @@ begin
|
|
333
375
|
end
|
334
376
|
end
|
335
377
|
|
336
|
-
|
337
|
-
|
378
|
+
describe "time values" do
|
379
|
+
it "return correct data type" do
|
338
380
|
assert_equal @time_value, @user_clone.time_value
|
339
381
|
assert @user.clone.time_value.kind_of?(Time)
|
340
382
|
end
|
341
383
|
|
342
|
-
|
384
|
+
it "coerce data type before save" do
|
343
385
|
now = Time.now
|
344
|
-
u
|
386
|
+
u = MongoidUser.new(time_value: now)
|
345
387
|
assert_equal now, u.time_value
|
346
388
|
assert u.time_value.kind_of?(Time)
|
347
389
|
end
|
348
390
|
|
349
|
-
|
391
|
+
it "permit replacing value with nil" do
|
350
392
|
@user_clone.time_value = nil
|
351
393
|
@user_clone.save!
|
352
394
|
|
@@ -355,8 +397,8 @@ begin
|
|
355
397
|
assert_nil @user.encrypted_time_value
|
356
398
|
end
|
357
399
|
|
358
|
-
|
359
|
-
new_time_value
|
400
|
+
it "permit replacing value" do
|
401
|
+
new_time_value = Time.new(1998, 10, 21, 8, 33, 28, "+04:00")
|
360
402
|
@user_clone.time_value = new_time_value
|
361
403
|
@user_clone.save!
|
362
404
|
|
@@ -365,20 +407,20 @@ begin
|
|
365
407
|
end
|
366
408
|
end
|
367
409
|
|
368
|
-
|
369
|
-
|
410
|
+
describe "date values" do
|
411
|
+
it "return correct data type" do
|
370
412
|
assert_equal @date_value, @user_clone.date_value
|
371
413
|
assert @user.clone.date_value.kind_of?(Date)
|
372
414
|
end
|
373
415
|
|
374
|
-
|
416
|
+
it "coerce data type before save" do
|
375
417
|
now = Time.now
|
376
|
-
u
|
418
|
+
u = MongoidUser.new(date_value: now)
|
377
419
|
assert_equal now.to_date, u.date_value
|
378
420
|
assert u.date_value.kind_of?(Date)
|
379
421
|
end
|
380
422
|
|
381
|
-
|
423
|
+
it "permit replacing value with nil" do
|
382
424
|
@user_clone.date_value = nil
|
383
425
|
@user_clone.save!
|
384
426
|
|
@@ -387,8 +429,8 @@ begin
|
|
387
429
|
assert_nil @user.encrypted_date_value
|
388
430
|
end
|
389
431
|
|
390
|
-
|
391
|
-
new_date_value
|
432
|
+
it "permit replacing value" do
|
433
|
+
new_date_value = Date.new(1998, 10, 21)
|
392
434
|
@user_clone.date_value = new_date_value
|
393
435
|
@user_clone.save!
|
394
436
|
|
@@ -397,19 +439,19 @@ begin
|
|
397
439
|
end
|
398
440
|
end
|
399
441
|
|
400
|
-
|
401
|
-
|
442
|
+
describe "true values" do
|
443
|
+
it "return correct data type" do
|
402
444
|
assert_equal true, @user_clone.true_value
|
403
445
|
assert @user.clone.true_value.kind_of?(TrueClass)
|
404
446
|
end
|
405
447
|
|
406
|
-
|
448
|
+
it "coerce data type before save" do
|
407
449
|
u = MongoidUser.new(true_value: "1")
|
408
450
|
assert_equal true, u.true_value
|
409
451
|
assert u.true_value.kind_of?(TrueClass)
|
410
452
|
end
|
411
453
|
|
412
|
-
|
454
|
+
it "permit replacing value with nil" do
|
413
455
|
@user_clone.true_value = nil
|
414
456
|
@user_clone.save!
|
415
457
|
|
@@ -418,8 +460,8 @@ begin
|
|
418
460
|
assert_nil @user.encrypted_true_value
|
419
461
|
end
|
420
462
|
|
421
|
-
|
422
|
-
new_value
|
463
|
+
it "permit replacing value" do
|
464
|
+
new_value = false
|
423
465
|
@user_clone.true_value = new_value
|
424
466
|
@user_clone.save!
|
425
467
|
|
@@ -428,19 +470,19 @@ begin
|
|
428
470
|
end
|
429
471
|
end
|
430
472
|
|
431
|
-
|
432
|
-
|
473
|
+
describe "false values" do
|
474
|
+
it "return correct data type" do
|
433
475
|
assert_equal false, @user_clone.false_value
|
434
476
|
assert @user.clone.false_value.kind_of?(FalseClass)
|
435
477
|
end
|
436
478
|
|
437
|
-
|
479
|
+
it "coerce data type before save" do
|
438
480
|
u = MongoidUser.new(false_value: "0")
|
439
481
|
assert_equal false, u.false_value
|
440
482
|
assert u.false_value.kind_of?(FalseClass)
|
441
483
|
end
|
442
484
|
|
443
|
-
|
485
|
+
it "permit replacing value with nil" do
|
444
486
|
@user_clone.false_value = nil
|
445
487
|
@user_clone.save!
|
446
488
|
|
@@ -449,8 +491,8 @@ begin
|
|
449
491
|
assert_nil @user.encrypted_false_value
|
450
492
|
end
|
451
493
|
|
452
|
-
|
453
|
-
new_value
|
494
|
+
it "permit replacing value" do
|
495
|
+
new_value = true
|
454
496
|
@user_clone.false_value = new_value
|
455
497
|
@user_clone.save!
|
456
498
|
|
@@ -459,8 +501,8 @@ begin
|
|
459
501
|
end
|
460
502
|
end
|
461
503
|
|
462
|
-
|
463
|
-
|
504
|
+
describe "JSON Serialization" do
|
505
|
+
before do
|
464
506
|
# JSON Does not support symbols, so they will come back as strings
|
465
507
|
# Convert symbols to string in the test
|
466
508
|
@h.keys.each do |k|
|
@@ -469,18 +511,18 @@ begin
|
|
469
511
|
end
|
470
512
|
end
|
471
513
|
|
472
|
-
|
514
|
+
it "return correct data type" do
|
473
515
|
assert_equal @h, @user_clone.data_json
|
474
516
|
assert @user.clone.data_json.kind_of?(Hash)
|
475
517
|
end
|
476
518
|
|
477
|
-
|
519
|
+
it "not coerce data type (leaves as hash) before save" do
|
478
520
|
u = MongoidUser.new(data_json: @h)
|
479
521
|
assert_equal @h, u.data_json
|
480
522
|
assert u.data_json.kind_of?(Hash)
|
481
523
|
end
|
482
524
|
|
483
|
-
|
525
|
+
it "permit replacing value with nil" do
|
484
526
|
@user_clone.data_json = nil
|
485
527
|
@user_clone.save!
|
486
528
|
|
@@ -489,9 +531,9 @@ begin
|
|
489
531
|
assert_nil @user.encrypted_data_json
|
490
532
|
end
|
491
533
|
|
492
|
-
|
493
|
-
new_value
|
494
|
-
new_value['c']
|
534
|
+
it "permit replacing value" do
|
535
|
+
new_value = @h.clone
|
536
|
+
new_value['c'] = 'C'
|
495
537
|
@user_clone.data_json = new_value
|
496
538
|
@user_clone.save!
|
497
539
|
|
@@ -500,19 +542,19 @@ begin
|
|
500
542
|
end
|
501
543
|
end
|
502
544
|
|
503
|
-
|
504
|
-
|
545
|
+
describe "YAML Serialization" do
|
546
|
+
it "return correct data type" do
|
505
547
|
assert_equal @h, @user_clone.data_yaml
|
506
548
|
assert @user.clone.data_yaml.kind_of?(Hash)
|
507
549
|
end
|
508
550
|
|
509
|
-
|
551
|
+
it "not coerce data type (leaves as hash) before save" do
|
510
552
|
u = MongoidUser.new(data_yaml: @h)
|
511
553
|
assert_equal @h, u.data_yaml
|
512
554
|
assert u.data_yaml.kind_of?(Hash)
|
513
555
|
end
|
514
556
|
|
515
|
-
|
557
|
+
it "permit replacing value with nil" do
|
516
558
|
@user_clone.data_yaml = nil
|
517
559
|
@user_clone.save!
|
518
560
|
|
@@ -521,9 +563,9 @@ begin
|
|
521
563
|
assert_nil @user.encrypted_data_yaml
|
522
564
|
end
|
523
565
|
|
524
|
-
|
525
|
-
new_value
|
526
|
-
new_value[:c]
|
566
|
+
it "permit replacing value" do
|
567
|
+
new_value = @h.clone
|
568
|
+
new_value[:c] = 'C'
|
527
569
|
@user_clone.data_yaml = new_value
|
528
570
|
@user_clone.save!
|
529
571
|
|
@@ -534,9 +576,25 @@ begin
|
|
534
576
|
|
535
577
|
end
|
536
578
|
|
579
|
+
describe 'uniqueness' do
|
580
|
+
before do
|
581
|
+
MongoidUniqueUser.destroy_all
|
582
|
+
@email = 'whatever@not-unique.com'
|
583
|
+
@username = 'gibby007'
|
584
|
+
@user = MongoidUniqueUser.create!(email: @email)
|
585
|
+
@email_user = MongoidUniqueUser.create!(username: @username)
|
586
|
+
end
|
587
|
+
|
588
|
+
it 'does not allow duplicate values' do
|
589
|
+
duplicate = MongoidUniqueUser.new(email: @email)
|
590
|
+
assert_equal false, duplicate.valid?
|
591
|
+
assert_equal 'has already been taken', duplicate.errors.messages[:encrypted_email].first
|
592
|
+
end
|
593
|
+
end
|
594
|
+
|
537
595
|
end
|
538
596
|
end
|
539
597
|
|
540
598
|
rescue LoadError
|
541
599
|
puts "Not running Mongoid tests because mongoid gem is not installed!!!"
|
542
|
-
end
|
600
|
+
end
|