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
@@ -0,0 +1,142 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'Encrypted has_and_belongs_to_many foreign keys' do
6
+ let(:model) do
7
+ klass = stub_const('ForeignKeyHabtmMember', Class.new)
8
+ klass.include(Mongoid::Document)
9
+ klass
10
+ end
11
+
12
+ after do
13
+ Mongoid.deregister_model(model)
14
+ end
15
+
16
+ [ true, { deterministic: true }, { deterministic: false } ].each do |encryption|
17
+ context "with encrypt: #{encryption.inspect}" do
18
+ it 'rejects the association when its foreign key is already encrypted' do
19
+ model.field(:secret_group_ids, type: Array, encrypt: encryption)
20
+
21
+ expect do
22
+ model.has_and_belongs_to_many(:groups, foreign_key: :secret_group_ids, inverse_of: nil)
23
+ end.to raise_error(Mongoid::Errors::InvalidEncryptedAssociation, /encrypt/i)
24
+ end
25
+
26
+ it 'rejects encryption when the foreign key already belongs to an association' do
27
+ model.has_and_belongs_to_many(:groups, inverse_of: nil)
28
+
29
+ expect do
30
+ model.field(:group_ids, type: Array, encrypt: encryption)
31
+ end.to raise_error(Mongoid::Errors::InvalidEncryptedAssociation, /encrypt/i)
32
+ end
33
+ end
34
+ end
35
+
36
+ it 'allows an unencrypted foreign key' do
37
+ model.field(:group_ids, type: Array)
38
+
39
+ expect do
40
+ model.has_and_belongs_to_many(:groups, inverse_of: nil)
41
+ end.not_to raise_error
42
+ end
43
+
44
+ context 'when the encrypted field is declared on a parent model' do
45
+ let(:parent_model) do
46
+ klass = stub_const('ForeignKeyHabtmParent', Class.new)
47
+ klass.include(Mongoid::Document)
48
+ klass
49
+ end
50
+
51
+ let(:custom_foreign_key) { nil }
52
+
53
+ let(:foreign_key) { custom_foreign_key || :group_ids }
54
+
55
+ let(:association_options) do
56
+ { inverse_of: nil }.tap do |options|
57
+ options[:foreign_key] = custom_foreign_key if custom_foreign_key
58
+ end
59
+ end
60
+
61
+ let(:child_model) do
62
+ parent_model
63
+ klass = stub_const('ForeignKeyHabtmChild', Class.new(parent_model))
64
+ klass.has_and_belongs_to_many(:groups, association_options)
65
+ klass
66
+ end
67
+
68
+ after do
69
+ Mongoid.deregister_model(child_model)
70
+ Mongoid.deregister_model(parent_model)
71
+ end
72
+
73
+ encryption_options = [ { deterministic: true }, { deterministic: false } ]
74
+ custom_foreign_keys = [ nil, :secret_group_ids ]
75
+ encryption_options.each do |encryption|
76
+ custom_foreign_keys.each do |custom_key|
77
+ key_type = custom_key ? 'a custom' : 'the default'
78
+ context "with #{encryption.inspect} and #{key_type} foreign key" do
79
+ let(:custom_foreign_key) { custom_key }
80
+
81
+ it 'rejects the field before changing either model' do
82
+ association = child_model.relations['groups']
83
+ existing_field = child_model.fields[foreign_key.to_s]
84
+
85
+ expect do
86
+ parent_model.field(foreign_key, type: Array, encrypt: encryption)
87
+ end.to raise_error(Mongoid::Errors::InvalidEncryptedAssociation, /encrypt/i)
88
+
89
+ expect(parent_model.fields).not_to have_key(foreign_key.to_s)
90
+ expect(child_model.fields[foreign_key.to_s]).to equal(existing_field)
91
+ expect(child_model.fields[foreign_key.to_s]).not_to be_encrypted
92
+ expect(child_model.relations['groups']).to equal(association)
93
+ end
94
+ end
95
+ end
96
+ end
97
+
98
+ it 'allows an encrypted field that no descendant uses as an association key' do
99
+ child_model
100
+
101
+ expect do
102
+ parent_model.field(:secret, type: String, encrypt: { deterministic: true })
103
+ end.not_to raise_error
104
+
105
+ expect(parent_model.fields['secret']).to be_encrypted
106
+ expect(child_model.fields['secret']).to be_encrypted
107
+ end
108
+ end
109
+
110
+ context 'when the encrypted field is declared on a child model' do
111
+ let(:parent_model) do
112
+ klass = stub_const('ForeignKeyHabtmParentWithChildField', Class.new)
113
+ klass.include(Mongoid::Document)
114
+ klass
115
+ end
116
+
117
+ let(:child_model) do
118
+ parent_model
119
+ klass = stub_const('ForeignKeyHabtmChildWithField', Class.new(parent_model))
120
+ klass.field(:group_ids, type: Array, encrypt: { deterministic: false })
121
+ klass
122
+ end
123
+
124
+ after do
125
+ Mongoid.deregister_model(child_model)
126
+ Mongoid.deregister_model(parent_model)
127
+ end
128
+
129
+ it 'rejects the association before changing either model' do
130
+ existing_field = child_model.fields['group_ids']
131
+
132
+ expect do
133
+ parent_model.has_and_belongs_to_many(:groups, inverse_of: nil)
134
+ end.to raise_error(Mongoid::Errors::InvalidEncryptedAssociation, /encrypt/i)
135
+
136
+ expect(parent_model.fields).not_to have_key('group_ids')
137
+ expect(parent_model.relations).not_to have_key('groups')
138
+ expect(child_model.fields['group_ids']).to equal(existing_field)
139
+ expect(child_model.fields['group_ids']).to be_encrypted
140
+ end
141
+ end
142
+ end
@@ -86,27 +86,47 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Buildable do
86
86
  end
87
87
  end
88
88
 
89
- context 'when provided an array of hashes' do
90
- let(:object) do
89
+ context 'when provided preloaded documents' do
90
+ let(:attributes) do
91
91
  [
92
92
  { '_id' => BSON::ObjectId.new, 'name' => 'Emails' },
93
93
  { '_id' => BSON::ObjectId.new, 'name' => 'SMS' }
94
94
  ]
95
95
  end
96
96
 
97
+ let(:object) do
98
+ Mongoid::Association::PreloadedDocuments.new(attributes)
99
+ end
100
+
97
101
  let(:preference1) do
98
- Preference.new(object[0])
102
+ Preference.new(attributes[0])
99
103
  end
100
104
 
101
105
  let(:preference2) do
102
- Preference.new(object[1])
106
+ Preference.new(attributes[1])
103
107
  end
104
108
 
105
- it 'returns the documents' do
109
+ it 'returns the preloaded documents' do
106
110
  expect(documents).to eq([ preference1, preference2 ])
107
111
  end
108
112
  end
109
113
 
114
+ context 'when the foreign key is an array of hashes' do
115
+ let(:object) do
116
+ [
117
+ { '_id' => BSON::ObjectId.new, 'name' => 'Emails' },
118
+ { '_id' => BSON::ObjectId.new, 'name' => 'SMS' }
119
+ ]
120
+ end
121
+
122
+ it 'does not instantiate the key as preloaded documents' do
123
+ allow(association).to receive(:query_criteria).and_return(Preference.none)
124
+ expect(Mongoid::Factory).not_to receive(:execute_from_db)
125
+
126
+ documents
127
+ end
128
+ end
129
+
110
130
  context 'when provided an empty array' do
111
131
  let(:object) do
112
132
  []
@@ -3590,4 +3590,52 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
3590
3590
  expect(contract.signature_ids).to eq([ signature.id ])
3591
3591
  end
3592
3592
  end
3593
+
3594
+ # Conditions that share a key with the association selector must be
3595
+ # intersected with it, whatever kind of key the caller used.
3596
+ key_variants = {
3597
+ 'String' => :stringify_keys,
3598
+ 'Symbol' => :symbolize_keys,
3599
+ 'indifferent access' => :with_indifferent_access
3600
+ }.freeze
3601
+
3602
+ %i[ delete_all destroy_all ].each do |method|
3603
+ describe "##{method} with conditions on a key the association selector uses" do
3604
+ key_variants.each do |label, converter|
3605
+ context "when the keys are #{label}" do
3606
+ let(:draft) { DeleteAllTag.create!(state: 'draft') }
3607
+ let(:outside) { DeleteAllTag.create!(state: 'draft') }
3608
+ let(:account) { DeleteAllAccount.create!(tags: [ draft, DeleteAllTag.create!(state: 'open') ]) }
3609
+ let(:proxy) { DeleteAllAccount.find(account.id).tags }
3610
+
3611
+ before { outside }
3612
+
3613
+ context 'with a condition on the primary key' do
3614
+ let(:conditions) { { _id: outside.id }.send(converter) }
3615
+
3616
+ it 'deletes nothing outside the association' do
3617
+ expect(proxy.send(method, conditions)).to eq(0)
3618
+ expect(DeleteAllTag.count).to eq(3)
3619
+ end
3620
+
3621
+ it 'does not modify the conditions' do
3622
+ original = conditions.deep_dup
3623
+ proxy.send(method, conditions)
3624
+ expect(conditions).to eq(original)
3625
+ end
3626
+ end
3627
+
3628
+ context 'with a condition on the primary key inside the association' do
3629
+ let(:conditions) { { _id: draft.id }.send(converter) }
3630
+
3631
+ it 'deletes only the matching document' do
3632
+ expect(proxy.send(method, conditions)).to eq(1)
3633
+ expect(DeleteAllTag.pluck(:_id)).not_to include(draft.id)
3634
+ expect(DeleteAllTag.count).to eq(2)
3635
+ end
3636
+ end
3637
+ end
3638
+ end
3639
+ end
3640
+ end
3593
3641
  end
@@ -102,15 +102,17 @@ describe Mongoid::Association::Referenced::HasMany::Buildable do
102
102
  end
103
103
  end
104
104
 
105
- context 'when provided an array of hashes' do
105
+ context 'when provided preloaded documents' do
106
106
  let(:object) do
107
- [
108
- { '_id' => BSON::ObjectId.new, 'title' => 'Post 1' },
109
- { '_id' => BSON::ObjectId.new, 'title' => 'Post 2' }
110
- ]
107
+ Mongoid::Association::PreloadedDocuments.new(
108
+ [
109
+ { '_id' => BSON::ObjectId.new, 'title' => 'Post 1' },
110
+ { '_id' => BSON::ObjectId.new, 'title' => 'Post 2' }
111
+ ]
112
+ )
111
113
  end
112
114
 
113
- it 'queries and returns the documents' do
115
+ it 'returns the preloaded documents' do
114
116
  for doc in documents
115
117
  expect(doc).to be_a(Post)
116
118
  end
@@ -118,6 +120,22 @@ describe Mongoid::Association::Referenced::HasMany::Buildable do
118
120
  end
119
121
  end
120
122
 
123
+ context 'when the key is an array of hashes' do
124
+ let(:object) do
125
+ [
126
+ { '_id' => BSON::ObjectId.new, 'title' => 'Post 1' },
127
+ { '_id' => BSON::ObjectId.new, 'title' => 'Post 2' }
128
+ ]
129
+ end
130
+
131
+ it 'does not instantiate the key as preloaded documents' do
132
+ allow(association).to receive(:query_criteria).and_return(Post.none)
133
+ expect(Mongoid::Factory).not_to receive(:execute_from_db)
134
+
135
+ documents
136
+ end
137
+ end
138
+
121
139
  context 'when provided a object' do
122
140
  let(:object) do
123
141
  [ Person.new ]
@@ -3537,4 +3537,85 @@ describe Mongoid::Association::Referenced::HasMany::Proxy do
3537
3537
  end
3538
3538
  end
3539
3539
  end
3540
+
3541
+ # Conditions that share a key with the association selector must be
3542
+ # intersected with it, whatever kind of key the caller used.
3543
+ key_variants = {
3544
+ 'String' => :stringify_keys,
3545
+ 'Symbol' => :symbolize_keys,
3546
+ 'indifferent access' => :with_indifferent_access
3547
+ }.freeze
3548
+
3549
+ %i[ delete_all destroy_all ].each do |method|
3550
+ describe "##{method} with conditions on a key the association selector uses" do
3551
+ let(:account) { DeleteAllAccount.create! }
3552
+ let(:other) { DeleteAllAccount.create! }
3553
+
3554
+ key_variants.each do |label, converter|
3555
+ context "when the keys are #{label}" do
3556
+ shared_examples 'an intersected bulk delete' do
3557
+ let(:reloaded) { DeleteAllAccount.find(account.id) }
3558
+ let(:proxy) { reloaded.send(association) }
3559
+ let(:conditions) { raw_conditions.send(converter) }
3560
+ let!(:expected) { DeleteAllAccount.find(account.id).send(association).where(raw_conditions).pluck(:_id) }
3561
+ let!(:everything) { klass.pluck(:_id) }
3562
+
3563
+ it 'deletes the same documents as the where chain' do
3564
+ expect(proxy.send(method, conditions)).to eq(expected.size)
3565
+ expect(klass.pluck(:_id)).to match_array(everything - expected)
3566
+ end
3567
+
3568
+ it 'does not modify the conditions' do
3569
+ original = conditions.deep_dup
3570
+ proxy.send(method, conditions)
3571
+ expect(conditions).to eq(original)
3572
+ end
3573
+ end
3574
+
3575
+ context 'with an association scope' do
3576
+ let(:klass) { DeleteAllItem }
3577
+ let(:association) { :items }
3578
+ let(:raw_conditions) { { state: 'draft' } }
3579
+
3580
+ before do
3581
+ 2.times { DeleteAllItem.create!(account: account, state: 'open') }
3582
+ DeleteAllItem.create!(account: account, state: 'draft')
3583
+ DeleteAllItem.create!(account: other, state: 'draft')
3584
+ DeleteAllItem.create!(account: account, state: 'closed')
3585
+ end
3586
+
3587
+ it_behaves_like 'an intersected bulk delete'
3588
+ end
3589
+
3590
+ context 'with a child default scope' do
3591
+ let(:klass) { DeleteAllScopedItem }
3592
+ let(:association) { :scoped_items }
3593
+ let(:raw_conditions) { { state: 'draft' } }
3594
+
3595
+ before do
3596
+ 2.times { DeleteAllScopedItem.create!(account: account, state: 'open') }
3597
+ DeleteAllScopedItem.create!(account: account, state: 'draft')
3598
+ DeleteAllScopedItem.create!(account: other, state: 'draft')
3599
+ DeleteAllScopedItem.collection.insert_one(account_id: account.id, state: 'closed')
3600
+ end
3601
+
3602
+ it_behaves_like 'an intersected bulk delete'
3603
+ end
3604
+
3605
+ context 'with a condition on the foreign key' do
3606
+ let(:klass) { DeleteAllItem }
3607
+ let(:association) { :items }
3608
+ let(:raw_conditions) { { account_id: other.id } }
3609
+
3610
+ before do
3611
+ 2.times { DeleteAllItem.create!(account: account, state: 'open') }
3612
+ DeleteAllItem.create!(account: other, state: 'open')
3613
+ end
3614
+
3615
+ it_behaves_like 'an intersected bulk delete'
3616
+ end
3617
+ end
3618
+ end
3619
+ end
3620
+ end
3540
3621
  end
@@ -75,17 +75,32 @@ describe Mongoid::Association::Referenced::HasOne::Buildable do
75
75
  end
76
76
  end
77
77
 
78
- context 'when provided an array of hashes' do
78
+ context 'when provided preloaded documents' do
79
79
  let(:object) do
80
- [ { '_id' => BSON::ObjectId.new, 'name' => 'Savings', 'balance' => 500 } ]
80
+ Mongoid::Association::PreloadedDocuments.new(
81
+ [ { '_id' => BSON::ObjectId.new, 'name' => 'Savings', 'balance' => 500 } ]
82
+ )
81
83
  end
82
84
 
83
- it 'returns the document' do
85
+ it 'returns the preloaded document' do
84
86
  expect(document.name).to eq('Savings')
85
87
  expect(document.balance).to eq(500)
86
88
  end
87
89
  end
88
90
 
91
+ context 'when the key is an array of hashes' do
92
+ let(:object) do
93
+ [ { '_id' => BSON::ObjectId.new, 'name' => 'Savings' } ]
94
+ end
95
+
96
+ it 'does not instantiate the key as a preloaded document' do
97
+ allow(association).to receive(:execute_query).and_return(nil)
98
+ expect(Mongoid::Factory).not_to receive(:execute_from_db)
99
+
100
+ document
101
+ end
102
+ end
103
+
89
104
  context 'when provided a object' do
90
105
  let(:object) do
91
106
  Account.new