def test_handler(monkeypatch, lambda_module, context, apigateway_event, payment_token, total): """ Test handler() """ def validate_payment_token(pt: str, a: int) -> bool: assert pt == payment_token assert a == total return True monkeypatch.setattr(lambda_module, "validate_payment_token", validate_payment_token) event = apigateway_event( iam="USER_ARN", body=json.dumps({ "paymentToken": payment_token, "total": total }) ) response = lambda_module.handler(event, context) assert response["statusCode"] == 200 assert "body" in response body = json.loads(response["body"]) assert "ok" in body assert body["ok"] == True
def test_handler(monkeypatch, lambda_module, context, apigateway_event): """ Test handler() """ connection_id = str(uuid.uuid4()) event = apigateway_event() event["requestContext"] = {"connectionId": connection_id} calls = { "delete_id": 0, "disable_rule": 0 } def delete_id(connection_id_req: str): calls["delete_id"] += 1 assert connection_id_req == connection_id monkeypatch.setattr(lambda_module, "delete_id", delete_id) def disable_rule(): calls["disable_rule"] += 1 monkeypatch.setattr(lambda_module, "disable_rule", disable_rule) result = lambda_module.handler(event, context) assert result["statusCode"] == 200 assert calls["delete_id"] == 1 assert calls["disable_rule"] == 1
def test_handler_missing_total(monkeypatch, lambda_module, context, apigateway_event, payment_token, total): """ Test handler() with a missing total """ def validate_payment_token(payment_token: str, total: int) -> bool: # This should never be called assert False return True monkeypatch.setattr(lambda_module, "validate_payment_token", validate_payment_token) event = apigateway_event( iam="USER_ARN", body=json.dumps({ "paymentToken": payment_token, }) ) response = lambda_module.handler(event, context) assert response["statusCode"] == 400 assert "body" in response body = json.loads(response["body"]) assert "ok" not in body assert "message" in body assert isinstance(body["message"], str) assert "total" in body["message"]
def test_handler_no_id(monkeypatch, lambda_module, context, apigateway_event): """ Test handler() without connectionId """ connection_id = str(uuid.uuid4()) service_name = "ecommerce.test" event = apigateway_event() event["body"] = json.dumps({"serviceName": service_name}) calls = { "register_service": 0 } def register_service(connection_id_req: str, service_name_req: str): calls["register_service"] += 1 assert connection_id_req == connection_id assert service_name_req == service_name monkeypatch.setattr(lambda_module, "register_service", register_service) result = lambda_module.handler(event, context) assert result["statusCode"] == 400 assert calls["register_service"] == 0
def test_handler_invalid_body(monkeypatch, lambda_module, context, apigateway_event): """ Test handler() without a correct JSON body """ connection_id = str(uuid.uuid4()) service_name = "ecommerce.test" event = apigateway_event() event["requestContext"] = {"connectionId": connection_id} event["body"] = "{" calls = { "register_service": 0 } def register_service(connection_id_req: str, service_name_req: str): calls["register_service"] += 1 assert connection_id_req == connection_id assert service_name_req == service_name monkeypatch.setattr(lambda_module, "register_service", register_service) result = lambda_module.handler(event, context) assert result["statusCode"] == 400 assert calls["register_service"] == 0
def test_handler_missing_products(monkeypatch, lambda_module, apigateway_event, context, product): """ Test the function handler with missing 'products' in request body """ def validate_products(products): assert False # This should never be called monkeypatch.setattr(lambda_module, "validate_products", validate_products) # Create request event = apigateway_event( iam="USER_ARN", body=json.dumps({}) ) # Parse request response = lambda_module.handler(event, context) # Check response assert response["statusCode"] == 400 # There should be a reason in the response body response_body = json.loads(response["body"]) assert "message" in response_body assert isinstance(response_body["message"], str)
def test_handler_correct(monkeypatch, lambda_module, apigateway_event, context, product): """ Test the function handler against an incorrect product """ def validate_products(products): return [], "" monkeypatch.setattr(lambda_module, "validate_products", validate_products) # Create request event = apigateway_event( iam="USER_ARN", body=json.dumps({"products": [product]}) ) # Parse request response = lambda_module.handler(event, context) # Check response assert response["statusCode"] == 200 # There should be 1 item in the response body response_body = json.loads(response["body"]) assert "message" in response_body assert isinstance(response_body["message"], str) assert "products" not in response_body
def test_handler(monkeypatch, lambda_module, context, apigateway_event, order): """ Test handler() """ event = apigateway_event(iam="USER_ARN", body=json.dumps({ "products": order["products"], "address": order["address"] })) def get_pricing(products: List[dict], address: dict) -> int: assert products == order["products"] assert address == order["address"] return 1000 monkeypatch.setattr(lambda_module, "get_pricing", get_pricing) retval = lambda_module.handler(event, context) assert "statusCode" in retval assert retval["statusCode"] == 200 assert "body" in retval body = json.loads(retval["body"]) assert "pricing" in body assert body["pricing"] == 1000
def test_handler_no_id(monkeypatch, lambda_module, context, apigateway_event): """ Test handler() """ connection_id = str(uuid.uuid4()) event = apigateway_event() calls = {"store_id": 0, "enable_rule": 0} def store_id(connection_id_req: str): calls["store_id"] += 1 assert connection_id_req == connection_id monkeypatch.setattr(lambda_module, "store_id", store_id) def enable_rule(): calls["enable_rule"] += 1 monkeypatch.setattr(lambda_module, "enable_rule", enable_rule) result = lambda_module.handler(event, context) assert result["statusCode"] == 400 assert calls["store_id"] == 0 assert calls["enable_rule"] == 0
def test_handler_no_address(lambda_module, context, apigateway_event, order): """ Test handler() with no IAM credentials """ event = apigateway_event(iam="USER_ARN", body=json.dumps({"products": order["products"]})) retval = lambda_module.handler(event, context) assert "statusCode" in retval assert retval["statusCode"] == 400 assert "body" in retval body = json.loads(retval["body"]) assert "message" in body
def test_handler_incorrect(lambda_module, apigateway_event, context, product): """ Test the function handler against an incorrect product """ product_incorrect = copy.deepcopy(product) product_incorrect["price"] += 200 # Create request event = apigateway_event( iam="USER_ARN", body=json.dumps({"products": [product_incorrect]}) ) # Stub boto3 table = stub.Stubber(lambda_module.table.meta.client) response = { "Item": {k: TypeSerializer().serialize(v) for k, v in product.items()} } expected_params = { "Key": {"productId": product["productId"]}, "ProjectionExpression": stub.ANY, "ExpressionAttributeNames": stub.ANY, "TableName": lambda_module.TABLE_NAME } table.add_response("get_item", response, expected_params) table.activate() # Parse request response = lambda_module.handler(event, context) # Remove stub table.deactivate() # Check response assert response["statusCode"] == 200 # There should be 1 item in the response body response_body = json.loads(response["body"]) assert "message" in response_body assert isinstance(response_body["message"], str) assert "products" in response_body assert len(response_body["products"]) == 1
def test_handler_missing_products(lambda_module, apigateway_event, context, product): """ Test the function handler with missing 'products' in request body """ # Create request event = apigateway_event( iam="USER_ARN", body=json.dumps({}) ) # Parse request response = lambda_module.handler(event, context) # Check response assert response["statusCode"] == 400 # There should be a reason in the response body response_body = json.loads(response["body"]) assert "message" in response_body assert isinstance(response_body["message"], str)