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,334 @@
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
+ min_server_version '5.0'
283
+
284
+ let(:document) { model_class.create!(settings: { 'theme' => 'light' }) }
285
+
286
+ context 'when the dotted field is readonly' do
287
+ let(:model_class) do
288
+ test_model do
289
+ field :settings, type: Hash
290
+ field :'settings.is_admin', type: String, default: 'literal field'
291
+ attr_readonly :'settings.is_admin'
292
+ end
293
+ end
294
+
295
+ it 'allows updating the separate field' do
296
+ document.settings = { 'theme' => 'dark' }
297
+ document.save!
298
+
299
+ expect_stored_value('settings', 'theme' => 'dark')
300
+ expect_stored_value('settings.is_admin', 'literal field')
301
+ end
302
+
303
+ it 'allows updating a nested hash with the same name as the dotted field' do
304
+ document.write_attribute('settings.is_admin.enabled', true)
305
+ document.save!
306
+
307
+ expect_stored_value('settings', 'theme' => 'light', 'is_admin' => { 'enabled' => true })
308
+ expect_stored_value('settings.is_admin', 'literal field')
309
+ end
310
+
311
+ it 'still treats the dotted field as readonly' do
312
+ expect do
313
+ document.update_attribute('settings.is_admin', 'new value')
314
+ end.to raise_error(Mongoid::Errors::ReadonlyAttribute)
315
+ end
316
+ end
317
+
318
+ context 'when the separate field is readonly' do
319
+ let(:model_class) do
320
+ test_model do
321
+ field :settings, type: Hash
322
+ field :'settings.is_admin', type: String, default: 'literal field'
323
+ attr_readonly :settings
324
+ end
325
+ end
326
+
327
+ it 'raises the invalid dotted assignment error for the dotted field' do
328
+ expect do
329
+ document.update_attribute('settings.is_admin', 'new value')
330
+ end.to raise_error(Mongoid::Errors::InvalidDotDollarAssignment)
331
+ end
332
+ end
333
+ end
334
+ end