Skip to content

Commit ec242d4

Browse files
committed
Get specs passing and change name to Options::Redacted
1 parent 75a58af commit ec242d4

File tree

7 files changed

+65
-32
lines changed

7 files changed

+65
-32
lines changed

lib/mongo/client.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def use(name)
217217
# @since 2.0.0
218218
def with(new_options = {})
219219
clone.tap do |client|
220-
opts = new_options || {}
220+
opts = Options::Redacted.new(new_options) || Options::Redacted.new
221221
client.options.update(opts)
222222
Database.create(client)
223223
# We can't use the same cluster if some options that would affect it
@@ -292,14 +292,14 @@ def list_databases
292292
private
293293

294294
def create_from_addresses(addresses, opts = {})
295-
@options = Options::SensitiveOptions.new.merge(Database::DEFAULT_OPTIONS.merge(opts)).freeze
295+
@options = Options::Redacted.new.merge(Database::DEFAULT_OPTIONS.merge(opts)).freeze
296296
@cluster = Cluster.new(addresses, @monitoring, options)
297297
@database = Database.new(self, options[:database], options)
298298
end
299299

300300
def create_from_uri(connection_string, opts = {})
301301
uri = URI.new(connection_string, opts)
302-
@options = Options::SensitiveOptions.new.merge(Database::DEFAULT_OPTIONS.merge(uri.client_options.merge(opts))).freeze
302+
@options = Options::Redacted.new.merge(Database::DEFAULT_OPTIONS.merge(uri.client_options.merge(opts))).freeze
303303
@cluster = Cluster.new(uri.servers, @monitoring, options)
304304
@database = Database.new(self, options[:database], options)
305305
end
@@ -314,7 +314,7 @@ def initialize_copy(original)
314314

315315
def cluster_modifying?(new_options)
316316
cluster_options = new_options.reject do |name|
317-
CRUD_OPTIONS.include?(name)
317+
CRUD_OPTIONS.include?(name.to_sym)
318318
end
319319
cluster_options.any? do |name, value|
320320
options[name] != value

lib/mongo/database.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,12 @@ def collections
148148
#
149149
# @return [ Hash ] The result of the command execution.
150150
def command(operation, opts = {})
151-
preference = opts[:read] ? ServerSelector.get(opts[:read].merge(options)) : read_preference
151+
if opts[:read]
152+
preference = ServerSelector.get(Options::Redacted.new(
153+
opts[:read]).merge(options))
154+
else
155+
preference = read_preference
156+
end
152157
server = preference.select_server(cluster)
153158
Operation::Command.new({
154159
:selector => operation,

lib/mongo/options.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313
# limitations under the License.
1414

1515
require 'mongo/options/mapper'
16-
require 'mongo/options/sensitive_options'
16+
require 'mongo/options/redacted'

lib/mongo/options/sensitive_options.rb renamed to lib/mongo/options/redacted.rb

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,31 @@
1515
module Mongo
1616
module Options
1717

18-
SENSITIVE_OPTIONS = [:password, :pwd]
18+
REDACTED_OPTIONS = [:password, :pwd]
1919
REDACTED_STRING = '<REDACTED>'
2020

21-
class SensitiveOptions < BSON::Document
21+
class Redacted < BSON::Document
2222

2323
def inspect
24-
'{' + reduce('') do |string, (k, v)|
25-
string << "#{k.inspect}=>#{redact(k, v, __method__)}"
26-
end + '}'
24+
'{' + reduce([]) do |list, (k, v)|
25+
list << "#{k.inspect}=>#{redact(k, v, __method__)}"
26+
end.join(', ') + '}'
2727
end
2828

2929
def to_s
30-
'{' + reduce('') do |string, (k, v)|
31-
string << "#{k.to_s}=>#{redact(k, v, __method__)}"
32-
end + '}'
30+
'{' + reduce([]) do |list, (k, v)|
31+
list << "#{k.to_s}=>#{redact(k, v, __method__)}"
32+
end.join(', ') + '}'
33+
end
34+
35+
def has_key?(key)
36+
super(convert_key(key))
3337
end
3438

3539
private
3640

3741
def redact(k, v, method)
38-
return REDACTED_STRING if SENSITIVE_OPTIONS.include?(k.to_sym)
42+
return REDACTED_STRING if REDACTED_OPTIONS.include?(k.to_sym)
3943
v.send(method)
4044
end
4145
end

lib/mongo/server/connection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def deliver(messages)
183183
def setup_authentication!
184184
if options[:user]
185185
default_mechanism = @server.features.scram_sha_1_enabled? ? :scram : :mongodb_cr
186-
user = Auth::User.new(Options::SensitiveOptions.new(:auth_mech => default_mechanism).merge(options))
186+
user = Auth::User.new(Options::Redacted.new(:auth_mech => default_mechanism).merge(options))
187187
@authenticator = Auth.get(user)
188188
end
189189
end

spec/mongo/client_spec.rb

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,15 @@
172172
)
173173
end
174174

175+
let(:options) do
176+
Mongo::Options::Redacted.new(:read => { :mode => :primary },
177+
:local_threshold_ms => 10,
178+
:server_selection_timeout_ms => 10000,
179+
:database => TEST_DB)
180+
end
181+
175182
let(:expected) do
176-
[client.cluster, { :read => { :mode => :primary },
177-
:local_threshold_ms => 10,
178-
:server_selection_timeout_ms => 10000,
179-
:database => TEST_DB }].hash
183+
[client.cluster, options].hash
180184
end
181185

182186
it 'returns a hash of the cluster and options' do
@@ -291,8 +295,12 @@
291295
described_class.new(uri)
292296
end
293297

298+
let(:expected_options) do
299+
Mongo::Options::Redacted.new(:write => { :w => 3 }, :database => 'testdb')
300+
end
301+
294302
it 'sets the options' do
295-
expect(client.options).to eq(:write => { :w => 3 }, :database => 'testdb')
303+
expect(client.options).to eq(expected_options)
296304
end
297305
end
298306

@@ -306,8 +314,12 @@
306314
described_class.new(uri, :write => { :w => 3 })
307315
end
308316

317+
let(:expected_options) do
318+
Mongo::Options::Redacted.new(:write => { :w => 3 }, :database => 'testdb')
319+
end
320+
309321
it 'sets the options' do
310-
expect(client.options).to eq(:write => { :w => 3 }, :database => 'testdb')
322+
expect(client.options).to eq(expected_options)
311323
end
312324
end
313325

@@ -321,8 +333,12 @@
321333
described_class.new(uri, :write => { :w => 4 })
322334
end
323335

336+
let(:expected_options) do
337+
Mongo::Options::Redacted.new(:write => { :w => 4 }, :database => 'testdb')
338+
end
339+
324340
it 'allows explicit options to take preference' do
325-
expect(client.options).to eq(:write => { :w => 4 }, :database => 'testdb')
341+
expect(client.options).to eq(expected_options)
326342
end
327343
end
328344

@@ -497,20 +513,28 @@
497513
client.with(:read => { :mode => :primary })
498514
end
499515

516+
let(:new_options) do
517+
Mongo::Options::Redacted.new(:read => { :mode => :primary },
518+
:write => { :w => 1 },
519+
:database => TEST_DB)
520+
end
521+
522+
let(:original_options) do
523+
Mongo::Options::Redacted.new(:read => { :mode => :secondary },
524+
:write => { :w => 1 },
525+
:database => TEST_DB)
526+
end
527+
500528
it 'returns a new client' do
501529
expect(new_client).not_to equal(client)
502530
end
503531

504532
it 'replaces the existing options' do
505-
expect(new_client.options).to eq({
506-
:read => { :mode => :primary }, :write => { :w => 1 }, :database => TEST_DB
507-
})
533+
expect(new_client.options).to eq(new_options)
508534
end
509535

510536
it 'does not modify the original client' do
511-
expect(client.options).to eq({
512-
:read => { :mode => :secondary }, :write => { :w => 1 }, :database => TEST_DB
513-
})
537+
expect(client.options).to eq(original_options)
514538
end
515539

516540
it 'keeps the same cluster' do
@@ -579,7 +603,7 @@
579603
end
580604

581605
it 'returns a acknowledged write concern' do
582-
expect(concern.get_last_error).to eq(:getlasterror => 1, :j => true)
606+
expect(concern.get_last_error).to eq(:getlasterror => 1, 'j' => true)
583607
end
584608
end
585609

spec/mongo/grid/stream/write_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,14 @@
8787
context 'when chunks are inserted' do
8888

8989
it 'uses that write concern' do
90-
expect(stream.send(:chunks_collection).write_concern.options).to eq(expected)
90+
expect(stream.send(:chunks_collection).write_concern.options[:w]).to eq(expected[:w])
9191
end
9292
end
9393

9494
context 'when a files document is inserted' do
9595

9696
it 'uses that write concern' do
97-
expect(stream.send(:files_collection).write_concern.options).to eq(expected)
97+
expect(stream.send(:files_collection).write_concern.options[:w]).to eq(expected[:w])
9898
end
9999
end
100100
end

0 commit comments

Comments
 (0)