forked from ankane/lockbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_types_test.rb
605 lines (489 loc) · 16.4 KB
/
model_types_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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
require_relative "test_helper"
class ModelTypesTest < Minitest::Test
def setup
skip if mongoid?
end
def test_string
assert_attribute :country, "USA", format: "USA"
end
def test_string_utf8
assert_attribute :country, "Łukasz", format: "Łukasz"
end
def test_string_non_utf8
if postgresql? || mysql?
error = assert_raises(ActiveRecord::StatementInvalid) do
assert_attribute :country, "Hi \255", format: "Hi \255"
end
if postgresql?
assert_includes error.message, "PG::CharacterNotInRepertoire"
else
assert_includes error.message, "Incorrect string value"
end
else
assert_attribute :country, "Hi \255", format: "Hi \255"
end
end
def test_boolean_true
assert_attribute :active, true, format: "t"
end
def test_boolean_false
assert_attribute :active, false, format: "f"
end
def test_boolean_bytesize
assert_bytesize :active, true, false, size: 1
end
def test_boolean_invalid
# non-falsey values are true
assert_attribute :active, "invalid", expected: true
end
def test_boolean_empty_string
assert_attribute :active, "", expected: nil
end
def test_boolean_query_attribute
user = User.create!(active: true, active2: true)
assert user.active?
assert user.active2?
user = User.last
assert user.active?
assert user.active2?
user = User.create!(active: false, active2: false)
refute user.active?
refute user.active2?
user = User.last
refute user.active?
refute user.active2?
end
def test_date
born_on = Date.current
assert_attribute :born_on, born_on, format: born_on.strftime("%Y-%m-%d")
end
def test_date_bytesize
assert_bytesize :born_on, Date.current, Date.current + 10000, size: 10
assert_bytesize :born_on, Date.current, Date.current - 10000, size: 10
assert_bytesize :born_on, Date.current, Date.parse("999-01-01"), size: 10
refute_bytesize :born_on, Date.current, Date.parse("99999-01-01")
end
def test_date_invalid
assert_attribute :born_on, "invalid", expected: nil
end
def test_datetime
skip if mysql?
signed_at = Time.current.round(6)
assert_attribute :signed_at, signed_at, format: signed_at.utc.iso8601(9), time_zone: true
end
def test_datetime_bytesize
assert_bytesize :signed_at, Time.current, Time.current + 100.years, size: 30
assert_bytesize :signed_at, Time.current, Time.current - 100.years, size: 30
end
def test_datetime_invalid
assert_attribute :signed_at, "invalid", expected: nil
end
def test_time
skip if mysql?
opens_at = Time.current.round(6).utc.change(year: 2000, month: 1, day: 1)
assert_attribute :opens_at, opens_at, format: opens_at.utc.strftime("%H:%M:%S.%N")
end
def test_time_bytesize
assert_bytesize :opens_at, Time.current, Time.current + 5.minutes, size: 18
end
def test_time_invalid
assert_attribute :opens_at, "invalid", expected: nil
end
def test_integer
sign_in_count = 10
assert_attribute :sign_in_count, sign_in_count, format: [sign_in_count].pack("q>")
end
def test_integer_negative
sign_in_count = -10
assert_attribute :sign_in_count, sign_in_count, format: [sign_in_count].pack("q>")
end
def test_integer_bytesize
assert_bytesize :sign_in_count, 10, 1_000_000_000, size: 8
assert_bytesize :sign_in_count, -1_000_000_000, 1_000_000_000, size: 8
end
def test_integer_invalid
assert_attribute :sign_in_count, "invalid", expected: 0
assert_attribute :sign_in_count, "55invalid", expected: 55
end
def test_integer_in_range
value = 2**63 - 1
assert_attribute :sign_in_count, value, expected: value
value = -(2**63)
assert_attribute :sign_in_count, value, expected: value
end
def test_integer_out_of_range
assert_raises ActiveModel::RangeError do
User.create!(sign_in_count: 2**63)
end
assert_raises ActiveModel::RangeError do
User.create!(sign_in_count2: 2**63)
end
assert_raises ActiveModel::RangeError do
User.create!(sign_in_count: -(2**63 + 1))
end
assert_raises ActiveModel::RangeError do
User.create!(sign_in_count2: -(2**63 + 1))
end
end
def test_integer_query_attribute
user = User.create!(sign_in_count: 1, sign_in_count2: 1)
assert user.sign_in_count?
assert user.sign_in_count2?
user = User.last
assert user.sign_in_count?
assert user.sign_in_count2?
user = User.create!(sign_in_count: 0, sign_in_count2: 0)
refute user.sign_in_count?
refute user.sign_in_count2?
user = User.last
refute user.sign_in_count?
refute user.sign_in_count2?
end
def test_float
skip if mysql?
latitude = 10.12345678
assert_attribute :latitude, latitude, format: [latitude].pack("G")
end
def test_float_negative
skip if mysql?
latitude = -10.12345678
assert_attribute :latitude, latitude, format: [latitude].pack("G")
end
def test_float_bigdecimal
skip if postgresql? || mysql?
latitude = BigDecimal("123456789.123456789123456789")
assert_attribute :latitude, latitude, expected: latitude.to_f, format: [latitude].pack("G")
end
def test_float_bytesize
assert_bytesize :latitude, 10, 1_000_000_000.123, size: 8
assert_bytesize :latitude, -1_000_000_000.123, 1_000_000_000.123, size: 8
end
def test_float_invalid
assert_attribute :latitude, "invalid", expected: 0.0
assert_attribute :latitude, "1.2invalid", expected: 1.2
end
def test_float_infinity
skip if mysql?
assert_attribute :latitude, Float::INFINITY, expected: Float::INFINITY, format: [Float::INFINITY].pack("G")
assert_attribute :latitude, -Float::INFINITY, expected: -Float::INFINITY, format: [-Float::INFINITY].pack("G")
end
def test_float_nan
skip if mysql?
assert_attribute :latitude, Float::NAN, expected: Float::NAN, format: [Float::NAN].pack("G")
end
def test_binary
video = SecureRandom.random_bytes(512)
assert_attribute :video, video, format: video
end
def test_binary_bytesize
refute_bytesize :video, SecureRandom.random_bytes(15), SecureRandom.random_bytes(16)
end
def test_json
skip if mysql?
data = {a: 1, b: "hi"}.as_json
assert_attribute :data, data, format: data.to_json
user = User.last
new_data = {c: Time.now}.as_json
user.data = new_data
assert_equal [data, new_data], user.changes["data"]
user.data2 = new_data
assert_equal [data, new_data], user.changes["data2"]
end
def test_json_in_place
user = User.create!(data2: {a: 1, b: "hi"})
user.data2[:c] = "world"
user.save!
user = User.last
assert_equal "world", user.data2["c"]
end
def test_json_in_place_callbacks
Person.create!(data: {"count" => 0})
person = Person.last
assert_equal 1, person.data["count"]
person.save!
person = Person.last
assert_equal 2, person.data["count"]
end
def test_json_save_twice
data2 = {a: 1, b: "hi"}
user = User.create!(data2: data2)
user.reload
user.save!
user.data2
user.save!
new_data2 = {"a" => 1, "b" => "hi"}
assert_equal new_data2, user.data2
end
def test_hash
info = {a: 1, b: "hi"}
assert_attribute :info, info, format: info.to_yaml
# TODO see why keys are strings instead of symbols
user = User.last
new_info = {c: Time.now}
user.info = new_info
assert_equal [info.stringify_keys, new_info.stringify_keys], user.changes["info"]
user.info2 = new_info
assert_equal [info.stringify_keys, new_info.stringify_keys], user.changes["info2"]
end
def test_hash_invalid
assert_raises ActiveRecord::SerializationTypeMismatch do
User.create!(info: "invalid")
end
assert_raises ActiveRecord::SerializationTypeMismatch do
User.create!(info2: "invalid")
end
end
def test_hash_in_place
user = User.create!(info2: {a: 1, b: "hi"})
user.info2[:c] = "world"
user.save!
user = User.last
assert_equal "world", user.info2[:c]
end
def test_hash_save_twice
info2 = {a: 1, b: "hi"}
user = User.create!(info2: info2)
user.reload
user.save!
user.info2
user.save!
assert_equal info2, user.info2
end
def test_hash_empty
user = User.create!
assert_equal({}, user.info)
assert_equal({}, user.info2)
end
def test_array
coordinates = [1, 2, 3]
assert_attribute :coordinates, coordinates, format: coordinates.to_yaml
user = User.last
new_coordinates = [1, 2, 3, 4, 5]
user.coordinates = new_coordinates
assert_equal [coordinates, new_coordinates], user.changes["coordinates"]
user.coordinates2 = new_coordinates
assert_equal [coordinates, new_coordinates], user.changes["coordinates2"]
end
def test_array_invalid
assert_raises ActiveRecord::SerializationTypeMismatch do
User.create!(coordinates: "invalid")
end
assert_raises ActiveRecord::SerializationTypeMismatch do
User.create!(coordinates2: "invalid")
end
end
def test_array_in_place
user = User.create!(coordinates2: [1, 2, 3])
user.coordinates2[3] = 4
user.save!
user = User.last
assert_equal 4, user.coordinates2[3]
end
def test_array_save_twice
coordinates2 = [1, 2, 3]
user = User.create!(coordinates2: coordinates2)
user.reload
user.save!
user.coordinates2
user.save!
assert_equal coordinates2, user.coordinates2
end
def test_array_empty
user = User.create!
assert_equal [], user.coordinates
assert_equal [], user.coordinates2
end
def test_serialize_json
properties = {a: 1, b: "hi"}.as_json
assert_attribute :properties, properties, format: properties.to_json
user = User.last
new_properties = {c: Time.now}.as_json
user.properties = new_properties
assert_equal [properties, new_properties], user.changes["properties"]
user.properties2 = new_properties
assert_equal [properties, new_properties], user.changes["properties2"]
end
def test_serialize_json_in_place
user = User.create!(properties2: {a: 1, b: "hi"})
user.properties2[:c] = "world"
user.save!
user = User.last
assert_equal "world", user.properties2["c"]
end
def test_serialize_hash
settings = {a: 1, b: "hi"}
assert_attribute :settings, settings, format: settings.to_yaml
# TODO see why changes keys are strings instead of symbols
user = User.last
new_settings = {c: Time.now}
user.settings = new_settings
assert_equal [settings.stringify_keys, new_settings.stringify_keys], user.changes["settings"]
user.settings2 = new_settings
assert_equal [settings.stringify_keys, new_settings.stringify_keys], user.changes["settings2"]
end
def test_serialize_hash_in_place
user = User.create!(settings2: {a: 1, b: "hi"})
user.settings2[:c] = "world"
user.save!
user = User.last
assert_equal "world", user.settings2[:c]
end
def test_serialize_hash_invalid
assert_raises ActiveRecord::SerializationTypeMismatch do
User.create!(settings: "invalid")
end
assert_raises ActiveRecord::SerializationTypeMismatch do
User.create!(settings2: "invalid")
end
end
def test_serialize_array
messages = [1, 2, 3]
assert_attribute :messages, messages, format: messages.to_yaml
user = User.last
new_messages = [4]
user.messages = new_messages
assert_equal [messages, new_messages], user.changes["messages"]
user.messages2 = new_messages
assert_equal [messages, new_messages], user.changes["messages2"]
end
def test_serialize_array_in_place
user = User.create!(messages2: [1, 2, 3])
user.messages2[3] = 4
user.save!
user = User.last
assert_equal 4, user.messages2[3]
end
def test_serialize_array_invalid
assert_raises ActiveRecord::SerializationTypeMismatch do
User.create!(messages: "invalid")
end
assert_raises ActiveRecord::SerializationTypeMismatch do
User.create!(messages2: "invalid")
end
end
def test_inet_ipv4
skip unless inet_supported?
ip = IPAddr.new("127.0.0.1")
assert_attribute :ip, ip, expected: ip, format: [0, 32, ip.hton, "\x00"*12].pack("cca4a12")
assert_attribute :ip, ip.to_s, expected: ip, format: [0, 32, ip.hton, "\x00"*12].pack("cca4a12")
end
def test_inet_ipv4_prefix
skip unless inet_supported?
ip = IPAddr.new("127.0.0.0/24")
assert_attribute :ip, ip, expected: ip, format: [0, 24, ip.hton, "\x00"*12].pack("cca4a12")
assert_attribute :ip, "127.0.0.0/24", expected: ip, format: [0, 24, ip.hton, "\x00"*12].pack("cca4a12")
end
def test_inet_ipv6
skip unless inet_supported?
ip = IPAddr.new("::")
assert_attribute :ip, ip, expected: ip, format: [1, 128, ip.hton].pack("cca16")
assert_attribute :ip, ip.to_s, expected: ip, format: [1, 128, ip.hton].pack("cca16")
end
def test_inet_bytesize
skip unless inet_supported?
assert_bytesize :ip, "127.0.0.1", "255.255.255.255", size: 18
assert_bytesize :ip, "::", "2606:4700:4700::64", size: 18
assert_bytesize :ip, "127.0.0.0/24", "255.255.255.255", size: 18
end
def test_inet_invalid
skip unless inet_supported?
assert_attribute :ip, "invalid", expected: nil
end
def test_store
credentials = {a: 1, b: "hi"}.as_json
assert_attribute :credentials, credentials, format: credentials.to_json
assert_attribute :username, "hello", check_nil: false
end
def test_custom
assert_attribute :configuration, "USA", format: "USA!!"
end
def test_custom_attribute
assert_attribute :config, "USA", format: "USA!!"
end
def test_migrating
user = User.create!(conf: "Hi")
key = Lockbox.attribute_key(table: "users", attribute: "conf_ciphertext")
box = Lockbox.new(key: key, encode: true)
assert_equal "Hi!!", box.decrypt_str(user.conf_ciphertext)
end
private
def assert_attribute(attribute, value, format: nil, time_zone: false, check_nil: true, **options)
attribute2 = "#{attribute}2".to_sym
encrypted_attribute = "#{attribute2}_ciphertext"
expected = options.key?(:expected) ? options[:expected] : value
user = User.create!(attribute => value, attribute2 => value)
assert_equal expected, user.send(attribute)
assert_equal expected, user.send(attribute2)
assert_nil user.send(encrypted_attribute) if expected.nil?
# encoding
if expected.is_a?(String)
assert_equal expected.encoding, user.send(attribute).encoding
assert_equal expected.encoding, user.send(attribute2).encoding
end
# time zone
if time_zone
assert_equal Time.zone, user.send(attribute).time_zone
assert_equal Time.zone, user.send(attribute2).time_zone
end
user = User.last
# SQLite does not support NaN
assert_equal expected, user.send(attribute) unless expected.try(:nan?) && !ENV["ADAPTER"]
assert_equal expected, user.send(attribute2)
# encoding
if expected.is_a?(String)
assert_equal expected.encoding, user.send(attribute).encoding
assert_equal expected.encoding, user.send(attribute2).encoding
end
# time zone
if time_zone
assert_equal Time.zone, user.send(attribute).time_zone
assert_equal Time.zone, user.send(attribute2).time_zone
end
if format
key = Lockbox.attribute_key(table: "users", attribute: encrypted_attribute)
box = Lockbox.new(key: key, encode: true)
assert_equal format.force_encoding(Encoding::BINARY), box.decrypt(user.send(encrypted_attribute))
end
if check_nil
user.send("#{attribute2}=", nil)
assert_nil user.send(encrypted_attribute)
end
end
def assert_equal(exp, act)
if exp.try(:nan?)
assert act.try(:nan?), "Expected NaN"
elsif exp.nil?
assert_nil act
else
super
end
end
def assert_bytesize(*args, size: nil)
sizes = bytesizes(*args)
assert_equal(*sizes)
assert_equal size, sizes[0] - 12 - 16 if size
end
def refute_bytesize(*args)
refute_equal(*bytesizes(*args))
end
def bytesizes(attribute, value1, value2)
attribute = "#{attribute}2".to_sym
encrypted_attribute = "#{attribute}_ciphertext"
user1 = User.create!(attribute => value1)
user2 = User.create!(attribute => value2)
result1 = Base64.decode64(user1.send(encrypted_attribute)).bytesize
result2 = Base64.decode64(user2.send(encrypted_attribute)).bytesize
[result1, result2]
end
def mysql?
ENV["ADAPTER"] == "mysql2"
end
def postgresql?
ENV["ADAPTER"] == "postgresql"
end
def inet_supported?
# no NoMethodError for prefix method
# but it exists in Ruby 2.4 docs
postgresql? && RUBY_VERSION.to_f > 2.4
end
end