def test_handler(lambda_module, apigateway_event, order, context):
    """
    Test handler()
    """

    # Stub boto3
    table = stub.Stubber(lambda_module.table.meta.client)
    response = {
        "Item": {k: TypeSerializer().serialize(v)
                 for k, v in order.items()},
        # We do not use ConsumedCapacity
        "ConsumedCapacity": {}
    }
    expected_params = {
        "TableName": lambda_module.TABLE_NAME,
        "Key": {
            "orderId": order["orderId"]
        }
    }
    table.add_response("get_item", response, expected_params)
    table.activate()

    # Send request
    response = lambda_module.handler(apigateway_event, context)

    # Remove stub
    table.assert_no_pending_responses()
    table.deactivate()

    assert response["statusCode"] == 200
    assert "body" in response
    body = json.loads(response["body"])
    compare_dict(order, body)
def test_handler(monkeypatch, lambda_module, context, order):
    """
    Test handler()
    """

    def validate_true(order: dict) -> Tuple[bool, str]:
        return (True, "")

    def store_order(order: dict) -> None:
        pass

    monkeypatch.setattr(lambda_module, "validate_delivery", validate_true)
    monkeypatch.setattr(lambda_module, "validate_payment", validate_true)
    monkeypatch.setattr(lambda_module, "validate_products", validate_true)
    monkeypatch.setattr(lambda_module, "store_order", store_order)

    user_id = order["userId"]
    order = copy.deepcopy(order)
    del order["userId"]

    response = lambda_module.handler({
        "order": order,
        "userId": user_id
    }, context)

    print(response)
    assert response["success"] == True
    assert len(response.get("errors", [])) == 0
    assert "order" in response
    compare_dict(order, response["order"])
def test_get_order(lambda_module, order):
    """
    Test get_order()
    """

    # Stub boto3
    table = stub.Stubber(lambda_module.table.meta.client)
    response = {
        "Item": {k: TypeSerializer().serialize(v)
                 for k, v in order.items()},
        # We do not use ConsumedCapacity
        "ConsumedCapacity": {}
    }
    expected_params = {
        "TableName": lambda_module.TABLE_NAME,
        "Key": {
            "orderId": order["orderId"]
        }
    }
    table.add_response("get_item", response, expected_params)
    table.activate()

    # Gather orders
    ddb_order = lambda_module.get_order(order["orderId"])

    # Remove stub
    table.assert_no_pending_responses()
    table.deactivate()

    # Check response
    compare_dict(order, ddb_order)
def test_get_backend_order(endpoint_url, iam_auth, order):
    """
    Test GET /backend/{orderId}
    """

    res = requests.get(endpoint_url+"/backend/"+order["orderId"], auth=iam_auth)
    assert res.status_code == 200
    body = res.json()
    compare_dict(order, body)
Beispiel #5
0
def test_create_order(function_arn, table_name, order_request):
    """
    Test the CreateOrder function
    """

    order_request = copy.deepcopy(order_request)

    table = boto3.resource("dynamodb").Table(table_name)  #pylint: disable=no-member
    lambda_ = boto3.client("lambda")

    # Trigger the function
    response = lambda_.invoke(FunctionName=function_arn,
                              InvocationType="RequestResponse",
                              Payload=json.dumps(order_request).encode())
    response = json.load(response["Payload"])

    print(response)

    # Check the output of the Function
    assert response["success"] == True
    assert "order" in response
    assert len(response.get("errors", [])) == 0

    del order_request["order"]["products"]
    compare_dict(order_request["order"], response["order"])
    assert response["order"]["userId"] == order_request["userId"]

    # Check the table
    ddb_response = table.get_item(
        Key={"orderId": response["order"]["orderId"]})
    assert "Item" in ddb_response

    mandatory_fields = [
        "orderId", "userId", "createdDate", "modifiedDate", "status",
        "products", "address", "deliveryPrice", "total"
    ]
    for field in mandatory_fields:
        assert field in ddb_response["Item"]

    assert ddb_response["Item"]["status"] == "NEW"

    compare_dict(order_request["order"], ddb_response["Item"])

    # Cleanup the table
    table.delete_item(Key={"orderId": response["order"]["orderId"]})
def test_backend_validate_mixed(endpoint_url, iam_auth, product):
    """
    Test /backend/validate with a mix of correct and incorrect product
    """

    wrong_product = copy.deepcopy(product)
    wrong_product["price"] += 100

    res = requests.post(
        "{}/backend/validate".format(endpoint_url),
        auth=iam_auth(endpoint_url),
        json={"products": [product, wrong_product]}
    )

    assert res.status_code == 200
    body = res.json()
    assert "products" in body
    assert len(body["products"]) == 1
    compare_dict(body["products"][0], product)
def test_backend_validate_incorrect_package(endpoint_url, iam_auth, product):
    """
    Test POST /backend/validate with an incorrect product
    """

    wrong_product = copy.deepcopy(product)

    wrong_product["package"]["height"] += 100

    res = requests.post(
        "{}/backend/validate".format(endpoint_url),
        auth=iam_auth(endpoint_url),
        json={"products": [wrong_product]}
    )

    assert res.status_code == 200
    body = res.json()
    assert "products" in body
    assert len(body["products"]) == 1
    compare_dict(body["products"][0], product)