def test_logging_of_event_and_response(run_from_file_directory, caplog,
                                       log_config, expected_part_in_logging,
                                       stated_log_level):
    environ._load_config_from_file("api_response_wrapper_config.json")
    caplog.set_level(logging.INFO)
    from aws_serverless_wrapper.serverless_handler import (
        LambdaHandlerOfFunction, )

    def returning_api(_):
        return {
            "statusCode": 200,
            "body": test_body,
            "headers": {
                "Content-Type": "application/json"
            }
        }

    event = compose_ReST_event(
        httpMethod="POST",
        resource="/test_request_resource/{path_level1}/{path_level2}",
        pathParameters={
            "path_level1": "path_value1",
            "path_level2": "path_value2"
        },
        body=test_body)

    response = LambdaHandlerOfFunction(returning_api,
                                       **log_config).wrap_lambda(
                                           event.copy(), context)

    assert len(caplog.messages) == 1
    assert expected_part_in_logging in caplog.text
    assert caplog.text.startswith(stated_log_level)
    assert response["statusCode"] == 200
def test_parsing_config(caplog, run_from_file_directory, parse_config,
                        response_components, event_body, return_body,
                        raised_exception):
    def test_func(_):
        return {
            "statusCode": 200,
            "body": return_body,
            "headers": {
                "Content-Type": "application/json"
            }
        }

    from aws_serverless_wrapper._environ_variables import environ
    environ._load_config_from_file("api_response_wrapper_config.json")

    from aws_serverless_wrapper.serverless_handler import LambdaHandlerOfFunction

    event = compose_ReST_event(httpMethod="POST",
                               resource="/test_request_no_verification",
                               header={"Content-Type": "application/json"})
    event["body"] = event_body

    if not raised_exception:
        response = LambdaHandlerOfFunction(test_func,
                                           **parse_config).wrap_lambda(
                                               event, fake_context)
        for key in response_components:
            assert response[key] == response_components[key]
    else:
        with raises(raised_exception):
            LambdaHandlerOfFunction(test_func, **parse_config).wrap_lambda(
                event, fake_context)
def test_unspecified_status_code_with_being_transparent_to_client(
    response_validation_env, caplog
):
    environ["API_RESPONSE_VERIFICATION"]["RETURN_INTERNAL_SERVER_ERROR"] = True
    environ["ERROR_LOG"] = {"API_RESPONSE": True}
    from aws_serverless_wrapper.serverless_handler import (
        LambdaHandlerOfFunction,
    )

    event = compose_ReST_event(
        httpMethod="POST",
        resource="/test_response_resource",
        body={"response_statusCode": 418, "response_body": "I'm a teapot"},
    )

    expected_response = {
        "body": 'no specified response schema available for statusCode 418\n'
                'response: {\'statusCode\': 418, '
                '\'body\': "I\'m a teapot", \'headers\': {\'Content-Type\': \'text/plain\'}}',
        "headers": {"Content-Type": "text/plain"},
        "statusCode": 501,
    }
    response = LambdaHandlerOfFunction(response_test).wrap_lambda(event, fake_context)
    assert response == expected_response
    assert len(caplog.messages) == 1
    assert "no specified response schema available for statusCode 418" in caplog.text
def test_200_false_single_string_with_internal_server_error(
    response_validation_env, caplog
):
    environ["API_RESPONSE_VERIFICATION"]["RETURN_INTERNAL_SERVER_ERROR"] = True
    environ["ERROR_LOG"] = {"API_RESPONSE": True}
    from aws_serverless_wrapper.serverless_handler import (
        LambdaHandlerOfFunction,
    )

    event = compose_ReST_event(
        httpMethod="POST",
        resource="/test_response_resource",
        body={"response_statusCode": 200, "response_body": "not_allowed_answer"},
    )

    expected_response = {
        "body": {
            "basic": "internal server error",
            "error_log_item": {
                "aws_log_group": "test/log/group",
                "aws_request_id": "uuid",
                "body": {
                    "invalid response": {
                        "body": "not_allowed_answer",
                        "headers": {"Content-Type": "text/plain"},
                        "statusCode": 200,
                    },
                    "schema context": "[<ValidationError: "
                    "\"'not_allowed_answer' "
                    "is not one of "
                    "['single_allowed_answer']\">, "
                    "<ValidationError: "
                    "\"'not_allowed_answer' "
                    "is not of type "
                    "'object'\">]",
                },
                "function_version": "$LATEST",
                "lambda_name": "test_function",
                "service_name": "group",
                "statusCode": 500,
                "timestamp": 1577836800,
            },
        },
        "headers": {"Content-Type": "application/json"},
        "statusCode": 500,
    }
    response = LambdaHandlerOfFunction(response_test).wrap_lambda(event, fake_context)
    response["body"] = loads(response["body"])
    assert response == expected_response
    assert len(caplog.messages) == 1
    assert "invalid response" in caplog.text
def test_nested_api_resource(run_from_file_directory):
    environ._load_config_from_file("api_response_wrapper_config.json")

    from aws_serverless_wrapper.serverless_handler import (
        LambdaHandlerOfFunction, )

    event = compose_ReST_event(
        httpMethod="POST",
        resource="/test_request_resource/specific_resource/{some_id}",
        pathParameters={"some_id": "test_id"},
    )

    response = LambdaHandlerOfFunction(api_basic).wrap_lambda(event, context)

    assert response == {"statusCode": 200}
def test_invalid_logging_config(run_from_file_directory, caplog, log_config):
    environ._load_config_from_file("api_response_wrapper_config.json")
    from aws_serverless_wrapper.serverless_handler import (
        LambdaHandlerOfFunction, )

    event = compose_ReST_event(
        httpMethod="POST",
        resource="/test_request_resource/{path_level1}/{path_level2}",
        pathParameters={
            "path_level1": "path_value1",
            "path_level2": "path_value2"
        },
        body=test_body)

    from jsonschema.exceptions import ValidationError
    with raises(ValidationError):
        LambdaHandlerOfFunction(api_basic, **log_config).wrap_lambda(
            event.copy(), context)
def test_200_single_string(response_validation_env, caplog):
    from aws_serverless_wrapper.serverless_handler import (
        LambdaHandlerOfFunction,
    )

    event = compose_ReST_event(
        httpMethod="POST",
        resource="/test_response_resource",
        body={"response_statusCode": 200, "response_body": "single_allowed_answer"},
    )

    expected_response = {
        "statusCode": 200,
        "body": "single_allowed_answer",
        "headers": {"Content-Type": "text/plain"},
    }
    response = LambdaHandlerOfFunction(response_test).wrap_lambda(event, fake_context)
    assert response == expected_response
    assert len(caplog.messages) == 0
def test_unspecified_status_code_response(response_validation_env, caplog):
    from aws_serverless_wrapper.serverless_handler import (
        LambdaHandlerOfFunction,
    )

    event = compose_ReST_event(
        httpMethod="POST",
        resource="/test_response_resource",
        body={"response_statusCode": 418, "response_body": "I'm a teapot"},
    )

    expected_response = {
        "statusCode": 418,
        "body": "I'm a teapot",
        "headers": {"Content-Type": "text/plain"},
    }
    response = LambdaHandlerOfFunction(response_test).wrap_lambda(event, fake_context)
    assert response == expected_response
    assert len(caplog.messages) == 1
    assert "no specified response schema available for statusCode 418" in caplog.text
def test_200_single_string_with_internal_server_error_configured(
    response_validation_env, caplog
):
    environ["API_RESPONSE_VERIFICATION"]["RETURN_INTERNAL_SERVER_ERROR"] = True
    from aws_serverless_wrapper.serverless_handler import (
        LambdaHandlerOfFunction,
    )

    event = compose_ReST_event(
        httpMethod="POST",
        resource="/test_response_resource",
        body={"response_statusCode": 200, "response_body": "single_allowed_answer"},
    )

    expected_response = {
        "statusCode": 200,
        "body": "single_allowed_answer",
        "headers": {"Content-Type": "text/plain"},
    }
    response = LambdaHandlerOfFunction(response_test).wrap_lambda(event, fake_context)
    assert response == expected_response
    assert len(caplog.messages) == 0