def test_posting_multiple_assets(client, setup_api_test_data): """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("AssetAPI:post"), 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["message"]["json"]["_schema"][0] == "Invalid input type."
def test_post_an_asset_as_admin(client, setup_api_fresh_test_data, admin_kind): """ Post one extra asset, as an admin user. """ with AccountContext("Test Prosumer Account") as prosumer: post_data = get_asset_post_data( account_id=prosumer.id, asset_type_id=prosumer.generic_assets[0].generic_asset_type.id, ) if admin_kind == "site-admin": auth_token = get_auth_token(client, "*****@*****.**", "testtest") else: auth_token = get_auth_token(client, "*****@*****.**", "testtest") post_data["name"] = "Test battery 3" post_assets_response = client.post( url_for("AssetAPI:post"), 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: GenericAsset = GenericAsset.query.filter( GenericAsset.name == post_data["name"]).one_or_none() assert asset is not None assert asset.latitude == 30.1
def test_post_an_asset_with_nonexisting_field(client, setup_api_test_data): """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("AssetAPI:post"), json=post_data, headers={"content-type": "application/json", "Authorization": auth_token}, ) assert asset_creation.status_code == 422 assert asset_creation.json["message"]["json"]["nnname"][0] == "Unknown field."
def test_post_an_asset_with_existing_name(client, setup_api_test_data): """Catch DB error (Unique key violated) correctly""" with UserContext("*****@*****.**") as admin_user: auth_token = admin_user.get_auth_token() with AccountContext("Test Prosumer Account") as prosumer: prosumer_id = prosumer.id existing_asset = prosumer.generic_assets[0] post_data = get_asset_post_data() post_data["name"] = existing_asset.name post_data["account_id"] = prosumer_id asset_creation_response = client.post( url_for("AssetAPI:post"), json=post_data, headers={"content-type": "application/json", "Authorization": auth_token}, ) print(f"Creation Response: {asset_creation_response.json}") assert asset_creation_response.status_code == 422 assert ( "already exists" in asset_creation_response.json["message"]["json"]["name"][0] )
def test_post_an_asset_with_invalid_data(client, setup_api_test_data): """ 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["name"] = "Something new" post_data["longitude"] = 300.9 del post_data["generic_asset_type_id"] post_asset_response = client.post( url_for("AssetAPI:post"), 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 ( "exceeds the maximum longitude" in post_asset_response.json["message"]["json"]["longitude"][0] ) assert ( "required field" in post_asset_response.json["message"]["json"]["generic_asset_type_id"][0] ) assert ( GenericAsset.query.filter_by(account_id=prosumer.id).count() == num_assets_before )