def run(self, result=None): try: with MockMeshApplication() as mock_app: self.mock_app = mock_app self.uri = mock_app.uri with MeshClient(self.uri, alice_mailbox, alice_password, max_chunk_size=5, **default_ssl_opts) as alice: self.alice = alice self.bob = MeshClient(self.uri, bob_mailbox, bob_password, max_chunk_size=5, **default_ssl_opts) super(MeshClientTest, self).run(result) except HTTPError as e: print(e.read()) print_stack_frames() print("Message store", self.mock_app.messages) raise except: print_stack_frames() print("Message store", self.mock_app.messages) raise
def run(self, result=None): self.chunk_retry_call_counts = {} try: with MockMeshChunkRetryApplication(shared_key) as mock_app: self.mock_app = mock_app self.uri = mock_app.uri self.alice = MeshClient(self.uri, alice_mailbox, alice_password, shared_key, max_chunk_size=5, max_chunk_retries=3, **default_ssl_opts) self.bob = MeshClient(self.uri, bob_mailbox, bob_password, shared_key, max_chunk_size=5, max_chunk_retries=3, **default_ssl_opts) super(MeshChunkRetryClientTest, self).run(result) except HTTPError as e: print(e.read()) print_stack_frames() print("Message store", self.mock_app.messages) raise except: print_stack_frames() print("Message store", self.mock_app.messages) raise
def build_mesh_client(secret_manager: SsmSecretManager, mesh_mailbox_name: str): mesh_url = os.environ["MESH_URL"] mesh_client_cert_path = "/tmp/client_cert.pem" mesh_client_key_path = "/tmp/client_key.pem" mesh_ca_cert_path = "/tmp/ca_cert.pem" secret_manager.download_secret( os.environ["MESH_CLIENT_CERT_SSM_PARAM_NAME"], mesh_client_cert_path) secret_manager.download_secret( os.environ["MESH_CLIENT_KEY_SSM_PARAM_NAME"], mesh_client_key_path) secret_manager.download_secret(os.environ["MESH_CA_CERT_SSM_PARAM_NAME"], mesh_ca_cert_path) mesh_password = secret_manager.get_secret( os.environ["MESH_PASSWORD_SSM_PARAM_NAME"]) mesh_shared_key = secret_manager.get_secret( os.environ["MESH_SHARED_KEY_SSM_PARAM_NAME"]) client = MeshClient(url=mesh_url, mailbox=mesh_mailbox_name, password=mesh_password, shared_key=bytes(mesh_shared_key, "utf-8"), verify=mesh_ca_cert_path, cert=(mesh_client_cert_path, mesh_client_key_path)) client.handshake() return client
def test_timeout(self): with SlowMockMeshApplication() as mock_app: endpoint = LOCAL_MOCK_ENDPOINT._replace(url=mock_app.uri) client = MeshClient(endpoint, alice_mailbox, alice_password, max_chunk_size=5, timeout=0.5) with self.assertRaises(requests.exceptions.Timeout): client.list_messages()
def test_handshake(self): with MockMeshApplication() as mock_app: endpoint = LOCAL_MOCK_ENDPOINT._replace(url=mock_app.uri) client = MeshClient(endpoint, alice_mailbox, alice_password, max_chunk_size=5) hand_shook = client.handshake() self.assertEqual(hand_shook, b"hello")
def setUp(self): self.alice_mailbox = str(random.randint(0, 1000000000000)) self.bob_mailbox = str(random.randint(0, 1000000000000)) self.alice = MeshClient(self.uri, self.alice_mailbox, 'password', max_chunk_size=5, **default_ssl_opts) self.bob = MeshClient(self.uri, self.bob_mailbox, 'password', max_chunk_size=5, **default_ssl_opts)
class MeshClientTest(TestCase): uri = 'https://localhost:8829' @classmethod def setUpClass(cls): cls.server = make_server(host='127.0.0.1', port=8829, logging=True) cls.server_thread = threading.Thread(target=cls.server.start) cls.server_thread.start() @classmethod def tearDownClass(cls): cls.server.stop() cls.server_thread.join() def setUp(self): self.alice_mailbox = str(random.randint(0, 1000000000000)) self.bob_mailbox = str(random.randint(0, 1000000000000)) self.alice = MeshClient( self.uri, self.alice_mailbox, 'password', max_chunk_size=5, **default_ssl_opts) self.bob = MeshClient( self.uri, self.bob_mailbox, 'password', max_chunk_size=5, **default_ssl_opts) def test_handshake(self): alice = self.alice hand_shook = alice.handshake() self.assertEqual(hand_shook, b"hello") def test_send_receive(self): alice = self.alice bob = self.bob message_id = alice.send_message(self.bob_mailbox, b"Hello Bob 1") self.assertEqual([message_id], bob.list_messages()) msg = bob.retrieve_message(message_id) self.assertEqual(msg.read(), b"Hello Bob 1") self.assertEqual(msg.sender, self.alice_mailbox) self.assertEqual(msg.recipient, self.bob_mailbox) self.assertEqual(msg.filename, message_id + '.dat') msg.acknowledge() self.assertEqual([], bob.list_messages()) def test_optional_args(self): alice = self.alice bob = self.bob message_id = alice.send_message( self.bob_mailbox, b"Hello Bob 5", subject="Hello World", filename="upload.txt", local_id="12345", message_type="DATA", process_id="321", workflow_id="111", encrypted=False, compressed=False) with bob.retrieve_message(message_id) as msg: self.assertEqual(msg.subject, "Hello World") self.assertEqual(msg.filename, "upload.txt") self.assertEqual(msg.local_id, "12345") self.assertEqual(msg.message_type, "DATA") self.assertEqual(msg.process_id, "321") self.assertEqual(msg.workflow_id, "111") self.assertFalse(msg.encrypted) self.assertFalse(msg.compressed) message_id = alice.send_message( self.bob_mailbox, b"Hello Bob 5", encrypted=True, compressed=True) with bob.retrieve_message(message_id) as msg: self.assertTrue(msg.encrypted) self.assertTrue(msg.compressed) def test_endpoint_lookup(self): result = self.alice.lookup_endpoint('ORG1', 'WF1') result_list = result['results'] self.assertEqual(len(result_list), 1) self.assertEqual(result_list[0]['address'], 'ORG1HC001') self.assertEqual(result_list[0]['description'], 'ORG1 WF1 endpoint') self.assertEqual(result_list[0]['endpoint_type'], 'MESH') def test_tracking(self): alice = self.alice bob = self.bob tracking_id = 'Message1' msg_id = alice.send_message(self.bob_mailbox, b'Hello World', local_id=tracking_id) self.assertEqual(alice.get_tracking_info(message_id=msg_id)['status'], 'Accepted') self.assertIsNone(alice.get_tracking_info(message_id=msg_id)['downloadTimestamp']) bob.retrieve_message(msg_id).read() self.assertIsNotNone(alice.get_tracking_info(message_id=msg_id)['downloadTimestamp']) bob.acknowledge_message(msg_id) self.assertEqual(alice.get_tracking_info(message_id=msg_id)['status'], 'Acknowledged') def test_msg_id_tracking(self): alice = self.alice bob = self.bob msg_id = alice.send_message(self.bob_mailbox, b'Hello World') self.assertEqual(alice.get_tracking_info(message_id=msg_id)['status'], 'Accepted') self.assertIsNone(alice.get_tracking_info(message_id=msg_id)['downloadTimestamp']) bob.retrieve_message(msg_id).read() self.assertIsNotNone(alice.get_tracking_info(message_id=msg_id)['downloadTimestamp']) bob.acknowledge_message(msg_id) self.assertEqual(alice.get_tracking_info(message_id=msg_id)['status'], 'Acknowledged')