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_unpack_without_message_type_id_in_json():
"""
OCPP must contain the MessageTypeID as first element of the message.
This test validates if correct exception is raised when this is not
the case
"""
with pytest.raises(ProtocolError):
unpack(json.dumps([]))
def test_unpack_with_invalid_json():
"""
Test that correct exception is raised when unpack is called with invalid
JSON.
"""
with pytest.raises(FormatViolationError):
unpack(b'\x01')
def test_unpack_without_jsonified_list():
"""
OCPP messages are JSONified lists. This test make sure that the correct
exception is raised when input is not a JSONified list.
"""
with pytest.raises(ProtocolError):
unpack(json.dumps('3'))
def test_unpack_with_invalid_message_type_id_in_json():
"""
OCPP messages only have 3 valid values for MessageTypeID, that is the first
element of the OCPP message. This test validates that correct exception is
raised when this value is invalid.
"""
with pytest.raises(PropertyConstraintViolationError):
unpack(json.dumps([5, 1]))
async def route_message(self, raw_msg):
"""
Route a message received from a CP.
If the message is a of type Call the corresponding hooks are executed.
If the message is of type CallResult or CallError the message is passed
to the call() function via the response_queue.
"""
try:
msg = unpack(raw_msg)
except OCPPError as e:
LOGGER.exception("Unable to parse message: '%s', it doesn't seem "
"to be valid OCPP: %s", raw_msg, e)
return
if msg.message_type_id == MessageType.Call:
await self._handle_call(msg)
elif msg.message_type_id in \
[MessageType.CallResult, MessageType.CallError]:
self._response_queue.put_nowait(msg)