mongoid 9.0.13 → 9.0.14

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