Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_validate_payload_with_invalid_message_type_id():
"""
Test if validate_payload raises ValidationError when it is called with
a message type id other than 2, Call, or 3, CallError.
"""
with pytest.raises(ValidationError):
validate_payload(dict(), ocpp_version="1.6")
def test_validate_payload_with_invalid_payload():
"""
Test if validate_payload raises ValidationError when validation of
payload failes.
"""
message = CallResult(
unique_id="1234",
action="Heartbeat",
payload={'invalid_key': True},
)
with pytest.raises(ValidationError):
validate_payload(message, ocpp_version="1.6")
message.action == ['GetCompositeSchedule'])
):
schema = get_schema(
message.message_type_id, message.action,
ocpp_version, parse_float=decimal.Decimal
)
message.payload = json.loads(
json.dumps(message.payload), parse_float=decimal.Decimal
)
else:
schema = get_schema(
message.message_type_id, message.action, ocpp_version
)
except (OSError, json.JSONDecodeError) as e:
raise ValidationError("Failed to load validation schema for action "
f"'{message.action}': {e}")
try:
validate(message.payload, schema)
except SchemaValidationError as e:
raise ValidationError(f"Payload '{message.payload} for action "
f"'{message.action}' is not valid: {e}")