From 43ab83616333cfeb9fd8ed699206b77f06ad792b Mon Sep 17 00:00:00 2001 From: Dmitry Rybakov Date: Tue, 20 Aug 2024 15:07:32 +0200 Subject: [PATCH] Add prose tests --- .../crypt/explicit_encryption_context.rb | 7 ++- .../range_explicit_encryption_prose_spec.rb | 46 +++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/lib/mongo/crypt/explicit_encryption_context.rb b/lib/mongo/crypt/explicit_encryption_context.rb index 42ddf42e9d..1207073d8e 100644 --- a/lib/mongo/crypt/explicit_encryption_context.rb +++ b/lib/mongo/crypt/explicit_encryption_context.rb @@ -139,7 +139,12 @@ def set_algorithm_opts(options) def convert_range_opts(range_opts) range_opts.dup.tap do |opts| - opts[:sparsity] = BSON::Int64.new(opts[:sparsity]) unless opts[:sparsity].is_a?(BSON::Int64) + if opts[:sparsity] && !opts[:sparsity].is_a?(BSON::Int64) + opts[:sparsity] = BSON::Int64.new(opts[:sparsity]) + end + if opts[:trim_factor] + opts[:trimFactor] = opts.delete(:trim_factor) + end end end end diff --git a/spec/integration/client_side_encryption/range_explicit_encryption_prose_spec.rb b/spec/integration/client_side_encryption/range_explicit_encryption_prose_spec.rb index 49c6e49ce3..d0bdbcce0e 100644 --- a/spec/integration/client_side_encryption/range_explicit_encryption_prose_spec.rb +++ b/spec/integration/client_side_encryption/range_explicit_encryption_prose_spec.rb @@ -533,5 +533,51 @@ include_examples 'common cases' end + + describe 'Range Explicit Encryption applies defaults' do + let(:payload_defaults) do + client_encryption.encrypt( + 123, + key_id: key1_id, + algorithm: 'Range', + contention_factor: 0, + range_opts: { + min: 0, + max: 1000 + } + ) + end + + it 'uses libmongocrypt default' do + payload = client_encryption.encrypt( + 123, + key_id: key1_id, + algorithm: 'Range', + contention_factor: 0, + range_opts: { + min: 0, + max: 1000, + sparsity: 2, + trim_factor: 6 + } + ) + expect(payload.to_s.size).to eq(payload_defaults.to_s.size) + end + + it 'accepts trim_factor 0' do + payload = client_encryption.encrypt( + 123, + key_id: key1_id, + algorithm: 'Range', + contention_factor: 0, + range_opts: { + min: 0, + max: 1000, + trim_factor: 0 + } + ) + expect(payload.to_s.size).to eq(payload_defaults.to_s.size) + end + end end # rubocop:enable RSpec/ExampleLength