spec/mongo/auth/scram/negotiation_spec.rb in mongo-2.11.6 vs spec/mongo/auth/scram/negotiation_spec.rb in mongo-2.12.0.rc0
- old
+ new
@@ -1,22 +1,15 @@
require 'spec_helper'
-# This test should set cleanup: false on all clients, because due to
-# https://jira.mongodb.org/browse/RUBY-1772 we may be getting connections
-# established after the client is closed which screws up our assertions on
-# the auth calls. When 1772 is fixed, the cleanup should happen on existing
-# connections and thus should no longer interfere with auth assertions.
+# max_pool_size is set to 1 to force a single connection being used for
+# all operations in a client.
describe 'SCRAM-SHA auth mechanism negotiation' do
min_server_fcv '4.0'
+ # Test uses global assertions
clean_slate
- URI_OPTION_MAP = {
- :auth_source => 'authsource',
- :replica_set => 'replicaSet',
- }
-
let(:create_user!) do
root_authorized_admin_client.tap do |client|
users = client.database.users
if users.info(user.name).any?
users.remove(user.name)
@@ -50,11 +43,11 @@
o[:auth_mech] = auth_mech if auth_mech
end
new_local_client(
SpecConfig.instance.addresses,
- SpecConfig.instance.test_options.merge(opts).update(cleanup: false)
+ SpecConfig.instance.test_options.merge(opts).update(max_pool_size: 1)
)
end
context 'when the user exists' do
@@ -197,45 +190,55 @@
let(:auth_mech) do
:scram
end
- it 'authenticates successfully' do
+ before do
create_user!
+ end
- mechanism = nil
- expect(Mongo::Auth).to receive(:get).and_wrap_original do |m, user|
- # copy mechanism here rather than whole user
- # in case something mutates mechanism later
- mechanism = user.mechanism
- m.call(user)
- end
+ it 'authenticates successfully' do
+ RSpec::Mocks.with_temporary_scope do
+ mechanism = nil
+ expect(Mongo::Auth).to receive(:get).and_wrap_original do |m, user|
+ # copy mechanism here rather than whole user
+ # in case something mutates mechanism later
+ mechanism = user.mechanism
+ m.call(user)
+ end
- expect { result }.not_to raise_error
- expect(mechanism).to eq(:scram)
+ expect do
+ result
+ end.not_to raise_error
+ expect(mechanism).to eq(:scram)
+ end
end
end
context 'when SCRAM-SHA-256 is specified as the auth mechanism' do
let(:auth_mech) do
:scram256
end
- it 'authenticates successfully with SCRAM-SHA-256' do
+ before do
create_user!
+ end
- mechanism = nil
- expect(Mongo::Auth).to receive(:get).and_wrap_original do |m, user|
- # copy mechanism here rather than whole user
- # in case something mutates mechanism later
- mechanism = user.mechanism
- m.call(user)
- end
+ it 'authenticates successfully with SCRAM-SHA-256' do
+ RSpec::Mocks.with_temporary_scope do
+ mechanism = nil
+ expect(Mongo::Auth).to receive(:get).and_wrap_original do |m, user|
+ # copy mechanism here rather than whole user
+ # in case something mutates mechanism later
+ mechanism = user.mechanism
+ m.call(user)
+ end
- expect { result }.not_to raise_error
- expect(mechanism).to eq(:scram256)
+ expect { result }.not_to raise_error
+ expect(mechanism).to eq(:scram256)
+ end
end
end
end
end
@@ -310,34 +313,22 @@
end
context 'when the configuration is specified in the URI' do
let(:uri) do
- "mongodb://#{user.name}:#{password}@#{SpecConfig.instance.addresses.join(',')}/admin".tap do |uri|
- first = true
-
- if SpecConfig.instance.uri_options
- SpecConfig.instance.uri_options.each do |k, v|
- uri << (first ? '?' : '&')
- first = false
-
- k = URI_OPTION_MAP[k] || k
-
- uri << "#{k}=#{v}"
- end
- end
-
- if auth_mech
- uri << (first ? '?' : '&')
-
- uri << "authMechanism=#{Mongo::URI::AUTH_MECH_MAP.key(auth_mech)}"
- end
- end
+ Utils.create_mongodb_uri(
+ SpecConfig.instance.addresses,
+ username: user.name,
+ password: password,
+ uri_options: SpecConfig.instance.uri_options.merge(
+ auth_mech: auth_mech,
+ ),
+ )
end
let(:client) do
- new_local_client(uri, SpecConfig.instance.ssl_options.merge(cleanup: false))
+ new_local_client(uri, SpecConfig.instance.ssl_options.merge(max_pool_size: 1))
end
context 'when the user exists' do
context 'when the user only can use SCRAM-SHA-1 to authenticate' do
@@ -348,11 +339,11 @@
let(:user) do
Mongo::Auth::User.new(
user: 'sha1',
password: 'sha1',
- auth_mech: auth_mech
+ auth_mech: auth_mech,
)
end
context 'when no auth mechanism is specified' do
@@ -401,11 +392,11 @@
let(:user) do
Mongo::Auth::User.new(
user: 'sha256',
password: 'sha256',
- auth_mech: auth_mech
+ auth_mech: auth_mech,
)
end
context 'when no auth mechanism is specified' do
@@ -455,11 +446,11 @@
let(:user) do
Mongo::Auth::User.new(
user: 'both',
password: 'both',
- auth_mech: auth_mech
+ auth_mech: auth_mech,
)
end
context 'when no auth mechanism is specified' do
@@ -478,45 +469,53 @@
let(:auth_mech) do
:scram
end
- it 'authenticates successfully' do
+ before do
create_user!
expect(user.mechanism).to eq(:scram)
+ end
- mechanism = nil
- expect(Mongo::Auth).to receive(:get).and_wrap_original do |m, user|
- # copy mechanism here rather than whole user
- # in case something mutates mechanism later
- mechanism = user.mechanism
- m.call(user)
- end
+ it 'authenticates successfully' do
+ RSpec::Mocks.with_temporary_scope do
+ mechanism = nil
+ expect(Mongo::Auth).to receive(:get).and_wrap_original do |m, user|
+ # copy mechanism here rather than whole user
+ # in case something mutates mechanism later
+ mechanism = user.mechanism
+ m.call(user)
+ end
- expect { result }.not_to raise_error
- expect(mechanism).to eq(:scram)
+ expect { result }.not_to raise_error
+ expect(mechanism).to eq(:scram)
+ end
end
end
context 'when SCRAM-SHA-256 is specified as the auth mechanism' do
let(:auth_mech) do
:scram256
end
- it 'authenticates successfully with SCRAM-SHA-256' do
+ before do
create_user!
+ end
- mechanism = nil
- expect(Mongo::Auth).to receive(:get).and_wrap_original do |m, user|
- # copy mechanism here rather than whole user
- # in case something mutates mechanism later
- mechanism = user.mechanism
- m.call(user)
- end
+ it 'authenticates successfully with SCRAM-SHA-256' do
+ RSpec::Mocks.with_temporary_scope do
+ mechanism = nil
+ expect(Mongo::Auth).to receive(:get).and_wrap_original do |m, user|
+ # copy mechanism here rather than whole user
+ # in case something mutates mechanism later
+ mechanism = user.mechanism
+ m.call(user)
+ end
- expect { result }.not_to raise_error
- expect(mechanism).to eq(:scram256)
+ expect { result }.not_to raise_error
+ expect(mechanism).to eq(:scram256)
+ end
end
end
end
end
end