Пример #1
0
 def route(self, graph_name=None, context=None):
     try:
         if self._neo4j_version >= Version("4.0"):
             routers, readers, writers, ttl = self._route4(graph_name, context)
         else:
             routers, readers, writers, ttl = self._route1(graph_name, context)
         profiles, scheme = self._get_http_profiles()  # Convert Bolt addresses to HTTP
         return ([ConnectionProfile(_, scheme=scheme, address=profiles[_.address]) for _ in routers],
                 [ConnectionProfile(_, scheme=scheme, address=profiles[_.address]) for _ in readers],
                 [ConnectionProfile(_, scheme=scheme, address=profiles[_.address]) for _ in writers],
             ttl)
     finally:
         self.release()
Пример #2
0
def test_to_dict():
    p = ConnectionProfile()
    d = p.to_dict()
    assert d == {
        'address': IPv4Address(('localhost', 7687)),
        'host': 'localhost',
        'port': 7687,
        'port_number': 7687,
        'protocol': 'bolt',
        'scheme': 'bolt',
        'secure': False,
        'verify': True,
        'uri': 'bolt://neo4j@localhost:7687',
        'user': '******',
    }
Пример #3
0
    def open(cls,
             profile=None,
             user_agent=None,
             on_release=None,
             on_broken=None):
        """ Open and authenticate a Bolt connection.

        :param profile: :class:`.ConnectionProfile` detailing how and
            where to connect
        :param user_agent:
        :param on_release:
        :param on_broken:
        :returns: :class:`.Bolt` connection object
        :raises: :class:`.ConnectionUnavailable` if a connection cannot
            be opened, or a protocol version cannot be agreed
        """
        if profile is None:
            profile = ConnectionProfile(scheme="bolt")
        try:
            wire = cls._connect(profile, on_broken=on_broken)
            protocol_version = cls._handshake(wire)
            subclass = cls._get_subclass(protocol_version)
            if subclass is None:
                raise TypeError("Unable to agree supported protocol version")
            bolt = subclass(wire, profile, on_release=on_release)
            bolt._hello(user_agent or bolt_user_agent())
            return bolt
        except (TypeError, WireError) as error:
            raise_from(
                ConnectionUnavailable("Cannot open connection to %r" %
                                      profile), error)
Пример #4
0
 def _get_http_profiles(self):
     scheme = "https" if self.profile.secure else "http"
     profiles = {}
     try:
         result = self.auto_run("CALL dbms.cluster.overview")
         self.pull(result)
         while True:
             record = result.take()
             if record is None:
                 break
             key = None
             value = None
             for address in record[1]:
                 profile = ConnectionProfile(address)
                 if profile.scheme == "bolt":
                     key = profile.address
                 elif profile.scheme == scheme:
                     value = profile.address
                 else:
                     pass  # unusable value (should never happen)
             if key and value:
                 profiles[key] = value
     except Neo4jError as error:
         if error.title == "ProcedureNotFound":
             raise_from(TypeError("Neo4j service does not expose a cluster overview"), error)
         else:
             raise
     else:
         return profiles, scheme
Пример #5
0
def test_to_dict_with_password():
    p = ConnectionProfile()
    d = p.to_dict(include_password=True)
    assert d == {
        'address': IPv4Address(('localhost', 7687)),
        'auth': ('neo4j', 'password'),
        'host': 'localhost',
        'password': '******',
        'port': 7687,
        'port_number': 7687,
        'protocol': 'bolt',
        'scheme': 'bolt',
        'secure': False,
        'verify': True,
        'uri': 'bolt://localhost:7687',
        'user': '******',
    }
Пример #6
0
def test_profile_from_profile():
    prof1 = ConnectionProfile(password="******")
    prof2 = ConnectionProfile(prof1)
    data = dict(prof2)
    assert data == {
        'address': IPv4Address(('localhost', 7687)),
        'auth': ('neo4j', 'secret'),
        'host': 'localhost',
        'password': '******',
        'port': 7687,
        'port_number': 7687,
        'protocol': 'bolt',
        'scheme': 'bolt',
        'secure': False,
        'verify': True,
        'uri': 'bolt://neo4j@localhost:7687',
        'user': '******',
    }
Пример #7
0
 def accept(cls, wire, min_protocol_version=None):
     data = wire.read(20)
     if data[0:4] != BOLT_SIGNATURE:
         raise ProtocolError("Incoming connection did not provide Bolt signature")
     for major, minor in cls._proposed_versions(data, offset=4):
         if min_protocol_version and (major, minor) < min_protocol_version:
             continue
         try:
             subclass = Bolt._get_subclass((major, minor))
         except TypeError:
             continue
         else:
             wire.write(bytearray([0, 0, minor, major]))
             wire.send()
             return subclass(wire, ConnectionProfile(address=wire.remote_address))
     raise TypeError("Unable to agree supported protocol version")
Пример #8
0
def test_bolt_default_port():
    data = dict(ConnectionProfile("bolt://host"))
    assert data == {
        'address': IPv4Address(('host', 7687)),
        'auth': ('neo4j', 'password'),
        'host': 'host',
        'password': '******',
        'port': 7687,
        'port_number': 7687,
        'protocol': 'bolt',
        'scheme': 'bolt',
        'secure': False,
        'verify': True,
        'uri': 'bolt://host:7687',
        'user': '******',
    }
Пример #9
0
def test_bolt_uri_only():
    data = dict(ConnectionProfile("bolt://*****:*****@host:9999',
        'user': '******',
    }
Пример #10
0
def test_explicit_auth_tuple():
    data = dict(ConnectionProfile(auth=("bob", "secret")))
    assert data == {
        'address': IPv4Address(('localhost', 7687)),
        'auth': ('bob', 'secret'),
        'host': 'localhost',
        'password': '******',
        'port': 7687,
        'port_number': 7687,
        'protocol': 'bolt',
        'scheme': 'bolt',
        'secure': False,
        'verify': True,
        'uri': 'bolt://bob@localhost:7687',
        'user': '******',
    }
Пример #11
0
def test_secure_and_verify():
    data = dict(ConnectionProfile(secure=True, verify=True))
    assert data == {
        'address': IPv4Address(('localhost', 7687)),
        'auth': ('neo4j', 'password'),
        'host': 'localhost',
        'password': '******',
        'port': 7687,
        'port_number': 7687,
        'protocol': 'bolt',
        'scheme': 'bolt+s',
        'secure': True,
        'verify': True,
        'uri': 'bolt+s://neo4j@localhost:7687',
        'user': '******',
    }
Пример #12
0
def test_bolt_uri_with_user_and_password():
    data = dict(ConnectionProfile("bolt://*****:*****@host:9999"))
    assert data == {
        'address': IPv4Address(('host', 9999)),
        'auth': ('bob', 'secret'),
        'host': 'host',
        'password': '******',
        'port': 9999,
        'port_number': 9999,
        'protocol': 'bolt',
        'scheme': 'bolt',
        'secure': False,
        'verify': True,
        'uri': 'bolt://bob@host:9999',
        'user': '******',
    }
Пример #13
0
def test_uri_and_port():
    data = dict(ConnectionProfile("bolt://*****:*****@host:8888',
        'user': '******',
    }
Пример #14
0
def test_https_uri_without_secure():
    data = dict(ConnectionProfile("https://*****:*****@host:9999',
        'user': '******',
    }
Пример #15
0
def test_https_default_port():
    data = dict(ConnectionProfile("https://host"))
    assert data == {
        'address': IPv4Address(('host', 7473)),
        'auth': ('neo4j', 'password'),
        'host': 'host',
        'password': '******',
        'port': 7473,
        'port_number': 7473,
        'protocol': 'http',
        'scheme': 'https',
        'secure': True,
        'verify': True,
        'uri': 'https://neo4j@host:7473',
        'user': '******',
    }
Пример #16
0
def test_http_uri_wth_secure_and_no_verify():
    data = dict(
        ConnectionProfile("http://host:9999", secure=True, verify=False))
    assert data == {
        'address': IPv4Address(('host', 9999)),
        'auth': ('neo4j', 'password'),
        'host': 'host',
        'password': '******',
        'port': 9999,
        'port_number': 9999,
        'protocol': 'http',
        'scheme': 'http+ssc',
        'secure': True,
        'verify': False,
        'uri': 'http+ssc://host:9999',
        'user': '******',
    }
Пример #17
0
def test_profile_from_dict():
    dict1 = {
        'address': IPv4Address(('localhost', 7687)),
        'auth': ('neo4j', 'dictionary'),
        'host': 'localhost',
        'password': '******',
        'port': 7687,
        'port_number': 7687,
        'protocol': 'bolt',
        'scheme': 'bolt',
        'secure': False,
        'verify': True,
        'uri': 'bolt://localhost:7687',
        'user': '******',
    }
    prof1 = ConnectionProfile(dict1)
    data = dict(prof1)
    assert data == dict1
Пример #18
0
def test_loading_profile_from_file():
    filename = path.join(path.dirname(__file__),
                         "..", "resources", "example.ini")
    prof = ConnectionProfile.from_file(filename, "Neo4j")
    data = dict(prof)
    assert data == {
        'secure': False,
        'verify': True,
        'scheme': 'bolt',
        'user': '******',
        'password': '******',
        'address': IPv4Address(('graph.mystery.inc', 7777)),
        'auth': ('shaggy', 'velma'),
        'host': 'graph.mystery.inc',
        'port': 7777,
        'port_number': 7777,
        'protocol': 'bolt',
        'uri': 'bolt://[email protected]:7777',
    }
Пример #19
0
    def open(cls, profile=None, user_agent=None, on_release=None, on_broken=None):
        """ Open an HTTP connection to a server.

        :param profile: :class:`.ConnectionProfile` detailing how and
            where to connect
        :param user_agent:
        :param on_release:
        :param on_broken:
        :returns: :class:`.HTTP` connection object
        :raises: :class:`.ConnectionUnavailable` if a connection cannot
            be opened
        """
        if profile is None:
            profile = ConnectionProfile(scheme="http")
        try:
            http = cls(profile, on_release=on_release)
            http._hello(user_agent or http_user_agent())
            return http
        except HTTPError as error:
            raise_from(ConnectionUnavailable("Cannot open connection to %r", profile), error)
Пример #20
0
 def _get_routing_info(self, graph_name, query, parameters):
     try:
         result = self.auto_run(query, parameters, graph_name)
         self.pull(result)
         while True:
             record = result.take()
             if record is None:
                 break
             addresses = {}
             for a in record[1]:
                 addresses[a["role"]] = [
                     ConnectionProfile(self.profile, address=address)
                     for address in a["addresses"]
                 ]
             return (addresses.get("ROUTE", []), addresses.get("READ", []),
                     addresses.get("WRITE", []), record[0])
     except Neo4jError as error:
         if error.title == "ProcedureNotFound":
             raise_from(TypeError("Neo4j service does not support routing"),
                        error)
         else:
             raise
Пример #21
0
def test_get_non_existent_key():
    prof = ConnectionProfile()
    with raises(KeyError):
        _ = prof["routing"]
Пример #22
0
def test_profile_from_invalid_argument():
    with raises(TypeError):
        _ = ConnectionProfile(object())
Пример #23
0
def test_verify_env_var(monkeypatch):
    import py2neo
    monkeypatch.setattr(py2neo, "NEO4J_VERIFY", "1")
    prof = ConnectionProfile()
    assert prof.verify
Пример #24
0
def test_secure_env_var(monkeypatch):
    import py2neo
    monkeypatch.setattr(py2neo, "NEO4J_SECURE", "1")
    prof = ConnectionProfile()
    assert prof.secure
Пример #25
0
def test_auth_env_var(monkeypatch):
    import py2neo
    monkeypatch.setattr(py2neo, "NEO4J_AUTH", "alice:python")
    prof = ConnectionProfile()
    assert prof.auth == ("alice", "python")
Пример #26
0
def test_uri_env_var(monkeypatch):
    import py2neo
    monkeypatch.setattr(py2neo, "NEO4J_URI", "http://alice@somewhere:8899")
    prof = ConnectionProfile()
    assert prof.uri == "http://alice@somewhere:8899"
Пример #27
0
def test_length():
    prof1 = ConnectionProfile()
    assert len(prof1) == 12
Пример #28
0
def test_profile_repr():
    prof = ConnectionProfile()
    assert repr(prof).startswith("ConnectionProfile")
Пример #29
0
def test_profile_str():
    prof = ConnectionProfile()
    s = str(prof)
    assert s.startswith("«")
    assert s.endswith("»")
Пример #30
0
def test_explicit_address_string():
    profile = ConnectionProfile(address="victor:4291")
    assert profile.host == "victor"
    assert profile.port == 4291