Skip to content

Commit 61732c2

Browse files
Auth tests for the bulk API
1 parent 330c13c commit 61732c2

File tree

3 files changed

+265
-0
lines changed

3 files changed

+265
-0
lines changed

test/functional/authentication_test.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@
1515
require 'test_helper'
1616
require 'shared/authentication/basic_auth_shared'
1717
require 'shared/authentication/sasl_plain_shared'
18+
require 'shared/authentication/bulk_api_auth_shared'
1819

1920
class AuthenticationTest < Test::Unit::TestCase
2021
include Mongo
2122
include BasicAuthTests
2223
include SASLPlainTests
24+
include BulkAPIAuthTests
2325

2426
def setup
2527
@client = MongoClient.new(TEST_HOST, TEST_PORT)
28+
@version = @client.server_version
2629
@db = @client[TEST_DB]
2730
@host_info = host_port
2831
end

test/replica_set/authentication_test.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,18 @@
1515
require 'test_helper'
1616
require 'shared/authentication/basic_auth_shared'
1717
require 'shared/authentication/sasl_plain_shared'
18+
require 'shared/authentication/bulk_api_auth_shared'
1819

1920
class ReplicaSetAuthenticationTest < Test::Unit::TestCase
2021
include Mongo
2122
include BasicAuthTests
2223
include SASLPlainTests
24+
include BulkAPIAuthTests
2325

2426
def setup
2527
ensure_cluster(:rs)
2628
@client = MongoReplicaSetClient.new(@rs.repl_set_seeds)
29+
@version = @client.server_version
2730
@db = @client[TEST_DB]
2831
@host_info = @rs.repl_set_seeds.join(',')
2932
end
Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
# Copyright (C) 2009-2013 MongoDB, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License")
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0x
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
module BulkAPIAuthTests
16+
17+
include Mongo
18+
19+
def init_auth_bulk
20+
# enable authentication
21+
@admin = @client["admin"]
22+
@admin.add_user('admin', 'password', nil, :roles => ['readWriteAnyDatabase',
23+
'userAdminAnyDatabase',
24+
'dbAdminAnyDatabase'])
25+
@admin.authenticate('admin', 'password')
26+
27+
# Set up the test db
28+
@collection = @db["bulk-api-auth-tests"]
29+
30+
# db user can insert but not remove
31+
res = BSON::OrderedHash.new
32+
res[:db] = TEST_DB
33+
res[:collection] = ""
34+
35+
cmd = BSON::OrderedHash.new
36+
cmd[:createRole] = "insertOnly"
37+
cmd[:privileges] = [{:resource => res, :actions => [ "insert", "find" ]}]
38+
cmd[:roles] = []
39+
@db.command(cmd)
40+
@db.add_user('insertOnly', 'password', nil, :roles => ['insertOnly'])
41+
42+
# db user can insert and remove
43+
cmd = BSON::OrderedHash.new
44+
cmd[:createRole] = "insertAndRemove"
45+
cmd[:privileges] = [{:resource => res, :actions => [ "insert", "remove", "find" ]}]
46+
cmd[:roles] = []
47+
@db.command(cmd)
48+
@db.add_user('insertAndRemove', 'password', nil, :roles => ['insertAndRemove'])
49+
50+
# for 2.4 cleanup etc.
51+
@db.add_user('admin', 'password', nil, :roles => ['readWrite',
52+
'userAdmin',
53+
'dbAdmin'])
54+
@admin.logout
55+
end
56+
57+
def teardown_bulk
58+
remove_all_users_and_roles(@db, 'admin', 'password')
59+
remove_all_users_and_roles(@admin, 'admin', 'password')
60+
end
61+
62+
def clear_collection(collection)
63+
@admin.authenticate('admin', 'password')
64+
collection.remove
65+
@admin.logout
66+
end
67+
68+
def remove_all_users_and_roles(database, username, password)
69+
@admin.authenticate('admin', 'password')
70+
if @version < '2.5.3'
71+
database['system.users'].remove
72+
else
73+
database.command({:dropAllRolesFromDatabase => 1})
74+
database.command({:dropAllUsersFromDatabase => 1})
75+
end
76+
@admin.logout
77+
end
78+
79+
def test_auth_no_error
80+
return unless @version >= '2.5.3'
81+
init_auth_bulk
82+
with_write_commands_and_operations(@db.connection) do |wire_version|
83+
clear_collection(@collection)
84+
@db.authenticate('insertAndRemove', 'password')
85+
bulk = @collection.initialize_ordered_bulk_op
86+
bulk.insert({:a => 1})
87+
bulk.find({:a => 1}).remove_one
88+
89+
result = bulk.execute
90+
assert_match_document(
91+
{
92+
"ok" => 1,
93+
"nInserted" => 1,
94+
"nRemoved" => 1
95+
}, result, "wire_version:#{wire_version}")
96+
assert_equal 0, @collection.count
97+
@db.logout
98+
end
99+
teardown_bulk
100+
end
101+
102+
def test_auth_error
103+
return unless @version >= '2.5.3'
104+
init_auth_bulk
105+
with_write_commands_and_operations(@db.connection) do |wire_version|
106+
clear_collection(@collection)
107+
@db.authenticate('insertOnly', 'password')
108+
bulk = @collection.initialize_ordered_bulk_op
109+
bulk.insert({:a => 1})
110+
bulk.find({:a => 1}).remove
111+
bulk.insert({:a => 2})
112+
113+
ex = assert_raise Mongo::BulkWriteError do
114+
bulk.execute
115+
end
116+
result = ex.result
117+
assert_match_document(
118+
{
119+
"ok" => 1,
120+
"n" => 1,
121+
"writeErrors" =>
122+
[{
123+
"index" => 1,
124+
"code" => 13,
125+
"errmsg" => /not authorized/
126+
}],
127+
"code" => 65,
128+
"errmsg" => "batch item errors occurred",
129+
"nInserted" => 1
130+
}, result, "wire_version:#{wire_version}")
131+
assert_equal 1, @collection.count
132+
@db.logout
133+
end
134+
teardown_bulk
135+
end
136+
137+
def test_auth_error_unordered
138+
return unless @version >= '2.5.3'
139+
init_auth_bulk
140+
with_write_commands_and_operations(@db.connection) do |wire_version|
141+
clear_collection(@collection)
142+
@db.authenticate('insertOnly', 'password')
143+
bulk = @collection.initialize_unordered_bulk_op
144+
bulk.insert({:a => 1})
145+
bulk.find({:a => 1}).remove_one
146+
bulk.insert({:a => 2})
147+
148+
ex = assert_raise Mongo::BulkWriteError do
149+
bulk.execute
150+
end
151+
result = ex.result
152+
assert_equal 1, result["writeErrors"].length
153+
assert_equal 2, result["n"]
154+
assert_equal 2, result["nInserted"]
155+
assert_equal 2, @collection.count
156+
@db.logout
157+
end
158+
teardown_bulk
159+
end
160+
161+
def test_duplicate_key_with_auth_error
162+
return unless @version >= '2.5.3'
163+
init_auth_bulk
164+
with_write_commands_and_operations(@db.connection) do |wire_version|
165+
clear_collection(@collection)
166+
@db.authenticate('insertOnly', 'password')
167+
bulk = @collection.initialize_ordered_bulk_op
168+
bulk.insert({:_id => 1, :a => 1})
169+
bulk.insert({:_id => 1, :a => 2})
170+
bulk.find({:a => 1}).remove_one
171+
172+
ex = assert_raise Mongo::BulkWriteError do
173+
bulk.execute
174+
end
175+
result = ex.result
176+
assert_match_document(
177+
{
178+
"ok" => 1,
179+
"n" => 1,
180+
"writeErrors" =>
181+
[{
182+
"index" => 1,
183+
"code" => 11000,
184+
"errmsg" => /duplicate key error/
185+
}],
186+
"code" => 65,
187+
"errmsg" => "batch item errors occurred",
188+
"nInserted" => 1
189+
}, result, "wire_version:#{wire_version}")
190+
assert_equal 1, @collection.count
191+
@db.logout
192+
end
193+
teardown_bulk
194+
end
195+
196+
def test_duplicate_key_with_auth_error_unordered
197+
return unless @version >= '2.5.3'
198+
init_auth_bulk
199+
with_write_commands_and_operations(@db.connection) do |wire_version|
200+
clear_collection(@collection)
201+
@db.authenticate('insertOnly', 'password')
202+
bulk = @collection.initialize_unordered_bulk_op
203+
bulk.insert({:_id => 1, :a => 1})
204+
bulk.insert({:_id => 1, :a => 1})
205+
bulk.find({:a => 1}).remove_one
206+
207+
ex = assert_raise Mongo::BulkWriteError do
208+
bulk.execute
209+
end
210+
result = ex.result
211+
assert_equal 2, result["writeErrors"].length
212+
assert_equal 1, result["n"]
213+
assert_equal 1, result["nInserted"]
214+
assert_equal 1, @collection.count
215+
@db.logout
216+
end
217+
teardown_bulk
218+
end
219+
220+
def test_write_concern_error_with_auth_error
221+
with_no_replication(@db.connection) do
222+
return unless @version >= '2.5.3'
223+
init_auth_bulk
224+
with_write_commands_and_operations(@db.connection) do |wire_version|
225+
clear_collection(@collection)
226+
@db.authenticate('insertOnly', 'password')
227+
bulk = @collection.initialize_ordered_bulk_op
228+
bulk.insert({:_id => 1, :a => 1})
229+
bulk.insert({:_id => 2, :a => 1})
230+
bulk.find({:a => 1}).remove_one
231+
232+
ex = assert_raise Mongo::BulkWriteError do
233+
bulk.execute({:w => 2})
234+
end
235+
result = ex.result
236+
237+
assert_match_document(
238+
{
239+
"ok" => 0,
240+
"n" => 0,
241+
"nInserted" => 0,
242+
"writeErrors" =>
243+
[{
244+
"index" => 0,
245+
"code" => 2,
246+
"errmsg" => /'w' > 1/
247+
}],
248+
"code" => 65,
249+
"errmsg" => "batch item errors occurred"
250+
}, result, "wire_version#{wire_version}")
251+
# Re-visit this when RUBY-731 is resolved:
252+
assert (@collection.count == batch_commands?(wire_version) ? 0 : 1)
253+
@db.logout
254+
end
255+
teardown_bulk
256+
end
257+
end
258+
259+
end

0 commit comments

Comments
 (0)