14
14
15
15
module BasicAuthTests
16
16
17
- def init_auth
17
+ def init_auth_basic
18
18
# enable authentication by creating and logging in as admin user
19
19
@admin = @client [ 'admin' ]
20
20
@admin . add_user ( 'admin' , 'password' , nil , :roles => [ 'readAnyDatabase' ,
@@ -28,7 +28,7 @@ def init_auth
28
28
@db . add_user ( 'admin' , 'cleanup' , nil , :roles => [ ] )
29
29
end
30
30
31
- def teardown
31
+ def teardown_basic
32
32
remove_all_users ( @db , 'admin' , 'cleanup' )
33
33
remove_all_users ( @admin , 'admin' , 'password' ) if has_auth? ( @admin . name )
34
34
end
@@ -48,15 +48,21 @@ def has_auth?(db_name)
48
48
end
49
49
50
50
def test_add_remove_user
51
+ init_auth_basic
52
+
51
53
# add user
52
54
silently { @db . add_user ( 'bob' , 'user' ) }
53
55
assert @db . authenticate ( 'bob' , 'user' )
54
56
55
57
# remove user
56
58
assert @db . remove_user ( 'bob' )
59
+
60
+ teardown_basic
57
61
end
58
62
59
63
def test_update_user
64
+ init_auth_basic
65
+
60
66
# add user
61
67
silently { @db . add_user ( 'bob' , 'user' ) }
62
68
assert @db . authenticate ( 'bob' , 'user' )
@@ -68,47 +74,63 @@ def test_update_user
68
74
@db . authenticate ( 'bob' , 'user' )
69
75
end
70
76
assert @db . authenticate ( 'bob' , 'updated' )
77
+
78
+ teardown_basic
71
79
end
72
80
73
81
def test_remove_non_existent_user
82
+ init_auth_basic
83
+
74
84
if @client . server_version < '2.5'
75
85
assert_equal false , @db . remove_user ( 'joe' )
76
86
else
77
87
assert_raise Mongo ::OperationFailure do
78
88
assert @db . remove_user ( 'joe' )
79
89
end
80
90
end
91
+ teardown_basic
81
92
end
82
93
83
94
def test_authenticate
95
+ init_auth_basic
84
96
silently { @db . add_user ( 'peggy' , 'user' ) }
85
97
assert @db . authenticate ( 'peggy' , 'user' )
86
98
@db . remove_user ( 'peggy' )
99
+ teardown_basic
87
100
end
88
101
89
102
def test_authenticate_non_existent_user
103
+ init_auth_basic
90
104
assert_raise Mongo ::AuthenticationError do
91
105
@db . authenticate ( 'frank' , 'thetank' )
92
106
end
107
+ teardown_basic
93
108
end
94
109
95
110
def test_logout
111
+ init_auth_basic
96
112
silently { @db . add_user ( 'peggy' , 'user' ) }
97
113
assert @db . authenticate ( 'peggy' , 'user' )
98
114
assert @db . logout
115
+ teardown_basic
99
116
end
100
117
101
118
def test_authenticate_with_special_characters
119
+ init_auth_basic
102
120
silently { assert @db . add_user ( 'foo:bar' , '@foo' ) }
103
121
assert @db . authenticate ( 'foo:bar' , '@foo' )
122
+ teardown_basic
104
123
end
105
124
106
125
def test_authenticate_read_only
126
+ init_auth_basic
107
127
silently { @db . add_user ( 'randy' , 'readonly' , true ) }
108
128
assert @db . authenticate ( 'randy' , 'readonly' )
129
+ teardown_basic
109
130
end
110
131
111
132
def test_authenticate_with_connection_uri
133
+ init_auth_basic
112
134
silently { @db . add_user ( 'eunice' , 'uritest' ) }
113
135
114
136
uri = "mongodb://eunice:uritest@#{ @host_info } /#{ @db . name } "
@@ -122,9 +144,11 @@ def test_authenticate_with_connection_uri
122
144
assert_equal @db . name , auth [ :db_name ]
123
145
assert_equal 'eunice' , auth [ :username ]
124
146
assert_equal 'uritest' , auth [ :password ]
147
+ teardown_basic
125
148
end
126
149
127
150
def test_socket_auths
151
+ init_auth_basic
128
152
# setup
129
153
db_a = @client [ TEST_DB + '_a' ]
130
154
silently { db_a . add_user ( 'user_a' , 'password' ) }
@@ -156,11 +180,12 @@ def test_socket_auths
156
180
remove_all_users ( db_a , 'user_a' , 'password' )
157
181
remove_all_users ( db_b , 'user_b' , 'password' )
158
182
remove_all_users ( db_c , 'user_c' , 'password' )
183
+ teardown_basic
159
184
end
160
185
161
186
def test_default_roles_non_admin
162
187
return unless @client . server_version >= '2.5.3'
163
-
188
+ init_auth_basic
164
189
silently { @db . add_user ( 'user' , 'pass' ) }
165
190
silently { @db . authenticate ( 'user' , 'pass' ) }
166
191
info = @db . command ( :usersInfo => 'user' ) [ 'users' ] . first
@@ -173,11 +198,13 @@ def test_default_roles_non_admin
173
198
info = @db . command ( :usersInfo => 'ro-user' ) [ 'users' ] . first
174
199
assert_equal 'read' , info [ 'roles' ] . first [ 'role' ]
175
200
@db . logout
201
+ teardown_basic
176
202
end
177
203
178
204
def test_delegated_authentication
179
205
return unless @client . server_version >= '2.4' && @client . server_version < '2.5'
180
206
with_auth ( @client ) do
207
+ init_auth_basic
181
208
# create user in test databases
182
209
accounts = @client [ TEST_DB + '_accounts' ]
183
210
silently do
@@ -210,11 +237,13 @@ def test_delegated_authentication
210
237
# clean-up
211
238
@admin . authenticate ( 'admin' , 'password' )
212
239
remove_all_users ( accounts , 'debbie' , 'delegate' )
240
+ teardown_basic
213
241
end
214
242
end
215
243
216
244
def test_non_admin_default_roles
217
245
return if @client . server_version < '2.5'
246
+ init_auth_basic
218
247
219
248
# add read-only user and verify that role is 'read'
220
249
@db . add_user ( 'randy' , 'password' , nil , :roles => [ 'read' ] )
@@ -228,10 +257,12 @@ def test_non_admin_default_roles
228
257
@db . authenticate ( 'emily' , 'password' )
229
258
users = @db . command ( :usersInfo => 'emily' ) [ 'users' ]
230
259
assert_equal 'dbOwner' , users . first [ 'roles' ] . first [ 'role' ]
260
+ teardown_basic
231
261
end
232
262
233
263
def test_update_user_to_read_only
234
264
with_auth ( @client ) do
265
+ init_auth_basic
235
266
silently { @db . add_user ( 'emily' , 'password' ) }
236
267
@admin . logout
237
268
@db . authenticate ( 'emily' , 'password' )
@@ -248,6 +279,7 @@ def test_update_user_to_read_only
248
279
end
249
280
@db . logout
250
281
@admin . authenticate ( 'admin' , 'password' )
282
+ teardown_basic
251
283
end
252
284
end
253
285
0 commit comments