Skip to content

Commit bafe537

Browse files
committed
RUBY-577: add new tests for delegated auth
1 parent 2f52be4 commit bafe537

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

test/shared/authentication.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,43 @@ def test_authenticate_non_existent_user
5757
end
5858
end
5959

60+
def test_delegated_authentication
61+
return if @client.server_version < '2.4'
62+
63+
doc = {'_id' => 'test'}
64+
# create accounts database to hold user credentials
65+
accounts = @client['accounts']
66+
accounts['system.users'].remove
67+
accounts.add_user('tyler', 'brock', nil, :roles => [])
68+
69+
# insert test data and give user permissions on test db
70+
@db['test'].remove
71+
@db['test'].insert(doc)
72+
@db.add_user('tyler', nil, nil, :roles => ['read'], :userSource => 'accounts')
73+
@admin.logout
74+
75+
# auth must occur on the db where the user is defined
76+
assert_raise Mongo::AuthenticationError do
77+
@db.authenticate('tyler', 'brock')
78+
end
79+
80+
# auth directly
81+
assert accounts.authenticate('tyler', 'brock')
82+
assert_equal doc, @db['test'].find_one
83+
accounts.logout
84+
assert_raise Mongo::OperationFailure do
85+
@db['test'].find_one
86+
end
87+
88+
# auth using source
89+
@db.authenticate('tyler', 'brock', true, 'accounts')
90+
assert_equal doc, @db['test'].find_one
91+
@db.logout
92+
assert_raise Mongo::OperationFailure do
93+
@db['test'].find_one
94+
end
95+
end
96+
6097
def test_logout
6198
@db.add_user('peggy', 'user')
6299
assert @db.authenticate('peggy', 'user')

0 commit comments

Comments
 (0)