mongoid 8.0.13 → 8.1.0

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 (225) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/CHANGELOG.md +3 -3
  4. data/README.md +3 -3
  5. data/Rakefile +17 -66
  6. data/lib/config/locales/en.yml +46 -40
  7. data/lib/mongoid/association/accessors.rb +3 -7
  8. data/lib/mongoid/association/builders.rb +1 -1
  9. data/lib/mongoid/association/depending.rb +4 -8
  10. data/lib/mongoid/association/eager_loadable.rb +0 -3
  11. data/lib/mongoid/association/embedded/batchable.rb +2 -2
  12. data/lib/mongoid/association/embedded/embedded_in/buildable.rb +2 -2
  13. data/lib/mongoid/association/embedded/embedded_in/proxy.rb +2 -1
  14. data/lib/mongoid/association/embedded/embeds_many/buildable.rb +3 -2
  15. data/lib/mongoid/association/embedded/embeds_many/proxy.rb +6 -6
  16. data/lib/mongoid/association/embedded/embeds_one/buildable.rb +1 -1
  17. data/lib/mongoid/association/embedded/embeds_one/proxy.rb +2 -2
  18. data/lib/mongoid/association/macros.rb +0 -6
  19. data/lib/mongoid/association/nested/many.rb +3 -38
  20. data/lib/mongoid/association/nested/nested_buildable.rb +0 -14
  21. data/lib/mongoid/association/nested/one.rb +40 -2
  22. data/lib/mongoid/association/proxy.rb +1 -1
  23. data/lib/mongoid/association/referenced/counter_cache.rb +2 -2
  24. data/lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb +1 -1
  25. data/lib/mongoid/association/referenced/has_many/enumerable.rb +6 -23
  26. data/lib/mongoid/association/referenced/has_many/proxy.rb +4 -103
  27. data/lib/mongoid/association/reflections.rb +2 -2
  28. data/lib/mongoid/atomic.rb +7 -16
  29. data/lib/mongoid/attributes/dynamic.rb +1 -1
  30. data/lib/mongoid/attributes/nested.rb +2 -2
  31. data/lib/mongoid/attributes/processing.rb +5 -29
  32. data/lib/mongoid/attributes/projector.rb +1 -1
  33. data/lib/mongoid/attributes/readonly.rb +1 -1
  34. data/lib/mongoid/attributes.rb +8 -2
  35. data/lib/mongoid/changeable.rb +107 -5
  36. data/lib/mongoid/clients/factory.rb +0 -4
  37. data/lib/mongoid/clients/storage_options.rb +2 -5
  38. data/lib/mongoid/clients/validators/storage.rb +1 -13
  39. data/lib/mongoid/collection_configurable.rb +58 -0
  40. data/lib/mongoid/composable.rb +2 -0
  41. data/lib/mongoid/config/defaults.rb +60 -0
  42. data/lib/mongoid/config/options.rb +0 -3
  43. data/lib/mongoid/config/validators/async_query_executor.rb +24 -0
  44. data/lib/mongoid/config/validators.rb +1 -0
  45. data/lib/mongoid/config.rb +88 -89
  46. data/lib/mongoid/contextual/aggregable/memory.rb +2 -5
  47. data/lib/mongoid/contextual/atomic.rb +1 -1
  48. data/lib/mongoid/contextual/memory.rb +240 -51
  49. data/lib/mongoid/contextual/mongo/documents_loader.rb +177 -0
  50. data/lib/mongoid/contextual/mongo.rb +370 -133
  51. data/lib/mongoid/contextual/none.rb +162 -7
  52. data/lib/mongoid/contextual.rb +12 -0
  53. data/lib/mongoid/criteria/findable.rb +2 -2
  54. data/lib/mongoid/criteria/includable.rb +4 -3
  55. data/lib/mongoid/criteria/queryable/extensions/numeric.rb +1 -15
  56. data/lib/mongoid/criteria/queryable/key.rb +1 -1
  57. data/lib/mongoid/criteria/queryable/mergeable.rb +1 -13
  58. data/lib/mongoid/criteria/queryable/optional.rb +8 -8
  59. data/lib/mongoid/criteria/queryable/selectable.rb +43 -121
  60. data/lib/mongoid/criteria/queryable/selector.rb +1 -1
  61. data/lib/mongoid/criteria/queryable/storable.rb +1 -1
  62. data/lib/mongoid/criteria.rb +6 -5
  63. data/lib/mongoid/deprecable.rb +1 -2
  64. data/lib/mongoid/deprecation.rb +3 -3
  65. data/lib/mongoid/document.rb +1 -8
  66. data/lib/mongoid/errors/create_collection_failure.rb +33 -0
  67. data/lib/mongoid/errors/drop_collection_failure.rb +27 -0
  68. data/lib/mongoid/errors/immutable_attribute.rb +26 -0
  69. data/lib/mongoid/errors/invalid_async_query_executor.rb +25 -0
  70. data/lib/mongoid/errors/invalid_global_executor_concurrency.rb +22 -0
  71. data/lib/mongoid/errors/invalid_storage_parent.rb +2 -0
  72. data/lib/mongoid/errors.rb +4 -2
  73. data/lib/mongoid/extensions/hash.rb +3 -51
  74. data/lib/mongoid/extensions/object.rb +2 -2
  75. data/lib/mongoid/extensions/time.rb +2 -0
  76. data/lib/mongoid/fields/localized.rb +10 -0
  77. data/lib/mongoid/fields/standard.rb +10 -0
  78. data/lib/mongoid/fields.rb +59 -35
  79. data/lib/mongoid/findable.rb +27 -3
  80. data/lib/mongoid/interceptable.rb +6 -116
  81. data/lib/mongoid/matchable.rb +1 -6
  82. data/lib/mongoid/matcher/eq_impl.rb +1 -1
  83. data/lib/mongoid/matcher/eq_impl_with_regexp.rb +5 -3
  84. data/lib/mongoid/matcher/regex.rb +9 -8
  85. data/lib/mongoid/matcher/type.rb +1 -1
  86. data/lib/mongoid/matcher.rb +0 -1
  87. data/lib/mongoid/persistable/creatable.rb +1 -0
  88. data/lib/mongoid/persistable/deletable.rb +1 -1
  89. data/lib/mongoid/persistable/savable.rb +13 -1
  90. data/lib/mongoid/persistable/unsettable.rb +2 -2
  91. data/lib/mongoid/persistable/updatable.rb +51 -1
  92. data/lib/mongoid/persistable/upsertable.rb +20 -1
  93. data/lib/mongoid/persistable.rb +3 -0
  94. data/lib/mongoid/query_cache.rb +5 -1
  95. data/lib/mongoid/railties/database.rake +7 -2
  96. data/lib/mongoid/reloadable.rb +5 -3
  97. data/lib/mongoid/stateful.rb +22 -1
  98. data/lib/mongoid/tasks/database.rake +12 -0
  99. data/lib/mongoid/tasks/database.rb +20 -0
  100. data/lib/mongoid/threaded.rb +0 -3
  101. data/lib/mongoid/timestamps/created.rb +1 -8
  102. data/lib/mongoid/traversable.rb +0 -12
  103. data/lib/mongoid/utils.rb +22 -0
  104. data/lib/mongoid/validatable/associated.rb +17 -98
  105. data/lib/mongoid/validatable/macros.rb +5 -20
  106. data/lib/mongoid/validatable.rb +4 -10
  107. data/lib/mongoid/version.rb +1 -5
  108. data/lib/mongoid/warnings.rb +17 -2
  109. data/lib/mongoid.rb +16 -3
  110. data/spec/integration/app_spec.rb +2 -20
  111. data/spec/integration/associations/embeds_one_spec.rb +5 -25
  112. data/spec/integration/associations/has_and_belongs_to_many_spec.rb +0 -55
  113. data/spec/integration/callbacks_spec.rb +99 -12
  114. data/spec/integration/discriminator_key_spec.rb +4 -5
  115. data/spec/integration/dots_and_dollars_spec.rb +2 -12
  116. data/spec/integration/i18n_fallbacks_spec.rb +3 -2
  117. data/spec/integration/matcher_operator_data/regex.yml +0 -21
  118. data/spec/mongoid/association/eager_spec.rb +2 -24
  119. data/spec/mongoid/association/embedded/embedded_in/proxy_spec.rb +27 -0
  120. data/spec/mongoid/association/embedded/embeds_many/proxy_spec.rb +20 -25
  121. data/spec/mongoid/association/embedded/embeds_many_models.rb +1 -0
  122. data/spec/mongoid/association/embedded/embeds_many_query_spec.rb +0 -4
  123. data/spec/mongoid/association/embedded/embeds_one/proxy_spec.rb +15 -2
  124. data/spec/mongoid/association/referenced/belongs_to_spec.rb +2 -18
  125. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +42 -103
  126. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +9 -195
  127. data/spec/mongoid/association/syncable_spec.rb +1 -1
  128. data/spec/mongoid/attributes/nested_spec.rb +10 -204
  129. data/spec/mongoid/attributes_spec.rb +3 -33
  130. data/spec/mongoid/changeable_spec.rb +299 -24
  131. data/spec/mongoid/clients/factory_spec.rb +0 -30
  132. data/spec/mongoid/clients_spec.rb +122 -13
  133. data/spec/mongoid/collection_configurable_spec.rb +158 -0
  134. data/spec/mongoid/config/defaults_spec.rb +160 -0
  135. data/spec/mongoid/config_spec.rb +154 -27
  136. data/spec/mongoid/contextual/aggregable/memory_spec.rb +0 -91
  137. data/spec/mongoid/contextual/memory_spec.rb +332 -253
  138. data/spec/mongoid/contextual/mongo/documents_loader_spec.rb +187 -0
  139. data/spec/mongoid/contextual/mongo_spec.rb +1009 -125
  140. data/spec/mongoid/contextual/none_spec.rb +49 -2
  141. data/spec/mongoid/copyable_spec.rb +2 -10
  142. data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +4 -10
  143. data/spec/mongoid/criteria/queryable/options_spec.rb +1 -1
  144. data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +419 -2
  145. data/spec/mongoid/criteria/queryable/selectable_spec.rb +1 -1
  146. data/spec/mongoid/criteria/queryable/selectable_where_spec.rb +0 -235
  147. data/spec/mongoid/criteria/queryable/selector_spec.rb +3 -76
  148. data/spec/mongoid/criteria/queryable/storable_spec.rb +0 -72
  149. data/spec/mongoid/criteria_projection_spec.rb +1 -4
  150. data/spec/mongoid/criteria_spec.rb +5 -11
  151. data/spec/mongoid/document_spec.rb +0 -27
  152. data/spec/mongoid/errors/readonly_document_spec.rb +2 -2
  153. data/spec/mongoid/extensions/hash_spec.rb +3 -239
  154. data/spec/mongoid/extensions/time_spec.rb +8 -43
  155. data/spec/mongoid/extensions/time_with_zone_spec.rb +7 -52
  156. data/spec/mongoid/fields/localized_spec.rb +46 -28
  157. data/spec/mongoid/fields_spec.rb +136 -77
  158. data/spec/mongoid/findable_spec.rb +391 -34
  159. data/spec/mongoid/indexable_spec.rb +16 -10
  160. data/spec/mongoid/interceptable_spec.rb +153 -442
  161. data/spec/mongoid/interceptable_spec_models.rb +111 -51
  162. data/spec/mongoid/persistable/deletable_spec.rb +26 -6
  163. data/spec/mongoid/persistable/destroyable_spec.rb +26 -6
  164. data/spec/mongoid/persistable/incrementable_spec.rb +37 -0
  165. data/spec/mongoid/persistable/logical_spec.rb +37 -0
  166. data/spec/mongoid/persistable/poppable_spec.rb +36 -0
  167. data/spec/mongoid/persistable/pullable_spec.rb +72 -0
  168. data/spec/mongoid/persistable/pushable_spec.rb +72 -0
  169. data/spec/mongoid/persistable/renamable_spec.rb +36 -0
  170. data/spec/mongoid/persistable/savable_spec.rb +96 -0
  171. data/spec/mongoid/persistable/settable_spec.rb +37 -0
  172. data/spec/mongoid/persistable/unsettable_spec.rb +36 -0
  173. data/spec/mongoid/persistable/updatable_spec.rb +20 -28
  174. data/spec/mongoid/persistable/upsertable_spec.rb +80 -6
  175. data/spec/mongoid/persistence_context_spec.rb +7 -57
  176. data/spec/mongoid/query_cache_spec.rb +56 -61
  177. data/spec/mongoid/reloadable_spec.rb +24 -28
  178. data/spec/mongoid/scopable_spec.rb +70 -0
  179. data/spec/mongoid/serializable_spec.rb +23 -44
  180. data/spec/mongoid/stateful_spec.rb +122 -8
  181. data/spec/mongoid/tasks/database_rake_spec.rb +74 -0
  182. data/spec/mongoid/tasks/database_spec.rb +127 -0
  183. data/spec/mongoid/timestamps/created_spec.rb +0 -23
  184. data/spec/mongoid/timestamps_spec.rb +9 -11
  185. data/spec/mongoid/touchable_spec.rb +277 -5
  186. data/spec/mongoid/touchable_spec_models.rb +3 -1
  187. data/spec/mongoid/traversable_spec.rb +9 -24
  188. data/spec/mongoid/validatable/associated_spec.rb +34 -27
  189. data/spec/mongoid/validatable/numericality_spec.rb +0 -16
  190. data/spec/mongoid/validatable/uniqueness_spec.rb +2 -3
  191. data/spec/mongoid_spec.rb +36 -10
  192. data/spec/shared/lib/mrss/docker_runner.rb +1 -1
  193. data/spec/shared/lib/mrss/server_version_registry.rb +1 -1
  194. data/spec/shared/lib/mrss/spec_organizer.rb +3 -32
  195. data/spec/shared/share/Dockerfile.erb +85 -15
  196. data/spec/shared/shlib/distro.sh +0 -10
  197. data/spec/shared/shlib/server.sh +27 -34
  198. data/spec/shared/shlib/set_env.sh +68 -9
  199. data/spec/spec_helper.rb +5 -0
  200. data/spec/support/expectations.rb +17 -20
  201. data/spec/support/immutable_ids.rb +118 -0
  202. data/spec/support/macros.rb +47 -15
  203. data/spec/support/models/artist.rb +0 -1
  204. data/spec/support/models/band.rb +1 -0
  205. data/spec/support/models/building.rb +2 -0
  206. data/spec/support/models/name.rb +0 -10
  207. data/spec/support/models/person.rb +0 -1
  208. data/spec/support/models/product.rb +1 -0
  209. data.tar.gz.sig +0 -0
  210. metadata +715 -671
  211. metadata.gz.sig +2 -0
  212. data/lib/mongoid/errors/in_memory_regexp_timeout.rb +0 -26
  213. data/lib/mongoid/field_readable.rb +0 -70
  214. data/lib/mongoid/matcher/regexp_budget.rb +0 -389
  215. data/lib/mongoid/validatable/numericality.rb +0 -19
  216. data/spec/integration/matcher_regexp_timeout_spec.rb +0 -215
  217. data/spec/integration/query_operator_guard_spec.rb +0 -89
  218. data/spec/mongoid/criteria/queryable/extensions/bignum_spec.rb +0 -61
  219. data/spec/mongoid/criteria/queryable/extensions/fixnum_spec.rb +0 -61
  220. data/spec/mongoid/matcher/regexp_budget_spec.rb +0 -570
  221. data/spec/shared/CANDIDATE.md +0 -28
  222. data/spec/shared/lib/mrss/release/candidate.rb +0 -281
  223. data/spec/shared/lib/mrss/release/product_data.rb +0 -144
  224. data/spec/shared/lib/tasks/candidate.rake +0 -64
  225. data/spec/support/models/purse.rb +0 -9
@@ -1,215 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'benchmark'
4
- require 'spec_helper'
5
-
6
- describe 'in-memory regexp time limit' do
7
- # See the comment in spec/mongoid/matcher/regexp_budget_spec.rb: costly to
8
- # match but bounded on every supported Ruby. A pattern relying on nested
9
- # quantifiers would hang before Ruby 3.2, where the examples that have no
10
- # interrupt available could not stop it.
11
- let(:slow_pattern) { "(?:#{(1..300).map { |i| "a#{i}" }.join('|')})Z" }
12
- let(:slow_street) { 'a' * 5_000 }
13
-
14
- let(:person) do
15
- # Address derives its _id from the street, so the streets have to differ or
16
- # the embedded association collapses to a single document.
17
- Person.new(addresses: Array.new(30) { |i| Address.new(street: "#{slow_street}#{i}") })
18
- end
19
-
20
- let(:one_address) { Person.new(addresses: [ Address.new(street: slow_street) ]) }
21
-
22
- # What one match of the fixture costs on the machine running the suite. It
23
- # varies by more than an order of magnitude between implementations - around
24
- # 6ms on MRI, 110ms on JRuby - so the examples below calibrate against a
25
- # measurement instead of hard-coding a threshold that only holds on one.
26
- let(:single_match_cost) do
27
- regexp = Regexp.new(slow_pattern)
28
- 3.times { slow_street =~ regexp }
29
- Benchmark.realtime { slow_street =~ regexp }
30
- end
31
-
32
- # Comfortably more than one match, comfortably less than twenty. The examples
33
- # only need the ordering to hold, so the margin either side is wide.
34
- let(:calibrated_limit) { single_match_cost * 5 }
35
-
36
- context 'when the limit is disabled' do
37
- config_override :in_memory_regexp_time_limit, nil
38
-
39
- it 'evaluates the query without a bound' do
40
- expect(person.addresses.where(street: { '$regex' => slow_pattern }).to_a).to eq([])
41
- end
42
- end
43
-
44
- context 'when a limit is configured' do
45
- config_override :in_memory_regexp_time_limit, nil
46
-
47
- before { Mongoid::Config.in_memory_regexp_time_limit = calibrated_limit }
48
-
49
- it 'leaves ordinary queries alone' do
50
- person = Person.new(addresses: [ Address.new(street: 'Clarkson') ])
51
- expect(person.addresses.where(street: { '$regex' => '\AClark' }).to_a.size).to eq(1)
52
- end
53
-
54
- it 'does not raise for a single document' do
55
- # The counterpart to the example below, and what makes it meaningful: one
56
- # document stays under the limit, so a scan that raises can only have got
57
- # there by accumulating cost across documents.
58
- expect do
59
- one_address.addresses.where(street: { '$regex' => slow_pattern }).to_a
60
- end.not_to raise_error
61
- end
62
-
63
- it 'raises for a scan over many documents under that same limit' do
64
- expect do
65
- person.addresses.where(street: { '$regex' => slow_pattern }).to_a
66
- end.to raise_error(Mongoid::Errors::InMemoryRegexpTimeout)
67
- end
68
-
69
- it 'raises when $in multiplies the cost within a single document' do
70
- conditions = Array.new(20) { BSON::Regexp::Raw.new(slow_pattern) }
71
-
72
- expect do
73
- one_address.addresses.where(street: { '$in' => conditions }).to_a
74
- end.to raise_error(Mongoid::Errors::InMemoryRegexpTimeout)
75
- end
76
-
77
- it 'raises through the public _matches? API' do
78
- # 20 conditions rather than one, because a single match is deliberately
79
- # kept well under the limit by the calibration above.
80
- conditions = Array.new(20) { BSON::Regexp::Raw.new(slow_pattern) }
81
-
82
- expect do
83
- person.addresses.first._matches?('street' => { '$in' => conditions })
84
- end.to raise_error(Mongoid::Errors::InMemoryRegexpTimeout)
85
- end
86
- end
87
-
88
- context 'when the pattern is pathological' do
89
- config_override :in_memory_regexp_time_limit, 0.2
90
-
91
- # The ticket's Case 1, verbatim. It backtracks exponentially and is the
92
- # reason the guard exists, so every entry point that evaluates a selector
93
- # in memory has to bound it. The subject is kept short enough that the
94
- # match still finishes on its own in a few seconds, so an entry point that
95
- # fails to bound it shows up as a failing example rather than a hung run.
96
- let(:evil_pattern) { '^(a+)+\1?$' }
97
- let(:evil_subject) { "#{'a' * 28}X" }
98
-
99
- it 'bounds a scan over an embedded association' do
100
- person = Person.new(addresses: [ Address.new(street: evil_subject) ])
101
-
102
- expect do
103
- person.addresses.where(street: { '$regex' => evil_pattern }).to_a
104
- end.to raise_error(Mongoid::Errors::InMemoryRegexpTimeout)
105
- end
106
-
107
- it 'bounds the public _matches? API' do
108
- expect do
109
- Address.new(street: evil_subject)._matches?('street' => { '$regex' => evil_pattern })
110
- end.to raise_error(Mongoid::Errors::InMemoryRegexpTimeout)
111
- end
112
-
113
- it 'bounds removal from a referenced association' do
114
- owner = Person.create!
115
- owner.posts.create!(title: evil_subject)
116
-
117
- expect do
118
- owner.posts.delete_all(title: { '$regex' => evil_pattern })
119
- end.to raise_error(Mongoid::Errors::InMemoryRegexpTimeout)
120
- end
121
-
122
- it 'deletes nothing when removal is bounded' do
123
- # The scan runs before the delete, so the error means what a caller would
124
- # take it to mean: the documents are still there.
125
- owner = Person.create!
126
- owner.posts.create!(title: evil_subject)
127
-
128
- expect do
129
- owner.posts.delete_all(title: { '$regex' => evil_pattern })
130
- end.to raise_error(Mongoid::Errors::InMemoryRegexpTimeout)
131
-
132
- expect(Post.count).to eq(1)
133
- expect(owner.reload.posts.size).to eq(1)
134
- end
135
- end
136
-
137
- context 'when the association being removed from has not been loaded' do
138
- config_override :in_memory_regexp_time_limit, 0.2
139
-
140
- let!(:owner) do
141
- person = Person.create!
142
- person.posts.create!(title: 'Testing')
143
- Person.find(person._id)
144
- end
145
-
146
- # The query that loads the association, which removal has to run before it
147
- # has anything to match against.
148
- let(:load_query) { owner.posts._target._unloaded }
149
-
150
- # A stall standing in for a slow network. It is longer than the limit, and
151
- # none of it is time spent on regular expressions.
152
- before do
153
- allow(load_query).to receive(:each).and_wrap_original do |original, *args, &block|
154
- sleep(0.3)
155
- original.call(*args, &block)
156
- end
157
- end
158
-
159
- it 'does not load it, so no query can be counted against the limit' do
160
- # On the Timeout path a deadline covering the fetch failed a slow query
161
- # with an error about regular expressions, and the asynchronous exception
162
- # could land inside the driver's socket read, leaving the connection with
163
- # unconsumed bytes. Nothing is fetched now: the scan sees only what the
164
- # association already holds.
165
- stub_const('Mongoid::Matcher::RegexpBudget::PER_REGEXP_TIMEOUT', false)
166
-
167
- expect do
168
- owner.posts.delete_all(title: { '$regex' => '\Azzz' })
169
- end.not_to raise_error
170
-
171
- expect(load_query).not_to have_received(:each)
172
- end
173
-
174
- it 'runs no pattern against documents it never loaded' do
175
- # The counterpart to the referenced-association examples below, where the
176
- # documents are in memory and the pattern is run against every one of
177
- # them. Here there is nothing to run it against, and the server applies
178
- # the same selector for the delete, so nothing is missed by not looking.
179
- expect(Mongoid::Matcher::RegexpBudget).not_to receive(:match?)
180
-
181
- owner.posts.delete_all(title: { '$regex' => '\Azzz' })
182
- end
183
-
184
- it 'still removes the documents that match' do
185
- expect(owner.posts.delete_all(title: { '$regex' => '\ATest' })).to eq(1)
186
- expect(Post.count).to eq(0)
187
- expect(owner.posts.size).to eq(0)
188
- end
189
- end
190
-
191
- context 'when removing documents from a referenced association' do
192
- config_override :in_memory_regexp_time_limit, nil
193
-
194
- let!(:owner) { Person.create! }
195
-
196
- before do
197
- 30.times { owner.posts.create!(title: slow_street) }
198
- Mongoid::Config.in_memory_regexp_time_limit = calibrated_limit
199
- end
200
-
201
- it 'raises rather than scanning every loaded document' do
202
- expect do
203
- owner.posts.delete_all(title: { '$regex' => slow_pattern })
204
- end.to raise_error(Mongoid::Errors::InMemoryRegexpTimeout)
205
- end
206
-
207
- it 'leaves the documents in place' do
208
- expect do
209
- owner.posts.delete_all(title: { '$regex' => slow_pattern })
210
- end.to raise_error(Mongoid::Errors::InMemoryRegexpTimeout)
211
-
212
- expect(Post.count).to eq(30)
213
- end
214
- end
215
- end
@@ -1,89 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe 'query operator injection guard' do
6
- let(:js) { 'this.name == "admin"' }
7
-
8
- let(:nested_function) do
9
- { '$expr' => { '$function' => { 'body' => 'function() { return true; }', 'args' => [], 'lang' => 'js' } } }
10
- end
11
-
12
- let(:band) { Band.create!(name: 'Depeche Mode') }
13
-
14
- context 'with default configuration' do
15
- it 'rejects unsafe operators without any opt-out' do
16
- expect(Mongoid.allow_unsafe_query_operators).to be false
17
- end
18
-
19
- context 'when querying with where' do
20
- it 'rejects a top-level $where' do
21
- expect { Band.where('$where' => js).first }.to raise_error(Mongoid::Errors::InvalidQuery)
22
- end
23
-
24
- it 'rejects a $function nested in $expr' do
25
- expect { Band.where(nested_function).first }.to raise_error(Mongoid::Errors::InvalidQuery)
26
- end
27
-
28
- it 'permits an ordinary query' do
29
- expect(Band.where(name: band.name).first).to eq(band)
30
- end
31
- end
32
-
33
- context 'when querying with find_by' do
34
- it 'rejects a top-level $where' do
35
- expect { Band.find_by('$where' => js) }.to raise_error(Mongoid::Errors::InvalidQuery)
36
- end
37
-
38
- it 'rejects a $function nested in $expr' do
39
- expect { Band.find_by(nested_function) }.to raise_error(Mongoid::Errors::InvalidQuery)
40
- end
41
- end
42
-
43
- context 'when querying with find_or_create_by' do
44
- it 'rejects a top-level $where' do
45
- expect { Band.find_or_create_by('$where' => js) }.to raise_error(Mongoid::Errors::InvalidQuery)
46
- end
47
- end
48
-
49
- context 'when querying with find_or_initialize_by' do
50
- it 'rejects a top-level $where' do
51
- expect { Band.find_or_initialize_by('$where' => js) }.to raise_error(Mongoid::Errors::InvalidQuery)
52
- end
53
- end
54
-
55
- context 'when querying an association with find_or_create_by' do
56
- it 'rejects a top-level $where' do
57
- expect do
58
- band.records.find_or_create_by('$where' => js)
59
- end.to raise_error(Mongoid::Errors::InvalidQuery)
60
- end
61
- end
62
-
63
- context 'when querying with a logical operator' do
64
- it 'rejects a $where smuggled through or' do
65
- expect { Band.or('$where' => js).first }.to raise_error(Mongoid::Errors::InvalidQuery)
66
- end
67
-
68
- it 'rejects a $where smuggled through any_of' do
69
- expect { Band.any_of('$where' => js).first }.to raise_error(Mongoid::Errors::InvalidQuery)
70
- end
71
- end
72
- end
73
-
74
- context 'when allow_unsafe_query_operators is true' do
75
- config_override :allow_unsafe_query_operators, true
76
-
77
- it 'permits a top-level $where' do
78
- expect(Band.where('$where' => "this.name == '#{band.name}'").first).to eq(band)
79
- end
80
-
81
- it 'permits a $function nested in $expr' do
82
- expect { Band.where(nested_function).first }.not_to raise_error
83
- end
84
-
85
- it 'permits a $where smuggled through or' do
86
- expect { Band.or('$where' => js).first }.not_to raise_error
87
- end
88
- end
89
- end
@@ -1,61 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- describe 'Bignum' do
6
- ruby_version_lt "2.4"
7
-
8
- describe ".evolve" do
9
-
10
- context "when provided a string" do
11
-
12
- context "when the string is a number" do
13
-
14
- context "when the string is an bignum" do
15
-
16
- it "returns an bignum" do
17
- expect(described_class.evolve("1")).to eq(1)
18
- end
19
- end
20
-
21
- context "when the string is a float" do
22
-
23
- it "converts it to a float" do
24
- expect(described_class.evolve("2.23")).to eq(2.23)
25
- end
26
- end
27
- end
28
-
29
- context "when the string is not a number" do
30
-
31
- it "returns the string" do
32
- expect(described_class.evolve("testing")).to eq("testing")
33
- end
34
- end
35
- end
36
- end
37
-
38
- context "when provided a number" do
39
-
40
- context "when the number is an bignum" do
41
-
42
- it "returns an bignum" do
43
- expect(described_class.evolve(1)).to eq(1)
44
- end
45
- end
46
-
47
- context "when the number is a float" do
48
-
49
- it "returns the float" do
50
- expect(described_class.evolve(2.23)).to eq(2.23)
51
- end
52
- end
53
- end
54
-
55
- context "when provided nil" do
56
-
57
- it "returns nil" do
58
- expect(described_class.evolve(nil)).to be_nil
59
- end
60
- end
61
- end
@@ -1,61 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- describe 'Fixnum' do
6
- ruby_version_lt "2.4"
7
-
8
- describe ".evolve" do
9
-
10
- context "when provided a string" do
11
-
12
- context "when the string is a number" do
13
-
14
- context "when the string is an fixnum" do
15
-
16
- it "returns an fixnum" do
17
- expect(described_class.evolve("1")).to eq(1)
18
- end
19
- end
20
-
21
- context "when the string is a float" do
22
-
23
- it "converts it to a float" do
24
- expect(described_class.evolve("2.23")).to eq(2.23)
25
- end
26
- end
27
- end
28
-
29
- context "when the string is not a number" do
30
-
31
- it "returns the string" do
32
- expect(described_class.evolve("testing")).to eq("testing")
33
- end
34
- end
35
- end
36
- end
37
-
38
- context "when provided a number" do
39
-
40
- context "when the number is an fixnum" do
41
-
42
- it "returns an fixnum" do
43
- expect(described_class.evolve(1)).to eq(1)
44
- end
45
- end
46
-
47
- context "when the number is a float" do
48
-
49
- it "returns the float" do
50
- expect(described_class.evolve(2.23)).to eq(2.23)
51
- end
52
- end
53
- end
54
-
55
- context "when provided nil" do
56
-
57
- it "returns nil" do
58
- expect(described_class.evolve(nil)).to be_nil
59
- end
60
- end
61
- end