def test_wrong_body(run_from_file_directory): environ._load_config_from_file("api_response_wrapper_config.json") from aws_serverless_wrapper.serverless_handler import ( LambdaHandlerOfFunction, ) event = load_single( f"../schema_validation/test_data/api/request_basic.json") from json import dumps event["body"] = loads(event["body"]) event["body"]["body_key1"] = 123 event["body"] = dumps(event["body"]) response = LambdaHandlerOfFunction(api_basic).wrap_lambda(event, context) assert response == { "statusCode": 400, "body": "123 is not of type 'string'\n\n" "Failed validating 'type' in " "schema['properties']['body']['properties']['body_key1']:\n" " {'description': 'containing only a string', 'type': 'string'}\n\n" "On instance['body']['body_key1']:\n" " 123", "headers": { "Content-Type": "text/plain" }, }
def test_basic_function_run_through(run_from_file_directory): environ._load_config_from_file("api_response_wrapper_config.json") from aws_serverless_wrapper.serverless_handler import ( LambdaHandlerOfFunction, ) event = load_single( f"../schema_validation/test_data/api/request_basic.json") def api_basic(event_data): assert event_data == event LambdaHandlerOfFunction(api_basic).wrap_lambda(event, context)
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_function_run_through(run_from_file_directory): from aws_serverless_wrapper import aws_serverless_wrapper environ._load_config_from_file("api_response_wrapper_config.json") event = load_single( f"../schema_validation/test_data/api/request_basic.json") @aws_serverless_wrapper def api_basic(event_data): assert event_data == event response = api_basic(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_class_run_through(run_from_file_directory): from aws_serverless_wrapper import aws_serverless_wrapper, ServerlessBaseClass environ._load_config_from_file("api_response_wrapper_config.json") event = load_single( f"../schema_validation/test_data/api/request_basic.json") @aws_serverless_wrapper class api_basic(ServerlessBaseClass): def main(self): assert self.event == event assert self.context == context response = api_basic(event, context) assert response["statusCode"] == 200
def test_wrapper_with_charset_utf8_content_type(run_from_file_directory): from aws_serverless_wrapper import aws_serverless_wrapper environ._load_config_from_file("api_response_wrapper_config.json") event = load_single( f"../schema_validation/test_data/api/request_basic.json") event["headers"]["Content-Type"] += "; charset=utf-8" @aws_serverless_wrapper def api_basic(event_data): assert event_data == event response = api_basic(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_base_class_missing_handler(run_from_file_directory): from aws_serverless_wrapper import aws_serverless_wrapper, ServerlessBaseClass environ["ERROR_LOG"] = {"API_RESPONSE": True} @aws_serverless_wrapper class api_basic(ServerlessBaseClass): pass event = load_single( f"../schema_validation/test_data/api/request_basic.json") with raises(TypeError) as te: response = api_basic(event, context) assert ( te.value.args[0] == f"Can't instantiate abstract class api_basic with abstract methods main" )
def test_function_occurring_exception(run_from_file_directory): def api_basic(): raise Exception("test") environ._load_config_from_file("api_response_wrapper_config.json") from aws_serverless_wrapper.serverless_handler import ( LambdaHandlerOfFunction, ) event = load_single( f"../schema_validation/test_data/api/request_basic.json") response = LambdaHandlerOfFunction(api_basic).wrap_lambda(event, context) assert response == { "statusCode": 500, "body": "internal server error", "headers": { "Content-Type": "text/plain" }, }
def test_class_run_through_with_response(run_from_file_directory): from aws_serverless_wrapper import aws_serverless_wrapper, ServerlessBaseClass @aws_serverless_wrapper class api_basic(ServerlessBaseClass): def main(self): return { "statusCode": 400, "body": "some response text", "headers": { "Content-Type": "text/plain" }, } environ._load_config_from_file("api_response_wrapper_config.json") event = load_single( f"../schema_validation/test_data/api/request_basic.json") response = api_basic(event, context) assert response["statusCode"] == 400 assert response["body"] == "some response text" assert response["headers"] == {"Content-Type": "text/plain"}
def test_basic_function_run_through_no_verification(run_from_file_directory, caplog): environ._load_config_from_file("api_response_wrapper_config.json") from aws_serverless_wrapper.serverless_handler import ( LambdaHandlerOfFunction, ) event = load_single( f"../schema_validation/test_data/api/request_basic.json") def api_basic(event_data): assert event_data == event return { "statusCode": 200, "body": "Faulty Data", "headers": { "Content-Type": "text/plain" } } event["body"] = "wrong_body" assert LambdaHandlerOfFunction(api_basic).wrap_lambda( event, context)["statusCode"] != 200 assert len(caplog.messages) == 0 assert LambdaHandlerOfFunction(api_basic, PARSE_BODY=False, API_INPUT_VERIFICATION=False).wrap_lambda( event, context)["statusCode"] == 200 assert len(caplog.messages) == 1 assert "no specified response schema available" in caplog.text caplog.clear() environ._load_config_from_file("api_response_wrapper_config.json") assert LambdaHandlerOfFunction( api_basic, PARSE_BODY=False, API_INPUT_VERIFICATION=False, API_RESPONSE_VERIFICATION=False).wrap_lambda( event, context)["statusCode"] == 200 assert len(caplog.messages) == 0
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_function_occurring_exception_with_error_log(run_from_file_directory): def api_basic(): 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 ( LambdaHandlerOfFunction, ) event = load_single( f"../schema_validation/test_data/api/request_basic.json") response = LambdaHandlerOfFunction(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", }