Ejemplo n.º 1
0
def test_logging_without_request():
    """ Test logger is usable in non request context """
    app = Flask(__name__)
    _set_up_flask_logging(app)
    logger, stream = config_logger('main.logger')
    logger.info('works')
    assert check_log_record(stream, JOB_LOG_SCHEMA, {'msg': v_str('works')}) == {}
def test_logging_without_request():
    """ Test logger is usable in non request context """
    app = falcon.API()
    _set_up_falcon_logging(app)
    cf_logging.FRAMEWORK.context.set_correlation_id('value')

    logger, stream = config_logger('main.logger')
    logger.info('works')
    assert check_log_record(stream, JOB_LOG_SCHEMA,
                            {'msg': v_str('works')}) == {}
def test_falcon_request_logs_user(user):
    user_dict = dict([(user.key or 'username', user.name)])
    basic_auth = BasicAuthBackend(lambda username, password: user_dict)
    app = falcon.API(middleware=[
        falcon_logging.LoggingMiddleware(),
        FalconAuthMiddleware(basic_auth)
    ])
    app.add_route('/test/path', TestResource())

    args = [app, user.key] if user.key else [app]
    _set_up_falcon_logging(*args)

    expected = {'remote_user': v_str(user.name)}
    _check_falcon_request_log(
        app, {'Authorization': str(auth_basic(user.name, 'pass'))}, expected)
def test_missing_request():
    """ That the correlation id is missing when the request is missing """
    _user_logging({'X-CorrelationID': '298ebf9d-be1d-11e7-88ff-2c44fd152860'},
                  {}, {'correlation_id': v_str('-')}, False)
def test_correlation_id():
    """ Test the correlation id is logged when coming from the headers """
    _user_logging(
        {'X-CorrelationID': '298ebf9d-be1d-11e7-88ff-2c44fd152860'}, {},
        {'correlation_id': v_str('298ebf9d-be1d-11e7-88ff-2c44fd152860')},
        True)
def test_web_log():
    """ That the custom properties are logged """
    _user_logging({}, {'myproperty': 'myval'}, {'myproperty': v_str('myval')})
Ejemplo n.º 7
0
    enable_sensitive_fields_logging,
    config_logger,
)


# pylint: disable=protected-access

@pytest.mark.xfail(raises=TypeError, strict=True)
def test_flask_requires_valid_app():
    """ Test the init api expects a valid app """
    flask_logging.init({})


FIXTURE = get_web_record_header_fixtures()
FIXTURE.append(({'Authorization': auth_basic('user', 'pass')},
                {'remote_user': v_str('user')}))
FIXTURE.append(({}, {'response_size_b': v_num(val=2)}))


@pytest.fixture(autouse=True)
def before_each():
    # pylint: disable=duplicate-code
    """ enable all fields to be logged """
    enable_sensitive_fields_logging()
    yield


@pytest.mark.parametrize("headers, expected", FIXTURE)
def test_flask_request_log(headers, expected):
    """ That the expected records are logged by the logging library """
def test_logs_correlation_id():
    """ Test the setting of the correlation id based on the headers """
    _user_logging(
        {'X-CorrelationID': '298ebf9d-be1d-11e7-88ff-2c44fd152860'}, {},
        {'correlation_id': v_str('298ebf9d-be1d-11e7-88ff-2c44fd152860')},
        True)
def test_web_log():
    """ Test that custom attributes are logged """
    _user_logging({}, {'myprop': 'myval'}, {'myprop': v_str('myval')}, False)