Skip to content

Commit 5991474

Browse files
committed
RUBY-890 Fix test suite to work with auth
Adjustments to MONGODB_CR
1 parent 66d84a6 commit 5991474

File tree

5 files changed

+19
-48
lines changed

5 files changed

+19
-48
lines changed

lib/mongo/auth/cr.rb

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class CR
2828
# Instantiate a new authenticator.
2929
#
3030
# @example Create the authenticator.
31-
# Mongo::Auth::X509.new(user)
31+
# Mongo::Auth::CR.new(user)
3232
#
3333
# @param [ Mongo::Auth::User ] user The user to authenticate.
3434
#
@@ -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

lib/mongo/server/connection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Connection
2222
include Connectable
2323
extend Forwardable
2424

25-
# @return [ Mongo::Auth::CR, Mongo::Auth::X509, Mongo::Auth:LDAP ]
25+
# @return [ Mongo::Auth::CR, Mongo::Auth::X509, Mongo::Auth:LDAP, Mongo::Auth::SCRAM ]
2626
# authenticator The authentication strategy.
2727
attr_reader :authenticator
2828

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 & 26 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: [
@@ -112,20 +111,6 @@
112111
write: WRITE_CONCERN
113112
)
114113

115-
# Provides an authorized mongo client on the default test database for the
116-
# default root system administrator.
117-
#
118-
# @since 2.0.0
119-
ROOT_AUTHORIZED_CLIENT = Mongo::Client.new(
120-
ADDRESSES,
121-
auth_source: Mongo::Database::ADMIN,
122-
database: TEST_DB,
123-
user: ROOT_USER.name,
124-
password: ROOT_USER.password,
125-
max_pool_size: 1,
126-
write: WRITE_CONCERN
127-
)
128-
129114
# Provides an unauthorized mongo client on the default test database.
130115
#
131116
# @since 2.0.0
@@ -147,13 +132,15 @@
147132
write: WRITE_CONCERN
148133
)
149134

150-
# Get an authorized client on the admin database logged in as the admin
135+
# Get an authorized client on the test database logged in as the admin
151136
# root user.
152137
#
153138
# @since 2.0.0
154-
ADMIN_AUTHORIZED_CLIENT = ADMIN_UNAUTHORIZED_CLIENT.with(
139+
ADMIN_AUTHORIZED_TEST_CLIENT = ADMIN_UNAUTHORIZED_CLIENT.with(
155140
user: ROOT_USER.name,
156-
password: ROOT_USER.password
141+
password: ROOT_USER.password,
142+
database: TEST_DB,
143+
auth_source: Mongo::Database::ADMIN
157144
)
158145

159146
module Authorization
@@ -181,12 +168,6 @@ def self.included(context)
181168
# @since 2.0.0
182169
context.let(:authorized_client) { AUTHORIZED_CLIENT }
183170

184-
# Provides an authorized mongo client on the default test database for the
185-
# default root system administrator.
186-
#
187-
# @since 2.0.0
188-
context.let(:root_authorized_client) { ROOT_AUTHORIZED_CLIENT }
189-
190171
# Provides an unauthorized mongo client on the default test database.
191172
#
192173
# @since 2.0.0
@@ -198,11 +179,11 @@ def self.included(context)
198179
# @since 2.0.0
199180
context.let!(:admin_unauthorized_client) { ADMIN_UNAUTHORIZED_CLIENT }
200181

201-
# Get an authorized client on the admin database logged in as the admin
182+
# Get an authorized client on the test database logged in as the admin
202183
# root user.
203184
#
204185
# @since 2.0.0
205-
context.let!(:admin_authorized_client) { ADMIN_AUTHORIZED_CLIENT }
186+
context.let!(:root_authorized_client) { ADMIN_AUTHORIZED_TEST_CLIENT }
206187

207188
# Gets the default test collection from the authorized client.
208189
#

0 commit comments

Comments
 (0)