mongoid 9.0.12 → 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 (58) hide show
  1. checksums.yaml +4 -4
  2. data/lib/config/locales/en.yml +44 -11
  3. data/lib/mongoid/association/embedded/batchable.rb +8 -0
  4. data/lib/mongoid/association/referenced/belongs_to.rb +16 -8
  5. data/lib/mongoid/association/referenced/has_and_belongs_to_many.rb +12 -0
  6. data/lib/mongoid/association/referenced/has_many/proxy.rb +4 -2
  7. data/lib/mongoid/attributes/readonly.rb +19 -1
  8. data/lib/mongoid/changeable.rb +21 -1
  9. data/lib/mongoid/clients/factory.rb +2 -0
  10. data/lib/mongoid/config/encryption.rb +152 -19
  11. data/lib/mongoid/contextual/none.rb +51 -0
  12. data/lib/mongoid/criteria/scopable.rb +7 -1
  13. data/lib/mongoid/criteria.rb +45 -1
  14. data/lib/mongoid/document.rb +10 -0
  15. data/lib/mongoid/encryptable.rb +43 -16
  16. data/lib/mongoid/errors/invalid_encrypted_association.rb +28 -0
  17. data/lib/mongoid/errors/invalid_encryption_schema.rb +27 -0
  18. data/lib/mongoid/errors/no_encryption_schema.rb +5 -3
  19. data/lib/mongoid/errors.rb +2 -0
  20. data/lib/mongoid/fields/encrypted.rb +14 -3
  21. data/lib/mongoid/fields/foreign_key.rb +9 -0
  22. data/lib/mongoid/fields/standard.rb +10 -0
  23. data/lib/mongoid/fields/validators/macro.rb +29 -0
  24. data/lib/mongoid/fields.rb +42 -2
  25. data/lib/mongoid/persistence_context.rb +46 -4
  26. data/lib/mongoid/reloadable.rb +1 -0
  27. data/lib/mongoid/search_indexable.rb +5 -1
  28. data/lib/mongoid/tasks/database.rb +10 -7
  29. data/lib/mongoid/threaded.rb +1 -1
  30. data/lib/mongoid/version.rb +1 -1
  31. data/lib/mongoid/warnings.rb +4 -0
  32. data/spec/integration/atomic/modifiers_spec.rb +57 -0
  33. data/spec/integration/encryption_spec.rb +418 -0
  34. data/spec/mongoid/association/referenced/encrypted_foreign_key_spec.rb +144 -0
  35. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +48 -0
  36. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +83 -0
  37. data/spec/mongoid/attributes/readonly_dotted_spec.rb +332 -0
  38. data/spec/mongoid/attributes/readonly_spec.rb +389 -0
  39. data/spec/mongoid/changeable_spec.rb +18 -0
  40. data/spec/mongoid/clients/factory_spec.rb +36 -0
  41. data/spec/mongoid/config/encryption_spec.rb +450 -0
  42. data/spec/mongoid/contextual/none_spec.rb +22 -5
  43. data/spec/mongoid/criteria_spec.rb +17 -0
  44. data/spec/mongoid/encryptable_spec.rb +54 -0
  45. data/spec/mongoid/fields_spec.rb +29 -0
  46. data/spec/mongoid/findable_spec.rb +14 -0
  47. data/spec/mongoid/matcher/regexp_budget_spec.rb +6 -0
  48. data/spec/mongoid/scopable_spec.rb +103 -2
  49. data/spec/mongoid/search_indexable_spec.rb +34 -1
  50. data/spec/mongoid/tasks/database_spec.rb +13 -0
  51. data/spec/spec_helper.rb +8 -0
  52. data/spec/support/crypt/models.rb +138 -0
  53. data/spec/support/models/delete_all_account.rb +10 -0
  54. data/spec/support/models/delete_all_item.rb +9 -0
  55. data/spec/support/models/delete_all_scoped_item.rb +11 -0
  56. data/spec/support/models/delete_all_tag.rb +7 -0
  57. data/spec/support/models/modifier_models.rb +32 -0
  58. metadata +1995 -1958
@@ -3375,4 +3375,87 @@ describe Mongoid::Association::Referenced::HasMany::Proxy do
3375
3375
  expect(agent.basic_ids).to eq [ basic.id ]
3376
3376
  end
3377
3377
  end
3378
+
3379
+ # Conditions that share a key with the association selector must be
3380
+ # intersected with it, whatever kind of key the caller used.
3381
+ key_variants = {
3382
+ 'String' => :stringify_keys,
3383
+ 'Symbol' => :symbolize_keys,
3384
+ 'indifferent access' => :with_indifferent_access
3385
+ }.freeze
3386
+
3387
+ %i[ delete_all destroy_all ].each do |method|
3388
+ describe "##{method} with conditions on a key the association selector uses" do
3389
+ let(:account) { DeleteAllAccount.create! }
3390
+ let(:other) { DeleteAllAccount.create! }
3391
+
3392
+ key_variants.each do |label, converter|
3393
+ context "when the keys are #{label}" do
3394
+ # rubocop:disable RSpec/MultipleMemoizedHelpers
3395
+ shared_examples 'an intersected bulk delete' do
3396
+ let(:reloaded) { DeleteAllAccount.find(account.id) }
3397
+ let(:proxy) { reloaded.send(association) }
3398
+ let(:conditions) { raw_conditions.send(converter) }
3399
+ let!(:expected) { DeleteAllAccount.find(account.id).send(association).where(raw_conditions).pluck(:_id) }
3400
+ let!(:everything) { klass.pluck(:_id) }
3401
+
3402
+ it 'deletes the same documents as the where chain' do
3403
+ expect(proxy.send(method, conditions)).to eq(expected.size)
3404
+ expect(klass.pluck(:_id)).to match_array(everything - expected)
3405
+ end
3406
+
3407
+ it 'does not modify the conditions' do
3408
+ original = conditions.deep_dup
3409
+ proxy.send(method, conditions)
3410
+ expect(conditions).to eq(original)
3411
+ end
3412
+ end
3413
+ # rubocop:enable RSpec/MultipleMemoizedHelpers
3414
+
3415
+ context 'with an association scope' do
3416
+ let(:klass) { DeleteAllItem }
3417
+ let(:association) { :items }
3418
+ let(:raw_conditions) { { state: 'draft' } }
3419
+
3420
+ before do
3421
+ 2.times { DeleteAllItem.create!(account: account, state: 'open') }
3422
+ DeleteAllItem.create!(account: account, state: 'draft')
3423
+ DeleteAllItem.create!(account: other, state: 'draft')
3424
+ DeleteAllItem.create!(account: account, state: 'closed')
3425
+ end
3426
+
3427
+ it_behaves_like 'an intersected bulk delete'
3428
+ end
3429
+
3430
+ context 'with a child default scope' do
3431
+ let(:klass) { DeleteAllScopedItem }
3432
+ let(:association) { :scoped_items }
3433
+ let(:raw_conditions) { { state: 'draft' } }
3434
+
3435
+ before do
3436
+ 2.times { DeleteAllScopedItem.create!(account: account, state: 'open') }
3437
+ DeleteAllScopedItem.create!(account: account, state: 'draft')
3438
+ DeleteAllScopedItem.create!(account: other, state: 'draft')
3439
+ DeleteAllScopedItem.collection.insert_one(account_id: account.id, state: 'closed')
3440
+ end
3441
+
3442
+ it_behaves_like 'an intersected bulk delete'
3443
+ end
3444
+
3445
+ context 'with a condition on the foreign key' do
3446
+ let(:klass) { DeleteAllItem }
3447
+ let(:association) { :items }
3448
+ let(:raw_conditions) { { account_id: other.id } }
3449
+
3450
+ before do
3451
+ 2.times { DeleteAllItem.create!(account: account, state: 'open') }
3452
+ DeleteAllItem.create!(account: other, state: 'open')
3453
+ end
3454
+
3455
+ it_behaves_like 'an intersected bulk delete'
3456
+ end
3457
+ end
3458
+ end
3459
+ end
3460
+ end
3378
3461
  end
@@ -0,0 +1,332 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'Readonly dotted attributes' do
6
+ let(:model_class) do
7
+ test_model do
8
+ field :stored_settings, as: :settings, type: Hash
9
+ field :other_settings, type: Hash
10
+ attr_readonly :settings
11
+ end
12
+ end
13
+
14
+ let(:document) do
15
+ model_class.create!(settings: { 'is_admin' => false }, other_settings: { 'enabled' => false })
16
+ end
17
+
18
+ def expect_stored_value(field, value)
19
+ expect(model_class.collection.find(_id: document.id).first[field]).to eq(value)
20
+ end
21
+
22
+ shared_examples 'a readonly dotted attribute' do |path, field, original_value|
23
+ it 'ignores write_attribute' do
24
+ document.write_attribute(path, true)
25
+ document.save!
26
+
27
+ expect_stored_value(field, original_value)
28
+ end
29
+
30
+ it 'ignores the bracket setter' do
31
+ document[path.to_sym] = true
32
+ document.save!
33
+
34
+ expect_stored_value(field, original_value)
35
+ end
36
+
37
+ it 'raises ReadonlyAttribute from update_attribute' do
38
+ expect do
39
+ document.update_attribute(path, true)
40
+ end.to raise_error(Mongoid::Errors::ReadonlyAttribute)
41
+
42
+ expect_stored_value(field, original_value)
43
+ end
44
+ end
45
+
46
+ context 'without dynamic attributes' do
47
+ context 'with the database field name' do
48
+ include_examples 'a readonly dotted attribute',
49
+ 'stored_settings.is_admin', 'stored_settings', { 'is_admin' => false }
50
+ end
51
+
52
+ context 'with the field alias' do
53
+ include_examples 'a readonly dotted attribute',
54
+ 'settings.is_admin', 'stored_settings', { 'is_admin' => false }
55
+ end
56
+
57
+ context 'with a new key in the readonly field' do
58
+ include_examples 'a readonly dotted attribute',
59
+ 'settings.new_key', 'stored_settings', { 'is_admin' => false }
60
+ end
61
+
62
+ context 'with a deeply nested key' do
63
+ let(:document) do
64
+ model_class.create!(settings: { 'permissions' => { 'is_admin' => false } })
65
+ end
66
+
67
+ include_examples 'a readonly dotted attribute',
68
+ 'settings.permissions.is_admin', 'stored_settings', { 'permissions' => { 'is_admin' => false } }
69
+ end
70
+
71
+ it 'allows the readonly field to be initialized on a new document' do
72
+ expect_stored_value('stored_settings', 'is_admin' => false)
73
+ end
74
+
75
+ it 'allows dotted writes to an unprotected field' do
76
+ document.write_attribute('other_settings.enabled', true)
77
+ document.save!
78
+
79
+ expect_stored_value('other_settings', 'enabled' => true)
80
+ end
81
+
82
+ it 'continues to protect an exact dotted readonly name' do
83
+ model_class.attr_readonly('other_settings.enabled')
84
+ document.write_attribute('other_settings.enabled', true)
85
+ document.save!
86
+
87
+ expect_stored_value('other_settings', 'enabled' => false)
88
+ end
89
+
90
+ it 'rejects an unknown dotted field during mass assignment' do
91
+ expect do
92
+ document.update('stored_settings.is_admin' => true)
93
+ end.to raise_error(Mongoid::Errors::UnknownAttribute)
94
+
95
+ expect_stored_value('stored_settings', 'is_admin' => false)
96
+ end
97
+ end
98
+
99
+ context 'with dynamic attributes' do
100
+ let(:model_class) do
101
+ test_model do
102
+ include Mongoid::Attributes::Dynamic
103
+
104
+ field :stored_settings, as: :settings, type: Hash
105
+ field :other_settings, type: Hash
106
+ attr_readonly :settings
107
+ end
108
+ end
109
+
110
+ context 'with the database field name' do
111
+ include_examples 'a readonly dotted attribute',
112
+ 'stored_settings.is_admin', 'stored_settings', { 'is_admin' => false }
113
+ end
114
+
115
+ context 'with the field alias' do
116
+ include_examples 'a readonly dotted attribute',
117
+ 'settings.is_admin', 'stored_settings', { 'is_admin' => false }
118
+ end
119
+
120
+ context 'with a new key in the readonly field' do
121
+ include_examples 'a readonly dotted attribute',
122
+ 'settings.new_key', 'stored_settings', { 'is_admin' => false }
123
+
124
+ it 'ignores mass assignment' do
125
+ document.update('settings.new_key' => true)
126
+
127
+ expect_stored_value('stored_settings', 'is_admin' => false)
128
+ end
129
+ end
130
+
131
+ context 'with a deeply nested key' do
132
+ let(:document) do
133
+ model_class.create!(settings: { 'permissions' => { 'is_admin' => false } })
134
+ end
135
+
136
+ include_examples 'a readonly dotted attribute',
137
+ 'settings.permissions.is_admin', 'stored_settings', { 'permissions' => { 'is_admin' => false } }
138
+
139
+ it 'ignores mass assignment' do
140
+ document.update('settings.permissions.is_admin' => true)
141
+
142
+ expect_stored_value('stored_settings', 'permissions' => { 'is_admin' => false })
143
+ end
144
+ end
145
+
146
+ it 'ignores mass assignment using the database field name' do
147
+ document.update('stored_settings.is_admin' => true)
148
+
149
+ expect_stored_value('stored_settings', 'is_admin' => false)
150
+ end
151
+
152
+ it 'ignores mass assignment using the field alias' do
153
+ document.update('settings.is_admin' => true)
154
+
155
+ expect_stored_value('stored_settings', 'is_admin' => false)
156
+ end
157
+
158
+ it 'does not persist the dotted field through update_attribute' do
159
+ begin
160
+ document.update_attribute('settings.is_admin', true)
161
+ rescue Mongoid::Errors::ReadonlyAttribute
162
+ # update_attribute is expected to raise for readonly attributes.
163
+ end
164
+
165
+ expect_stored_value('stored_settings', 'is_admin' => false)
166
+ end
167
+ end
168
+
169
+ context 'when a nested path is readonly' do
170
+ let(:model_class) do
171
+ test_model do
172
+ field :stored_settings, as: :settings, type: Hash
173
+ attr_readonly 'settings.permissions.protected'
174
+ end
175
+ end
176
+
177
+ let(:document) do
178
+ model_class.create!(settings: { 'permissions' => { 'protected' => { 'is_admin' => false } } })
179
+ end
180
+
181
+ include_examples 'a readonly dotted attribute',
182
+ 'settings.permissions.protected.is_admin', 'stored_settings',
183
+ { 'permissions' => { 'protected' => { 'is_admin' => false } } }
184
+
185
+ include_examples 'a readonly dotted attribute',
186
+ 'stored_settings.permissions.protected.is_admin', 'stored_settings',
187
+ { 'permissions' => { 'protected' => { 'is_admin' => false } } }
188
+
189
+ it 'allows writes to the immediate parent' do
190
+ document.write_attribute('settings.permissions', 'protected' => { 'is_admin' => true })
191
+ document.save!
192
+
193
+ expect_stored_value('stored_settings', 'permissions' => { 'protected' => { 'is_admin' => true } })
194
+ end
195
+
196
+ it 'allows the bracket setter for the top-level parent' do
197
+ document[:settings] = { 'permissions' => { 'protected' => { 'is_admin' => true } } }
198
+ document.save!
199
+
200
+ expect_stored_value('stored_settings', 'permissions' => { 'protected' => { 'is_admin' => true } })
201
+ end
202
+
203
+ it 'allows mass assignment to the top-level parent' do
204
+ document.update(settings: { 'permissions' => { 'protected' => { 'is_admin' => true } } })
205
+
206
+ expect_stored_value('stored_settings', 'permissions' => { 'protected' => { 'is_admin' => true } })
207
+ end
208
+
209
+ it 'allows update_attribute on the top-level parent' do
210
+ document.update_attribute('settings', 'permissions' => { 'protected' => { 'is_admin' => true } })
211
+
212
+ expect_stored_value('stored_settings', 'permissions' => { 'protected' => { 'is_admin' => true } })
213
+ end
214
+
215
+ it 'allows removing the top-level parent' do
216
+ document.remove_attribute('settings')
217
+ document.save!
218
+
219
+ expect_stored_value('stored_settings', nil)
220
+ end
221
+
222
+ it 'allows replacing the immediate parent with a scalar' do
223
+ document.write_attribute('settings.permissions', 'none')
224
+ document.save!
225
+
226
+ expect_stored_value('stored_settings', 'permissions' => 'none')
227
+ end
228
+
229
+ it 'allows an in-place change to the parent hash' do
230
+ document.settings['permissions']['protected']['is_admin'] = true
231
+ document.save!
232
+
233
+ expect_stored_value('stored_settings', 'permissions' => { 'protected' => { 'is_admin' => true } })
234
+ end
235
+
236
+ it 'allows replacing a parent inside the hash' do
237
+ document.settings['permissions'] = { 'protected' => { 'is_admin' => true } }
238
+ document.save!
239
+
240
+ expect_stored_value('stored_settings', 'permissions' => { 'protected' => { 'is_admin' => true } })
241
+ end
242
+
243
+ it 'allows writes to a sibling with a similar name' do
244
+ document.write_attribute('settings.permissions.protected_extra', true)
245
+ document.save!
246
+
247
+ expect_stored_value(
248
+ 'stored_settings',
249
+ 'permissions' => { 'protected' => { 'is_admin' => false }, 'protected_extra' => true }
250
+ )
251
+ end
252
+
253
+ context 'with dynamic attributes' do
254
+ let(:model_class) do
255
+ test_model do
256
+ include Mongoid::Attributes::Dynamic
257
+
258
+ field :stored_settings, as: :settings, type: Hash
259
+ attr_readonly 'settings.permissions.protected'
260
+ end
261
+ end
262
+
263
+ include_examples 'a readonly dotted attribute',
264
+ 'settings.permissions.protected.is_admin', 'stored_settings',
265
+ { 'permissions' => { 'protected' => { 'is_admin' => false } } }
266
+
267
+ it 'ignores mass assignment to a descendant' do
268
+ document.update('settings.permissions.protected.is_admin' => true)
269
+
270
+ expect_stored_value('stored_settings', 'permissions' => { 'protected' => { 'is_admin' => false } })
271
+ end
272
+
273
+ it 'allows mass assignment to a parent' do
274
+ document.update('settings.permissions' => { 'protected' => { 'is_admin' => true } })
275
+
276
+ expect_stored_value('stored_settings', 'permissions' => { 'protected' => { 'is_admin' => true } })
277
+ end
278
+ end
279
+ end
280
+
281
+ context 'with a separate field whose name contains a dot' do
282
+ let(:document) { model_class.create!(settings: { 'theme' => 'light' }) }
283
+
284
+ context 'when the dotted field is readonly' do
285
+ let(:model_class) do
286
+ test_model do
287
+ field :settings, type: Hash
288
+ field :'settings.is_admin', type: String, default: 'literal field'
289
+ attr_readonly :'settings.is_admin'
290
+ end
291
+ end
292
+
293
+ it 'allows updating the separate field' do
294
+ document.settings = { 'theme' => 'dark' }
295
+ document.save!
296
+
297
+ expect_stored_value('settings', 'theme' => 'dark')
298
+ expect_stored_value('settings.is_admin', 'literal field')
299
+ end
300
+
301
+ it 'allows updating a nested hash with the same name as the dotted field' do
302
+ document.write_attribute('settings.is_admin.enabled', true)
303
+ document.save!
304
+
305
+ expect_stored_value('settings', 'theme' => 'light', 'is_admin' => { 'enabled' => true })
306
+ expect_stored_value('settings.is_admin', 'literal field')
307
+ end
308
+
309
+ it 'still treats the dotted field as readonly' do
310
+ expect do
311
+ document.update_attribute('settings.is_admin', 'new value')
312
+ end.to raise_error(Mongoid::Errors::ReadonlyAttribute)
313
+ end
314
+ end
315
+
316
+ context 'when the separate field is readonly' do
317
+ let(:model_class) do
318
+ test_model do
319
+ field :settings, type: Hash
320
+ field :'settings.is_admin', type: String, default: 'literal field'
321
+ attr_readonly :settings
322
+ end
323
+ end
324
+
325
+ it 'raises the invalid dotted assignment error for the dotted field' do
326
+ expect do
327
+ document.update_attribute('settings.is_admin', 'new value')
328
+ end.to raise_error(Mongoid::Errors::InvalidDotDollarAssignment)
329
+ end
330
+ end
331
+ end
332
+ end