6
6
from aioetcd3 .help import range_all , PER_RW
7
7
from aioetcd3 .exceptions import AuthError , Unauthenticated , PermissionDenied
8
8
9
+ from .utils import switch_auth_off , switch_auth_on
10
+
9
11
10
12
def asynctest (f ):
11
13
@functools .wraps (f )
@@ -153,22 +155,12 @@ async def setUp(self):
153
155
self .endpoints = "127.0.0.1:2379"
154
156
self .unauthenticated_client = client (endpoint = self .endpoints )
155
157
await self .cleanUp ()
156
- await self .prepare_users_and_roles ( )
158
+ await switch_auth_on ( self .unauthenticated_client )
157
159
self .client_client = client (
158
160
endpoint = self .endpoints , username = "client" , password = "client"
159
161
)
160
162
self .root_client = client (endpoint = self .endpoints , username = "root" , password = "root" )
161
163
162
- async def prepare_users_and_roles (self ):
163
- await self .unauthenticated_client .user_add (username = "root" , password = "root" )
164
- await self .unauthenticated_client .role_add (name = 'root' )
165
- await self .unauthenticated_client .user_grant_role (username = 'root' , role = 'root' )
166
-
167
- await self .unauthenticated_client .user_add (username = 'client' , password = 'client' )
168
- await self .unauthenticated_client .role_add (name = 'client' )
169
- await self .unauthenticated_client .user_grant_role (username = 'client' , role = 'client' )
170
- await self .unauthenticated_client .auth_enable ()
171
-
172
164
async def create_kv_for_test (self ):
173
165
await self .root_client .put ('/foo' , '/foo' )
174
166
value , meta = await self .root_client .get ('/foo' )
@@ -208,30 +200,12 @@ async def test_wrong_token(self):
208
200
with self .assertRaises (Unauthenticated ) as exc :
209
201
await new_client .get ("/foo" )
210
202
211
- async def delete_all_users (self ):
212
- # It's necessary to use unauthenticated client here, because root user
213
- # cannot be deleted when the auth is enabled
214
- users = await self .unauthenticated_client .user_list ()
215
-
216
- for u in users :
217
- await self .unauthenticated_client .user_delete (username = u )
218
-
219
- async def delete_all_roles (self ):
220
- # It's necessary to use unauthenticated client here, because root user
221
- # cannot be deleted when the auth is enabled
222
- roles = await self .unauthenticated_client .role_list ()
223
-
224
- for r in roles :
225
- await self .unauthenticated_client .role_delete (name = r )
226
-
227
203
async def cleanUp (self ):
228
204
await self .unauthenticated_client .delete (range_all ())
229
- await self .delete_all_users ()
230
- await self .delete_all_roles ()
231
205
232
206
@asynctest
233
207
async def tearDown (self ):
234
- await self .root_client . auth_disable ( )
208
+ await switch_auth_off ( self .root_client , self . unauthenticated_client )
235
209
await self .cleanUp ()
236
210
237
211
@@ -244,23 +218,13 @@ async def setUp(self):
244
218
ca_file = "test/cfssl/ca.pem" ,
245
219
)
246
220
await self .cleanUp ()
247
- await self .prepare_users_and_roles ( )
221
+ await switch_auth_on ( self .unauthenticated_client )
248
222
self .root_client = ssl_client (endpoint = self .endpoints , ca_file = "test/cfssl/ca.pem" ,
249
223
username = "root" , password = "root" )
250
224
251
225
self .client_client = ssl_client (endpoint = self .endpoints , ca_file = "test/cfssl/ca.pem" ,
252
226
username = "client" , password = "client" )
253
227
254
- async def prepare_users_and_roles (self ):
255
- await self .unauthenticated_client .user_add (username = "root" , password = "root" )
256
- await self .unauthenticated_client .role_add (name = 'root' )
257
- await self .unauthenticated_client .user_grant_role (username = 'root' , role = 'root' )
258
-
259
- await self .unauthenticated_client .user_add (username = 'client' , password = 'client' )
260
- await self .unauthenticated_client .role_add (name = 'client' )
261
- await self .unauthenticated_client .user_grant_role (username = 'client' , role = 'client' )
262
- await self .unauthenticated_client .auth_enable ()
263
-
264
228
async def create_kv_for_test (self ):
265
229
await self .root_client .put ('/foo' , '/foo' )
266
230
value , meta = await self .root_client .get ('/foo' )
@@ -306,28 +270,10 @@ async def test_wrong_token(self):
306
270
with self .assertRaises (Unauthenticated ) as exc :
307
271
await new_client .get ("/foo" )
308
272
309
- async def delete_all_users (self ):
310
- # It's necessary to use unauthenticated client here, because root user
311
- # cannot be deleted when the auth is enabled
312
- users = await self .unauthenticated_client .user_list ()
313
-
314
- for u in users :
315
- await self .unauthenticated_client .user_delete (username = u )
316
-
317
- async def delete_all_roles (self ):
318
- # It's necessary to use unauthenticated client here, because root user
319
- # cannot be deleted when the auth is enabled
320
- roles = await self .unauthenticated_client .role_list ()
321
-
322
- for r in roles :
323
- await self .unauthenticated_client .role_delete (name = r )
324
-
325
273
async def cleanUp (self ):
326
274
await self .unauthenticated_client .delete (range_all ())
327
- await self .delete_all_users ()
328
- await self .delete_all_roles ()
329
275
330
276
@asynctest
331
277
async def tearDown (self ):
332
- await self .root_client . auth_disable ( )
278
+ await switch_auth_off ( self .root_client , self . unauthenticated_client )
333
279
await self .cleanUp ()
0 commit comments