kbaum-mongo 0.18.3p → 0.18.3.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +33 -22
- data/Rakefile +5 -0
- data/examples/gridfs.rb +25 -70
- data/lib/mongo/collection.rb +5 -1
- data/lib/mongo/connection.rb +67 -25
- data/lib/mongo/db.rb +18 -6
- data/lib/mongo/gridfs/grid_io.rb +4 -3
- data/lib/mongo.rb +1 -1
- data/test/auxillary/authentication_test.rb +68 -0
- data/test/auxillary/autoreconnect_test.rb +1 -2
- data/test/connection_test.rb +33 -3
- data/test/grid_store_test.rb +5 -2
- data/test/unit/connection_test.rb +5 -2
- metadata +52 -51
data/README.rdoc
CHANGED
@@ -70,40 +70,51 @@ Here's how to start MongoDB and run the "simple.rb" example:
|
|
70
70
|
|
71
71
|
See also the test code, especially test/test_db_api.rb.
|
72
72
|
|
73
|
-
=
|
73
|
+
= GridFS
|
74
74
|
|
75
|
-
The GridStore class
|
76
|
-
|
77
|
-
details, and see examples/gridfs.rb for code that uses many of the GridStore
|
78
|
-
features (metadata, content type, rewind/seek/tell, etc).
|
75
|
+
Note: The GridStore class has been deprecated. Use either the Grid or GridFileSystem
|
76
|
+
classes to take advantage of GridFS.
|
79
77
|
|
80
|
-
|
81
|
-
|
78
|
+
The Ruby driver include two abstractions for storing large files: Grid and GridFileSystem.
|
79
|
+
The Grid class is a Ruby implementation of MongoDB's GridFS file storage
|
80
|
+
specification. GridFileSystem is essentailly the same, but provides a more filesystem-like API
|
81
|
+
and assumes that filenames are unique.
|
82
82
|
|
83
|
-
|
83
|
+
An instance of both classes represents an individual file store. See the API reference
|
84
|
+
for details, and see examples/gridfs.rb for code that uses many of the Grid
|
85
|
+
features (metadata, content type, seek, tell, etc).
|
84
86
|
|
85
|
-
|
87
|
+
Examples:
|
88
|
+
include Mongo
|
89
|
+
|
90
|
+
# Get a database
|
91
|
+
db = Mongo::Connection.new.db('app-db')
|
86
92
|
|
87
|
-
# Store the text "Hello, world!" in the
|
88
|
-
|
89
|
-
|
93
|
+
# GridFileSystem. Store the text "Hello, world!" in the fs.
|
94
|
+
fs = GridFileSystem.new(db)
|
95
|
+
fs.open('filename', 'w') do |f|
|
96
|
+
f.write "Hello, world!"
|
90
97
|
end
|
91
98
|
|
92
|
-
# Output "Hello, world!"
|
93
|
-
|
99
|
+
# GridFileSystem. Output "Hello, world!"
|
100
|
+
fs = GridFileSystem.new(db)
|
101
|
+
fs.open('filename', 'r') do |f|
|
94
102
|
puts f.read
|
95
103
|
end
|
96
104
|
|
97
|
-
#
|
98
|
-
|
99
|
-
|
100
|
-
|
105
|
+
# Write a file on disk to the Grid
|
106
|
+
file = File.open('image.jpg')
|
107
|
+
grid = GridFileSystem.new(db)
|
108
|
+
id = grid.put(file)
|
101
109
|
|
102
|
-
# Retrieve
|
103
|
-
|
104
|
-
|
105
|
-
end
|
110
|
+
# Retrieve the file
|
111
|
+
file = grid.get(id)
|
112
|
+
file.read
|
106
113
|
|
114
|
+
# Get all the file's metata
|
115
|
+
file.filename
|
116
|
+
file.content_type
|
117
|
+
file.metadata
|
107
118
|
|
108
119
|
= Notes
|
109
120
|
|
data/Rakefile
CHANGED
@@ -81,6 +81,11 @@ namespace :test do
|
|
81
81
|
t.verbose = true
|
82
82
|
end
|
83
83
|
|
84
|
+
Rake::TestTask.new(:authentication) do |t|
|
85
|
+
t.test_files = FileList['test/auxillary/authentication_test.rb']
|
86
|
+
t.verbose = true
|
87
|
+
end
|
88
|
+
|
84
89
|
task :drop_databases do |t|
|
85
90
|
puts "Dropping test database..."
|
86
91
|
require File.join(File.dirname(__FILE__), 'lib', 'mongo')
|
data/examples/gridfs.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
def assert
|
3
|
+
raise "Failed!" unless yield
|
4
|
+
end
|
2
5
|
|
3
6
|
require 'mongo'
|
4
|
-
require 'mongo/gridfs'
|
5
|
-
|
6
7
|
include Mongo
|
7
|
-
include GridFS
|
8
8
|
|
9
9
|
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
10
10
|
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
|
@@ -12,77 +12,32 @@ port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
|
|
12
12
|
puts "Connecting to #{host}:#{port}"
|
13
13
|
db = Connection.new(host, port).db('ruby-mongo-examples')
|
14
14
|
|
15
|
-
|
16
|
-
GridStore.open(db, fname, 'r') { |f| puts f.read }
|
17
|
-
end
|
18
|
-
|
19
|
-
# Write a new file
|
20
|
-
GridStore.open(db, 'foobar', 'w') { |f| f.write("hello, world!") }
|
21
|
-
|
22
|
-
# Read it and print out the contents
|
23
|
-
dump(db, 'foobar')
|
24
|
-
|
25
|
-
# Append more data
|
26
|
-
GridStore.open(db, 'foobar', 'w+') { |f| f.write("\n"); f.puts "line two" }
|
27
|
-
dump(db, 'foobar')
|
15
|
+
data = "hello, world!"
|
28
16
|
|
29
|
-
|
30
|
-
GridStore.open(db, 'foobar', 'w') { |f| f.puts "hello, sailor!" }
|
31
|
-
dump(db, 'foobar')
|
17
|
+
grid = Grid.new(db)
|
32
18
|
|
33
|
-
#
|
34
|
-
|
35
|
-
puts "File 'does-not-exist' exists: #{GridStore.exist?(db, 'does-not-exist')}"
|
19
|
+
# Write a new file. data can be a string or an io object responding to #read.
|
20
|
+
id = grid.put(data, 'hello.txt')
|
36
21
|
|
37
|
-
# Read
|
38
|
-
|
22
|
+
# Read it and print out the contents
|
23
|
+
file = grid.get(id)
|
24
|
+
puts file.read
|
39
25
|
|
40
|
-
#
|
41
|
-
|
42
|
-
f.write "hello, world!"
|
43
|
-
f.rewind
|
44
|
-
f.write "xyzzz"
|
45
|
-
puts f.tell # => 5
|
46
|
-
f.seek(4)
|
47
|
-
f.write('y')
|
48
|
-
}
|
49
|
-
dump(db, 'foobar') # => 'xyzzy'
|
26
|
+
# Delete the file
|
27
|
+
grid.delete(id)
|
50
28
|
|
51
|
-
|
52
|
-
|
53
|
-
|
29
|
+
begin
|
30
|
+
grid.get(id)
|
31
|
+
rescue => e
|
32
|
+
assert {e.class == Mongo::GridError}
|
33
|
+
end
|
54
34
|
|
55
35
|
# Metadata
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
# Add some metadata; change content type
|
65
|
-
GridStore.open(db, 'foobar', 'w+') { |f|
|
66
|
-
f.content_type = 'text/xml'
|
67
|
-
f.metadata = {'a' => 1}
|
68
|
-
}
|
69
|
-
# Print it
|
70
|
-
GridStore.open(db, 'foobar', 'r') { |f|
|
71
|
-
puts f.content_type
|
72
|
-
puts f.upload_date
|
73
|
-
puts f.chunk_size
|
74
|
-
puts f.metadata.inspect
|
75
|
-
}
|
76
|
-
|
77
|
-
# You can also set metadata when initially writing the file. Setting :root
|
78
|
-
# means that the file and its chunks are stored in a different root
|
79
|
-
# collection: instead of gridfs.files and gridfs.chunks, here we use
|
80
|
-
# my_files.files and my_files.chunks.
|
81
|
-
GridStore.open(db, 'foobar', 'w',
|
82
|
-
:content_type => 'text/plain',
|
83
|
-
:metadata => {'a' => 1},
|
84
|
-
:chunk_size => 1024 * 4,
|
85
|
-
:root => 'my_files') { |f|
|
86
|
-
f.puts 'hello, world'
|
87
|
-
}
|
88
|
-
|
36
|
+
id = grid.put(data, 'hello.txt', :content_type => 'text/plain', :metadata => {'name' => 'hello'})
|
37
|
+
file = grid.get(id)
|
38
|
+
|
39
|
+
p file.content_type
|
40
|
+
p file.metadata.inspect
|
41
|
+
p file.chunk_size
|
42
|
+
p file.file_length
|
43
|
+
p file.data
|
data/lib/mongo/collection.rb
CHANGED
@@ -338,7 +338,11 @@ module Mongo
|
|
338
338
|
:ns => "#{@db.name}.#{@name}",
|
339
339
|
:key => field_h,
|
340
340
|
:unique => unique }
|
341
|
-
|
341
|
+
begin
|
342
|
+
insert_documents([sel], Mongo::DB::SYSTEM_INDEX_COLLECTION, false, true)
|
343
|
+
rescue Mongo::OperationFailure
|
344
|
+
raise Mongo::OperationFailure, "Failed to create index #{sel.inspect}."
|
345
|
+
end
|
342
346
|
name
|
343
347
|
end
|
344
348
|
|
data/lib/mongo/connection.rb
CHANGED
@@ -90,8 +90,10 @@ module Mongo
|
|
90
90
|
#
|
91
91
|
# @core connections
|
92
92
|
def initialize(pair_or_host=nil, port=nil, options={})
|
93
|
+
@auths = []
|
94
|
+
|
93
95
|
if block_given?
|
94
|
-
@nodes
|
96
|
+
@nodes = yield self
|
95
97
|
else
|
96
98
|
@nodes = format_pair(pair_or_host, port)
|
97
99
|
end
|
@@ -126,10 +128,7 @@ module Mongo
|
|
126
128
|
@options = options
|
127
129
|
|
128
130
|
should_connect = options[:connect].nil? ? true : options[:connect]
|
129
|
-
if should_connect
|
130
|
-
connect_to_master
|
131
|
-
authenticate_databases if @auths
|
132
|
-
end
|
131
|
+
connect_to_master if should_connect
|
133
132
|
end
|
134
133
|
|
135
134
|
# Initialize a paired connection to MongoDB.
|
@@ -154,7 +153,7 @@ module Mongo
|
|
154
153
|
# Block returns an array, the first element being an array of nodes and the second an array
|
155
154
|
# of authorizations for the database.
|
156
155
|
new(nil, nil, opts) do |con|
|
157
|
-
[
|
156
|
+
[con.pair_val_to_connection(nodes[0]), con.pair_val_to_connection(nodes[1])]
|
158
157
|
end
|
159
158
|
end
|
160
159
|
|
@@ -172,12 +171,68 @@ module Mongo
|
|
172
171
|
end
|
173
172
|
end
|
174
173
|
|
174
|
+
# Apply each of the saved database authentications.
|
175
|
+
#
|
176
|
+
# @return [Boolean] returns true if authentications exist and succeeed, false
|
177
|
+
# if none exists.
|
178
|
+
#
|
179
|
+
# @raise [AuthenticationError] raises an exception if any one
|
180
|
+
# authentication fails.
|
181
|
+
def apply_saved_authentication
|
182
|
+
return false if @auths.empty?
|
183
|
+
@auths.each do |auth|
|
184
|
+
self[auth['db_name']].authenticate(auth['username'], auth['password'], false)
|
185
|
+
end
|
186
|
+
true
|
187
|
+
end
|
188
|
+
|
189
|
+
# Save an authentication to this connection. When connecting,
|
190
|
+
# the connection will attempt to re-authenticate on every db
|
191
|
+
# specificed in the list of auths.
|
192
|
+
#
|
193
|
+
# @param [String] db_name
|
194
|
+
# @param [String] username
|
195
|
+
# @param [String] password
|
196
|
+
#
|
197
|
+
# @return [Hash] a hash representing the authentication just added.
|
198
|
+
def add_auth(db_name, username, password)
|
199
|
+
remove_auth(db_name)
|
200
|
+
auth = {}
|
201
|
+
auth['db_name'] = db_name
|
202
|
+
auth['username'] = username
|
203
|
+
auth['password'] = password
|
204
|
+
@auths << auth
|
205
|
+
auth
|
206
|
+
end
|
207
|
+
|
208
|
+
# Remove a saved authentication for this connection.
|
209
|
+
#
|
210
|
+
# @param [String] db_name
|
211
|
+
#
|
212
|
+
# @return [Boolean]
|
213
|
+
def remove_auth(db_name)
|
214
|
+
return unless @auths
|
215
|
+
if @auths.reject! { |a| a['db_name'] == db_name }
|
216
|
+
true
|
217
|
+
else
|
218
|
+
false
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
# Remove all authenication information stored in this connection.
|
223
|
+
#
|
224
|
+
# @return [true] this operation return true because it always succeeds.
|
225
|
+
def clear_auths
|
226
|
+
@auths = []
|
227
|
+
true
|
228
|
+
end
|
229
|
+
|
175
230
|
# Return a hash with all database names
|
176
231
|
# and their respective sizes on disk.
|
177
232
|
#
|
178
233
|
# @return [Hash]
|
179
234
|
def database_info
|
180
|
-
doc = self['admin'].command(:listDatabases => 1)
|
235
|
+
doc = self['admin'].command({:listDatabases => 1}, false, true)
|
181
236
|
returning({}) do |info|
|
182
237
|
doc['databases'].each { |db| info[db['name']] = db['sizeOnDisk'].to_i }
|
183
238
|
end
|
@@ -232,7 +287,7 @@ module Mongo
|
|
232
287
|
oh[:fromhost] = from_host
|
233
288
|
oh[:fromdb] = from
|
234
289
|
oh[:todb] = to
|
235
|
-
self["admin"].command(oh)
|
290
|
+
self["admin"].command(oh, false, true)
|
236
291
|
end
|
237
292
|
|
238
293
|
# Increment and return the next available request id.
|
@@ -250,7 +305,7 @@ module Mongo
|
|
250
305
|
#
|
251
306
|
# @return [Hash]
|
252
307
|
def server_info
|
253
|
-
|
308
|
+
self["admin"].command({:buildinfo => 1}, false, true)
|
254
309
|
end
|
255
310
|
|
256
311
|
# Get the build version of the current server.
|
@@ -365,6 +420,7 @@ module Mongo
|
|
365
420
|
result = self['admin'].command({:ismaster => 1}, false, false, socket)
|
366
421
|
if result['ok'] == 1 && ((is_master = result['ismaster'] == 1) || @slave_ok)
|
367
422
|
@host, @port = host, port
|
423
|
+
apply_saved_authentication
|
368
424
|
end
|
369
425
|
|
370
426
|
# Note: slave_ok can be true only when connecting to a single node.
|
@@ -470,13 +526,13 @@ module Mongo
|
|
470
526
|
raise MongoArgumentError, "MongoDB URI must include all three of username, password, " +
|
471
527
|
"and db if any one of these is specified."
|
472
528
|
else
|
473
|
-
|
529
|
+
add_auth(db, uname, pwd)
|
474
530
|
end
|
475
531
|
|
476
532
|
nodes << [host, port]
|
477
533
|
end
|
478
534
|
|
479
|
-
|
535
|
+
nodes
|
480
536
|
end
|
481
537
|
|
482
538
|
private
|
@@ -654,19 +710,5 @@ module Mongo
|
|
654
710
|
end
|
655
711
|
message
|
656
712
|
end
|
657
|
-
|
658
|
-
# Authenticate for any auth info provided on instantiating the connection.
|
659
|
-
# Only called when a MongoDB URI has been used to instantiate the connection, and
|
660
|
-
# when that connection specifies databases and authentication credentials.
|
661
|
-
#
|
662
|
-
# @raise [MongoDBError]
|
663
|
-
def authenticate_databases
|
664
|
-
@auths.each do |auth|
|
665
|
-
user = auth[0]
|
666
|
-
pwd = auth[1]
|
667
|
-
db_name = auth[2]
|
668
|
-
self.db(db_name).authenticate(user, pwd)
|
669
|
-
end
|
670
|
-
end
|
671
713
|
end
|
672
714
|
end
|
data/lib/mongo/db.rb
CHANGED
@@ -82,7 +82,7 @@ module Mongo
|
|
82
82
|
# @raise [AuthenticationError]
|
83
83
|
#
|
84
84
|
# @core authenticate authenticate-instance_method
|
85
|
-
def authenticate(username, password)
|
85
|
+
def authenticate(username, password, save_authorization=true)
|
86
86
|
doc = command(:getnonce => 1)
|
87
87
|
raise "error retrieving nonce: #{doc}" unless ok?(doc)
|
88
88
|
nonce = doc['nonce']
|
@@ -92,8 +92,14 @@ module Mongo
|
|
92
92
|
auth['user'] = username
|
93
93
|
auth['nonce'] = nonce
|
94
94
|
auth['key'] = Digest::MD5.hexdigest("#{nonce}#{username}#{hash_password(username, password)}")
|
95
|
-
ok?(command(auth))
|
96
|
-
|
95
|
+
if ok?(command(auth))
|
96
|
+
if save_authorization
|
97
|
+
@connection.add_auth(@name, username, password)
|
98
|
+
end
|
99
|
+
true
|
100
|
+
else
|
101
|
+
raise(Mongo::AuthenticationError, "Failed to authenticate user '#{username}' on db '#{self.name}'")
|
102
|
+
end
|
97
103
|
end
|
98
104
|
|
99
105
|
# Adds a user to this database for use with authentication. If the user already
|
@@ -125,15 +131,21 @@ module Mongo
|
|
125
131
|
end
|
126
132
|
end
|
127
133
|
|
128
|
-
# Deauthorizes use for this database for this connection.
|
134
|
+
# Deauthorizes use for this database for this connection. Also removes
|
135
|
+
# any saved authorization in the connection class associated with this
|
136
|
+
# database.
|
129
137
|
#
|
130
138
|
# @raise [MongoDBError] if logging out fails.
|
131
139
|
#
|
132
140
|
# @return [Boolean]
|
133
141
|
def logout
|
134
142
|
doc = command(:logout => 1)
|
135
|
-
|
136
|
-
|
143
|
+
if ok?(doc)
|
144
|
+
@connection.remove_auth(@name)
|
145
|
+
true
|
146
|
+
else
|
147
|
+
raise MongoDBError, "error logging out: #{doc.inspect}"
|
148
|
+
end
|
137
149
|
end
|
138
150
|
|
139
151
|
# Get an array of collection names in this database.
|
data/lib/mongo/gridfs/grid_io.rb
CHANGED
@@ -29,7 +29,7 @@ module Mongo
|
|
29
29
|
DEFAULT_CONTENT_TYPE = 'binary/octet-stream'
|
30
30
|
|
31
31
|
attr_reader :content_type, :chunk_size, :upload_date, :files_id, :filename,
|
32
|
-
:metadata, :server_md5, :client_md5
|
32
|
+
:metadata, :server_md5, :client_md5, :file_length
|
33
33
|
|
34
34
|
# Create a new GridIO object. Note that most users will not need to use this class directly;
|
35
35
|
# the Grid and GridFileSystem classes will instantiate this class
|
@@ -289,8 +289,9 @@ module Mongo
|
|
289
289
|
@files_id = opts[:_id] || Mongo::ObjectID.new
|
290
290
|
@content_type = opts[:content_type] || (defined? MIME) && get_content_type || DEFAULT_CONTENT_TYPE
|
291
291
|
@chunk_size = opts[:chunk_size] || DEFAULT_CHUNK_SIZE
|
292
|
-
@file_length = 0
|
293
292
|
@metadata = opts[:metadata] if opts[:metadata]
|
293
|
+
@aliases = opts[:aliases] if opts[:aliases]
|
294
|
+
@file_length = 0
|
294
295
|
|
295
296
|
@current_chunk = create_chunk(0)
|
296
297
|
@file_position = 0
|
@@ -319,7 +320,7 @@ module Mongo
|
|
319
320
|
if @safe
|
320
321
|
@client_md5 = @local_md5.hexdigest
|
321
322
|
if @local_md5 != @server_md5
|
322
|
-
raise
|
323
|
+
raise GridError, "File on server failed MD5 check"
|
323
324
|
end
|
324
325
|
else
|
325
326
|
@server_md5
|
data/lib/mongo.rb
CHANGED
@@ -0,0 +1,68 @@
|
|
1
|
+
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
require 'mongo'
|
3
|
+
require 'test/unit'
|
4
|
+
require 'test/test_helper'
|
5
|
+
|
6
|
+
# NOTE: This test requires bouncing the server.
|
7
|
+
# It also requires that a user exists on the admin database.
|
8
|
+
class AuthenticationTest < Test::Unit::TestCase
|
9
|
+
include Mongo
|
10
|
+
|
11
|
+
def setup
|
12
|
+
@conn = Mongo::Connection.new
|
13
|
+
@db1 = @conn.db('mongo-ruby-test-auth1')
|
14
|
+
@db2 = @conn.db('mongo-ruby-test-auth2')
|
15
|
+
@admin = @conn.db('admin')
|
16
|
+
end
|
17
|
+
|
18
|
+
def teardown
|
19
|
+
@db1.authenticate('user1', 'secret')
|
20
|
+
@db2.authenticate('user2', 'secret')
|
21
|
+
@conn.drop_database('mongo-ruby-test-auth1')
|
22
|
+
@conn.drop_database('mongo-ruby-test-auth2')
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_authenticate
|
26
|
+
@admin.authenticate('bob', 'secret')
|
27
|
+
@db1.add_user('user1', 'secret')
|
28
|
+
@db2.add_user('user2', 'secret')
|
29
|
+
@admin.logout
|
30
|
+
|
31
|
+
assert_raise Mongo::OperationFailure do
|
32
|
+
@db1['stuff'].insert({:a => 2}, :safe => true)
|
33
|
+
end
|
34
|
+
|
35
|
+
assert_raise Mongo::OperationFailure do
|
36
|
+
@db2['stuff'].insert({:a => 2}, :safe => true)
|
37
|
+
end
|
38
|
+
|
39
|
+
@db1.authenticate('user1', 'secret')
|
40
|
+
@db2.authenticate('user2', 'secret')
|
41
|
+
|
42
|
+
assert @db1['stuff'].insert({:a => 2}, :safe => true)
|
43
|
+
assert @db2['stuff'].insert({:a => 2}, :safe => true)
|
44
|
+
|
45
|
+
puts "Please bounce the server."
|
46
|
+
gets
|
47
|
+
|
48
|
+
# Here we reconnect.
|
49
|
+
begin
|
50
|
+
@db1['stuff'].find.to_a
|
51
|
+
rescue Mongo::ConnectionFailure
|
52
|
+
end
|
53
|
+
|
54
|
+
assert @db1['stuff'].insert({:a => 2}, :safe => true)
|
55
|
+
assert @db2['stuff'].insert({:a => 2}, :safe => true)
|
56
|
+
|
57
|
+
@db1.logout
|
58
|
+
assert_raise Mongo::OperationFailure do
|
59
|
+
@db1['stuff'].insert({:a => 2}, :safe => true)
|
60
|
+
end
|
61
|
+
|
62
|
+
@db2.logout
|
63
|
+
assert_raise Mongo::OperationFailure do
|
64
|
+
assert @db2['stuff'].insert({:a => 2}, :safe => true)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
@@ -29,7 +29,7 @@ class AutoreconnectTest < Test::Unit::TestCase
|
|
29
29
|
|
30
30
|
begin
|
31
31
|
@coll.find.to_a
|
32
|
-
rescue Mongo::ConnectionFailure
|
32
|
+
rescue Mongo::ConnectionFailure
|
33
33
|
end
|
34
34
|
|
35
35
|
results = []
|
@@ -38,5 +38,4 @@ class AutoreconnectTest < Test::Unit::TestCase
|
|
38
38
|
assert results.any? {|r| r['a'] == a}, "Could not find record for a => #{a}"
|
39
39
|
end
|
40
40
|
end
|
41
|
-
|
42
41
|
end
|
data/test/connection_test.rb
CHANGED
@@ -69,8 +69,6 @@ class TestConnection < Test::Unit::TestCase
|
|
69
69
|
assert_kind_of Array, names
|
70
70
|
assert names.length >= 1
|
71
71
|
assert names.include?('ruby-mongo-info-test')
|
72
|
-
|
73
|
-
@mongo.drop_database('ruby-mongo-info-test')
|
74
72
|
end
|
75
73
|
|
76
74
|
def test_logging
|
@@ -80,7 +78,7 @@ class TestConnection < Test::Unit::TestCase
|
|
80
78
|
db = Connection.new(@host, @port, :logger => logger).db('ruby-mongo-test')
|
81
79
|
assert output.string.include?("admin.$cmd.find")
|
82
80
|
end
|
83
|
-
|
81
|
+
|
84
82
|
def test_connection_logger
|
85
83
|
output = StringIO.new
|
86
84
|
logger = Logger.new(output)
|
@@ -124,6 +122,38 @@ class TestConnection < Test::Unit::TestCase
|
|
124
122
|
assert_equal ['foo', 123], nodes[1]
|
125
123
|
end
|
126
124
|
|
125
|
+
context "Saved authentications" do
|
126
|
+
setup do
|
127
|
+
@conn = Mongo::Connection.new
|
128
|
+
@auth = {'db_name' => 'test', 'username' => 'bob', 'password' => 'secret'}
|
129
|
+
@conn.add_auth(@auth['db_name'], @auth['username'], @auth['password'])
|
130
|
+
end
|
131
|
+
|
132
|
+
should "save the authentication" do
|
133
|
+
assert_equal @auth, @conn.auths[0]
|
134
|
+
end
|
135
|
+
|
136
|
+
should "replace the auth if given a new auth for the same db" do
|
137
|
+
auth = {'db_name' => 'test', 'username' => 'mickey', 'password' => 'm0u53'}
|
138
|
+
@conn.add_auth(auth['db_name'], auth['username'], auth['password'])
|
139
|
+
assert_equal 1, @conn.auths.length
|
140
|
+
assert_equal auth, @conn.auths[0]
|
141
|
+
end
|
142
|
+
|
143
|
+
should "remove auths by database" do
|
144
|
+
@conn.remove_auth('non-existent database')
|
145
|
+
assert_equal 1, @conn.auths.length
|
146
|
+
|
147
|
+
@conn.remove_auth('test')
|
148
|
+
assert_equal 0, @conn.auths.length
|
149
|
+
end
|
150
|
+
|
151
|
+
should "remove all auths" do
|
152
|
+
@conn.clear_auths
|
153
|
+
assert_equal 0, @conn.auths.length
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
127
157
|
context "Connection exceptions" do
|
128
158
|
setup do
|
129
159
|
@conn = Mongo::Connection.new('localhost', 27017, :pool_size => 10, :timeout => 10)
|
data/test/grid_store_test.rb
CHANGED
@@ -130,7 +130,6 @@ class GridStoreTest < Test::Unit::TestCase
|
|
130
130
|
}
|
131
131
|
|
132
132
|
assert_equal 3, @@chunks.count
|
133
|
-
#assert_equal ('x' * size) + ('y' * size) + ('z' * size), GridStore.read(@@db, 'biggie')
|
134
133
|
end
|
135
134
|
|
136
135
|
def test_binary
|
@@ -140,8 +139,12 @@ class GridStoreTest < Test::Unit::TestCase
|
|
140
139
|
end
|
141
140
|
|
142
141
|
file.rewind
|
142
|
+
data = file.read
|
143
|
+
if data.respond_to?(:force_encoding)
|
144
|
+
data.force_encoding(:binary)
|
145
|
+
end
|
143
146
|
GridStore.open(@@db, 'zip', 'r') do |f|
|
144
|
-
assert_equal
|
147
|
+
assert_equal data.length, f.read.length
|
145
148
|
end
|
146
149
|
end
|
147
150
|
|
@@ -75,8 +75,10 @@ class ConnectionTest < Test::Unit::TestCase
|
|
75
75
|
@conn = Connection.from_uri("mongodb://kyle:s3cr3t@localhost:27017/app,mickey:m0u5e@mydb.com:27018/dsny", :connect => false)
|
76
76
|
assert_equal ['localhost', 27017], @conn.nodes[0]
|
77
77
|
assert_equal ['mydb.com', 27018], @conn.nodes[1]
|
78
|
-
|
79
|
-
assert_equal
|
78
|
+
auth_hash = {'username' => 'kyle', 'password' => 's3cr3t', 'db_name' => 'app'}
|
79
|
+
assert_equal auth_hash, @conn.auths[0]
|
80
|
+
auth_hash = {'username' => 'mickey', 'password' => 'm0u5e', 'db_name' => 'dsny'}
|
81
|
+
assert_equal auth_hash, @conn.auths[1]
|
80
82
|
end
|
81
83
|
|
82
84
|
should "attempt to connect" do
|
@@ -86,6 +88,7 @@ class ConnectionTest < Test::Unit::TestCase
|
|
86
88
|
admin_db = new_mock_db
|
87
89
|
admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1})
|
88
90
|
@conn.expects(:[]).with('admin').returns(admin_db)
|
91
|
+
@conn.expects(:apply_saved_authentication)
|
89
92
|
@conn.connect_to_master
|
90
93
|
end
|
91
94
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kbaum-mongo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.18.
|
4
|
+
version: 0.18.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jim Menard
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2010-02-
|
13
|
+
date: 2010-02-26 00:00:00 -05:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|
@@ -27,43 +27,43 @@ files:
|
|
27
27
|
- Rakefile
|
28
28
|
- mongo-ruby-driver.gemspec
|
29
29
|
- LICENSE.txt
|
30
|
-
- lib/mongo/
|
31
|
-
- lib/mongo/
|
32
|
-
- lib/mongo/
|
33
|
-
- lib/mongo/util/bson_c.rb
|
34
|
-
- lib/mongo/util/ordered_hash.rb
|
35
|
-
- lib/mongo/util/conversions.rb
|
36
|
-
- lib/mongo/util/support.rb
|
30
|
+
- lib/mongo/collection.rb
|
31
|
+
- lib/mongo/connection.rb
|
32
|
+
- lib/mongo/cursor.rb
|
37
33
|
- lib/mongo/db.rb
|
38
|
-
- lib/mongo/types/binary.rb
|
39
|
-
- lib/mongo/types/code.rb
|
40
|
-
- lib/mongo/types/dbref.rb
|
41
|
-
- lib/mongo/types/objectid.rb
|
42
|
-
- lib/mongo/types/min_max_keys.rb
|
43
|
-
- lib/mongo/types/regexp_of_holding.rb
|
44
34
|
- lib/mongo/exceptions.rb
|
45
|
-
- lib/mongo/gridfs/grid_store.rb
|
46
35
|
- lib/mongo/gridfs/chunk.rb
|
47
|
-
- lib/mongo/gridfs/grid_file_system.rb
|
48
36
|
- lib/mongo/gridfs/grid.rb
|
37
|
+
- lib/mongo/gridfs/grid_file_system.rb
|
49
38
|
- lib/mongo/gridfs/grid_io.rb
|
50
|
-
- lib/mongo/
|
51
|
-
- lib/mongo/collection.rb
|
39
|
+
- lib/mongo/gridfs/grid_store.rb
|
52
40
|
- lib/mongo/gridfs.rb
|
53
|
-
- lib/mongo/
|
41
|
+
- lib/mongo/types/binary.rb
|
42
|
+
- lib/mongo/types/code.rb
|
43
|
+
- lib/mongo/types/dbref.rb
|
44
|
+
- lib/mongo/types/min_max_keys.rb
|
45
|
+
- lib/mongo/types/objectid.rb
|
46
|
+
- lib/mongo/types/regexp_of_holding.rb
|
47
|
+
- lib/mongo/util/bson_c.rb
|
48
|
+
- lib/mongo/util/bson_ruby.rb
|
49
|
+
- lib/mongo/util/byte_buffer.rb
|
50
|
+
- lib/mongo/util/conversions.rb
|
51
|
+
- lib/mongo/util/ordered_hash.rb
|
52
|
+
- lib/mongo/util/server_version.rb
|
53
|
+
- lib/mongo/util/support.rb
|
54
54
|
- lib/mongo.rb
|
55
|
-
- examples/
|
56
|
-
- examples/types.rb
|
55
|
+
- examples/admin.rb
|
57
56
|
- examples/capped.rb
|
57
|
+
- examples/cursor.rb
|
58
58
|
- examples/gridfs.rb
|
59
59
|
- examples/index_test.rb
|
60
|
-
- examples/cursor.rb
|
61
|
-
- examples/admin.rb
|
62
|
-
- examples/simple.rb
|
63
|
-
- examples/queries.rb
|
64
60
|
- examples/info.rb
|
65
|
-
-
|
61
|
+
- examples/queries.rb
|
62
|
+
- examples/simple.rb
|
63
|
+
- examples/strict.rb
|
64
|
+
- examples/types.rb
|
66
65
|
- bin/bson_benchmark.rb
|
66
|
+
- bin/fail_if_no_c.rb
|
67
67
|
has_rdoc: true
|
68
68
|
homepage: http://www.mongodb.org
|
69
69
|
licenses: []
|
@@ -83,9 +83,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
83
83
|
version:
|
84
84
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
85
|
requirements:
|
86
|
-
- - "
|
86
|
+
- - ">="
|
87
87
|
- !ruby/object:Gem::Version
|
88
|
-
version:
|
88
|
+
version: "0"
|
89
89
|
version:
|
90
90
|
requirements: []
|
91
91
|
|
@@ -95,33 +95,34 @@ signing_key:
|
|
95
95
|
specification_version: 3
|
96
96
|
summary: Ruby driver for the MongoDB
|
97
97
|
test_files:
|
98
|
-
- test/
|
99
|
-
- test/unit/db_test.rb
|
100
|
-
- test/unit/collection_test.rb
|
101
|
-
- test/unit/cursor_test.rb
|
102
|
-
- test/grid_test.rb
|
103
|
-
- test/replica/insert_test.rb
|
104
|
-
- test/replica/query_test.rb
|
105
|
-
- test/replica/pooled_insert_test.rb
|
106
|
-
- test/replica/count_test.rb
|
107
|
-
- test/grid_store_test.rb
|
108
|
-
- test/connection_test.rb
|
109
|
-
- test/objectid_test.rb
|
110
|
-
- test/db_test.rb
|
111
|
-
- test/threading_test.rb
|
98
|
+
- test/auxillary/authentication_test.rb
|
112
99
|
- test/auxillary/autoreconnect_test.rb
|
113
|
-
- test/
|
114
|
-
- test/grid_file_system_test.rb
|
115
|
-
- test/conversions_test.rb
|
116
|
-
- test/collection_test.rb
|
100
|
+
- test/binary_test.rb
|
117
101
|
- test/bson_test.rb
|
118
102
|
- test/byte_buffer_test.rb
|
119
103
|
- test/chunk_test.rb
|
104
|
+
- test/collection_test.rb
|
105
|
+
- test/connection_test.rb
|
106
|
+
- test/conversions_test.rb
|
120
107
|
- test/cursor_test.rb
|
121
|
-
- test/db_connection_test.rb
|
122
108
|
- test/db_api_test.rb
|
123
|
-
- test/
|
124
|
-
- test/
|
125
|
-
- test/
|
109
|
+
- test/db_connection_test.rb
|
110
|
+
- test/db_test.rb
|
111
|
+
- test/grid_file_system_test.rb
|
126
112
|
- test/grid_io_test.rb
|
113
|
+
- test/grid_store_test.rb
|
114
|
+
- test/grid_test.rb
|
115
|
+
- test/objectid_test.rb
|
127
116
|
- test/ordered_hash_test.rb
|
117
|
+
- test/replica/count_test.rb
|
118
|
+
- test/replica/insert_test.rb
|
119
|
+
- test/replica/pooled_insert_test.rb
|
120
|
+
- test/replica/query_test.rb
|
121
|
+
- test/slave_connection_test.rb
|
122
|
+
- test/test_helper.rb
|
123
|
+
- test/threading/test_threading_large_pool.rb
|
124
|
+
- test/threading_test.rb
|
125
|
+
- test/unit/collection_test.rb
|
126
|
+
- test/unit/connection_test.rb
|
127
|
+
- test/unit/cursor_test.rb
|
128
|
+
- test/unit/db_test.rb
|