Skip to content

Commit 73f2112

Browse files
committed
RUBY-890 Fix test suite to work with auth
Adjustments to MONGODB_CR
1 parent f216aab commit 73f2112

File tree

4 files changed

+17
-47
lines changed

4 files changed

+17
-47
lines changed

lib/mongo/auth/cr.rb

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,24 +48,11 @@ def initialize(user)
4848
#
4949
# @since 2.0.0
5050
def login(connection)
51-
conversation = Conversation.new(user, auth_database(connection))
51+
conversation = Conversation.new(user)
5252
reply = connection.dispatch([ conversation.start ])
5353
reply = connection.dispatch([ conversation.continue(reply) ])
5454
conversation.finalize(reply)
5555
end
56-
57-
private
58-
59-
# If we are on MongoDB 2.6 and higher, we *always* authorize against the
60-
# admin database. Otherwise for 2.4 and lower we authorize against the
61-
# auth source provided. The logic for that is encapsulated in the User class.
62-
def auth_database(connection)
63-
if connection.features.write_command_enabled?
64-
Database::ADMIN
65-
else
66-
user.auth_source
67-
end
68-
end
6956
end
7057
end
7158
end

lib/mongo/auth/cr/conversation.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class Conversation
5656
def continue(reply)
5757
validate!(reply)
5858
Protocol::Query.new(
59-
database,
59+
user.auth_source,
6060
Database::COMMAND,
6161
LOGIN.merge(user: user.name, nonce: nonce, key: user.auth_key(nonce)),
6262
limit: -1
@@ -89,7 +89,11 @@ def finalize(reply)
8989
#
9090
# @since 2.0.0
9191
def start
92-
Protocol::Query.new(database, Database::COMMAND, Auth::GET_NONCE, limit: -1)
92+
Protocol::Query.new(
93+
user.auth_source,
94+
Database::COMMAND,
95+
Auth::GET_NONCE,
96+
limit: -1)
9397
end
9498

9599
# Create the new conversation.
@@ -101,9 +105,8 @@ def start
101105
# @param [ String ] database The database to authenticate against.
102106
#
103107
# @since 2.0.0
104-
def initialize(user, database)
108+
def initialize(user)
105109
@user = user
106-
@database = database
107110
end
108111

109112
private

spec/spec_helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@
4848
begin
4949
# Adds the test user to the test database with permissions on all
5050
# databases that will be used in the test suite.
51-
ADMIN_AUTHORIZED_CLIENT.database.users.create(TEST_USER)
51+
ADMIN_AUTHORIZED_TEST_CLIENT.database.users.create(TEST_USER)
5252
rescue Exception => e
5353
unless write_command_enabled?
5454
# If we are on versions less than 2.6, we need to create a user for
5555
# each database, since the users are not stored in the admin database
5656
# but in the system.users collection on the datbases themselves. Also,
5757
# roles in versions lower than 2.6 can only be strings, not hashes.
58-
begin ROOT_AUTHORIZED_CLIENT.database.users.create(TEST_READ_WRITE_USER); rescue; end
58+
begin ADMIN_AUTHORIZED_TEST_CLIENT.database.users.create(TEST_READ_WRITE_USER); rescue; end
5959
end
6060
end
6161
end

spec/support/authorization.rb

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
#
5555
# @since 2.0.0
5656
ROOT_USER = Mongo::Auth::User.new(
57-
database: Mongo::Database::ADMIN,
5857
user: ROOT_USER_NAME,
5958
password: ROOT_USER_PWD,
6059
roles: [
@@ -113,21 +112,6 @@
113112
connect: CONNECT
114113
)
115114

116-
# Provides an authorized mongo client on the default test database for the
117-
# default root system administrator.
118-
#
119-
# @since 2.0.0
120-
ROOT_AUTHORIZED_CLIENT = Mongo::Client.new(
121-
ADDRESSES,
122-
auth_source: Mongo::Database::ADMIN,
123-
database: TEST_DB,
124-
user: ROOT_USER.name,
125-
password: ROOT_USER.password,
126-
max_pool_size: 1,
127-
write: WRITE_CONCERN,
128-
connect: CONNECT
129-
)
130-
131115
# Provides an unauthorized mongo client on the default test database.
132116
#
133117
# @since 2.0.0
@@ -151,13 +135,15 @@
151135
connect: CONNECT
152136
)
153137

154-
# Get an authorized client on the admin database logged in as the admin
138+
# Get an authorized client on the test database logged in as the admin
155139
# root user.
156140
#
157141
# @since 2.0.0
158-
ADMIN_AUTHORIZED_CLIENT = ADMIN_UNAUTHORIZED_CLIENT.with(
142+
ADMIN_AUTHORIZED_TEST_CLIENT = ADMIN_UNAUTHORIZED_CLIENT.with(
159143
user: ROOT_USER.name,
160-
password: ROOT_USER.password
144+
password: ROOT_USER.password,
145+
database: TEST_DB,
146+
auth_source: Mongo::Database::ADMIN
161147
)
162148

163149
module Authorization
@@ -185,12 +171,6 @@ def self.included(context)
185171
# @since 2.0.0
186172
context.let(:authorized_client) { AUTHORIZED_CLIENT }
187173

188-
# Provides an authorized mongo client on the default test database for the
189-
# default root system administrator.
190-
#
191-
# @since 2.0.0
192-
context.let(:root_authorized_client) { ROOT_AUTHORIZED_CLIENT }
193-
194174
# Provides an unauthorized mongo client on the default test database.
195175
#
196176
# @since 2.0.0
@@ -202,11 +182,11 @@ def self.included(context)
202182
# @since 2.0.0
203183
context.let!(:admin_unauthorized_client) { ADMIN_UNAUTHORIZED_CLIENT }
204184

205-
# Get an authorized client on the admin database logged in as the admin
185+
# Get an authorized client on the test database logged in as the admin
206186
# root user.
207187
#
208188
# @since 2.0.0
209-
context.let!(:admin_authorized_client) { ADMIN_AUTHORIZED_CLIENT }
189+
context.let!(:root_authorized_client) { ADMIN_AUTHORIZED_TEST_CLIENT }
210190

211191
# Gets the default test collection from the authorized client.
212192
#

0 commit comments

Comments
 (0)