7
7
from unittest .mock import patch , mock_open , MagicMock
8
8
from jwcrypto import jwt , jwk
9
9
10
- from websockify .token_plugins import ReadOnlyTokenFile , JWTTokenApi , TokenRedis
10
+ from websockify .token_plugins import parse_source_args , ReadOnlyTokenFile , JWTTokenApi , TokenRedis
11
+
12
+ class ParseSourceArgumentsTestCase (unittest .TestCase ):
13
+ def test_parameterized (self ):
14
+ params = [
15
+ ('' , ['' ]),
16
+ (':' , ['' , '' ]),
17
+ ('::' , ['' , '' , '' ]),
18
+ ('"' , ['"' ]),
19
+ ('""' , ['""' ]),
20
+ ('"""' , ['"""' ]),
21
+ ('"localhost"' , ['localhost' ]),
22
+ ('"localhost":' , ['localhost' , '' ]),
23
+ ('"localhost"::' , ['localhost' , '' , '' ]),
24
+ ('"local:host"' , ['local:host' ]),
25
+ ('"local:host:"pass"' , ['"local' , 'host' , "pass" ]),
26
+ ('"local":"host"' , ['local' , 'host' ]),
27
+ ('"local":host"' , ['local' , 'host"' ]),
28
+ ('localhost:6379:1:pass"word:"my-app-namespace:dev"' ,
29
+ ['localhost' , '6379' , '1' , 'pass"word' , 'my-app-namespace:dev' ]),
30
+ ]
31
+ for src , args in params :
32
+ self .assertEqual (args , parse_source_args (src ))
11
33
12
34
class ReadOnlyTokenFileTestCase (unittest .TestCase ):
13
35
patch ('os.path.isdir' , MagicMock (return_value = False ))
@@ -267,13 +289,50 @@ def test_invalid_token(self, mock_redis):
267
289
instance .get .assert_called_once_with ('testhost' )
268
290
self .assertIsNone (result )
269
291
292
+ @patch ('redis.Redis' )
293
+ def test_token_without_namespace (self , mock_redis ):
294
+ plugin = TokenRedis ('127.0.0.1:1234' )
295
+ token = 'testhost'
296
+
297
+ def mock_redis_get (key ):
298
+ self .assertEqual (key , token )
299
+ return b'remote_host:remote_port'
300
+
301
+ instance = mock_redis .return_value
302
+ instance .get = mock_redis_get
303
+
304
+ result = plugin .lookup (token )
305
+
306
+ self .assertIsNotNone (result )
307
+ self .assertEqual (result [0 ], 'remote_host' )
308
+ self .assertEqual (result [1 ], 'remote_port' )
309
+
310
+ @patch ('redis.Redis' )
311
+ def test_token_with_namespace (self , mock_redis ):
312
+ plugin = TokenRedis ('127.0.0.1:1234:::namespace' )
313
+ token = 'testhost'
314
+
315
+ def mock_redis_get (key ):
316
+ self .assertEqual (key , "namespace:" + token )
317
+ return b'remote_host:remote_port'
318
+
319
+ instance = mock_redis .return_value
320
+ instance .get = mock_redis_get
321
+
322
+ result = plugin .lookup (token )
323
+
324
+ self .assertIsNotNone (result )
325
+ self .assertEqual (result [0 ], 'remote_host' )
326
+ self .assertEqual (result [1 ], 'remote_port' )
327
+
270
328
def test_src_only_host (self ):
271
329
plugin = TokenRedis ('127.0.0.1' )
272
330
273
331
self .assertEqual (plugin ._server , '127.0.0.1' )
274
332
self .assertEqual (plugin ._port , 6379 )
275
333
self .assertEqual (plugin ._db , 0 )
276
334
self .assertEqual (plugin ._password , None )
335
+ self .assertEqual (plugin ._namespace , "" )
277
336
278
337
def test_src_with_host_port (self ):
279
338
plugin = TokenRedis ('127.0.0.1:1234' )
@@ -282,6 +341,7 @@ def test_src_with_host_port(self):
282
341
self .assertEqual (plugin ._port , 1234 )
283
342
self .assertEqual (plugin ._db , 0 )
284
343
self .assertEqual (plugin ._password , None )
344
+ self .assertEqual (plugin ._namespace , "" )
285
345
286
346
def test_src_with_host_port_db (self ):
287
347
plugin = TokenRedis ('127.0.0.1:1234:2' )
@@ -290,6 +350,7 @@ def test_src_with_host_port_db(self):
290
350
self .assertEqual (plugin ._port , 1234 )
291
351
self .assertEqual (plugin ._db , 2 )
292
352
self .assertEqual (plugin ._password , None )
353
+ self .assertEqual (plugin ._namespace , "" )
293
354
294
355
def test_src_with_host_port_db_pass (self ):
295
356
plugin = TokenRedis ('127.0.0.1:1234:2:verysecret' )
@@ -298,67 +359,112 @@ def test_src_with_host_port_db_pass(self):
298
359
self .assertEqual (plugin ._port , 1234 )
299
360
self .assertEqual (plugin ._db , 2 )
300
361
self .assertEqual (plugin ._password , 'verysecret' )
362
+ self .assertEqual (plugin ._namespace , "" )
301
363
302
- def test_src_with_host_empty_port_empty_db_pass (self ):
364
+ def test_src_with_host_port_db_pass_namespace (self ):
365
+ plugin = TokenRedis ('127.0.0.1:1234:2:verysecret:namespace' )
366
+
367
+ self .assertEqual (plugin ._server , '127.0.0.1' )
368
+ self .assertEqual (plugin ._port , 1234 )
369
+ self .assertEqual (plugin ._db , 2 )
370
+ self .assertEqual (plugin ._password , 'verysecret' )
371
+ self .assertEqual (plugin ._namespace , "namespace:" )
372
+
373
+ def test_src_with_host_empty_port_empty_db_pass_no_namespace (self ):
303
374
plugin = TokenRedis ('127.0.0.1:::verysecret' )
304
375
305
376
self .assertEqual (plugin ._server , '127.0.0.1' )
306
377
self .assertEqual (plugin ._port , 6379 )
307
378
self .assertEqual (plugin ._db , 0 )
308
379
self .assertEqual (plugin ._password , 'verysecret' )
380
+ self .assertEqual (plugin ._namespace , "" )
381
+
382
+ def test_src_with_host_empty_port_empty_db_empty_pass_empty_namespace (self ):
383
+ plugin = TokenRedis ('127.0.0.1::::' )
309
384
310
- def test_src_with_host_empty_port_empty_db_empty_pass (self ):
385
+ self .assertEqual (plugin ._server , '127.0.0.1' )
386
+ self .assertEqual (plugin ._port , 6379 )
387
+ self .assertEqual (plugin ._db , 0 )
388
+ self .assertEqual (plugin ._password , None )
389
+ self .assertEqual (plugin ._namespace , "" )
390
+
391
+ def test_src_with_host_empty_port_empty_db_empty_pass_no_namespace (self ):
311
392
plugin = TokenRedis ('127.0.0.1:::' )
312
393
313
394
self .assertEqual (plugin ._server , '127.0.0.1' )
314
395
self .assertEqual (plugin ._port , 6379 )
315
396
self .assertEqual (plugin ._db , 0 )
316
397
self .assertEqual (plugin ._password , None )
398
+ self .assertEqual (plugin ._namespace , "" )
317
399
318
- def test_src_with_host_empty_port_empty_db_no_pass (self ):
400
+ def test_src_with_host_empty_port_empty_db_no_pass_no_namespace (self ):
319
401
plugin = TokenRedis ('127.0.0.1::' )
320
402
321
403
self .assertEqual (plugin ._server , '127.0.0.1' )
322
404
self .assertEqual (plugin ._port , 6379 )
323
405
self .assertEqual (plugin ._db , 0 )
324
406
self .assertEqual (plugin ._password , None )
407
+ self .assertEqual (plugin ._namespace , "" )
325
408
326
- def test_src_with_host_empty_port_no_db_no_pass (self ):
409
+ def test_src_with_host_empty_port_no_db_no_pass_no_namespace (self ):
327
410
plugin = TokenRedis ('127.0.0.1:' )
328
411
329
412
self .assertEqual (plugin ._server , '127.0.0.1' )
330
413
self .assertEqual (plugin ._port , 6379 )
331
414
self .assertEqual (plugin ._db , 0 )
332
415
self .assertEqual (plugin ._password , None )
416
+ self .assertEqual (plugin ._namespace , "" )
417
+
418
+ def test_src_with_host_empty_port_empty_db_empty_pass_namespace (self ):
419
+ plugin = TokenRedis ('127.0.0.1::::namespace' )
420
+
421
+ self .assertEqual (plugin ._server , '127.0.0.1' )
422
+ self .assertEqual (plugin ._port , 6379 )
423
+ self .assertEqual (plugin ._db , 0 )
424
+ self .assertEqual (plugin ._password , None )
425
+ self .assertEqual (plugin ._namespace , "namespace:" )
426
+
427
+ def test_src_with_host_empty_port_empty_db_empty_pass_nested_namespace (self ):
428
+ plugin = TokenRedis ('127.0.0.1::::"ns1:ns2"' )
429
+
430
+ self .assertEqual (plugin ._server , '127.0.0.1' )
431
+ self .assertEqual (plugin ._port , 6379 )
432
+ self .assertEqual (plugin ._db , 0 )
433
+ self .assertEqual (plugin ._password , None )
434
+ self .assertEqual (plugin ._namespace , "ns1:ns2:" )
333
435
334
- def test_src_with_host_empty_port_db_no_pass (self ):
436
+ def test_src_with_host_empty_port_db_no_pass_no_namespace (self ):
335
437
plugin = TokenRedis ('127.0.0.1::2' )
336
438
337
439
self .assertEqual (plugin ._server , '127.0.0.1' )
338
440
self .assertEqual (plugin ._port , 6379 )
339
441
self .assertEqual (plugin ._db , 2 )
340
442
self .assertEqual (plugin ._password , None )
443
+ self .assertEqual (plugin ._namespace , "" )
341
444
342
- def test_src_with_host_port_empty_db_pass (self ):
445
+ def test_src_with_host_port_empty_db_pass_no_namespace (self ):
343
446
plugin = TokenRedis ('127.0.0.1:1234::verysecret' )
344
447
345
448
self .assertEqual (plugin ._server , '127.0.0.1' )
346
449
self .assertEqual (plugin ._port , 1234 )
347
450
self .assertEqual (plugin ._db , 0 )
348
451
self .assertEqual (plugin ._password , 'verysecret' )
452
+ self .assertEqual (plugin ._namespace , "" )
349
453
350
- def test_src_with_host_empty_port_db_pass (self ):
454
+ def test_src_with_host_empty_port_db_pass_no_namespace (self ):
351
455
plugin = TokenRedis ('127.0.0.1::2:verysecret' )
352
456
353
457
self .assertEqual (plugin ._server , '127.0.0.1' )
354
458
self .assertEqual (plugin ._port , 6379 )
355
459
self .assertEqual (plugin ._db , 2 )
356
460
self .assertEqual (plugin ._password , 'verysecret' )
461
+ self .assertEqual (plugin ._namespace , "" )
357
462
358
- def test_src_with_host_empty_port_db_empty_pass (self ):
463
+ def test_src_with_host_empty_port_db_empty_pass_no_namespace (self ):
359
464
plugin = TokenRedis ('127.0.0.1::2:' )
360
465
361
466
self .assertEqual (plugin ._server , '127.0.0.1' )
362
467
self .assertEqual (plugin ._port , 6379 )
363
468
self .assertEqual (plugin ._db , 2 )
364
469
self .assertEqual (plugin ._password , None )
470
+ self .assertEqual (plugin ._namespace , "" )
0 commit comments