@@ -203,3 +203,153 @@ def test_simple(self, mock_redis):
203
203
self .assertIsNotNone (result )
204
204
self .assertEqual (result [0 ], 'remote_host' )
205
205
self .assertEqual (result [1 ], 'remote_port' )
206
+
207
+ @patch ('redis.Redis' )
208
+ def test_json_token_with_spaces (self , mock_redis ):
209
+ plugin = TokenRedis ('127.0.0.1:1234' )
210
+
211
+ instance = mock_redis .return_value
212
+ instance .get .return_value = b' {"host": "remote_host:remote_port"} '
213
+
214
+ result = plugin .lookup ('testhost' )
215
+
216
+ instance .get .assert_called_once_with ('testhost' )
217
+ self .assertIsNotNone (result )
218
+ self .assertEqual (result [0 ], 'remote_host' )
219
+ self .assertEqual (result [1 ], 'remote_port' )
220
+
221
+ @patch ('redis.Redis' )
222
+ def test_text_token (self , mock_redis ):
223
+ plugin = TokenRedis ('127.0.0.1:1234' )
224
+
225
+ instance = mock_redis .return_value
226
+ instance .get .return_value = b'remote_host:remote_port'
227
+
228
+ result = plugin .lookup ('testhost' )
229
+
230
+ instance .get .assert_called_once_with ('testhost' )
231
+ self .assertIsNotNone (result )
232
+ self .assertEqual (result [0 ], 'remote_host' )
233
+ self .assertEqual (result [1 ], 'remote_port' )
234
+
235
+ @patch ('redis.Redis' )
236
+ def test_text_token_with_spaces (self , mock_redis ):
237
+ plugin = TokenRedis ('127.0.0.1:1234' )
238
+
239
+ instance = mock_redis .return_value
240
+ instance .get .return_value = b' remote_host:remote_port '
241
+
242
+ result = plugin .lookup ('testhost' )
243
+
244
+ instance .get .assert_called_once_with ('testhost' )
245
+ self .assertIsNotNone (result )
246
+ self .assertEqual (result [0 ], 'remote_host' )
247
+ self .assertEqual (result [1 ], 'remote_port' )
248
+
249
+ @patch ('redis.Redis' )
250
+ def test_invalid_token (self , mock_redis ):
251
+ plugin = TokenRedis ('127.0.0.1:1234' )
252
+
253
+ instance = mock_redis .return_value
254
+ instance .get .return_value = b'{"host": "remote_host:remote_port" '
255
+
256
+ result = plugin .lookup ('testhost' )
257
+
258
+ instance .get .assert_called_once_with ('testhost' )
259
+ self .assertIsNone (result )
260
+
261
+ def test_src_only_host (self ):
262
+ plugin = TokenRedis ('127.0.0.1' )
263
+
264
+ self .assertEqual (plugin ._server , '127.0.0.1' )
265
+ self .assertEqual (plugin ._port , 6379 )
266
+ self .assertEqual (plugin ._db , 0 )
267
+ self .assertEqual (plugin ._password , None )
268
+
269
+ def test_src_with_host_port (self ):
270
+ plugin = TokenRedis ('127.0.0.1:1234' )
271
+
272
+ self .assertEqual (plugin ._server , '127.0.0.1' )
273
+ self .assertEqual (plugin ._port , 1234 )
274
+ self .assertEqual (plugin ._db , 0 )
275
+ self .assertEqual (plugin ._password , None )
276
+
277
+ def test_src_with_host_port_db (self ):
278
+ plugin = TokenRedis ('127.0.0.1:1234:2' )
279
+
280
+ self .assertEqual (plugin ._server , '127.0.0.1' )
281
+ self .assertEqual (plugin ._port , 1234 )
282
+ self .assertEqual (plugin ._db , 2 )
283
+ self .assertEqual (plugin ._password , None )
284
+
285
+ def test_src_with_host_port_db_pass (self ):
286
+ plugin = TokenRedis ('127.0.0.1:1234:2:verysecret' )
287
+
288
+ self .assertEqual (plugin ._server , '127.0.0.1' )
289
+ self .assertEqual (plugin ._port , 1234 )
290
+ self .assertEqual (plugin ._db , 2 )
291
+ self .assertEqual (plugin ._password , 'verysecret' )
292
+
293
+ def test_src_with_host_empty_port_empty_db_pass (self ):
294
+ plugin = TokenRedis ('127.0.0.1:::verysecret' )
295
+
296
+ self .assertEqual (plugin ._server , '127.0.0.1' )
297
+ self .assertEqual (plugin ._port , 6379 )
298
+ self .assertEqual (plugin ._db , 0 )
299
+ self .assertEqual (plugin ._password , 'verysecret' )
300
+
301
+ def test_src_with_host_empty_port_empty_db_empty_pass (self ):
302
+ plugin = TokenRedis ('127.0.0.1:::' )
303
+
304
+ self .assertEqual (plugin ._server , '127.0.0.1' )
305
+ self .assertEqual (plugin ._port , 6379 )
306
+ self .assertEqual (plugin ._db , 0 )
307
+ self .assertEqual (plugin ._password , None )
308
+
309
+ def test_src_with_host_empty_port_empty_db_no_pass (self ):
310
+ plugin = TokenRedis ('127.0.0.1::' )
311
+
312
+ self .assertEqual (plugin ._server , '127.0.0.1' )
313
+ self .assertEqual (plugin ._port , 6379 )
314
+ self .assertEqual (plugin ._db , 0 )
315
+ self .assertEqual (plugin ._password , None )
316
+
317
+ def test_src_with_host_empty_port_no_db_no_pass (self ):
318
+ plugin = TokenRedis ('127.0.0.1:' )
319
+
320
+ self .assertEqual (plugin ._server , '127.0.0.1' )
321
+ self .assertEqual (plugin ._port , 6379 )
322
+ self .assertEqual (plugin ._db , 0 )
323
+ self .assertEqual (plugin ._password , None )
324
+
325
+ def test_src_with_host_empty_port_db_no_pass (self ):
326
+ plugin = TokenRedis ('127.0.0.1::2' )
327
+
328
+ self .assertEqual (plugin ._server , '127.0.0.1' )
329
+ self .assertEqual (plugin ._port , 6379 )
330
+ self .assertEqual (plugin ._db , 2 )
331
+ self .assertEqual (plugin ._password , None )
332
+
333
+ def test_src_with_host_port_empty_db_pass (self ):
334
+ plugin = TokenRedis ('127.0.0.1:1234::verysecret' )
335
+
336
+ self .assertEqual (plugin ._server , '127.0.0.1' )
337
+ self .assertEqual (plugin ._port , 1234 )
338
+ self .assertEqual (plugin ._db , 0 )
339
+ self .assertEqual (plugin ._password , 'verysecret' )
340
+
341
+ def test_src_with_host_empty_port_db_pass (self ):
342
+ plugin = TokenRedis ('127.0.0.1::2:verysecret' )
343
+
344
+ self .assertEqual (plugin ._server , '127.0.0.1' )
345
+ self .assertEqual (plugin ._port , 6379 )
346
+ self .assertEqual (plugin ._db , 2 )
347
+ self .assertEqual (plugin ._password , 'verysecret' )
348
+
349
+ def test_src_with_host_empty_port_db_empty_pass (self ):
350
+ plugin = TokenRedis ('127.0.0.1::2:' )
351
+
352
+ self .assertEqual (plugin ._server , '127.0.0.1' )
353
+ self .assertEqual (plugin ._port , 6379 )
354
+ self .assertEqual (plugin ._db , 2 )
355
+ self .assertEqual (plugin ._password , None )
0 commit comments