def test_post_an_asset(client):
    """
    Post one extra asset, as an admin user.
    TODO: Soon we'll allow creating assets on an account-basis, i.e. for users
          who have the user role "account-admin" or sthg similar. Then we'll
          test that here.
    """
    auth_token = get_auth_token(client, "*****@*****.**", "testtest")
    post_data = get_asset_post_data()
    post_assets_response = client.post(
        url_for("flexmeasures_api_v2_0.post_assets"),
        json=post_data,
        headers={
            "content-type": "application/json",
            "Authorization": auth_token
        },
    )
    print("Server responded with:\n%s" % post_assets_response.json)
    assert post_assets_response.status_code == 201
    assert post_assets_response.json["latitude"] == 30.1

    asset: Asset = Asset.query.filter(
        Asset.name == "Test battery 2").one_or_none()
    assert asset is not None
    assert asset.capacity_in_mw == 3
Beispiel #2
0
def test_posting_multiple_assets(client):
    """We can only send one at a time"""
    with UserContext("*****@*****.**") as prosumer:
        auth_token = prosumer.get_auth_token()
    post_data1 = get_asset_post_data()
    post_data2 = get_asset_post_data()
    post_data2["name"] = "Test battery 3"
    asset_creation = client.post(
        url_for("flexmeasures_api_v2_0.post_assets"),
        json=[post_data1, post_data2],
        headers={
            "content-type": "application/json",
            "Authorization": auth_token
        },
    )
    print(f"Response: {asset_creation.json}")
    assert asset_creation.status_code == 422
    assert asset_creation.json["json"]["_schema"][0] == "Invalid input type."
Beispiel #3
0
def test_post_an_asset_with_nonexisting_field(client):
    """Posting a field that is unexpected leads to a 422"""
    with UserContext("*****@*****.**") as prosumer:
        auth_token = prosumer.get_auth_token()
    post_data = get_asset_post_data()
    post_data["nnname"] = "This field does not exist"
    asset_creation = client.post(
        url_for("flexmeasures_api_v2_0.post_assets"),
        json=post_data,
        headers={
            "content-type": "application/json",
            "Authorization": auth_token
        },
    )
    assert asset_creation.status_code == 422
    assert asset_creation.json["json"]["nnname"][0] == "Unknown field."
Beispiel #4
0
def test_post_an_asset_with_existing_name(client):
    """Catch DB error (Unique key violated) correctly"""
    with UserContext("*****@*****.**") as prosumer:
        auth_token = prosumer.get_auth_token()
        existing_asset = prosumer.assets[0]
    post_data = get_asset_post_data()
    post_data["name"] = existing_asset.name
    asset_creation = client.post(
        url_for("flexmeasures_api_v2_0.post_assets"),
        json=post_data,
        headers={
            "content-type": "application/json",
            "Authorization": auth_token
        },
    )
    assert asset_creation.status_code == 400
    assert "already exists" in asset_creation.json["message"]
    assert "asset_name" in asset_creation.json["detail"]
def test_post_an_asset_with_invalid_data(client, db):
    """
    Add an asset with some fields having invalid data and one field missing.
    The right error messages should be in the response and the number of assets has not increased.
    """
    with UserContext("*****@*****.**") as prosumer:
        num_assets_before = len(prosumer.assets)

    auth_token = get_auth_token(client, "*****@*****.**", "testtest")

    post_data = get_asset_post_data()
    post_data["latitude"] = 70.4
    post_data["longitude"] = 300.9
    post_data["capacity_in_mw"] = -100
    post_data["min_soc_in_mwh"] = 10
    post_data["max_soc_in_mwh"] = 5
    del post_data["unit"]

    post_asset_response = client.post(
        url_for("flexmeasures_api_v2_0.post_assets"),
        json=post_data,
        headers={
            "content-type": "application/json",
            "Authorization": auth_token
        },
    )
    print("Server responded with:\n%s" % post_asset_response.json)
    assert post_asset_response.status_code == 422

    assert (
        "Must be greater than or equal to 0"
        in post_asset_response.json["message"]["json"]["capacity_in_mw"][0])
    assert ("greater than or equal to -180 and less than or equal to 180"
            in post_asset_response.json["message"]["json"]["longitude"][0])
    assert "required field" in post_asset_response.json["message"]["json"][
        "unit"][0]
    assert ("must be equal or higher than the minimum soc"
            in post_asset_response.json["message"]["json"]["max_soc_in_mwh"])

    assert Asset.query.filter_by(
        owner_id=prosumer.id).count() == num_assets_before
Beispiel #6
0
def test_post_an_asset(client):
    """
    Post one extra asset, as the prosumer user (an admin).
    """
    auth_token = get_auth_token(client, "*****@*****.**", "testtest")
    post_data = get_asset_post_data()
    post_assets_response = client.post(
        url_for("flexmeasures_api_v2_0.post_assets"),
        json=post_data,
        headers={
            "content-type": "application/json",
            "Authorization": auth_token
        },
    )
    print("Server responded with:\n%s" % post_assets_response.json)
    assert post_assets_response.status_code == 201
    assert post_assets_response.json["latitude"] == 30.1

    asset: Asset = Asset.query.filter(
        Asset.name == "Test battery 2").one_or_none()
    assert asset is not None
    assert asset.capacity_in_mw == 3