def test_init_app_adds_stream_handler_with_plain_text_format_when_config_env_set(app):
    app.config['DM_PLAIN_TEXT_LOGS'] = True
    init_app(app)

    assert len(app.logger.handlers) == 1
    assert isinstance(app.logger.handlers[0], logging.StreamHandler)
    assert isinstance(app.logger.handlers[0].formatter, CustomLogFormatter)
def test_init_app_adds_file_handler_in_non_debug(app):
    with tempfile.NamedTemporaryFile() as f:
        app.config['DEBUG'] = False
        app.config['DM_LOG_PATH'] = f.name
        init_app(app)

        assert len(app.logger.handlers) == 1
        assert isinstance(app.logger.handlers[0], logging.FileHandler)
def test_init_app_adds_file_handler_with_log_path(app):
    with tempfile.NamedTemporaryFile() as f:
        app.config['DM_LOG_PATH'] = f.name
        init_app(app)

        assert len(app.logger.handlers) == 1
        assert isinstance(app.logger.handlers[0], logging.FileHandler)
        assert isinstance(app.logger.handlers[0].formatter, JSONFormatter)
def test_init_app_adds_file_handlers_with_log_path(app):
    with tempfile.NamedTemporaryFile() as f:
        app.config['DM_LOG_PATH'] = f.name
        init_app(app)

        assert len(app.logger.handlers) == 2
        assert isinstance(app.logger.handlers[0], logging.FileHandler)
        assert isinstance(app.logger.handlers[0].formatter, CustomLogFormatter)
        assert isinstance(app.logger.handlers[1], logging.FileHandler)
        assert isinstance(app.logger.handlers[1].formatter, JSONFormatter)
Ejemplo n.º 5
0
def test_init_app_adds_file_handler_with_log_path_and_stream_handler(app):
    with tempfile.NamedTemporaryFile() as f:
        app.config['DM_LOG_PATH'] = f.name
        init_app(app)

        assert len(app.logger.handlers) == 2
        assert isinstance(app.logger.handlers[0], logging.StreamHandler)
        assert isinstance(app.logger.handlers[1], logging.FileHandler)
        assert all(
            isinstance(handler.formatter, JSONFormatter)
            for handler in app.logger.handlers)
def test_urllib3_retry_logger_not_configured_for_search_api(app):
    app.config['DM_APP_NAME'] = 'search-api'
    for logger in logging.Logger.manager.loggerDict.values():
        logger.handlers = []

    init_app(app)

    loggers = {
        k
        for k, v in logging.Logger.manager.loggerDict.items()
        if v.handlers and isinstance(v.handlers[0], logging.StreamHandler)
    }

    assert 'urllib3.util.retry' not in loggers
def test_init_app_only_adds_handlers_to_defined_loggers(app):
    for logger in logging.Logger.manager.loggerDict.values():
        logger.handlers = []

    init_app(app)

    loggers = {
        k
        for k, v in logging.Logger.manager.loggerDict.items()
        if v.handlers and isinstance(v.handlers[0], logging.StreamHandler)
    }

    assert loggers == {
        'flask.app', 'dmutils', 'dmapiclient', 'flask_wtf.csrf',
        'urllib3.util.retry'
    }
def test_init_app_adds_stream_handler_without_log_path(app):
    init_app(app)

    assert len(app.logger.handlers) == 1
    assert isinstance(app.logger.handlers[0], logging.StreamHandler)
    assert isinstance(app.logger.handlers[0].formatter, JSONFormatter)
def test_init_app_adds_stream_handler_without_log_path(app):
    init_app(app)

    assert len(app.logger.handlers) == 1
    assert isinstance(app.logger.handlers[0], logging.StreamHandler)
def app_with_logging(app):
    with tempfile.NamedTemporaryFile() as f:
        app.config["DM_LOG_PATH"] = f.name
        init_app(app)
        yield app
def app():
    app = Flask(__name__)
    init_app(app)
    return app
def init_app(
    application,
    config_object,
    bootstrap=None,
    data_api_client=None,
    db=None,
    login_manager=None,
    search_api_client=None,
    error_handlers=frontend_error_handlers,
):

    application.config.from_object(config_object)
    if hasattr(config_object, 'init_app'):
        config_object.init_app(application)

    application.config.from_object(__name__)

    # all belong to dmutils
    config.init_app(application)
    logging.init_app(application)
    proxy_fix.init_app(application)
    request_id.init_app(application)
    cookie_probe.init_app(application)

    if bootstrap:
        bootstrap.init_app(application)
    if data_api_client:
        data_api_client.init_app(application)
    if db:
        db.init_app(application)
    if login_manager:
        login_manager.init_app(application)
        dmutils.session.init_app(application)
    if search_api_client:
        search_api_client.init_app(application)

    # allow us to use <safepurepath:...> components in route patterns
    application.url_map.converters["safepurepath"] = SafePurePathConverter

    @application.after_request
    def add_header(response):
        # Block sites from rendering our views inside <iframe>, <embed>, etc by default.
        # Individual views may set their own value (e.g. 'SAMEORIGIN')
        if not response.headers.get('X-Frame-Options'):
            response.headers.setdefault('X-Frame-Options', 'DENY')
        return response

    # Make filters accessible in templates.
    application.add_template_filter(filters.capitalize_first)
    application.add_template_filter(filters.format_links)
    application.add_template_filter(filters.nbsp)
    application.add_template_filter(filters.smartjoin)
    application.add_template_filter(filters.preserve_line_breaks)
    application.add_template_filter(filters.sub_country_codes)
    # Make select formats available in templates.
    application.add_template_filter(formats.dateformat)
    application.add_template_filter(formats.datetimeformat)
    application.add_template_filter(formats.datetodatetimeformat)
    application.add_template_filter(formats.displaytimeformat)
    application.add_template_filter(formats.shortdateformat)
    application.add_template_filter(formats.timeformat)
    application.add_template_filter(formats.utcdatetimeformat)
    application.add_template_filter(formats.utctoshorttimelongdateformat)

    @application.context_processor
    def inject_global_template_variables():
        return dict(pluralize=pluralize,
                    **(application.config['BASE_TEMPLATE_DATA'] or {}))

    for exc_or_code, handler in error_handlers.items():
        application.register_error_handler(exc_or_code, handler)
def test_init_app_adds_stream_handler_in_debug(app):
    app.config['DEBUG'] = True
    init_app(app)

    assert len(app.logger.handlers) == 1
    assert isinstance(app.logger.handlers[0], logging.StreamHandler)
def app_with_logging(app):
    with tempfile.NamedTemporaryFile() as f:
        app.config['DM_LOG_PATH'] = f.name
        init_app(app)
        yield app