def test_receive_valid_json_valid_message_from_old_peer(self): """ A good message is received then the node handles the message as expected. The cached protocol object for the peer node is expired since a new protocol object is used in this instance. """ nc = NetstringConnector(self.event_loop) old_protocol = mock.MagicMock() network_id = sha512(PUBLIC_KEY.encode('ascii')).hexdigest() nc._connections[network_id] = old_protocol ok = { 'uuid': str(uuid.uuid4()), 'recipient': PUBLIC_KEY, 'sender': PUBLIC_KEY, 'reply_port': 1908, 'version': self.version, } seal = get_seal(ok, PRIVATE_KEY) ok['seal'] = seal ok['message'] = 'ok' raw = json.dumps(ok) sender = '192.168.0.1' handler = Node(PUBLIC_KEY, PRIVATE_KEY, self.event_loop, nc, 1908) handler.message_received = mock.MagicMock() protocol = mock.MagicMock() nc.receive(raw, sender, handler, protocol) self.assertIn(network_id, nc._connections) self.assertEqual(nc._connections[network_id], protocol) msg = from_dict(ok) handler.message_received.assert_called_once_with(msg, 'netstring', sender, msg.reply_port)
def test_receive(self): """ The good case. Should return whatever handler.message_received returns. """ connector = HttpConnector(self.event_loop) ok = { 'uuid': str(uuid.uuid4()), 'recipient': PUBLIC_KEY, 'sender': PUBLIC_KEY, 'reply_port': 1908, 'version': self.version, } seal = get_seal(ok, PRIVATE_KEY) ok['seal'] = seal ok['message'] = 'ok' raw = json.dumps(ok).encode('utf-8') sender = '192.168.0.1' handler = Node(PUBLIC_KEY, PRIVATE_KEY, self.event_loop, connector, 1908) handler.message_received = mock.MagicMock() connector.receive(raw, sender, handler) msg = from_dict(ok) handler.message_received.assert_called_once_with(msg, 'http', sender, msg.reply_port)