def app_context(request_settings, init_db=True, clean_db=True, drop_db=False): try: os.environ.pop("FLASK_APP_CONFIG_FILE", None) conn_param = lm_db.db_connection_params(request_settings) if init_db: lm_db.create_db(conn_params=conn_param) logger.debug("DB created (conn params=%r)", conn_param) flask_app = create_app(env="testing", settings=request_settings, init_app=False) flask_app.before_request(process_auto_login) with flask_app.app_context() as ctx: logger.info("Starting application") initialize_app(flask_app, ctx, prom_registry=prometheus_client.CollectorRegistry()) if init_db: logger.debug("Initializing DB...") lm_db.db.create_all() logger.debug("DB initialized!") yield ctx # clean the database and # close all sessions and connections with flask_app.app_context() as ctx: if clean_db: # _clean_db() logger.debug("DB cleanup") if drop_db: lm_db.db.close_all_sessions() lm_db.db.engine.pool.dispose() lm_db.drop_db(conn_param) logger.debug("DB deleted (connection params=%r)", conn_param) except Exception as e: logger.exception(e) raise RuntimeError(e)
def test_config_instance(instance_config_file): instance_path = os.path.dirname(instance_config_file) instance_file = os.path.basename(instance_config_file) settings = {"FLASK_APP_INSTANCE_PATH": instance_path, "FLASK_APP_CONFIG_FILE": instance_file} settings.update({"PROPERTY": "_OLD_VALUE_"}) # this should be overwritten from instance_config flask_app = create_app(env="testing", settings=settings, init_app=False) # check if settings from config instance are set # and the "PROPERTY" from settings has been overwritten check_config_properties(testing_settings, flask_app)
def check_config_properties(settings, flask_app=None): logger.debug("App settings: %r", settings) if flask_app is None: flask_app = create_app(env="testing", settings=settings, init_app=False) logger.debug("App Config: %r", flask_app.config) for k, v in settings.items(): if k == "API_KEYS": continue conf_value = str(flask_app.config.get(k, None)) logger.debug("Checking %s: %s - %s", k, v, conf_value) assert conf_value == v, \ f"Inconsistent property '{k}': found {v} in settings, but {conf_value} in the Flask config"
# Copyright (c) 2020-2021 CRS4 # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. from lifemonitor import app if __name__ == '__main__': """ Start development server""" app.create_app().run(host="0.0.0.0", port=8000)
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. import os import ssl from lifemonitor.app import create_app # create an app instance application = create_app() if __name__ == '__main__': """ Start development server""" context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) context.load_cert_chain( os.environ.get("LIFEMONITOR_TLS_CERT", './certs/lm.crt'), os.environ.get("LIFEMONITOR_TLS_KEY", './certs/lm.key')) application.run(host="0.0.0.0", port=8000, ssl_context=context)
def test_absolute_path_instance_folder(instance_config_file): settings = {"FLASK_APP_INSTANCE_PATH": "a_relative_path"} with pytest.raises(ValueError, match=r".*must be absolute.*"): create_app(env="testing", settings=settings, init_app=False)