mongoid 9.1.2 → 9.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/lib/config/locales/en.yml +13 -0
  3. data/lib/mongoid/association/accessors.rb +4 -1
  4. data/lib/mongoid/association/referenced/belongs_to/buildable.rb +19 -15
  5. data/lib/mongoid/association/referenced/belongs_to.rb +11 -3
  6. data/lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb +21 -7
  7. data/lib/mongoid/association/referenced/has_and_belongs_to_many.rb +12 -0
  8. data/lib/mongoid/association/referenced/has_many/buildable.rb +17 -7
  9. data/lib/mongoid/association/referenced/has_many/proxy.rb +4 -2
  10. data/lib/mongoid/association/referenced/has_one/buildable.rb +20 -6
  11. data/lib/mongoid/association.rb +15 -0
  12. data/lib/mongoid/attributes/readonly.rb +19 -1
  13. data/lib/mongoid/changeable.rb +19 -1
  14. data/lib/mongoid/config/encryption.rb +5 -3
  15. data/lib/mongoid/contextual/none.rb +51 -0
  16. data/lib/mongoid/criteria/scopable.rb +5 -0
  17. data/lib/mongoid/criteria.rb +45 -1
  18. data/lib/mongoid/document.rb +10 -0
  19. data/lib/mongoid/encryptable.rb +1 -1
  20. data/lib/mongoid/errors/invalid_encrypted_association.rb +28 -0
  21. data/lib/mongoid/errors.rb +1 -0
  22. data/lib/mongoid/factory.rb +1 -1
  23. data/lib/mongoid/fields/encrypted.rb +14 -3
  24. data/lib/mongoid/fields/foreign_key.rb +9 -0
  25. data/lib/mongoid/fields/standard.rb +10 -0
  26. data/lib/mongoid/fields/validators/macro.rb +29 -0
  27. data/lib/mongoid/fields.rb +42 -2
  28. data/lib/mongoid/reloadable.rb +1 -0
  29. data/lib/mongoid/version.rb +1 -1
  30. data/spec/integration/encryption_spec.rb +269 -0
  31. data/spec/mongoid/association/referenced/belongs_to/buildable_spec.rb +35 -6
  32. data/spec/mongoid/association/referenced/encrypted_foreign_key_spec.rb +142 -0
  33. data/spec/mongoid/association/referenced/has_and_belongs_to_many/buildable_spec.rb +25 -5
  34. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +48 -0
  35. data/spec/mongoid/association/referenced/has_many/buildable_spec.rb +24 -6
  36. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +81 -0
  37. data/spec/mongoid/association/referenced/has_one/buildable_spec.rb +18 -3
  38. data/spec/mongoid/attributes/readonly_dotted_spec.rb +334 -0
  39. data/spec/mongoid/attributes/readonly_spec.rb +389 -0
  40. data/spec/mongoid/changeable_spec.rb +18 -0
  41. data/spec/mongoid/config/encryption_spec.rb +227 -0
  42. data/spec/mongoid/contextual/none_spec.rb +18 -0
  43. data/spec/mongoid/criteria_spec.rb +17 -0
  44. data/spec/mongoid/factory_spec.rb +8 -0
  45. data/spec/mongoid/fields_spec.rb +29 -0
  46. data/spec/mongoid/findable_spec.rb +14 -0
  47. data/spec/mongoid/scopable_spec.rb +102 -0
  48. data/spec/support/crypt/models.rb +42 -0
  49. data/spec/support/models/delete_all_account.rb +10 -0
  50. data/spec/support/models/delete_all_item.rb +9 -0
  51. data/spec/support/models/delete_all_scoped_item.rb +11 -0
  52. data/spec/support/models/delete_all_tag.rb +7 -0
  53. metadata +2045 -2011
@@ -77,6 +77,30 @@ describe Mongoid::Attributes::Readonly do
77
77
  it 'persists the third readonly value' do
78
78
  expect(person.reload.aliased_timestamp).to eq(Time.at(42))
79
79
  end
80
+
81
+ it 'reports the readonly value as a saved change' do
82
+ expect(person.previous_changes['title']).to eq([ nil, 'sir' ])
83
+ expect(person.saved_change_to_attribute('title')).to eq([ nil, 'sir' ])
84
+ expect(person.saved_change_to_attribute?(:title)).to be true
85
+ expect(person.title_previously_changed?).to be true
86
+ end
87
+
88
+ it 'reports the saved change for an aliased readonly field' do
89
+ expect(person.previous_changes['at']).to eq([ nil, Time.at(42) ])
90
+ expect(person.saved_change_to_attribute?(:aliased_timestamp)).to be true
91
+ end
92
+
93
+ it 'does not persist a later change to the readonly field' do
94
+ person.title = 'mr'
95
+ person.score = 1
96
+ person.save!
97
+
98
+ expect(person.previous_changes).not_to have_key('title')
99
+ expect(person.previous_changes).to have_key('score')
100
+
101
+ expect(person.reload.title).to eq('sir')
102
+ expect(person.reload.score).to eq(1)
103
+ end
80
104
  end
81
105
 
82
106
  context 'when updating an existing readonly field' do
@@ -213,6 +237,371 @@ describe Mongoid::Attributes::Readonly do
213
237
  end
214
238
  end
215
239
 
240
+ context 'when modifying a readonly field in place' do
241
+ let(:attributes) do
242
+ %i[map aliases array]
243
+ end
244
+
245
+ let(:person) do
246
+ Person.create!(map: { 'enabled' => false }, aliases: [ 'original' ], array: [ 'original' ], title: 'old')
247
+ end
248
+
249
+ it 'does not persist changes to a readonly hash' do
250
+ person.map['enabled'] = true
251
+ person.save!
252
+
253
+ expect(person.reload.map).to eq('enabled' => false)
254
+ end
255
+
256
+ it 'does not mark the document as changed' do
257
+ person.map['enabled'] = true
258
+
259
+ expect(person).not_to be_changed
260
+ end
261
+
262
+ it 'does not mark an aliased readonly field as changed' do
263
+ person.array << 'added'
264
+
265
+ expect(person).not_to be_changed
266
+ end
267
+
268
+ it 'allows in-place changes before the document is created' do
269
+ new_person = Person.new(map: { 'enabled' => false })
270
+ new_person.map['enabled'] = true
271
+ new_person.save!
272
+
273
+ expect(new_person.reload.map).to eq('enabled' => true)
274
+ end
275
+
276
+ it 'does not report the ignored change as saved' do
277
+ person.map['enabled'] = true
278
+ person.title = 'new'
279
+ person.save!
280
+
281
+ expect(person.previous_changes).not_to have_key('map')
282
+ expect(person.previous_changes).to have_key('title')
283
+ end
284
+
285
+ it 'does not update timestamps for an ignored change' do
286
+ model = test_model do
287
+ include Mongoid::Timestamps
288
+
289
+ field :settings, type: Hash
290
+ attr_readonly :settings
291
+ end
292
+ document = Timecop.freeze(Time.utc(2020, 1, 1)) do
293
+ model.create!(settings: { 'enabled' => false })
294
+ end
295
+
296
+ document.settings['enabled'] = true
297
+ Timecop.freeze(Time.utc(2020, 1, 2)) { document.save! }
298
+
299
+ expect(document.reload.updated_at).to eq(Time.utc(2020, 1, 1))
300
+ end
301
+
302
+ it 'does not persist changes made through attributes' do
303
+ person.map
304
+ person.attributes['map']['enabled'] = true
305
+ person.save!
306
+
307
+ expect(person.reload.map).to eq('enabled' => false)
308
+ end
309
+
310
+ it 'resets an in-place change to a readonly hash' do
311
+ person.map['enabled'] = true
312
+ person.reset_map!
313
+
314
+ expect(person.map).to eq('enabled' => false)
315
+ expect(person).not_to be_changed
316
+ end
317
+
318
+ context 'when a tracked field becomes readonly' do
319
+ let(:model) do
320
+ test_model do
321
+ include Mongoid::Timestamps
322
+
323
+ field :settings, type: Hash
324
+ field :title
325
+ end
326
+ end
327
+
328
+ let(:document) do
329
+ Timecop.freeze(Time.utc(2020, 1, 1)) do
330
+ model.create!(settings: { 'enabled' => false }, title: 'old')
331
+ end
332
+ end
333
+
334
+ before do
335
+ document.settings['enabled'] = true
336
+ model.attr_readonly :settings
337
+ end
338
+
339
+ it 'does not persist the ignored change' do
340
+ document.save!
341
+
342
+ expect(document.reload.settings).to eq('enabled' => false)
343
+ end
344
+
345
+ it 'does not report the ignored change as dirty' do
346
+ expect(document).not_to be_changed
347
+ expect(document.changes).not_to have_key('settings')
348
+ end
349
+
350
+ it 'does not report the readonly field as changed' do
351
+ expect(document.settings_changed?).to be false
352
+ end
353
+
354
+ it 'does not report the ignored change as saved' do
355
+ document.title = 'new'
356
+ document.save!
357
+
358
+ expect(document.previous_changes).not_to have_key('settings')
359
+ expect(document.saved_change_to_attribute?(:settings)).to be false
360
+ expect(document.previous_changes).to have_key('title')
361
+ end
362
+
363
+ it 'does not update timestamps for the ignored change' do
364
+ Timecop.freeze(Time.utc(2020, 1, 2)) { document.save! }
365
+
366
+ expect(document.reload.updated_at).to eq(Time.utc(2020, 1, 1))
367
+ end
368
+ end
369
+
370
+ it 'does not persist changes to a readonly array' do
371
+ person.aliases << 'added'
372
+ person.save!
373
+
374
+ expect(person.reload.aliases).to eq([ 'original' ])
375
+ end
376
+
377
+ it 'does not persist changes to an aliased readonly field' do
378
+ person.array << 'added'
379
+ person.save!
380
+
381
+ expect(person.reload.array).to eq([ 'original' ])
382
+ end
383
+
384
+ it 'persists changes to other fields' do
385
+ person.map['enabled'] = true
386
+ person.title = 'new'
387
+ person.save!
388
+
389
+ expect(person.reload.map).to eq('enabled' => false)
390
+ expect(person.title).to eq('new')
391
+ end
392
+
393
+ it 'still allows atomic set on a readonly field' do
394
+ person.set('map.enabled' => true)
395
+
396
+ expect(person.reload.map).to eq('enabled' => true)
397
+ end
398
+
399
+ it 'still allows atomic unset on a readonly field' do
400
+ person.unset('map.enabled')
401
+
402
+ expect(person.reload.map).to eq({})
403
+ end
404
+
405
+ context 'when an atomic array update fails' do
406
+ it 'restores the readonly array in memory and in the database' do
407
+ expect do
408
+ person.atomically do
409
+ person.push(aliases: 'added')
410
+ raise 'abort'
411
+ end
412
+ end.to raise_error('abort')
413
+
414
+ expect(Person.collection.find(_id: person.id).first['aliases']).to eq([ 'original' ])
415
+ expect(person.aliases).to eq([ 'original' ])
416
+ end
417
+
418
+ it 'still allows a successful atomic update' do
419
+ person.atomically { person.push(aliases: 'added') }
420
+
421
+ expect(person.reload.aliases).to eq(%w[original added])
422
+ end
423
+ end
424
+ end
425
+
426
+ context 'when an embedded document has a readonly hash' do
427
+ let(:attributes) do
428
+ :title
429
+ end
430
+
431
+ let(:person) do
432
+ Person.create!(addresses: [ { street: 'Main', map: { 'enabled' => false } } ])
433
+ end
434
+
435
+ around do |example|
436
+ original = Address.readonly_attributes
437
+ Address.attr_readonly :map
438
+ example.run
439
+ ensure
440
+ Address.readonly_attributes = original
441
+ end
442
+
443
+ it 'allows the initial value to be saved' do
444
+ expect(person.reload.addresses.first.map).to eq('enabled' => false)
445
+ end
446
+
447
+ it 'ignores in-place changes when saving the parent' do
448
+ person.addresses.first.map['enabled'] = true
449
+ person.save!
450
+
451
+ expect(person.reload.addresses.first.map).to eq('enabled' => false)
452
+ end
453
+
454
+ it 'ignores in-place changes when saving the embedded document' do
455
+ address = person.addresses.first
456
+ address.map['enabled'] = true
457
+ address.save!
458
+
459
+ expect(person.reload.addresses.first.map).to eq('enabled' => false)
460
+ end
461
+
462
+ it 'allows an initial value when adding a new embedded document' do
463
+ person.addresses.build(street: 'Second', map: { 'enabled' => true })
464
+ person.save!
465
+
466
+ expect(person.reload.addresses.last.map).to eq('enabled' => true)
467
+ end
468
+
469
+ it 'reports the initial value as a saved change' do
470
+ address = person.addresses.first
471
+
472
+ expect(address.previous_changes['map']).to eq([ nil, { 'enabled' => false } ])
473
+ expect(address.saved_change_to_attribute?(:map)).to be true
474
+ end
475
+ end
476
+
477
+ context 'when a persisted root document is converted with becomes' do
478
+ let(:attributes) do
479
+ :title
480
+ end
481
+
482
+ let(:model) do
483
+ test_model do
484
+ field :settings, type: Hash
485
+ attr_readonly :settings
486
+ end
487
+ end
488
+
489
+ let(:original) do
490
+ model.create!(settings: { 'enabled' => false })
491
+ end
492
+
493
+ it 'does not persist an in-place change to the readonly field of the became root document' do
494
+ became = original.becomes(model)
495
+ became.settings['enabled'] = true
496
+ became.save!
497
+
498
+ expect(model.collection.find(_id: original.id).first['settings']).to eq('enabled' => false)
499
+ end
500
+ end
501
+
502
+ context 'when a persisted document is converted to another class with becomes' do
503
+ let(:attributes) do
504
+ :title
505
+ end
506
+
507
+ let(:model) do
508
+ test_model do
509
+ field :settings, type: Hash
510
+ attr_readonly :settings
511
+ end
512
+ end
513
+
514
+ let(:subclass) do
515
+ Class.new(model)
516
+ end
517
+
518
+ let(:original) do
519
+ model.create!(settings: { 'enabled' => false })
520
+ end
521
+
522
+ it 'does not persist an in-place change to the readonly field of the became document' do
523
+ became = original.becomes(subclass)
524
+ became.settings['enabled'] = true
525
+ became.save!
526
+
527
+ expect(model.collection.find(_id: original.id).first['settings']).to eq('enabled' => false)
528
+ end
529
+ end
530
+
531
+ context 'when a persisted document with embedded documents is converted with becomes' do
532
+ let(:attributes) do
533
+ :title
534
+ end
535
+
536
+ let(:person) do
537
+ Person.create!(addresses: [ { street: 'Main', map: { 'enabled' => false } } ])
538
+ end
539
+
540
+ around do |example|
541
+ original = Address.readonly_attributes
542
+ Address.attr_readonly :map
543
+ example.run
544
+ ensure
545
+ Address.readonly_attributes = original
546
+ end
547
+
548
+ it 'does not persist an in-place change to the readonly field of a became embedded document' do
549
+ became = person.becomes(Person)
550
+ became.addresses.first.map['enabled'] = true
551
+ became.save!
552
+
553
+ stored = Person.collection.find(_id: person.id).first['addresses'].first['map']
554
+ expect(stored).to eq('enabled' => false)
555
+ end
556
+ end
557
+
558
+ context 'when a new document is converted with becomes' do
559
+ let(:attributes) do
560
+ :title
561
+ end
562
+
563
+ let(:model) do
564
+ test_model do
565
+ field :settings, type: Hash
566
+ attr_readonly :settings
567
+ end
568
+ end
569
+
570
+ it 'reports the readonly value as a saved change when the became document is created' do
571
+ became = model.new(settings: { 'enabled' => false }).becomes(model)
572
+ became.save!
573
+
574
+ expect(became.previous_changes['settings']).to eq([ nil, { 'enabled' => false } ])
575
+ expect(model.collection.find(_id: became.id).first['settings']).to eq('enabled' => false)
576
+ end
577
+ end
578
+
579
+ context 'when a field is named like an internal dirty tracking method' do
580
+ let(:attributes) do
581
+ :title
582
+ end
583
+
584
+ let(:model) do
585
+ test_model do
586
+ field :name, type: String
587
+ field :readonly_change_ignored, type: String
588
+ end
589
+ end
590
+
591
+ it 'tracks changes and creates the document' do
592
+ document = model.new(name: 'original', readonly_change_ignored: 'value')
593
+
594
+ expect(document.name_changed?).to be true
595
+
596
+ document.save!
597
+
598
+ expect(document.reload.name).to eq('original')
599
+ expect(document.readonly_change_ignored).to eq('value')
600
+ expect(document.readonly_change_ignored?).to be true
601
+ expect(document).not_to be_changed
602
+ end
603
+ end
604
+
216
605
  context 'when a foreign_key is defined as readonly' do
217
606
  let(:attributes) do
218
607
  :mother_id
@@ -1410,6 +1410,24 @@ describe Mongoid::Changeable do
1410
1410
  expect(person.title).to be_nil
1411
1411
  end
1412
1412
  end
1413
+
1414
+ context 'when a resizable attribute was read but its value has not changed' do
1415
+ let(:person) do
1416
+ Person.create!(map: { 'enabled' => false })
1417
+ end
1418
+
1419
+ it 'keeps the current value object so held references stay attached' do
1420
+ map = person.map
1421
+ person.reset_map!
1422
+
1423
+ expect(person.attributes['map']).to equal(map)
1424
+
1425
+ map['enabled'] = true
1426
+ person.save!
1427
+
1428
+ expect(person.reload.map).to eq('enabled' => true)
1429
+ end
1430
+ end
1413
1431
  end
1414
1432
 
1415
1433
  describe '#reset_attribute_to_default!' do
@@ -160,6 +160,164 @@ describe Mongoid::Config::Encryption do
160
160
  end
161
161
  end
162
162
 
163
+ # A belongs_to association generates an Object field for its foreign key.
164
+ # When the field was declared with a type of its own, that declared type
165
+ # has to reach the schema: it is what libmongocrypt checks the value
166
+ # against before encrypting it.
167
+ context 'when an encrypted field is a belongs_to foreign key' do
168
+ let(:defined_models) { [] }
169
+
170
+ let(:encryption_options) { { deterministic: true } }
171
+
172
+ let(:models) do
173
+ owner = Class.new do
174
+ include Mongoid::Document
175
+
176
+ store_in collection: 'crypt_encrypted_foreign_key_owners'
177
+ end
178
+ defined_models << owner
179
+ stub_const('Crypt::EncryptedForeignKeyOwner', owner)
180
+
181
+ reference = Class.new do
182
+ include Mongoid::Document
183
+
184
+ store_in collection: 'crypt_encrypted_foreign_key_references'
185
+ end
186
+ defined_models << reference
187
+ stub_const('Crypt::EncryptedForeignKeyReference', reference)
188
+ reference.field(:owner_id, type: BSON::ObjectId, encrypt: encryption_options)
189
+ reference.belongs_to(:owner, class_name: 'Crypt::EncryptedForeignKeyOwner', inverse_of: nil)
190
+
191
+ [ reference ]
192
+ end
193
+
194
+ after do
195
+ defined_models.each { |model| Mongoid.deregister_model(model) }
196
+ end
197
+
198
+ it 'uses the declared type of the foreign key' do
199
+ expect(encryption_schema_map['mongoid_test.crypt_encrypted_foreign_key_references']).to eq(
200
+ 'bsonType' => 'object',
201
+ 'properties' => {
202
+ 'owner_id' => {
203
+ 'encrypt' => {
204
+ 'bsonType' => 'objectId',
205
+ 'algorithm' => 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic'
206
+ }
207
+ }
208
+ }
209
+ )
210
+ end
211
+
212
+ context 'when the field specifies a key alt name' do
213
+ let(:encryption_options) { { deterministic: false, key_name_field: :owner_key_name } }
214
+
215
+ it 'keeps the key alt name of the field' do
216
+ expect(
217
+ encryption_schema_map.dig(
218
+ 'mongoid_test.crypt_encrypted_foreign_key_references',
219
+ 'properties', 'owner_id', 'encrypt'
220
+ )
221
+ ).to eq(
222
+ 'keyId' => '/owner_key_name',
223
+ 'bsonType' => 'objectId',
224
+ 'algorithm' => 'AEAD_AES_256_CBC_HMAC_SHA_512-Random'
225
+ )
226
+ end
227
+ end
228
+ end
229
+
230
+ # A parent and its subclass write to one collection. When each declares the
231
+ # belongs_to foreign key with its own encryption options, the declaration of
232
+ # the subclass must not be replaced by the association declared on the
233
+ # parent: the schema merge reports the disagreement instead.
234
+ context 'when a belongs_to foreign key is encrypted differently on a parent and a subclass' do
235
+ let(:defined_models) { [] }
236
+
237
+ let(:parent_key_id) { 'grolrnFVSSW9Gq04Q87R9Q==' }
238
+
239
+ let(:child_key_id) { 'S34mE/HhSFSym3yErpER6Q==' }
240
+
241
+ let(:parent_options) { { deterministic: true, key_id: parent_key_id } }
242
+
243
+ let(:child_options) { { deterministic: false, key_id: child_key_id } }
244
+
245
+ let(:parent) do
246
+ klass = Class.new do
247
+ include Mongoid::Document
248
+
249
+ store_in collection: 'crypt_inherited_foreign_keys'
250
+ end
251
+ defined_models << klass
252
+ stub_const('Crypt::SchemaInheritedParent', klass)
253
+ klass
254
+ end
255
+
256
+ let(:child) do
257
+ parent
258
+ klass = Class.new(parent)
259
+ defined_models << klass
260
+ stub_const('Crypt::SchemaInheritedChild', klass)
261
+ klass
262
+ end
263
+
264
+ let(:models) do
265
+ [ parent, child ]
266
+ end
267
+
268
+ before do
269
+ parent.field(:owner_id, type: BSON::ObjectId, encrypt: parent_options)
270
+ child.field(:owner_id, type: BSON::ObjectId, encrypt: child_options)
271
+ parent.belongs_to(:owner, class_name: 'Person', inverse_of: nil)
272
+ end
273
+
274
+ after do
275
+ defined_models.each { |model| Mongoid.deregister_model(model) }
276
+ end
277
+
278
+ it 'keeps the options of each field' do
279
+ parent_field = parent.fields['owner_id']
280
+ child_field = child.fields['owner_id']
281
+
282
+ expect(parent_field).to be_a(Mongoid::Fields::EncryptedForeignKey)
283
+ expect(parent_field.type).to eq(BSON::ObjectId)
284
+ expect(parent_field.deterministic?).to be true
285
+ expect(parent_field.key_id).to eq(parent_key_id)
286
+
287
+ expect(child_field).to be_a(Mongoid::Fields::EncryptedForeignKey)
288
+ expect(child_field.type).to eq(BSON::ObjectId)
289
+ expect(child_field.deterministic?).to be false
290
+ expect(child_field.key_id).to eq(child_key_id)
291
+ end
292
+
293
+ it 'raises for the shared collection' do
294
+ expect { encryption_schema_map }.to raise_error(Mongoid::Errors::InvalidEncryptionSchema)
295
+ end
296
+
297
+ context 'when both fields use the same encryption options' do
298
+ let(:child_options) { parent_options }
299
+
300
+ it 'keeps the shared options on both fields' do
301
+ expect(parent.fields['owner_id'].deterministic?).to be true
302
+ expect(parent.fields['owner_id'].key_id).to eq(parent_key_id)
303
+ expect(child.fields['owner_id'].deterministic?).to be true
304
+ expect(child.fields['owner_id'].key_id).to eq(parent_key_id)
305
+ end
306
+
307
+ it 'builds the schema for the shared collection' do
308
+ expect(
309
+ encryption_schema_map.dig('mongoid_test.crypt_inherited_foreign_keys', 'properties', 'owner_id')
310
+ ).to eq(
311
+ 'encrypt' => {
312
+ 'bsonType' => 'objectId',
313
+ 'algorithm' => 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic',
314
+ 'keyId' => [ BSON::Binary.new(Base64.decode64(parent_key_id), :uuid) ]
315
+ }
316
+ )
317
+ end
318
+ end
319
+ end
320
+
163
321
  # A field left out of the schema map is written in plaintext, with no
164
322
  # exception and nothing logged, so an omission here is silent data
165
323
  # exposure rather than a broken feature.
@@ -287,6 +445,75 @@ describe Mongoid::Config::Encryption do
287
445
  end
288
446
  end
289
447
 
448
+ # The document is persisted under the store_as key, so that is the key
449
+ # libmongocrypt has to find. Keyed by the association name, the fields
450
+ # would be written in plaintext.
451
+ context 'when the association uses store_as' do
452
+ let(:token_properties) do
453
+ {
454
+ 'bsonType' => 'object',
455
+ 'properties' => {
456
+ 'token' => {
457
+ 'encrypt' => {
458
+ 'bsonType' => 'string',
459
+ 'algorithm' => 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic'
460
+ }
461
+ }
462
+ }
463
+ }
464
+ end
465
+
466
+ let(:models) do
467
+ [ Crypt::Dossier, Crypt::DossierSecret ]
468
+ end
469
+
470
+ let(:properties) do
471
+ encryption_schema_map.dig('mongoid_test.crypt_dossiers', 'properties')
472
+ end
473
+
474
+ it 'keys the nested schema by the stored name' do
475
+ expect(properties.keys).to eq([ 's' ])
476
+ expect(properties['s']).to eq(token_properties)
477
+ end
478
+
479
+ it 'reports the stored name as the encrypted path' do
480
+ expect(Mongoid.config.encrypted_paths_for(Crypt::Dossier)).to eq([ %w[s token] ])
481
+ end
482
+ end
483
+
484
+ context 'when a nested association uses store_as' do
485
+ let(:token_properties) do
486
+ {
487
+ 'bsonType' => 'object',
488
+ 'properties' => {
489
+ 'token' => {
490
+ 'encrypt' => {
491
+ 'bsonType' => 'string',
492
+ 'algorithm' => 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic'
493
+ }
494
+ }
495
+ }
496
+ }
497
+ end
498
+
499
+ let(:models) do
500
+ [ Crypt::Archive, Crypt::ArchiveEntry, Crypt::ArchivePayload ]
501
+ end
502
+
503
+ it 'keys every level by its stored name' do
504
+ expect(encryption_schema_map.dig('mongoid_test.crypt_archives', 'properties')).to eq(
505
+ 'e' => {
506
+ 'bsonType' => 'object',
507
+ 'properties' => { 'p' => token_properties }
508
+ }
509
+ )
510
+ end
511
+
512
+ it 'reports the stored names as the encrypted path' do
513
+ expect(Mongoid.config.encrypted_paths_for(Crypt::Archive)).to eq([ %w[e p token] ])
514
+ end
515
+ end
516
+
290
517
  # The walk has to keep stopping at a model it is already inside of,
291
518
  # otherwise a self-embedding model recurses forever.
292
519
  context 'when a model embeds itself' do
@@ -51,6 +51,14 @@ describe Mongoid::Contextual::None do
51
51
  end
52
52
  end
53
53
 
54
+ describe '#destroy' do
55
+ it 'returns zero and destroys nothing' do
56
+ expect(context.destroy).to eq(0)
57
+ expect(context.destroy_all).to eq(0)
58
+ expect(Band.where(name: 'Depeche Mode').count).to eq(1)
59
+ end
60
+ end
61
+
54
62
  describe '#pluck' do
55
63
  context 'when plucking one field' do
56
64
  it 'returns an empty array' do
@@ -77,6 +85,16 @@ describe Mongoid::Contextual::None do
77
85
  end
78
86
  end
79
87
 
88
+ describe '#update' do
89
+ it 'returns false without attributes and nil with them, updating nothing' do
90
+ expect(context.update).to be(false)
91
+ expect(context.update(name: 'The Cure')).to be_nil
92
+ expect(context.update_all).to be(false)
93
+ expect(context.update_all(name: 'The Cure')).to be_nil
94
+ expect(Band.where(name: 'Depeche Mode').first.name).to eq('Depeche Mode')
95
+ end
96
+ end
97
+
80
98
  describe '#first' do
81
99
  it 'returns nil' do
82
100
  expect(context.first).to be_nil