def test_basic_class_run_through(run_from_file_directory):
    class api_basic(ServerlessBaseClass):
        def main(self) -> dict:
            pass

    environ._load_config_from_file("api_response_wrapper_config.json")
    from aws_serverless_wrapper.serverless_handler import LambdaHandlerOfClass

    event = load_single(
        f"../schema_validation/test_data/api/request_basic.json")
    response = LambdaHandlerOfClass(api_basic).wrap_lambda(event, context)
    assert response["statusCode"] == 200
def test_different_named_class_run_through(run_from_file_directory):
    class EventHandler(ServerlessBaseClass):
        def main(self) -> dict:
            pass

    environ._load_config_from_file("api_response_wrapper_config.json")
    environ["API_INPUT_VERIFICATION"][
        "SCHEMA_DIRECTORY"] = "../schema_validation/test_data/api/test_request_resource||{path_level1}||{path_level2}.json"
    from aws_serverless_wrapper.serverless_handler import LambdaHandlerOfClass

    event = load_single(
        f"../schema_validation/test_data/api/request_basic.json")
    response = LambdaHandlerOfClass(EventHandler).wrap_lambda(event, context)
    assert response["statusCode"] == 200
def test_unexpected_exception(run_from_file_directory):
    environ._load_config_from_file("api_response_wrapper_config.json")

    from aws_serverless_wrapper.serverless_handler import LambdaHandlerOfClass

    event = load_single(
        f"../schema_validation/test_data/api/request_basic.json")

    response = LambdaHandlerOfClass(RaiseUnexpectedException).wrap_lambda(
        event, context)

    assert response == {
        "statusCode": 500,
        "body": "internal server error",
        "headers": {
            "Content-Type": "text/plain"
        },
    }
def test_class_occurring_exception(run_from_file_directory):
    class api_basic(ServerlessBaseClass):
        def main(self) -> dict:
            raise Exception("test")

    environ._load_config_from_file("api_response_wrapper_config.json")
    from aws_serverless_wrapper.serverless_handler import LambdaHandlerOfClass

    event = load_single(
        f"../schema_validation/test_data/api/request_basic.json")

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

    assert response == {
        "statusCode": 500,
        "body": "internal server error",
        "headers": {
            "Content-Type": "text/plain"
        },
    }
def test_expected_exception_and_return_api_response(run_from_file_directory):
    environ._load_config_from_file("api_response_wrapper_config.json")
    from aws_serverless_wrapper._environ_variables import NoExceptDict

    environ["ERROR_LOG"] = NoExceptDict({"API_RESPONSE": True})

    from aws_serverless_wrapper.serverless_handler import LambdaHandlerOfClass

    event = load_single(
        f"../schema_validation/test_data/api/request_basic.json")

    response = LambdaHandlerOfClass(RaiseExpectedException,
                                    with_context=True).wrap_lambda(
                                        event, context)
    response["body"] = loads(response["body"])

    assert response == {
        "statusCode": 500,
        "body": {
            "basic": "internal server error",
            "error_log_item": {
                "body": "item in db not found",
                "lambda_name": "test_function",
                "service_name": "group",
                "function_version": "$LATEST",
                "headers": {
                    "Content-Type": "text/plain"
                },
                "aws_log_group": "test/log/group",
                "aws_request_id": "uuid",
                "statusCode": 404,
                "timestamp": 1577836800.0,
            },
        },
        "headers": {
            "Content-Type": "application/json"
        },
    }
def test_class_occurring_exception_with_error_log(run_from_file_directory):
    class api_basic(ServerlessBaseClass):
        def main(self) -> dict:
            raise Exception("test")

    environ._load_config_from_file("api_response_wrapper_config.json")

    environ["ERROR_LOG"] = {"API_RESPONSE": True}

    from aws_serverless_wrapper.serverless_handler import LambdaHandlerOfClass

    event = load_single(
        f"../schema_validation/test_data/api/request_basic.json")

    response = LambdaHandlerOfClass(api_basic).wrap_lambda(event, context)
    response["body"] = loads(response["body"])

    assert response["statusCode"] == 500
    assert response["headers"] == {"Content-Type": "application/json"}
    assert len(response["body"]) == 2
    assert len(response["body"]["error_log_item"]) == 12

    assert response["body"]["basic"] == "internal server error"
    assert set(response["body"]["error_log_item"].keys()) == {
        "aws_request_id",
        "aws_log_group",
        "lambda_name",
        "service_name",
        "function_version",
        "timestamp",
        "exception_type",
        "exception_text",
        "exception_file",
        "exception_line_no",
        "exception_function",
        "exception_stack",
    }