comparison test/test_redis_session.py @ 6823:fe0091279f50

Refactor session db logging and key generation for sessions/otks While I was working on the redis sessiondb stuff, I noticed that log_wanrning, get_logger ... was duplicated. Also there was code to generate a unique key for otks that was duplicated. Changes: creating new sessions_common.py and SessionsCommon class to provide methods: log_warning, log_info, log_debug, get_logger, getUniqueKey getUniqueKey method is closer to the method used to make session keys in client.py. sessions_common.py now report when random_.py chooses a weak random number generator. Removed same from rest.py. get_logger reconciles all logging under roundup.hyperdb.backends.<name of BasicDatabase class> some backends used to log to root logger. have BasicDatabase in other sessions_*.py modules inherit from SessionCommon. change logging to use log_* methods. In addition: remove unused imports reported by flake8 and other formatting changes modify actions.py, rest.py, templating.py to use getUniqueKey method. add tests for new methods test_redis_session.py swap out ModuleNotFoundError for ImportError to prevent crash in python2 when redis is not present. allow injection of username:password or just password into redis connection URL. set pytest_redis_pw envirnment variable to password or user:password when running test.
author John Rouillard <rouilj@ieee.org>
date Sun, 07 Aug 2022 01:51:11 -0400
parents 35f0952f4bc5
children 75a5946cf897
comparison
equal deleted inserted replaced
6822:5053ee6c846b 6823:fe0091279f50
21 import pytest 21 import pytest
22 22
23 try: 23 try:
24 from roundup.backends.sessions_redis import Sessions, OneTimeKeys 24 from roundup.backends.sessions_redis import Sessions, OneTimeKeys
25 skip_redis = lambda func, *args, **kwargs: func 25 skip_redis = lambda func, *args, **kwargs: func
26 except ModuleNotFoundError as e: 26 except ImportError as e:
27 from .pytest_patcher import mark_class 27 from .pytest_patcher import mark_class
28 skip_redis = mark_class(pytest.mark.skip( 28 skip_redis = mark_class(pytest.mark.skip(
29 reason='Skipping redis tests: redis module not available')) 29 reason='Skipping redis tests: redis module not available'))
30 30
31 from .test_sqlite import sqliteOpener 31 from .test_sqlite import sqliteOpener
35 35
36 class RedisSessionTest(SessionTest): 36 class RedisSessionTest(SessionTest):
37 def setUp(self): 37 def setUp(self):
38 SessionTest.setUp(self) 38 SessionTest.setUp(self)
39 39
40 import os
41 if 'pytest_redis_pw' in os.environ:
42 pw = os.environ['pytest_redis_pw']
43 if ':' in pw:
44 # pw is user:password
45 pw = "%s@" % pw
46 else:
47 # pw is just password
48 pw = ":%s@" % pw
49 else:
50 pw = ""
51
40 # redefine the session db's as redis. 52 # redefine the session db's as redis.
41 self.db.config.SESSIONDB_BACKEND = "redis" 53 self.db.config.SESSIONDB_BACKEND = "redis"
42 self.db.config.SESSIONDB_REDIS_URL = 'redis://localhost:6379/15?health_check_interval=2' 54 self.db.config.SESSIONDB_REDIS_URL = \
55 'redis://%slocalhost:6379/15?health_check_interval=2' % pw
43 self.db.Session = None 56 self.db.Session = None
44 self.db.Otk = None 57 self.db.Otk = None
45 self.sessions = self.db.getSessionManager() 58 self.sessions = self.db.getSessionManager()
46 self.otks = self.db.getOTKManager() 59 self.otks = self.db.getOTKManager()
47 60

Roundup Issue Tracker: http://roundup-tracker.org/