def test_create_secret_key(monkeypatch, tmp_path, data, content): monkeypatch.setattr(os, 'urandom', lambda n: bytes(n)) filename = tmp_path / "encKey" c.create_secret_key(str(filename), data) assert filename.exists() assert filename.read_bytes() == content permissions = stat.S_IMODE(filename.stat().st_mode) assert permissions == c.SECRET_FILE_PERMISSIONS
def base_app(tmp_path, request, sqlalchemy_uri, key_directory): """ App instance without context Creates and returns a bare app. If you wish an app with an initialised application context, use the `app` fixture instead """ db_fd, db_path = None, None try: # ------------------------------------------------------------------ -- # if sqlalchemy_uri is the fallback, establish a temp file if sqlalchemy_uri == 'sqlite:///{}': db_fd, db_path = tempfile.mkstemp() sqlalchemy_uri = sqlalchemy_uri.format(db_path) # ------------------------------------------------------------------ -- # Skip test if incompatible with sqlite if sqlalchemy_uri.startswith("sqlite:"): if request.node.get_closest_marker('exclude_sqlite'): pytest.skip("non sqlite database required for test") # ------------------------------------------------------------------ -- # create the app with common test config base_app_config = dict( ENV='testing', # doesn't make a huge difference for us TESTING=True, DATABASE_URI=sqlalchemy_uri, SQLALCHEMY_TRACK_MODIFICATIONS=False, ROOT_DIR=tmp_path, CACHE_DIR=tmp_path / "cache", DATA_DIR=tmp_path / "data", LOGFILE_DIR=tmp_path / "logs", AUDIT_PUBLIC_KEY_FILE=key_directory / "audit-public.pem", AUDIT_PRIVATE_KEY_FILE=key_directory / "audit-private.pem", SECRET_FILE=key_directory / "encKey", LOGGING_LEVEL="DEBUG", LOGGING_CONSOLE_LEVEL="DEBUG", ) config = request.node.get_closest_marker("app_config") if config is not None: base_app_config.update(config.args[0]) os.environ["LINOTP_CFG"] = "" # Pre-generate the important directories for key in ('CACHE_DIR', 'DATA_DIR', 'LOGFILE_DIR'): os.makedirs(base_app_config[key], mode=0o770, exist_ok=True) # Create secrete key / audit key if necessary. with mock.patch('linotp.cli.init_cmd.current_app') as mock_app: # The cli commands use current_app.echo to display messages, but this # fails here because there no context yet. So we temporary route # echo to plain print. mock_app.echo = Echo() # Fake running `linotp init enc-key` secret_file = base_app_config['SECRET_FILE'] if not os.path.exists(secret_file): create_secret_key(filename=secret_file) # Fake running `linotp init audit-keys` audit_private_key_file = str( base_app_config['AUDIT_PRIVATE_KEY_FILE']) if not os.path.exists(audit_private_key_file): create_audit_keys( audit_private_key_file, str(base_app_config['AUDIT_PUBLIC_KEY_FILE'])) os.environ["LINOTP_CMD"] = "init-database" app = create_app('testing', base_app_config) # Fake running `linotp init database` with app.app_context(): init_db_tables(app, drop_data=False, add_defaults=True) yield app finally: # ------------------------------------------------------------------ -- # in case of sqlite tempfile fallback, we have to wipe the dishes here if db_fd: os.close(db_fd) if db_path: os.unlink(db_path)
def base_app(tmp_path, request, sqlalchemy_uri, key_directory): """ App instance without context Creates and returns a bare app. If you wish an app with an initialised application context, use the `app` fixture instead """ db_fd, db_path = None, None try: # ------------------------------------------------------------------ -- # if sqlalchemy_uri is the fallback, establish a temp file if sqlalchemy_uri == "sqlite:///{}": db_fd, db_path = tempfile.mkstemp() sqlalchemy_uri = sqlalchemy_uri.format(db_path) # ------------------------------------------------------------------ -- # Skip test if incompatible with sqlite if sqlalchemy_uri.startswith("sqlite:"): if request.node.get_closest_marker("exclude_sqlite"): pytest.skip("non sqlite database required for test") # ------------------------------------------------------------------ -- # create the app with common test config base_app_config = dict( ENV="testing", # doesn't make a huge difference for us TESTING=True, DATABASE_URI=sqlalchemy_uri, AUDIT_DATABASE_URI="SHARED", SQLALCHEMY_TRACK_MODIFICATIONS=False, ROOT_DIR=tmp_path, CACHE_DIR=tmp_path / "cache", DATA_DIR=tmp_path / "data", LOG_FILE_DIR=tmp_path / "logs", AUDIT_PUBLIC_KEY_FILE=key_directory / "audit-public.pem", AUDIT_PRIVATE_KEY_FILE=key_directory / "audit-private.pem", SECRET_FILE=key_directory / "encKey", LOGGING_LEVEL="DEBUG", LOGGING_CONSOLE_LEVEL="DEBUG", DISABLE_CONTROLLERS="", ) config = request.node.get_closest_marker("app_config") if config is not None: base_app_config.update(config.args[0]) os.environ["LINOTP_CFG"] = "" # Pre-generate the important directories for key in ("CACHE_DIR", "DATA_DIR", "LOG_FILE_DIR"): os.makedirs(base_app_config[key], mode=0o770, exist_ok=True) # ----------------------------------------------------------------------- # Fake running `linotp init enc-key` secret_file = base_app_config["SECRET_FILE"] if not os.path.exists(secret_file): sec_key = 3 * "0123456789abcdef" * 4 create_secret_key(filename=secret_file, data=sec_key) # Fake running `linotp init audit-keys` audit_private_key_file = str(base_app_config["AUDIT_PRIVATE_KEY_FILE"]) if not os.path.exists(audit_private_key_file): create_audit_keys( audit_private_key_file, str(base_app_config["AUDIT_PUBLIC_KEY_FILE"]), ) # ----------------------------------------------------------------------- os.environ["LINOTP_CMD"] = "init-database" app = create_app("testing", base_app_config) # Fake running `linotp init database` with app.app_context(): init_db_tables(app, drop_data=True, add_defaults=True) yield app finally: # ------------------------------------------------------------------ -- # in case of sqlite tempfile fallback, we have to wipe the dishes here if db_fd: os.close(db_fd) if db_path: os.unlink(db_path)