1414
1515module BasicAuthTests
1616
17- def init_auth
17+ def init_auth_basic
1818 # enable authentication by creating and logging in as admin user
1919 @admin = @client [ 'admin' ]
2020 @admin . add_user ( 'admin' , 'password' , nil , :roles => [ 'readAnyDatabase' ,
@@ -28,7 +28,7 @@ def init_auth
2828 @db . add_user ( 'admin' , 'cleanup' , nil , :roles => [ ] )
2929 end
3030
31- def teardown
31+ def teardown_basic
3232 remove_all_users ( @db , 'admin' , 'cleanup' )
3333 remove_all_users ( @admin , 'admin' , 'password' ) if has_auth? ( @admin . name )
3434 end
@@ -48,15 +48,21 @@ def has_auth?(db_name)
4848 end
4949
5050 def test_add_remove_user
51+ init_auth_basic
52+
5153 # add user
5254 silently { @db . add_user ( 'bob' , 'user' ) }
5355 assert @db . authenticate ( 'bob' , 'user' )
5456
5557 # remove user
5658 assert @db . remove_user ( 'bob' )
59+
60+ teardown_basic
5761 end
5862
5963 def test_update_user
64+ init_auth_basic
65+
6066 # add user
6167 silently { @db . add_user ( 'bob' , 'user' ) }
6268 assert @db . authenticate ( 'bob' , 'user' )
@@ -68,47 +74,63 @@ def test_update_user
6874 @db . authenticate ( 'bob' , 'user' )
6975 end
7076 assert @db . authenticate ( 'bob' , 'updated' )
77+
78+ teardown_basic
7179 end
7280
7381 def test_remove_non_existent_user
82+ init_auth_basic
83+
7484 if @client . server_version < '2.5'
7585 assert_equal false , @db . remove_user ( 'joe' )
7686 else
7787 assert_raise Mongo ::OperationFailure do
7888 assert @db . remove_user ( 'joe' )
7989 end
8090 end
91+ teardown_basic
8192 end
8293
8394 def test_authenticate
95+ init_auth_basic
8496 silently { @db . add_user ( 'peggy' , 'user' ) }
8597 assert @db . authenticate ( 'peggy' , 'user' )
8698 @db . remove_user ( 'peggy' )
99+ teardown_basic
87100 end
88101
89102 def test_authenticate_non_existent_user
103+ init_auth_basic
90104 assert_raise Mongo ::AuthenticationError do
91105 @db . authenticate ( 'frank' , 'thetank' )
92106 end
107+ teardown_basic
93108 end
94109
95110 def test_logout
111+ init_auth_basic
96112 silently { @db . add_user ( 'peggy' , 'user' ) }
97113 assert @db . authenticate ( 'peggy' , 'user' )
98114 assert @db . logout
115+ teardown_basic
99116 end
100117
101118 def test_authenticate_with_special_characters
119+ init_auth_basic
102120 silently { assert @db . add_user ( 'foo:bar' , '@foo' ) }
103121 assert @db . authenticate ( 'foo:bar' , '@foo' )
122+ teardown_basic
104123 end
105124
106125 def test_authenticate_read_only
126+ init_auth_basic
107127 silently { @db . add_user ( 'randy' , 'readonly' , true ) }
108128 assert @db . authenticate ( 'randy' , 'readonly' )
129+ teardown_basic
109130 end
110131
111132 def test_authenticate_with_connection_uri
133+ init_auth_basic
112134 silently { @db . add_user ( 'eunice' , 'uritest' ) }
113135
114136 uri = "mongodb://eunice:uritest@#{ @host_info } /#{ @db . name } "
@@ -122,9 +144,11 @@ def test_authenticate_with_connection_uri
122144 assert_equal @db . name , auth [ :db_name ]
123145 assert_equal 'eunice' , auth [ :username ]
124146 assert_equal 'uritest' , auth [ :password ]
147+ teardown_basic
125148 end
126149
127150 def test_socket_auths
151+ init_auth_basic
128152 # setup
129153 db_a = @client [ TEST_DB + '_a' ]
130154 silently { db_a . add_user ( 'user_a' , 'password' ) }
@@ -156,11 +180,12 @@ def test_socket_auths
156180 remove_all_users ( db_a , 'user_a' , 'password' )
157181 remove_all_users ( db_b , 'user_b' , 'password' )
158182 remove_all_users ( db_c , 'user_c' , 'password' )
183+ teardown_basic
159184 end
160185
161186 def test_default_roles_non_admin
162187 return unless @client . server_version >= '2.5.3'
163-
188+ init_auth_basic
164189 silently { @db . add_user ( 'user' , 'pass' ) }
165190 silently { @db . authenticate ( 'user' , 'pass' ) }
166191 info = @db . command ( :usersInfo => 'user' ) [ 'users' ] . first
@@ -173,11 +198,13 @@ def test_default_roles_non_admin
173198 info = @db . command ( :usersInfo => 'ro-user' ) [ 'users' ] . first
174199 assert_equal 'read' , info [ 'roles' ] . first [ 'role' ]
175200 @db . logout
201+ teardown_basic
176202 end
177203
178204 def test_delegated_authentication
179205 return unless @client . server_version >= '2.4' && @client . server_version < '2.5'
180206 with_auth ( @client ) do
207+ init_auth_basic
181208 # create user in test databases
182209 accounts = @client [ TEST_DB + '_accounts' ]
183210 silently do
@@ -210,11 +237,13 @@ def test_delegated_authentication
210237 # clean-up
211238 @admin . authenticate ( 'admin' , 'password' )
212239 remove_all_users ( accounts , 'debbie' , 'delegate' )
240+ teardown_basic
213241 end
214242 end
215243
216244 def test_non_admin_default_roles
217245 return if @client . server_version < '2.5'
246+ init_auth_basic
218247
219248 # add read-only user and verify that role is 'read'
220249 @db . add_user ( 'randy' , 'password' , nil , :roles => [ 'read' ] )
@@ -228,10 +257,12 @@ def test_non_admin_default_roles
228257 @db . authenticate ( 'emily' , 'password' )
229258 users = @db . command ( :usersInfo => 'emily' ) [ 'users' ]
230259 assert_equal 'dbOwner' , users . first [ 'roles' ] . first [ 'role' ]
260+ teardown_basic
231261 end
232262
233263 def test_update_user_to_read_only
234264 with_auth ( @client ) do
265+ init_auth_basic
235266 silently { @db . add_user ( 'emily' , 'password' ) }
236267 @admin . logout
237268 @db . authenticate ( 'emily' , 'password' )
@@ -248,6 +279,7 @@ def test_update_user_to_read_only
248279 end
249280 @db . logout
250281 @admin . authenticate ( 'admin' , 'password' )
282+ teardown_basic
251283 end
252284 end
253285
0 commit comments