mongo 1.9.2 → 1.10.0.rc0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (113) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/LICENSE +1 -1
  5. data/README.md +94 -334
  6. data/Rakefile +6 -4
  7. data/VERSION +1 -1
  8. data/bin/mongo_console +13 -6
  9. data/lib/mongo.rb +22 -27
  10. data/lib/mongo/bulk_write_collection_view.rb +352 -0
  11. data/lib/mongo/collection.rb +128 -188
  12. data/lib/mongo/collection_writer.rb +348 -0
  13. data/lib/mongo/connection.rb +19 -0
  14. data/lib/mongo/{util → connection}/node.rb +15 -1
  15. data/lib/mongo/{util → connection}/pool.rb +34 -19
  16. data/lib/mongo/{util → connection}/pool_manager.rb +8 -2
  17. data/lib/mongo/{util → connection}/sharding_pool_manager.rb +1 -1
  18. data/lib/mongo/connection/socket.rb +18 -0
  19. data/lib/mongo/{util → connection/socket}/socket_util.rb +5 -2
  20. data/lib/mongo/{util → connection/socket}/ssl_socket.rb +3 -4
  21. data/lib/mongo/{util → connection/socket}/tcp_socket.rb +25 -15
  22. data/lib/mongo/{util → connection/socket}/unix_socket.rb +6 -4
  23. data/lib/mongo/cursor.rb +113 -47
  24. data/lib/mongo/db.rb +203 -131
  25. data/lib/mongo/{exceptions.rb → exception.rb} +7 -1
  26. data/lib/mongo/functional.rb +19 -0
  27. data/lib/mongo/functional/authentication.rb +303 -0
  28. data/lib/mongo/{util → functional}/logging.rb +1 -1
  29. data/lib/mongo/{util → functional}/read_preference.rb +49 -1
  30. data/lib/mongo/{util → functional}/uri_parser.rb +81 -69
  31. data/lib/mongo/{util → functional}/write_concern.rb +2 -1
  32. data/{test/unit/pool_test.rb → lib/mongo/gridfs.rb} +5 -10
  33. data/lib/mongo/gridfs/grid.rb +1 -3
  34. data/lib/mongo/gridfs/grid_ext.rb +1 -1
  35. data/lib/mongo/gridfs/grid_file_system.rb +1 -1
  36. data/lib/mongo/gridfs/grid_io.rb +1 -1
  37. data/lib/mongo/legacy.rb +63 -8
  38. data/lib/mongo/mongo_client.rb +128 -154
  39. data/lib/mongo/mongo_replica_set_client.rb +17 -11
  40. data/lib/mongo/mongo_sharded_client.rb +2 -1
  41. data/lib/mongo/networking.rb +19 -10
  42. data/lib/mongo/utils.rb +19 -0
  43. data/lib/mongo/{util → utils}/conversions.rb +1 -1
  44. data/lib/mongo/{util → utils}/core_ext.rb +1 -1
  45. data/lib/mongo/{util → utils}/server_version.rb +1 -1
  46. data/lib/mongo/{util → utils}/support.rb +10 -57
  47. data/lib/mongo/{util → utils}/thread_local_variable_manager.rb +1 -1
  48. data/test/functional/authentication_test.rb +8 -21
  49. data/test/functional/bulk_write_collection_view_test.rb +782 -0
  50. data/test/functional/{connection_test.rb → client_test.rb} +153 -78
  51. data/test/functional/collection_test.rb +343 -97
  52. data/test/functional/collection_writer_test.rb +83 -0
  53. data/test/functional/conversions_test.rb +1 -3
  54. data/test/functional/cursor_fail_test.rb +3 -3
  55. data/test/functional/cursor_message_test.rb +3 -3
  56. data/test/functional/cursor_test.rb +38 -3
  57. data/test/functional/db_api_test.rb +5 -5
  58. data/test/functional/db_connection_test.rb +2 -2
  59. data/test/functional/db_test.rb +35 -11
  60. data/test/functional/grid_file_system_test.rb +2 -2
  61. data/test/functional/grid_io_test.rb +2 -2
  62. data/test/functional/grid_test.rb +2 -2
  63. data/test/functional/pool_test.rb +2 -3
  64. data/test/functional/safe_test.rb +5 -5
  65. data/test/functional/ssl_test.rb +22 -102
  66. data/test/functional/support_test.rb +1 -1
  67. data/test/functional/timeout_test.rb +6 -22
  68. data/test/functional/uri_test.rb +113 -12
  69. data/test/functional/write_concern_test.rb +6 -6
  70. data/test/helpers/general.rb +50 -0
  71. data/test/helpers/test_unit.rb +309 -0
  72. data/test/replica_set/authentication_test.rb +8 -23
  73. data/test/replica_set/basic_test.rb +41 -14
  74. data/test/replica_set/client_test.rb +179 -117
  75. data/test/replica_set/complex_connect_test.rb +6 -7
  76. data/test/replica_set/connection_test.rb +46 -38
  77. data/test/replica_set/count_test.rb +2 -2
  78. data/test/replica_set/cursor_test.rb +8 -8
  79. data/test/replica_set/insert_test.rb +64 -2
  80. data/test/replica_set/max_values_test.rb +59 -10
  81. data/test/replica_set/pinning_test.rb +2 -2
  82. data/test/replica_set/query_test.rb +2 -2
  83. data/test/replica_set/read_preference_test.rb +6 -6
  84. data/test/replica_set/refresh_test.rb +7 -7
  85. data/test/replica_set/replication_ack_test.rb +5 -5
  86. data/test/replica_set/ssl_test.rb +24 -106
  87. data/test/sharded_cluster/basic_test.rb +43 -15
  88. data/test/shared/authentication/basic_auth_shared.rb +215 -0
  89. data/test/shared/authentication/sasl_plain_shared.rb +96 -0
  90. data/test/shared/ssl_shared.rb +173 -0
  91. data/test/test_helper.rb +31 -199
  92. data/test/threading/basic_test.rb +29 -3
  93. data/test/tools/mongo_config.rb +45 -20
  94. data/test/tools/mongo_config_test.rb +1 -1
  95. data/test/unit/client_test.rb +136 -57
  96. data/test/unit/collection_test.rb +31 -55
  97. data/test/unit/connection_test.rb +135 -72
  98. data/test/unit/cursor_test.rb +2 -2
  99. data/test/unit/db_test.rb +19 -15
  100. data/test/unit/grid_test.rb +2 -2
  101. data/test/unit/mongo_sharded_client_test.rb +17 -15
  102. data/test/unit/node_test.rb +2 -2
  103. data/test/unit/pool_manager_test.rb +7 -5
  104. data/test/unit/read_pref_test.rb +82 -2
  105. data/test/unit/read_test.rb +14 -14
  106. data/test/unit/safe_test.rb +9 -9
  107. data/test/unit/sharding_pool_manager_test.rb +11 -5
  108. data/test/unit/write_concern_test.rb +9 -9
  109. metadata +71 -56
  110. metadata.gz.sig +0 -0
  111. data/test/functional/threading_test.rb +0 -109
  112. data/test/shared/authentication.rb +0 -121
  113. data/test/unit/util_test.rb +0 -69
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB, Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -14,43 +14,45 @@
14
14
 
15
15
  require 'test_helper'
16
16
 
17
- class CollectionTest < Test::Unit::TestCase
17
+ module Mongo
18
+ class Collection
19
+ attr_reader :operation_writer,
20
+ :command_writer
21
+ end
22
+ end
23
+
24
+ class CollectionUnitTest < Test::Unit::TestCase
18
25
 
19
26
  context "Basic operations: " do
20
27
  setup do
21
28
  @logger = mock()
22
29
  @logger.stubs(:level => 0)
23
30
  @logger.expects(:debug)
31
+
32
+ @client = MongoClient.new('localhost', 27017, :logger => @logger, :connect => false)
33
+ @db = @client[TEST_DB]
34
+ @coll = @db.collection('collection-unit-test')
24
35
  end
25
36
 
26
37
  should "send update message" do
27
- @client = MongoClient.new('localhost', 27017, :logger => @logger, :connect => false)
28
- @db = @client['testing']
29
- @coll = @db.collection('books')
30
38
  @client.expects(:send_message_with_gle).with do |op, msg, log|
31
39
  op == 2001
32
40
  end
33
- @coll.stubs(:log_operation)
41
+ @coll.operation_writer.stubs(:log_operation)
34
42
  @coll.update({}, {:title => 'Moby Dick'})
35
43
  end
36
44
 
37
45
  should "send insert message" do
38
- @client = MongoClient.new('localhost', 27017, :logger => @logger, :connect => false)
39
- @db = @client['testing']
40
- @coll = @db.collection('books')
41
46
  @client.expects(:send_message_with_gle).with do |op, msg, log|
42
47
  op == 2002
43
48
  end
44
- @coll.expects(:log_operation).with do |name, payload|
45
- (name == :insert) && payload[:documents][0][:title].include?('Moby')
49
+ @coll.operation_writer.expects(:log_operation).with do |name, payload|
50
+ (name == :insert) && payload[:documents][:title].include?('Moby')
46
51
  end
47
52
  @coll.insert({:title => 'Moby Dick'})
48
53
  end
49
54
 
50
55
  should "send sort data" do
51
- @client = MongoClient.new('localhost', 27017, :logger => @logger, :connect => false)
52
- @db = @client['testing']
53
- @coll = @db.collection('books')
54
56
  @client.expects(:checkout_reader).returns(new_mock_socket)
55
57
  @client.expects(:receive_message).with do |op, msg, log, sock|
56
58
  op == 2004
@@ -60,60 +62,48 @@ class CollectionTest < Test::Unit::TestCase
60
62
  end
61
63
 
62
64
  should "not log binary data" do
63
- @client = MongoClient.new('localhost', 27017, :logger => @logger, :connect => false)
64
- @db = @client['testing']
65
- @coll = @db.collection('books')
66
65
  data = BSON::Binary.new(("BINARY " * 1000).unpack("c*"))
67
66
  @client.expects(:send_message_with_gle).with do |op, msg, log|
68
67
  op == 2002
69
68
  end
70
- @coll.expects(:log_operation).with do |name, payload|
71
- (name == :insert) && payload[:documents][0][:data].inspect.include?('Binary')
69
+ @coll.operation_writer.expects(:log_operation).with do |name, payload|
70
+ (name == :insert) && payload[:documents][:data].inspect.include?('Binary')
72
71
  end
73
72
  @coll.insert({:data => data})
74
73
  end
75
74
 
76
75
  should "send safe update message" do
77
- @client = MongoClient.new('localhost', 27017, :logger => @logger, :connect => false)
78
- @db = @client['testing']
79
- @coll = @db.collection('books')
80
76
  @client.expects(:send_message_with_gle).with do |op, msg, db_name, log|
81
77
  op == 2001
82
78
  end
83
- @coll.expects(:log_operation).with do |name, payload|
84
- (name == :update) && payload[:document][:title].include?('Moby')
79
+ @coll.operation_writer.expects(:log_operation).with do |name, payload|
80
+ (name == :update) && payload[:documents][:title].include?('Moby')
85
81
  end
86
82
  @coll.update({}, {:title => 'Moby Dick'})
87
83
  end
88
84
 
89
85
  should "send safe update message with legacy" do
90
- @connection = Connection.new('localhost', 27017, :logger => @logger, :safe => true, :connect => false)
91
- @db = @connection['testing']
92
- @coll = @db.collection('books')
93
- @connection.expects(:send_message_with_gle).with do |op, msg, db_name, log|
86
+ connection = Connection.new('localhost', 27017, :safe => true, :connect => false)
87
+ db = connection[TEST_DB]
88
+ coll = db.collection('collection-unit-test')
89
+ connection.expects(:send_message_with_gle).with do |op, msg, db_name, log|
94
90
  op == 2001
95
91
  end
96
- @coll.expects(:log_operation).with do |name, payload|
97
- (name == :update) && payload[:document][:title].include?('Moby')
92
+ coll.operation_writer.expects(:log_operation).with do |name, payload|
93
+ (name == :update) && payload[:documents][:title].include?('Moby')
98
94
  end
99
- @coll.update({}, {:title => 'Moby Dick'})
95
+ coll.update({}, {:title => 'Moby Dick'})
100
96
  end
101
97
 
102
98
  should "send safe insert message" do
103
- @client = MongoClient.new('localhost', 27017, :logger => @logger, :connect => false)
104
- @db = @client['testing']
105
- @coll = @db.collection('books')
106
99
  @client.expects(:send_message_with_gle).with do |op, msg, db_name, log|
107
100
  op == 2001
108
101
  end
109
- @coll.stubs(:log_operation)
102
+ @coll.operation_writer.stubs(:log_operation)
110
103
  @coll.update({}, {:title => 'Moby Dick'})
111
104
  end
112
105
 
113
106
  should "not call insert for each ensure_index call" do
114
- @client = MongoClient.new('localhost', 27017, :logger => @logger, :connect => false)
115
- @db = @client['testing']
116
- @coll = @db.collection('books')
117
107
  @coll.expects(:generate_indexes).once
118
108
 
119
109
  @coll.ensure_index [["x", Mongo::DESCENDING]]
@@ -121,21 +111,15 @@ class CollectionTest < Test::Unit::TestCase
121
111
  end
122
112
 
123
113
  should "call generate_indexes for a new type on the same field for ensure_index" do
124
- @client = MongoClient.new('localhost', 27017, :logger => @logger, :connect => false)
125
- @db = @client['testing']
126
- @coll = @db.collection('books')
127
114
  @coll.expects(:generate_indexes).twice
128
115
 
129
116
  @coll.ensure_index [["x", Mongo::DESCENDING]]
130
117
  @coll.ensure_index [["x", Mongo::ASCENDING]]
131
-
132
118
  end
133
119
 
134
120
  should "call generate_indexes twice because the cache time is 0 seconds" do
135
- @client = MongoClient.new('localhost', 27017, :logger => @logger, :connect => false)
136
- @db = @client['testing']
137
121
  @db.cache_time = 0
138
- @coll = @db.collection('books')
122
+ @coll = @db.collection('collection-unit-test')
139
123
  @coll.expects(:generate_indexes).twice
140
124
 
141
125
  @coll.ensure_index [["x", Mongo::DESCENDING]]
@@ -143,10 +127,8 @@ class CollectionTest < Test::Unit::TestCase
143
127
  end
144
128
 
145
129
  should "call generate_indexes for each key when calling ensure_indexes" do
146
- @client = MongoClient.new('localhost', 27017, :logger => @logger, :connect => false)
147
- @db = @client['testing']
148
130
  @db.cache_time = 300
149
- @coll = @db.collection('books')
131
+ @coll = @db.collection('collection-unit-test')
150
132
  @coll.expects(:generate_indexes).once.with do |a, b, c|
151
133
  a == {"x"=>-1, "y"=>-1}
152
134
  end
@@ -155,10 +137,8 @@ class CollectionTest < Test::Unit::TestCase
155
137
  end
156
138
 
157
139
  should "call generate_indexes for each key when calling ensure_indexes with a hash" do
158
- @client = MongoClient.new('localhost', 27017, :logger => @logger, :connect => false)
159
- @db = @client['testing']
160
140
  @db.cache_time = 300
161
- @coll = @db.collection('books')
141
+ @coll = @db.collection('collection-unit-test')
162
142
  oh = BSON::OrderedHash.new
163
143
  oh['x'] = Mongo::DESCENDING
164
144
  oh['y'] = Mongo::DESCENDING
@@ -176,11 +156,7 @@ class CollectionTest < Test::Unit::TestCase
176
156
  end
177
157
  end
178
158
 
179
-
180
159
  should "use the connection's logger" do
181
- @client = MongoClient.new('localhost', 27017, :logger => @logger, :connect => false)
182
- @db = @client['testing']
183
- @coll = @db.collection('books')
184
160
  @logger.expects(:warn).with do |msg|
185
161
  msg == "MONGODB [WARNING] test warning"
186
162
  end
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB, Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
14
14
 
15
15
  require 'test_helper'
16
16
 
17
- class ConnectionTest < Test::Unit::TestCase
17
+ class ConnectionUnitTest < Test::Unit::TestCase
18
18
  context "Mongo::MongoClient initialization " do
19
19
  context "given a single node" do
20
20
  setup do
@@ -66,22 +66,6 @@ class ConnectionTest < Test::Unit::TestCase
66
66
  connection.send(:initialize, *args)
67
67
  end
68
68
 
69
- context "given a replica set" do
70
-
71
- # should "warn if invalid options are specified" do
72
- # connection = Mongo::ReplSetConnection.allocate
73
- # opts = {:connect => false}
74
-
75
- # # Mongo::Connection::CLIENT_ONLY_OPTS.each do |opt|
76
- # connection.expects(:warn).with("#{:slave_ok} is not a valid option for #{connection.class}")
77
- # opts[:slave_ok] = true
78
- # # end
79
-
80
- # args = [['localhost:27017'], opts]
81
- # connection.send(:initialize, *args)
82
- # end
83
-
84
- end
85
69
  end
86
70
 
87
71
  context "initializing with a unix socket" do
@@ -100,11 +84,19 @@ class ConnectionTest < Test::Unit::TestCase
100
84
  assert_equal ['localhost', 27017], @connection.host_port
101
85
  end
102
86
 
103
- #should "parse a unix socket" do
104
- # socket_address = "/tmp/mongodb-27017.sock"
105
- # @client = MongoClient.from_uri("mongodb://#{socket_address}")
106
- # assert_equal socket_address, @client.host_port.first
107
- #end
87
+ should "set auth source" do
88
+ @connection = Mongo::Connection.from_uri("mongodb://user:pass@localhost?authSource=foo", :connect => false)
89
+ assert_equal 'foo', @connection.auths.first[:source]
90
+ end
91
+
92
+ should "set auth mechanism" do
93
+ @connection = Mongo::Connection.from_uri("mongodb://user@localhost?authMechanism=MONGODB-X509", :connect => false)
94
+ assert_equal 'MONGODB-X509', @connection.auths.first[:mechanism]
95
+
96
+ assert_raise MongoArgumentError do
97
+ Mongo::Connection.from_uri("mongodb://localhost?authMechanism=INVALID", :connect => false)
98
+ end
99
+ end
108
100
 
109
101
  should "allow a complex host names" do
110
102
  host_name = "foo.bar-12345.org"
@@ -136,8 +128,15 @@ class ConnectionTest < Test::Unit::TestCase
136
128
  should "parse a uri with a hyphen & underscore in the username or password" do
137
129
  @connection = Mongo::Connection.from_uri("mongodb://hyphen-user_name:p-s_s@localhost:27017/db", :connect => false)
138
130
  assert_equal ['localhost', 27017], @connection.host_port
139
- auth_hash = { :db_name => 'db', :username => 'hyphen-user_name', :password => 'p-s_s' }
140
- assert_equal auth_hash, @connection.auths[0]
131
+
132
+ auth_hash = {
133
+ :db_name => 'db',
134
+ :username => 'hyphen-user_name',
135
+ :password => 'p-s_s',
136
+ :source => 'db',
137
+ :mechanism => Authentication::DEFAULT_MECHANISM
138
+ }
139
+ assert_equal auth_hash, @connection.auths.first
141
140
  end
142
141
 
143
142
  should "attempt to connect" do
@@ -160,99 +159,163 @@ class ConnectionTest < Test::Unit::TestCase
160
159
  end
161
160
  end
162
161
 
163
- should "require all of username, if password and db are specified" do
162
+ should "require password if using legacy auth and username present" do
164
163
  assert Mongo::Connection.from_uri("mongodb://kyle:jones@localhost/db", :connect => false)
165
164
 
166
165
  assert_raise MongoArgumentError do
167
- Mongo::Connection.from_uri("mongodb://kyle:password@localhost", :connect => false)
166
+ Mongo::Connection.from_uri("mongodb://kyle:@localhost", :connect => false)
167
+ end
168
+
169
+ assert_raise MongoArgumentError do
170
+ Mongo::Connection.from_uri("mongodb://kyle@localhost", :connect => false)
168
171
  end
169
172
  end
170
173
  end
171
174
 
172
175
  context "initializing with ENV['MONGODB_URI']" do
173
- setup do
174
- @old_mongodb_uri = ENV['MONGODB_URI']
176
+ should "parse a simple uri" do
177
+ uri = "mongodb://localhost?connect=false"
178
+ with_preserved_env_uri(uri) do
179
+ @connection = Mongo::Connection.new
180
+ assert_equal ['localhost', 27017], @connection.host_port
181
+ end
175
182
  end
176
183
 
177
- teardown do
178
- ENV['MONGODB_URI'] = @old_mongodb_uri
184
+ should "set auth source" do
185
+ uri = "mongodb://user:pass@localhost?authSource=foo&connect=false"
186
+ with_preserved_env_uri(uri) do
187
+ @connection = Mongo::Connection.new
188
+ assert_equal 'foo', @connection.auths.first[:source]
189
+ end
179
190
  end
180
191
 
181
- should "parse a simple uri" do
182
- ENV['MONGODB_URI'] = "mongodb://localhost?connect=false"
183
- @connection = Mongo::Connection.new
184
- assert_equal ['localhost', 27017], @connection.host_port
192
+ should "set auth mechanism" do
193
+ uri = "mongodb://user@localhost?authMechanism=MONGODB-X509&connect=false"
194
+ with_preserved_env_uri(uri) do
195
+ @connection = Mongo::Connection.new
196
+ assert_equal 'MONGODB-X509', @connection.auths.first[:mechanism]
197
+
198
+ ENV['MONGODB_URI'] = "mongodb://user@localhost?authMechanism=INVALID&connect=false"
199
+ assert_raise MongoArgumentError do
200
+ Mongo::Connection.new
201
+ end
202
+ end
185
203
  end
186
204
 
187
205
  should "allow a complex host names" do
188
206
  host_name = "foo.bar-12345.org"
189
- ENV['MONGODB_URI'] = "mongodb://#{host_name}?connect=false"
190
- @connection = Mongo::Connection.new
191
- assert_equal [host_name, 27017], @connection.host_port
207
+ uri = "mongodb://#{host_name}?connect=false"
208
+ with_preserved_env_uri(uri) do
209
+ @connection = Mongo::Connection.new
210
+ assert_equal [host_name, 27017], @connection.host_port
211
+ end
192
212
  end
193
213
 
194
214
  should "allow db without username and password" do
195
215
  host_name = "foo.bar-12345.org"
196
- ENV['MONGODB_URI'] = "mongodb://#{host_name}/foo?connect=false"
197
- @connection = Mongo::Connection.new
198
- assert_equal [host_name, 27017], @connection.host_port
216
+ uri = "mongodb://#{host_name}/foo?connect=false"
217
+ with_preserved_env_uri(uri) do
218
+ @connection = Mongo::Connection.new
219
+ assert_equal [host_name, 27017], @connection.host_port
220
+ end
199
221
  end
200
222
 
201
223
  should "set safe options on connection" do
202
224
  host_name = "localhost"
203
225
  opts = "safe=true&w=2&wtimeoutMS=1000&fsync=true&journal=true&connect=false"
204
- ENV['MONGODB_URI'] = "mongodb://#{host_name}/foo?#{opts}"
205
- @connection = Mongo::Connection.new
206
- assert_equal({:w => 2, :wtimeout => 1000, :fsync => true, :j => true}, @connection.safe)
226
+ uri = "mongodb://#{host_name}/foo?#{opts}"
227
+ with_preserved_env_uri(uri) do
228
+ @connection = Mongo::Connection.new
229
+ assert_equal({:w => 2, :wtimeout => 1000, :fsync => true, :j => true}, @connection.safe)
230
+ end
207
231
  end
208
232
 
209
233
  should "set timeout options on connection" do
210
234
  host_name = "localhost"
211
235
  opts = "connectTimeoutMS=1000&socketTimeoutMS=5000&connect=false"
212
- ENV['MONGODB_URI'] = "mongodb://#{host_name}/foo?#{opts}"
213
- @connection = Mongo::Connection.new
214
- assert_equal 1, @connection.connect_timeout
215
- assert_equal 5, @connection.op_timeout
236
+ uri = "mongodb://#{host_name}/foo?#{opts}"
237
+ with_preserved_env_uri(uri) do
238
+ @connection = Mongo::Connection.new
239
+ assert_equal 1, @connection.connect_timeout
240
+ assert_equal 5, @connection.op_timeout
241
+ end
216
242
  end
217
243
 
218
244
  should "parse a uri with a hyphen & underscore in the username or password" do
219
- ENV['MONGODB_URI'] = "mongodb://hyphen-user_name:p-s_s@localhost:27017/db?connect=false"
220
- @connection = Mongo::Connection.new
221
- assert_equal ['localhost', 27017], @connection.host_port
222
- auth_hash = { :db_name => 'db', :username => 'hyphen-user_name', :password => 'p-s_s' }
223
- assert_equal auth_hash, @connection.auths[0]
245
+ uri = "mongodb://hyphen-user_name:p-s_s@localhost:27017/db?connect=false"
246
+ with_preserved_env_uri(uri) do
247
+ @connection = Mongo::Connection.new
248
+ assert_equal ['localhost', 27017], @connection.host_port
249
+
250
+ auth_hash = {
251
+ :db_name => 'db',
252
+ :username => 'hyphen-user_name',
253
+ :password => 'p-s_s',
254
+ :source => 'db',
255
+ :mechanism => Authentication::DEFAULT_MECHANISM
256
+ }
257
+ assert_equal auth_hash, @connection.auths.first
258
+ end
224
259
  end
225
260
 
226
261
  should "attempt to connect" do
227
262
  TCPSocket.stubs(:new).returns(new_mock_socket)
228
- ENV['MONGODB_URI'] = "mongodb://localhost?connect=false" # connect=false ??
229
- @connection = Mongo::Connection.new
230
-
231
- admin_db = new_mock_db
232
- admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1})
233
- @connection.expects(:[]).with('admin').returns(admin_db)
234
- @connection.connect
263
+ uri = "mongodb://localhost?connect=false"
264
+ with_preserved_env_uri(uri) do
265
+ @connection = Mongo::Connection.new
266
+
267
+ admin_db = new_mock_db
268
+ admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1})
269
+ @connection.expects(:[]).with('admin').returns(admin_db)
270
+ @connection.connect
271
+ end
235
272
  end
236
273
 
237
274
  should "raise an error on invalid uris" do
238
- ENV['MONGODB_URI'] = "mongo://localhost"
239
- assert_raise MongoArgumentError do
240
- Mongo::Connection.new
275
+ uri = "mongo://localhost"
276
+ with_preserved_env_uri(uri) do
277
+ assert_raise MongoArgumentError do
278
+ Mongo::Connection.new
279
+ end
280
+
281
+ ENV['MONGODB_URI'] = "mongodb://localhost:abc"
282
+ assert_raise MongoArgumentError do
283
+ Mongo::Connection.new
284
+ end
241
285
  end
286
+ end
242
287
 
243
- ENV['MONGODB_URI'] = "mongodb://localhost:abc"
244
- assert_raise MongoArgumentError do
245
- Mongo::Connection.new
288
+ should "require password if using legacy auth and username present" do
289
+ uri = "mongodb://kyle:jones@localhost/db?connect=false"
290
+ with_preserved_env_uri(uri) do
291
+ assert Mongo::Connection.new
292
+
293
+ ENV['MONGODB_URI'] = "mongodb://kyle:@localhost?connect=false"
294
+ assert_raise MongoArgumentError do
295
+ Mongo::Connection.new
296
+ end
297
+
298
+ ENV['MONGODB_URI'] = "mongodb://kyle@localhost?connect=false"
299
+ assert_raise MongoArgumentError do
300
+ Mongo::Connection.new
301
+ end
246
302
  end
247
303
  end
248
304
 
249
- should "require all of username, if password and db are specified" do
250
- ENV['MONGODB_URI'] = "mongodb://kyle:jones@localhost/db?connect=false"
251
- assert Mongo::Connection.new
305
+ should "require password if using PLAIN auth and username present" do
306
+ uri = "mongodb://kyle:jones@localhost/db?connect=false&authMechanism=PLAIN"
307
+ with_preserved_env_uri(uri) do
308
+ assert Mongo::Connection.new
252
309
 
253
- ENV['MONGODB_URI'] = "mongodb://kyle:password@localhost"
254
- assert_raise MongoArgumentError do
255
- Mongo::Connection.new
310
+ ENV['MONGODB_URI'] = "mongodb://kyle:@localhost?connect=false&authMechanism=PLAIN"
311
+ assert_raise MongoArgumentError do
312
+ Mongo::Connection.new
313
+ end
314
+
315
+ ENV['MONGODB_URI'] = "mongodb://kyle@localhost?connect=false&authMechanism=PLAIN"
316
+ assert_raise MongoArgumentError do
317
+ Mongo::Connection.new
318
+ end
256
319
  end
257
320
  end
258
321
  end