mongo 1.10.2 → 1.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/README.md +43 -9
  5. data/VERSION +1 -1
  6. data/lib/mongo.rb +1 -0
  7. data/lib/mongo/collection.rb +36 -21
  8. data/lib/mongo/connection/pool.rb +14 -22
  9. data/lib/mongo/cursor.rb +13 -0
  10. data/lib/mongo/db.rb +18 -13
  11. data/lib/mongo/functional.rb +0 -2
  12. data/lib/mongo/functional/authentication.rb +35 -25
  13. data/lib/mongo/legacy.rb +4 -4
  14. data/mongo.gemspec +0 -5
  15. data/test/functional/authentication_test.rb +3 -2
  16. data/test/functional/bulk_write_collection_view_test.rb +9 -14
  17. data/test/functional/client_test.rb +42 -43
  18. data/test/functional/collection_test.rb +1073 -995
  19. data/test/functional/collection_writer_test.rb +1 -1
  20. data/test/functional/cursor_fail_test.rb +3 -9
  21. data/test/functional/cursor_message_test.rb +14 -15
  22. data/test/functional/cursor_test.rb +224 -166
  23. data/test/functional/db_api_test.rb +262 -261
  24. data/test/functional/db_connection_test.rb +1 -3
  25. data/test/functional/db_test.rb +116 -115
  26. data/test/functional/grid_file_system_test.rb +108 -108
  27. data/test/functional/pool_test.rb +73 -0
  28. data/test/functional/timeout_test.rb +2 -0
  29. data/test/helpers/test_unit.rb +146 -11
  30. data/test/replica_set/authentication_test.rb +4 -2
  31. data/test/replica_set/basic_test.rb +5 -13
  32. data/test/replica_set/client_test.rb +8 -6
  33. data/test/replica_set/complex_connect_test.rb +3 -0
  34. data/test/replica_set/count_test.rb +2 -0
  35. data/test/replica_set/cursor_test.rb +5 -0
  36. data/test/replica_set/insert_test.rb +1 -1
  37. data/test/replica_set/max_values_test.rb +1 -1
  38. data/test/replica_set/pinning_test.rb +1 -1
  39. data/test/replica_set/query_test.rb +1 -1
  40. data/test/replica_set/read_preference_test.rb +7 -1
  41. data/test/replica_set/refresh_test.rb +11 -8
  42. data/test/replica_set/replication_ack_test.rb +2 -1
  43. data/test/sharded_cluster/basic_test.rb +17 -11
  44. data/test/shared/authentication/basic_auth_shared.rb +59 -98
  45. data/test/shared/authentication/bulk_api_auth_shared.rb +11 -21
  46. data/test/shared/authentication/gssapi_shared.rb +28 -21
  47. data/test/test_helper.rb +5 -0
  48. data/test/tools/mongo_config.rb +96 -11
  49. metadata +4 -5
  50. metadata.gz.sig +0 -0
  51. data/lib/mongo/functional/sasl_java.rb +0 -48
@@ -19,8 +19,10 @@ class ReplicaSetCountTest < Test::Unit::TestCase
19
19
  def setup
20
20
  ensure_cluster(:rs)
21
21
  @client = MongoReplicaSetClient.new(@rs.repl_set_seeds, :read => :primary_preferred)
22
+ authenticate_client(@client)
22
23
  assert @client.primary_pool
23
24
  @primary = MongoClient.new(@client.primary_pool.host, @client.primary_pool.port)
25
+ authenticate_client(@primary)
24
26
  @db = @client.db(TEST_DB)
25
27
  @db.drop_collection("test-sets")
26
28
  @coll = @db.collection("test-sets")
@@ -66,6 +66,7 @@ class ReplicaSetCursorTest < Test::Unit::TestCase
66
66
  route_read ||= read
67
67
  # Setup ReplicaSet Connection
68
68
  @client = MongoReplicaSetClient.new(@rs.repl_set_seeds, :read => read)
69
+ authenticate_client(@client)
69
70
 
70
71
  @db = @client.db(TEST_DB)
71
72
  @db.drop_collection("cursor_tests")
@@ -74,6 +75,7 @@ class ReplicaSetCursorTest < Test::Unit::TestCase
74
75
 
75
76
  # Setup Direct Connections
76
77
  @primary = Mongo::MongoClient.new(*@client.manager.primary)
78
+ authenticate_client(@primary)
77
79
  end
78
80
 
79
81
  def insert_docs
@@ -93,6 +95,7 @@ class ReplicaSetCursorTest < Test::Unit::TestCase
93
95
  pool = cursor.instance_variable_get(:@pool)
94
96
  cursor.close
95
97
  @read = Mongo::MongoClient.new(pool.host, pool.port, :slave_ok => true)
98
+ authenticate_client(@read)
96
99
  tag
97
100
  rescue Mongo::ConnectionFailure
98
101
  false
@@ -110,6 +113,7 @@ class ReplicaSetCursorTest < Test::Unit::TestCase
110
113
  @client.db(TEST_DB).profiling_level = :all
111
114
  @client.secondaries.each do |node|
112
115
  node = Mongo::MongoClient.new(node[0], node[1], :slave_ok => true)
116
+ authenticate_client(node)
113
117
  node.db(TEST_DB).profiling_level = :all
114
118
  end
115
119
 
@@ -120,6 +124,7 @@ class ReplicaSetCursorTest < Test::Unit::TestCase
120
124
  @client.db(TEST_DB).profiling_level = :off
121
125
  @client.secondaries.each do |node|
122
126
  node = Mongo::MongoClient.new(node[0], node[1], :slave_ok => true)
127
+ authenticate_client(node)
123
128
  node.db(TEST_DB).profiling_level = :off
124
129
  end
125
130
  # do a query on system.profile of the reader to see if it was used for the query
@@ -18,7 +18,7 @@ class ReplicaSetInsertTest < Test::Unit::TestCase
18
18
 
19
19
  def setup
20
20
  ensure_cluster(:rs)
21
- @client = MongoReplicaSetClient.new @rs.repl_set_seeds
21
+ @client = MongoReplicaSetClient.from_uri(@uri)
22
22
  @version = @client.server_version
23
23
  @db = @client.db(TEST_DB)
24
24
  @db.drop_collection("test-sets")
@@ -20,7 +20,7 @@ class MaxValuesTest < Test::Unit::TestCase
20
20
 
21
21
  def setup
22
22
  ensure_cluster(:rs)
23
- @client = MongoReplicaSetClient.new(@rs.repl_set_seeds, :name => @rs.repl_set_name)
23
+ @client = MongoReplicaSetClient.from_uri(@uri)
24
24
  @db = new_mock_db
25
25
  @client.stubs(:[]).returns(@db)
26
26
  @ismaster = {
@@ -17,7 +17,7 @@ require 'test_helper'
17
17
  class ReplicaSetPinningTest < Test::Unit::TestCase
18
18
  def setup
19
19
  ensure_cluster(:rs)
20
- @client = MongoReplicaSetClient.new(@rs.repl_set_seeds, :name => @rs.repl_set_name)
20
+ @client = MongoReplicaSetClient.from_uri(@uri)
21
21
  @db = @client.db(TEST_DB)
22
22
  @coll = @db.collection("test-sets")
23
23
  @coll.insert({:a => 1})
@@ -18,7 +18,7 @@ class ReplicaSetQueryTest < Test::Unit::TestCase
18
18
 
19
19
  def setup
20
20
  ensure_cluster(:rs)
21
- @client = MongoReplicaSetClient.new @rs.repl_set_seeds
21
+ @client = MongoReplicaSetClient.from_uri(@uri)
22
22
  @db = @client.db(TEST_DB)
23
23
  @db.drop_collection("test-sets")
24
24
  @coll = @db.collection("test-sets")
@@ -22,6 +22,7 @@ class ReadPreferenceTest < Test::Unit::TestCase
22
22
  # Insert data
23
23
  primary = @rs.primary
24
24
  conn = Connection.new(primary.host, primary.port)
25
+ authenticate_client(conn)
25
26
  db = conn.db(TEST_DB)
26
27
  coll = db.collection("test-sets")
27
28
  coll.save({:a => 20}, {:w => 2})
@@ -190,12 +191,15 @@ class ReadPreferenceTest < Test::Unit::TestCase
190
191
 
191
192
  # Setup direct connections
192
193
  @primary_direct = Connection.new(@rs.config['host'], @primary.read_pool.port)
194
+ authenticate_client(@primary_direct)
193
195
  @secondary_direct = Connection.new(@rs.config['host'], @secondary.read_pool.port, :slave_ok => true)
196
+ authenticate_client(@secondary_direct)
194
197
  end
195
198
 
196
199
  def make_connection(mode = :primary, opts = {})
197
200
  opts.merge!({:read => mode})
198
- MongoReplicaSetClient.new(@rs.repl_set_seeds, opts)
201
+ client = MongoReplicaSetClient.new(@rs.repl_set_seeds, opts)
202
+ authenticate_client(client)
199
203
  end
200
204
 
201
205
  def query_count(connection)
@@ -204,6 +208,8 @@ class ReadPreferenceTest < Test::Unit::TestCase
204
208
 
205
209
  def assert_query_route(test_connection, expected_target)
206
210
  #puts "#{test_connection.read_pool.port} #{expected_target.read_pool.port}"
211
+ authenticate_client(test_connection)
212
+ authenticate_client(expected_target)
207
213
  queries_before = query_count(expected_target)
208
214
  assert_nothing_raised do
209
215
  test_connection[TEST_DB]['test-sets'].find_one
@@ -23,6 +23,7 @@ class ReplicaSetRefreshTest < Test::Unit::TestCase
23
23
  def test_connect_and_manual_refresh_with_secondary_down
24
24
  num_secondaries = @rs.secondaries.size
25
25
  client = MongoReplicaSetClient.new(@rs.repl_set_seeds, :refresh_mode => false)
26
+ authenticate_client(client)
26
27
 
27
28
  assert_equal num_secondaries, client.secondaries.size
28
29
  assert client.connected?
@@ -57,6 +58,7 @@ class ReplicaSetRefreshTest < Test::Unit::TestCase
57
58
  num_secondaries = @rs.secondaries.size
58
59
  client = MongoReplicaSetClient.new(@rs.repl_set_seeds,
59
60
  :refresh_interval => 1, :refresh_mode => :sync, :read => :secondary_preferred)
61
+ authenticate_client(client)
60
62
 
61
63
  # Ensure secondaries are all recognized by client and client is connected
62
64
  assert_equal num_secondaries, client.secondaries.size
@@ -98,6 +100,7 @@ class ReplicaSetRefreshTest < Test::Unit::TestCase
98
100
  nthreads = factor * 10
99
101
  threads = []
100
102
  client = MongoReplicaSetClient.new(@rs.repl_set_seeds, :refresh_mode => :sync, :refresh_interval => 1)
103
+ authenticate_client(client)
101
104
 
102
105
  nthreads.times do |i|
103
106
  threads << Thread.new do
@@ -123,21 +126,20 @@ class ReplicaSetRefreshTest < Test::Unit::TestCase
123
126
  end
124
127
  end
125
128
 
126
-
127
129
  def test_manager_recursive_locking
128
- # See RUBY-775
129
- # This tests that there isn't recursive locking when a pool manager reconnects
130
- # to all replica set members. The bug in RUBY-775 occurred because the same lock
131
- # acquired in order to connect the pool manager was used to read the pool manager's
132
- # state.
133
- client = MongoReplicaSetClient.new(@rs.repl_set_seeds)
130
+ # See RUBY-775
131
+ # This tests that there isn't recursive locking when a pool manager reconnects
132
+ # to all replica set members. The bug in RUBY-775 occurred because the same lock
133
+ # acquired in order to connect the pool manager was used to read the pool manager's
134
+ # state.
135
+ client = MongoReplicaSetClient.from_uri(@uri)
134
136
 
135
137
  cursor = client[TEST_DB]['rs-refresh-test'].find
136
138
  client.stubs(:receive_message).raises(ConnectionFailure)
137
139
  client.manager.stubs(:refresh_required?).returns(true)
138
140
  client.manager.stubs(:check_connection_health).returns(true)
139
141
  assert_raise ConnectionFailure do
140
- cursor.next
142
+ cursor.next
141
143
  end
142
144
  end
143
145
 
@@ -145,6 +147,7 @@ class ReplicaSetRefreshTest < Test::Unit::TestCase
145
147
  def test_automated_refresh_with_removed_node
146
148
  client = MongoReplicaSetClient.new(@rs.repl_set_seeds,
147
149
  :refresh_interval => 1, :refresh_mode => :sync)
150
+ authenticate_client(client)
148
151
 
149
152
  num_secondaries = client.secondary_pools.length
150
153
  old_refresh_version = client.refresh_version
@@ -18,11 +18,12 @@ class ReplicaSetAckTest < Test::Unit::TestCase
18
18
 
19
19
  def setup
20
20
  ensure_cluster(:rs)
21
- @client = MongoReplicaSetClient.new(@rs.repl_set_seeds)
21
+ @client = MongoReplicaSetClient.from_uri(@uri)
22
22
 
23
23
  @slave1 = MongoClient.new(
24
24
  @client.secondary_pools.first.host,
25
25
  @client.secondary_pools.first.port, :slave_ok => true)
26
+ authenticate_client(@slave1)
26
27
 
27
28
  assert !@slave1.read_primary?
28
29
 
@@ -29,7 +29,7 @@ class ShardedClusterBasicTest < Test::Unit::TestCase
29
29
 
30
30
  # TODO member.primary? ==> true
31
31
  def test_connect
32
- @client = MongoShardedClient.new(@seeds)
32
+ @client = sharded_connection
33
33
  assert @client.connected?
34
34
  assert_equal(@seeds.size, @client.seeds.size)
35
35
  probe(@seeds.size)
@@ -54,39 +54,39 @@ class ShardedClusterBasicTest < Test::Unit::TestCase
54
54
  end
55
55
 
56
56
  def test_find_one_with_read_secondary
57
- @client = MongoShardedClient.new(@seeds, { :read => :secondary })
57
+ @client = sharded_connection(:read => :secondary)
58
58
  @client[TEST_DB]["users"].insert([ @document ])
59
59
  assert_equal @client[TEST_DB]['users'].find_one["name"], "test_user"
60
60
  end
61
61
 
62
62
  def test_find_one_with_read_secondary_preferred
63
- @client = MongoShardedClient.new(@seeds, { :read => :secondary_preferred })
63
+ @client = sharded_connection(:read => :secondary_preferred)
64
64
  @client[TEST_DB]["users"].insert([ @document ])
65
65
  assert_equal @client[TEST_DB]['users'].find_one["name"], "test_user"
66
66
  end
67
67
 
68
68
  def test_find_one_with_read_primary
69
- @client = MongoShardedClient.new(@seeds, { :read => :primary })
69
+ @client = sharded_connection(:read => :primary)
70
70
  @client[TEST_DB]["users"].insert([ @document ])
71
71
  assert_equal @client[TEST_DB]['users'].find_one["name"], "test_user"
72
72
  end
73
73
 
74
74
  def test_find_one_with_read_primary_preferred
75
- @client = MongoShardedClient.new(@seeds, { :read => :primary_preferred })
75
+ @client = sharded_connection(:read => :primary_preferred)
76
76
  @client[TEST_DB]["users"].insert([ @document ])
77
77
  assert_equal @client[TEST_DB]['users'].find_one["name"], "test_user"
78
78
  end
79
79
 
80
80
  def test_read_from_sharded_client
81
81
  tags = [{:dc => "mongolia"}]
82
- @client = MongoShardedClient.new(@seeds, {:read => :secondary, :tag_sets => tags})
82
+ @client = sharded_connection(:read => :secondary, :tag_sets => tags)
83
83
  assert @client.connected?
84
84
  cursor = Cursor.new(@client[TEST_DB]['whatever'], {})
85
85
  assert_equal cursor.construct_query_spec['$readPreference'], {:mode => 'secondary', :tags => tags}
86
86
  end
87
87
 
88
88
  def test_hard_refresh
89
- @client = MongoShardedClient.new(@seeds)
89
+ @client = sharded_connection
90
90
  assert @client.connected?
91
91
  @client.hard_refresh!
92
92
  assert @client.connected?
@@ -94,7 +94,7 @@ class ShardedClusterBasicTest < Test::Unit::TestCase
94
94
  end
95
95
 
96
96
  def test_reconnect
97
- @client = MongoShardedClient.new(@seeds)
97
+ @client = sharded_connection
98
98
  assert @client.connected?
99
99
  router = @sc.servers(:routers).first
100
100
  router.stop
@@ -104,7 +104,7 @@ class ShardedClusterBasicTest < Test::Unit::TestCase
104
104
  end
105
105
 
106
106
  def test_mongos_failover
107
- @client = MongoShardedClient.new(@seeds, :refresh_interval => 5, :refresh_mode => :sync)
107
+ @client = sharded_connection(:refresh_interval => 5, :refresh_mode => :sync)
108
108
  assert @client.connected?
109
109
  # do a find to pin a pool
110
110
  @client[TEST_DB]['test'].find_one
@@ -122,7 +122,7 @@ class ShardedClusterBasicTest < Test::Unit::TestCase
122
122
  end
123
123
 
124
124
  def test_all_down
125
- @client = MongoShardedClient.new(@seeds)
125
+ @client = sharded_connection
126
126
  assert @client.connected?
127
127
  @sc.servers(:routers).each{|router| router.stop}
128
128
  assert_raises Mongo::ConnectionFailure do
@@ -133,7 +133,7 @@ class ShardedClusterBasicTest < Test::Unit::TestCase
133
133
  end
134
134
 
135
135
  def test_cycle
136
- @client = MongoShardedClient.new(@seeds)
136
+ @client = sharded_connection
137
137
  assert @client.connected?
138
138
  routers = @sc.servers(:routers)
139
139
  while routers.size > 0 do
@@ -191,7 +191,13 @@ class ShardedClusterBasicTest < Test::Unit::TestCase
191
191
 
192
192
  private
193
193
 
194
+ def sharded_connection(opts={})
195
+ client = MongoShardedClient.new(@seeds, opts)
196
+ authenticate_client(client)
197
+ end
198
+
194
199
  def probe(size)
200
+ authenticate_client(@client)
195
201
  assert_equal(size, @client['config']['mongos'].find.to_a.size)
196
202
  end
197
203
  end
@@ -15,67 +15,58 @@
15
15
  module BasicAuthTests
16
16
 
17
17
  def init_auth_basic
18
- # enable authentication by creating and logging in as admin user
19
- @admin = @client['admin']
20
- @admin.add_user('admin', 'password', nil, :roles => ['readAnyDatabase',
21
- 'readWriteAnyDatabase',
22
- 'userAdminAnyDatabase',
23
- 'dbAdminAnyDatabase',
24
- 'clusterAdmin'])
25
- @admin.authenticate('admin', 'password')
18
+ @test_user = 'bob'
19
+ @test_user_pwd = 'user'
26
20
 
27
21
  # db user for cleanup (for pre-2.4)
28
22
  @db.add_user('admin', 'cleanup', nil, :roles => [])
29
23
  end
30
24
 
31
- def teardown_basic
32
- remove_all_users(@db, 'admin', 'cleanup')
33
- remove_all_users(@admin, 'admin', 'password') if has_auth?(@admin.name)
34
- end
25
+ def teardown
26
+ @client[TEST_DB].authenticate(TEST_USER, TEST_USER_PWD) unless has_auth?(TEST_DB, TEST_USER)
35
27
 
36
- def remove_all_users(database, username, password)
37
- database.authenticate(username, password) unless has_auth?(database.name)
38
28
  if @client.server_version < '2.5'
39
- database['system.users'].remove
29
+ @db['system.users'].remove
40
30
  else
41
- database.command(:dropAllUsersFromDatabase => 1)
31
+ @db.command(:dropAllUsersFromDatabase => 1)
42
32
  end
33
+ end
34
+
35
+ def remove_user(database, username, password)
36
+ database.authenticate(username, password) unless has_auth?(database.name, username)
37
+ database.remove_user(username)
43
38
  database.logout
44
39
  end
45
40
 
46
- def has_auth?(db_name)
47
- @client.auths.any? { |a| a[:source] == db_name }
41
+ def has_auth?(db_name, username)
42
+ @client.auths.any? { |a| a[:source] == db_name && a[:username] == username }
48
43
  end
49
44
 
50
45
  def test_add_remove_user
51
46
  init_auth_basic
52
47
 
53
48
  # add user
54
- silently { @db.add_user('bob','user') }
55
- assert @db.authenticate('bob', 'user')
49
+ silently { @db.add_user(@test_user, @test_user_pwd) }
50
+ assert @db.authenticate(@test_user, @test_user_pwd)
56
51
 
57
52
  # remove user
58
- assert @db.remove_user('bob')
59
-
60
- teardown_basic
53
+ assert @db.remove_user(@test_user)
61
54
  end
62
55
 
63
56
  def test_update_user
64
57
  init_auth_basic
65
58
 
66
59
  # add user
67
- silently { @db.add_user('bob', 'user') }
68
- assert @db.authenticate('bob', 'user')
60
+ silently { @db.add_user(@test_user, @test_user_pwd) }
61
+ assert @db.authenticate(@test_user, @test_user_pwd)
69
62
  @db.logout
70
63
 
71
64
  # update user
72
- silently { @db.add_user('bob', 'updated') }
65
+ silently { @db.add_user(@test_user, 'updated') }
73
66
  assert_raise Mongo::AuthenticationError do
74
67
  @db.authenticate('bob', 'user')
75
68
  end
76
69
  assert @db.authenticate('bob', 'updated')
77
-
78
- teardown_basic
79
70
  end
80
71
 
81
72
  def test_remove_non_existent_user
@@ -88,15 +79,12 @@ module BasicAuthTests
88
79
  assert @db.remove_user('joe')
89
80
  end
90
81
  end
91
- teardown_basic
92
82
  end
93
83
 
94
84
  def test_authenticate
95
85
  init_auth_basic
96
- silently { @db.add_user('peggy', 'user') }
97
- assert @db.authenticate('peggy', 'user')
98
- @db.remove_user('peggy')
99
- teardown_basic
86
+ silently { @db.add_user(@test_user, @test_user_pwd) }
87
+ assert @db.authenticate(@test_user, @test_user_pwd)
100
88
  end
101
89
 
102
90
  def test_authenticate_non_existent_user
@@ -104,47 +92,42 @@ module BasicAuthTests
104
92
  assert_raise Mongo::AuthenticationError do
105
93
  @db.authenticate('frank', 'thetank')
106
94
  end
107
- teardown_basic
108
95
  end
109
96
 
110
97
  def test_logout
111
98
  init_auth_basic
112
- silently { @db.add_user('peggy', 'user') }
113
- assert @db.authenticate('peggy', 'user')
99
+ silently { @db.add_user(@test_user, @test_user_pwd) }
100
+ assert @db.authenticate(@test_user, @test_user_pwd)
114
101
  assert @db.logout
115
- teardown_basic
116
102
  end
117
103
 
118
104
  def test_authenticate_with_special_characters
119
105
  init_auth_basic
120
106
  silently { assert @db.add_user('foo:bar','@foo') }
121
107
  assert @db.authenticate('foo:bar','@foo')
122
- teardown_basic
123
108
  end
124
109
 
125
110
  def test_authenticate_read_only
126
111
  init_auth_basic
127
- silently { @db.add_user('randy', 'readonly', true) }
128
- assert @db.authenticate('randy', 'readonly')
129
- teardown_basic
112
+ silently { @db.add_user(@test_user, @test_user_pwd, true) }
113
+ assert @db.authenticate(@test_user, @test_user_pwd)
130
114
  end
131
115
 
132
116
  def test_authenticate_with_connection_uri
133
117
  init_auth_basic
134
- silently { @db.add_user('eunice', 'uritest') }
118
+ silently { @db.add_user(@test_user, @test_user_pwd) }
135
119
 
136
- uri = "mongodb://eunice:uritest@#{@host_info}/#{@db.name}"
120
+ uri = "mongodb://#{@test_user}:#{@test_user_pwd}@#{@host_info}/#{@db.name}"
137
121
  client = Mongo::URIParser.new(uri).connection
138
122
 
139
123
  assert client
140
124
  assert_equal client.auths.size, 1
141
- assert client[TEST_DB]['auth_test'].count
125
+ assert client[@db.name]['auth_test'].count
142
126
 
143
127
  auth = client.auths.first
144
128
  assert_equal @db.name, auth[:db_name]
145
- assert_equal 'eunice', auth[:username]
146
- assert_equal 'uritest', auth[:password]
147
- teardown_basic
129
+ assert_equal @test_user, auth[:username]
130
+ assert_equal @test_user_pwd, auth[:password]
148
131
  end
149
132
 
150
133
  def test_socket_auths
@@ -177,28 +160,30 @@ module BasicAuthTests
177
160
 
178
161
  # clean-up
179
162
  db_b.authenticate('user_b', 'password')
180
- remove_all_users(db_a, 'user_a', 'password')
181
- remove_all_users(db_b, 'user_b', 'password')
182
- remove_all_users(db_c, 'user_c', 'password')
183
- teardown_basic
163
+ remove_user(db_a, 'user_a', 'password')
164
+ remove_user(db_b, 'user_b', 'password')
165
+ remove_user(db_c, 'user_c', 'password')
184
166
  end
185
167
 
186
168
  def test_default_roles_non_admin
187
- return unless @client.server_version >= '2.5.3'
188
169
  init_auth_basic
189
- silently { @db.add_user('user', 'pass') }
190
- silently { @db.authenticate('user', 'pass') }
191
- info = @db.command(:usersInfo => 'user')['users'].first
192
- assert_equal 'dbOwner', info['roles'].first['role']
170
+ @db.stubs(:command).returns({}, true)
171
+ @db.expects(:command).with do |command, cmd_opts|
172
+ command[:createUser] == @test_user
173
+ cmd_opts[:roles] == ['dbOwner'] if cmd_opts
174
+ end
193
175
 
194
- # read-only
195
- silently { @db.add_user('ro-user', 'pass', true) }
196
- @db.logout
197
- @db.authenticate('ro-user', 'pass')
198
- info = @db.command(:usersInfo => 'ro-user')['users'].first
199
- assert_equal 'read', info['roles'].first['role']
200
- @db.logout
201
- teardown_basic
176
+ silently { @db.add_user(@test_user, @test_user_pwd) }
177
+ end
178
+
179
+ def test_default_roles_non_admin_read_only
180
+ init_auth_basic
181
+ @db.stubs(:command).returns({}, true)
182
+ @db.expects(:command).with do |command, cmd_opts|
183
+ command[:createUser] == @test_user
184
+ cmd_opts[:roles] == ['read'] if cmd_opts
185
+ end
186
+ silently { @db.add_user(@test_user, @test_user_pwd, true) }
202
187
  end
203
188
 
204
189
  def test_delegated_authentication
@@ -208,18 +193,18 @@ module BasicAuthTests
208
193
  # create user in test databases
209
194
  accounts = @client[TEST_DB + '_accounts']
210
195
  silently do
211
- accounts.add_user('debbie', 'delegate')
212
- @db.add_user('debbie', nil, nil, :roles => ['read'], :userSource => accounts.name)
196
+ accounts.add_user('emily', 'password')
197
+ @db.add_user('emily', nil, nil, :roles => ['read'], :userSource => accounts.name)
213
198
  end
214
199
  @admin.logout
215
200
 
216
201
  # validate that direct authentication is not allowed
217
202
  assert_raise Mongo::AuthenticationError do
218
- @db.authenticate('debbie', 'delegate')
203
+ @db.authenticate('emily', 'password')
219
204
  end
220
205
 
221
206
  # validate delegated authentication
222
- assert accounts.authenticate('debbie', 'delegate')
207
+ assert accounts.authenticate('emily', 'password')
223
208
  assert @db.collection_names
224
209
  accounts.logout
225
210
  assert_raise Mongo::OperationFailure do
@@ -227,49 +212,27 @@ module BasicAuthTests
227
212
  end
228
213
 
229
214
  # validate auth using source database
230
- @db.authenticate('debbie', 'delegate', nil, accounts.name)
215
+ @db.authenticate('emily', 'password', nil, accounts.name)
231
216
  assert @db.collection_names
232
217
  accounts.logout
233
218
  assert_raise Mongo::OperationFailure do
234
219
  @db.collection_names
235
220
  end
236
221
 
237
- # clean-up
238
- @admin.authenticate('admin', 'password')
239
- remove_all_users(accounts, 'debbie', 'delegate')
240
- teardown_basic
222
+ remove_user(accounts, 'emily', 'password')
241
223
  end
242
224
  end
243
225
 
244
- def test_non_admin_default_roles
245
- return if @client.server_version < '2.5'
246
- init_auth_basic
247
-
248
- # add read-only user and verify that role is 'read'
249
- @db.add_user('randy', 'password', nil, :roles => ['read'])
250
- @db.authenticate('randy', 'password')
251
- users = @db.command(:usersInfo => 'randy')['users']
252
- assert_equal 'read', users.first['roles'].first['role']
253
- @db.logout
254
-
255
- # add dbOwner (default) user and verify role
256
- silently { @db.add_user('emily', 'password') }
257
- @db.authenticate('emily', 'password')
258
- users = @db.command(:usersInfo => 'emily')['users']
259
- assert_equal 'dbOwner', users.first['roles'].first['role']
260
- teardown_basic
261
- end
262
-
263
226
  def test_update_user_to_read_only
264
227
  with_auth(@client) do
265
228
  init_auth_basic
266
- silently { @db.add_user('emily', 'password') }
229
+ silently { @db.add_user(@test_user, @test_user_pwd) }
267
230
  @admin.logout
268
- @db.authenticate('emily', 'password')
231
+ @db.authenticate(@test_user, @test_user_pwd)
269
232
  @db['test'].insert({})
270
233
  @db.logout
271
234
 
272
- @admin.authenticate('admin', 'password')
235
+ @admin.authenticate(TEST_USER, TEST_USER_PWD)
273
236
  silently { @db.add_user('emily', 'password', true) }
274
237
  @admin.logout
275
238
 
@@ -278,9 +241,7 @@ module BasicAuthTests
278
241
  @db['test'].insert({})
279
242
  end
280
243
  @db.logout
281
- @admin.authenticate('admin', 'password')
282
- teardown_basic
244
+ @admin.authenticate(TEST_USER, TEST_USER_PWD)
283
245
  end
284
246
  end
285
-
286
247
  end