Skip to content

Commit

Permalink
String/Bytes fix
Browse files Browse the repository at this point in the history
  • Loading branch information
RedProkofiev authored and tofu-rocketry committed Feb 6, 2024
1 parent 9323a4a commit 1e8f3b0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
8 changes: 7 additions & 1 deletion ssm/ssm2.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,13 @@ def _handle_msg(self, text):

def _save_msg_to_queue(self, body, empaid):
"""Extract message contents and add to the accept or reject queue."""
try:
# if not bytes will fail with "'str' obj has no attribute decode"
body = body.decode('utf-8')
except (AttributeError):
# Message type is something string related
pass

extracted_msg, signer, err_msg = self._handle_msg(body)
try:
# If the message is empty or the error message is not empty
Expand All @@ -332,7 +339,6 @@ def _save_msg_to_queue(self, body, empaid):
body = extracted_msg

log.warning("Message rejected: %s", err_msg)

name = self._rejectq.add({'body': body,
'signer': signer,
'empaid': empaid,
Expand Down
10 changes: 10 additions & 0 deletions test/test_ssm.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import print_function

import os
Expand Down Expand Up @@ -93,6 +94,15 @@ def test_on_message(self):
# Check that msg with ID and no real content doesn't raise exception.
test_ssm.on_message({'empa-id': '012345'}, 'body')

def test_str_bytes_outgoing(self):
"""Test to ensure that outgoing messages are converted from Bytes to Str"""
test_ssm = Ssm2(self._brokers, self._msgdir, TEST_CERT_FILE,
self._key_path, dest=self._dest, listen=self._listen)

message = "Appelle Hippocampéléphantocamélos"
byte_message = message.encode()
test_ssm.on_message({'empa-id': '012345'}, byte_message)

def test_init_expired_cert(self):
"""Test right exception is thrown creating an SSM with expired cert."""
expected_error = ('Certificate %s has expired or will expire '
Expand Down

0 comments on commit 1e8f3b0

Please sign in to comment.