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
@@ -156,9 +156,92 @@ describe Mongoid::Scopable do
156
156
  end
157
157
  end
158
158
 
159
- context "when parent class has default scope" do
159
+ context 'when the default scope is none' do
160
+ before do
161
+ Band.create!(name: 'Depeche Mode', active: true)
162
+ Band.default_scope -> { Band.none }
163
+ end
164
+
165
+ after do
166
+ Band.default_scoping = nil
167
+ end
168
+
169
+ it 'does not return any documents' do
170
+ expect(Band.count).to eq(0)
171
+ expect(Band.all.to_a).to be_empty
172
+ expect(Band.where(name: 'Depeche Mode').first).to be_nil
173
+ end
174
+
175
+ it 'does not update documents' do
176
+ Band.update_all(active: false)
177
+ expect(Band.unscoped.first.active).to be(true)
178
+ end
179
+
180
+ it 'does not delete documents' do
181
+ expect(Band.delete_all).to eq(0)
182
+ expect(Band.unscoped.count).to eq(1)
183
+ end
184
+
185
+ it 'removes the none from the default scope when unscoping' do
186
+ crit = Band.all
187
+ expect(crit.count).to eq(0)
188
+ expect(crit.unscoped.count).to eq(1)
189
+ expect(Band.all.merge(Band.all).unscoped.count).to eq(1)
190
+ end
191
+
192
+ it 'keeps an explicit none when unscoping' do
193
+ expect(Band.none.unscoped.count).to eq(0)
194
+ expect(Band.all.none.unscoped.count).to eq(0)
195
+ expect(Band.all.merge(Band.none).unscoped.count).to eq(0)
196
+ end
197
+ end
198
+
199
+ context 'when the default scope is conditionally none' do
200
+ before do
201
+ Band.create!(name: 'Depeche Mode', active: true)
202
+ Band.default_scope -> { allow_all ? Band.where(active: true) : Band.none }
203
+ end
160
204
 
161
- let (:selector) do
205
+ after do
206
+ Band.default_scoping = nil
207
+ end
208
+
209
+ context 'when the condition restricts the scope' do
210
+ let(:allow_all) { false }
211
+
212
+ it 'does not return any documents' do
213
+ expect(Band.count).to eq(0)
214
+ end
215
+ end
216
+
217
+ context 'when the condition allows all documents' do
218
+ let(:allow_all) { true }
219
+
220
+ it 'returns the documents' do
221
+ expect(Band.count).to eq(1)
222
+ end
223
+ end
224
+ end
225
+
226
+ context 'when multiple default scopes are combined with none last' do
227
+ before do
228
+ Band.create!(name: 'Depeche Mode', active: true)
229
+ Band.default_scope -> { Band.where(active: true) }
230
+ Band.default_scope -> { Band.none }
231
+ end
232
+
233
+ after do
234
+ Band.default_scoping = nil
235
+ end
236
+
237
+ it 'does not return any documents' do
238
+ expect(Band.count).to eq(0)
239
+ expect(Band.all.to_a).to be_empty
240
+ end
241
+ end
242
+
243
+ context 'when parent class has default scope' do
244
+ let(:selector) do
162
245
  AudibleSound.all.selector
163
246
  end
164
247
 
@@ -927,6 +1010,24 @@ describe Mongoid::Scopable do
927
1010
  expect(Circle.visible.selector).to eq("radius" => 5)
928
1011
  end
929
1012
  end
1013
+
1014
+ context 'when the scope name matches an internal criteria helper' do
1015
+ before do
1016
+ Band.scope(:none_state, -> { where(active: true) })
1017
+ Band.scope(:clear_default_scope_none, -> { where(origin: 'England') })
1018
+ end
1019
+
1020
+ after do
1021
+ remove_scope(Band, :none_state)
1022
+ remove_scope(Band, :clear_default_scope_none)
1023
+ end
1024
+
1025
+ it 'applies both scopes when chained on a criteria' do
1026
+ selector = Band.where(name: 'Depeche Mode').none_state.clear_default_scope_none.selector
1027
+
1028
+ expect(selector).to eq('name' => 'Depeche Mode', 'active' => true, 'origin' => 'England')
1029
+ end
1030
+ end
930
1031
  end
931
1032
 
932
1033
  describe ".scoped" do
@@ -62,7 +62,7 @@ class SearchIndexHelper
62
62
  end
63
63
 
64
64
  # rubocop:disable RSpec/MultipleMemoizedHelpers
65
- describe Mongoid::SearchIndexable do
65
+ describe Mongoid::SearchIndexable do # rubocop:disable RSpec/MultipleDescribes
66
66
  before do
67
67
  skip "#{described_class} requires at Atlas environment (set ATLAS_URI)" if ENV['ATLAS_URI'].nil?
68
68
  end
@@ -145,3 +145,36 @@ describe Mongoid::SearchIndexable do
145
145
  end
146
146
  end
147
147
  # rubocop:enable RSpec/MultipleMemoizedHelpers
148
+
149
+ # A unit test that does not need an Atlas connection.
150
+ describe Mongoid::SearchIndexable, '.wait_for_search_indexes' do
151
+ # Mongo::Collection#search_indexes was added in driver 2.19; the
152
+ # driver-min and driver-oldstable CI jobs pin the driver to 2.18-stable.
153
+ min_driver_version '2.19'
154
+
155
+ context 'when an index is ready' do
156
+ let(:model) do
157
+ Class.new do
158
+ include Mongoid::Document
159
+
160
+ store_in collection: BSON::ObjectId.new.to_s
161
+ search_index :idx, mappings: { dynamic: false }
162
+ end
163
+ end
164
+
165
+ it 'reads the index status inside collection management' do
166
+ managing = nil
167
+ search_indexes = [ { 'name' => 'idx', 'queryable' => true, 'status' => 'READY' } ]
168
+ collection = instance_double(Mongo::Collection)
169
+ allow(collection).to receive(:search_indexes) do
170
+ managing = Mongoid::Threaded.managing_collection?
171
+ search_indexes
172
+ end
173
+ allow(model).to receive(:collection).and_return(collection)
174
+
175
+ model.wait_for_search_indexes([ 'idx' ])
176
+
177
+ expect(managing).to be true
178
+ end
179
+ end
180
+ end
@@ -265,6 +265,19 @@ describe Mongoid::Tasks::Database do
265
265
  described_class.create_search_indexes([searchable_model_spy], wait: false)
266
266
  end
267
267
  end
268
+
269
+ context 'when waiting for the indexes' do
270
+ it 'runs the wait inside collection management' do
271
+ managing = nil
272
+ allow(described_class).to receive(:wait_for_search_indexes) do
273
+ managing = Mongoid::Threaded.managing_collection?
274
+ end
275
+
276
+ described_class.create_search_indexes([ searchable_model_spy ], wait: true)
277
+
278
+ expect(managing).to be true
279
+ end
280
+ end
268
281
  end
269
282
 
270
283
  describe '.remove_search_indexes' do
data/spec/spec_helper.rb CHANGED
@@ -29,6 +29,14 @@ def database_id_alt
29
29
  "mongoid_test_alt"
30
30
  end
31
31
 
32
+ def test_model(name: 'TestModel', &block)
33
+ Class.new.tap do |klass|
34
+ klass.define_singleton_method(:name) { name.to_s }
35
+ klass.include Mongoid::Document
36
+ klass.class_eval(&block) if block_given?
37
+ end
38
+ end
39
+
32
40
  begin
33
41
  require 'mrss/cluster_config'
34
42
  require 'support/client_registry'
@@ -148,6 +148,48 @@ module Crypt
148
148
  embedded_in :account, class_name: 'Crypt::Account'
149
149
  end
150
150
 
151
+ # Embedded associations persisted under a store_as key that differs from
152
+ # the association name. The schema has to be keyed by the stored name.
153
+ class Dossier
154
+ include Mongoid::Document
155
+
156
+ encrypt_with key_id: 'grolrnFVSSW9Gq04Q87R9Q=='
157
+
158
+ embeds_one :secret, class_name: 'Crypt::DossierSecret', store_as: :s
159
+ end
160
+
161
+ class DossierSecret
162
+ include Mongoid::Document
163
+
164
+ field :token, type: String, encrypt: { deterministic: true }
165
+
166
+ embedded_in :dossier, class_name: 'Crypt::Dossier'
167
+ end
168
+
169
+ # The same, one level down: both associations use store_as.
170
+ class Archive
171
+ include Mongoid::Document
172
+
173
+ encrypt_with key_id: 'grolrnFVSSW9Gq04Q87R9Q=='
174
+
175
+ embeds_one :entry, class_name: 'Crypt::ArchiveEntry', store_as: :e
176
+ end
177
+
178
+ class ArchiveEntry
179
+ include Mongoid::Document
180
+
181
+ embedded_in :archive, class_name: 'Crypt::Archive'
182
+ embeds_one :payload, class_name: 'Crypt::ArchivePayload', store_as: :p
183
+ end
184
+
185
+ class ArchivePayload
186
+ include Mongoid::Document
187
+
188
+ field :token, type: String, encrypt: { deterministic: true }
189
+
190
+ embedded_in :entry, class_name: 'Crypt::ArchiveEntry'
191
+ end
192
+
151
193
  # A model that embeds itself. The walk over embedded relations has to stop
152
194
  # here, or generating the map never terminates.
153
195
  class Comment
@@ -204,4 +246,100 @@ module Crypt
204
246
 
205
247
  embeds_one :missing_note, class_name: 'Crypt::MissingNote'
206
248
  end
249
+
250
+ # Two unrelated models stored in one collection, each with its own encrypted
251
+ # field.
252
+ class Badge
253
+ include Mongoid::Document
254
+
255
+ store_in collection: 'crypt_badges'
256
+
257
+ encrypt_with key_id: 'grolrnFVSSW9Gq04Q87R9Q==', deterministic: true
258
+
259
+ field :serial, type: String, encrypt: true
260
+ end
261
+
262
+ class BadgeScan
263
+ include Mongoid::Document
264
+
265
+ store_in collection: 'crypt_badges'
266
+
267
+ encrypt_with key_id: 'grolrnFVSSW9Gq04Q87R9Q==', deterministic: true
268
+
269
+ field :holder, type: String, encrypt: true
270
+ end
271
+
272
+ # Single collection inheritance. Only Member is registered in
273
+ # Mongoid.models; the subclasses are not.
274
+ class Member
275
+ include Mongoid::Document
276
+
277
+ encrypt_with key_id: 'grolrnFVSSW9Gq04Q87R9Q==', deterministic: true
278
+
279
+ field :ssn, type: String, encrypt: true
280
+ end
281
+
282
+ class Employee < Member
283
+ field :salary, type: String, encrypt: true
284
+ end
285
+
286
+ class Contractor < Member
287
+ field :rate, type: String, encrypt: true
288
+ end
289
+
290
+ # Single collection inheritance where only the subclass is encrypted.
291
+ class Listing
292
+ include Mongoid::Document
293
+
294
+ field :title, type: String
295
+ end
296
+
297
+ class PrivateListing < Listing
298
+ encrypt_with key_id: 'grolrnFVSSW9Gq04Q87R9Q==', deterministic: true
299
+
300
+ field :owner_ssn, type: String, encrypt: true
301
+ end
302
+
303
+ # An embeds_one target whose subclass adds an encrypted field.
304
+ class Box
305
+ include Mongoid::Document
306
+
307
+ encrypt_with key_id: 'grolrnFVSSW9Gq04Q87R9Q==', deterministic: true
308
+
309
+ embeds_one :item, class_name: 'Crypt::Item'
310
+ end
311
+
312
+ class Item
313
+ include Mongoid::Document
314
+
315
+ field :label, type: String, encrypt: true
316
+
317
+ embedded_in :box, class_name: 'Crypt::Box'
318
+ end
319
+
320
+ class SealedItem < Item
321
+ field :seal, type: String, encrypt: true
322
+ end
323
+
324
+ # An embeds_one target with no encrypted field, whose subclass has one.
325
+ # Nothing on Locker or Content declares encryption.
326
+ class Locker
327
+ include Mongoid::Document
328
+
329
+ embeds_one :content, class_name: 'Crypt::Content'
330
+ end
331
+
332
+ class Content
333
+ include Mongoid::Document
334
+
335
+ field :label, type: String
336
+
337
+ embedded_in :locker, class_name: 'Crypt::Locker'
338
+ end
339
+
340
+ class SecretContent < Content
341
+ encrypt_with key_id: 'grolrnFVSSW9Gq04Q87R9Q==', deterministic: true
342
+
343
+ field :code, type: String, encrypt: true
344
+ end
207
345
  end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DeleteAllAccount
4
+ include Mongoid::Document
5
+
6
+ has_many :items, class_name: 'DeleteAllItem', foreign_key: :account_id, inverse_of: :account,
7
+ scope: -> { where(:state.in => %w[draft open]) }
8
+ has_many :scoped_items, class_name: 'DeleteAllScopedItem', foreign_key: :account_id, inverse_of: :account
9
+ has_and_belongs_to_many :tags, class_name: 'DeleteAllTag', inverse_of: nil
10
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DeleteAllItem
4
+ include Mongoid::Document
5
+
6
+ field :state, type: String
7
+
8
+ belongs_to :account, class_name: 'DeleteAllAccount', inverse_of: :items
9
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DeleteAllScopedItem
4
+ include Mongoid::Document
5
+
6
+ field :state, type: String
7
+
8
+ belongs_to :account, class_name: 'DeleteAllAccount', inverse_of: :scoped_items
9
+
10
+ default_scope -> { where(:state.in => %w[draft open]) }
11
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DeleteAllTag
4
+ include Mongoid::Document
5
+
6
+ field :state, type: String
7
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ModifierModels
4
+ class Cabinet
5
+ include Mongoid::Document
6
+
7
+ embeds_one :drawer, class_name: 'ModifierModels::Drawer', autobuild: true
8
+ end
9
+
10
+ class Drawer
11
+ include Mongoid::Document
12
+
13
+ field :matcher, type: String
14
+ embedded_in :cabinet, class_name: 'ModifierModels::Cabinet'
15
+ embeds_many :items, class_name: 'ModifierModels::Item'
16
+ end
17
+
18
+ class Item
19
+ include Mongoid::Document
20
+
21
+ field :name, type: String
22
+ embedded_in :drawer, class_name: 'ModifierModels::Drawer'
23
+ embeds_many :widgets, class_name: 'ModifierModels::Widget'
24
+ end
25
+
26
+ class Widget
27
+ include Mongoid::Document
28
+
29
+ field :label, type: String
30
+ embedded_in :item, class_name: 'ModifierModels::Item'
31
+ end
32
+ end