def do_connect(): connect_message = ConnectionRequest(endpoint="endpoint") roles = {"network": AuthorizationType.TRUST} network = MockNetwork(roles) handler = ConnectHandler(network) handler_status = handler.handle("connection_id", connect_message.SerializeToString()) return handler_status
def test_connect_bad_endpoint(self): """ Test the ConnectHandler correctly responds to a ConnectionRequest. """ connect_message = ConnectionRequest(endpoint="tcp://0.0.0.0:8800") roles = {"network": AuthorizationType.TRUST} network = MockNetwork(roles) handler = ConnectHandler(network) handler_status = handler.handle("connection_id", connect_message.SerializeToString()) self.assertEqual(handler_status.status, HandlerStatus.RETURN_AND_CLOSE) self.assertEqual( handler_status.message_type, validator_pb2.Message.AUTHORIZATION_CONNECTION_RESPONSE)
def test_connect_bad_role_type(self): """ Test the ConnectHandler closes the connection if the role has an unsupported role type. """ connect_message = ConnectionRequest(endpoint="endpoint") roles = {"network": "other"} network = MockNetwork(roles) handler = ConnectHandler(network) handler_status = handler.handle("connection_id", connect_message.SerializeToString()) self.assertEqual(handler_status.status, HandlerStatus.RETURN_AND_CLOSE) self.assertEqual( handler_status.message_type, validator_pb2.Message.AUTHORIZATION_CONNECTION_RESPONSE)
def test_connect_not_allowing_incoming_connections(self): """ Test the ConnectHandler closes a connection if we are not accepting incoming connections """ connect_message = ConnectionRequest(endpoint="endpoint") roles = {"network": AuthorizationType.TRUST} network = MockNetwork(roles, allow_inbound=False) handler = ConnectHandler(network) handler_status = handler.handle("connection_id", connect_message.SerializeToString()) self.assertEqual(handler_status.status, HandlerStatus.RETURN_AND_CLOSE) self.assertEqual( handler_status.message_type, validator_pb2.Message.AUTHORIZATION_CONNECTION_RESPONSE)
def test_connect_wrong_previous_message(self): """ Test the ConnectHandler closes a connection if any authorization message has been recieved before this connection request. """ connect_message = ConnectionRequest(endpoint="endpoint") roles = {"network": AuthorizationType.TRUST} network = MockNetwork(roles, connection_status={"connection_id": "other"}) handler = ConnectHandler(network) handler_status = handler.handle("connection_id", connect_message.SerializeToString()) self.assertEqual(handler_status.status, HandlerStatus.RETURN_AND_CLOSE) self.assertEqual( handler_status.message_type, validator_pb2.Message.AUTHORIZATION_CONNECTION_RESPONSE)