From 95ff47008f4246c6ce82bf0bb27b062fcd360063 Mon Sep 17 00:00:00 2001 From: Pontus Melke Date: Thu, 25 Feb 2016 14:18:28 +0100 Subject: [PATCH] Support for auth in driver --- example.py | 3 +- examples/test_examples.py | 26 +++++++------- neo4j/v1/__init__.py | 1 + neo4j/v1/auth.py | 27 ++++++++++++++ neo4j/v1/connection.py | 9 ++++- runtests.sh | 2 ++ test/tck/tck_util.py | 5 ++- test/test_session.py | 75 ++++++++++++++++++++------------------- test/util.py | 27 ++++++++++++++ 9 files changed, 120 insertions(+), 55 deletions(-) create mode 100644 neo4j/v1/auth.py diff --git a/example.py b/example.py index 321f2f2a5..538de7a38 100644 --- a/example.py +++ b/example.py @@ -19,8 +19,9 @@ # limitations under the License. from neo4j.v1.session import GraphDatabase +from neo4j.v1.auth import basic_auth -driver = GraphDatabase.driver("bolt://localhost") +driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")) session = driver.session() session.run("MERGE (a:Person {name:'Alice'})") diff --git a/examples/test_examples.py b/examples/test_examples.py index 001f9008c..2745047fa 100644 --- a/examples/test_examples.py +++ b/examples/test_examples.py @@ -22,7 +22,7 @@ from test.util import ServerTestCase # tag::minimal-example-import[] -from neo4j.v1 import GraphDatabase +from neo4j.v1 import GraphDatabase, basic_auth # end::minimal-example-import[] @@ -30,7 +30,7 @@ class FreshDatabaseTestCase(ServerTestCase): def setUp(self): ServerTestCase.setUp(self) - session = GraphDatabase.driver("bolt://localhost").session() + session = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")).session() session.run("MATCH (n) DETACH DELETE n") session.close() @@ -39,7 +39,7 @@ class MinimalWorkingExampleTestCase(FreshDatabaseTestCase): def test_minimal_working_example(self): # tag::minimal-example[] - driver = GraphDatabase.driver("bolt://localhost") + driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")) session = driver.session() session.run("CREATE (neo:Person {name:'Neo', age:23})") @@ -56,7 +56,7 @@ class ExamplesTestCase(FreshDatabaseTestCase): def test_construct_driver(self): # tag::construct-driver[] - driver = GraphDatabase.driver("bolt://localhost") + driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")) # end::construct-driver[] return driver @@ -85,7 +85,7 @@ def test_tls_signed(self): # end::tls-signed[] def test_statement(self): - driver = GraphDatabase.driver("bolt://localhost") + driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")) session = driver.session() # tag::statement[] session.run("CREATE (person:Person {name: {name}})", {"name": "Neo"}).close() @@ -93,7 +93,7 @@ def test_statement(self): session.close() def test_statement_without_parameters(self): - driver = GraphDatabase.driver("bolt://localhost") + driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")) session = driver.session() # tag::statement-without-parameters[] session.run("CREATE (person:Person {name: 'Neo'})").close() @@ -101,7 +101,7 @@ def test_statement_without_parameters(self): session.close() def test_result_cursor(self): - driver = GraphDatabase.driver("bolt://localhost") + driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")) session = driver.session() # tag::result-cursor[] search_term = "hammer" @@ -114,7 +114,7 @@ def test_result_cursor(self): session.close() def test_cursor_nesting(self): - driver = GraphDatabase.driver("bolt://localhost") + driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")) session = driver.session() # tag::retain-result-query[] result = session.run("MATCH (person:Person) WHERE person.dept = {dept} " @@ -127,7 +127,7 @@ def test_cursor_nesting(self): session.close() def test_result_retention(self): - driver = GraphDatabase.driver("bolt://localhost") + driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")) session = driver.session() # tag::retain-result-process[] result = session.run("MATCH (person:Person) WHERE person.dept = {dept} " @@ -142,7 +142,7 @@ def test_result_retention(self): session.close() def test_transaction_commit(self): - driver = GraphDatabase.driver("bolt://localhost") + driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")) session = driver.session() # tag::transaction-commit[] tx = session.begin_transaction() @@ -156,7 +156,7 @@ def test_transaction_commit(self): session.close() def test_transaction_rollback(self): - driver = GraphDatabase.driver("bolt://localhost") + driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")) session = driver.session() # tag::transaction-rollback[] tx = session.begin_transaction() @@ -170,7 +170,7 @@ def test_transaction_rollback(self): session.close() def test_result_summary_query_profile(self): - driver = GraphDatabase.driver("bolt://localhost") + driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")) session = driver.session() # tag::result-summary-query-profile[] result = session.run("PROFILE MATCH (p:Person {name: {name}}) " @@ -183,7 +183,7 @@ def test_result_summary_query_profile(self): session.close() def test_result_summary_notifications(self): - driver = GraphDatabase.driver("bolt://localhost") + driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")) session = driver.session() # tag::result-summary-notifications[] result = session.run("EXPLAIN MATCH (a), (b) RETURN a,b") diff --git a/neo4j/v1/__init__.py b/neo4j/v1/__init__.py index 1a1b454b3..7954792a5 100644 --- a/neo4j/v1/__init__.py +++ b/neo4j/v1/__init__.py @@ -21,3 +21,4 @@ from .constants import * from .session import * from .typesystem import * +from .auth import * diff --git a/neo4j/v1/auth.py b/neo4j/v1/auth.py new file mode 100644 index 000000000..af64b12bd --- /dev/null +++ b/neo4j/v1/auth.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- + +# Copyright (c) 2002-2016 "Neo Technology," +# Network Engine for Objects in Lund AB [http://neotechnology.com] +# +# This file is part of Neo4j. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from collections import namedtuple + +AuthToken = namedtuple("AuthToken", ("scheme", "principal", "credentials")) + + +def basic_auth(user, password): + return AuthToken("basic", user, password) diff --git a/neo4j/v1/connection.py b/neo4j/v1/connection.py index 165b4c30d..e4b8d06c0 100644 --- a/neo4j/v1/connection.py +++ b/neo4j/v1/connection.py @@ -218,10 +218,11 @@ def __init__(self, sock, **config): def on_failure(metadata): raise ProtocolError("Initialisation failed") + self.auth_token = config.get("auth") response = Response(self) response.on_failure = on_failure - self.append(INIT, (self.user_agent,), response=response) + self.append(INIT, (self.user_agent,self._auth_token_dict(),), response=response) self.send() while not response.complete: self.fetch() @@ -229,6 +230,12 @@ def on_failure(metadata): def __del__(self): self.close() + def _auth_token_dict(self): + if self.auth_token: + return self.auth_token._asdict() + else: + return {} + def append(self, signature, fields=(), response=None): """ Add a message to the outgoing queue. diff --git a/runtests.sh b/runtests.sh index 833464e6d..2387eb25b 100755 --- a/runtests.sh +++ b/runtests.sh @@ -61,6 +61,8 @@ fi echo "Running tests with $(python --version)" pip install --upgrade -r ${DRIVER_HOME}/test_requirements.txt echo "" +python -c 'from test.util import *; change_password("neo4j", "neo4j", "tmp")' +python -c 'from test.util import *; change_password("neo4j", "tmp", "neo4j")' TEST_RUNNER="coverage run -m ${UNITTEST} discover -vfs ${TEST}" EXAMPLES_RUNNER="coverage run -m ${UNITTEST} discover -vfs examples" BEHAVE_RUNNER="behave --tags=-db --tags=-in_dev test/tck" diff --git a/test/tck/tck_util.py b/test/tck/tck_util.py index cb755fbe1..1da413987 100644 --- a/test/tck/tck_util.py +++ b/test/tck/tck_util.py @@ -18,12 +18,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -from neo4j.v1 import GraphDatabase, Relationship, Node, Path, SECURITY_NONE +from neo4j.v1 import GraphDatabase, Relationship, Node, Path, SECURITY_NONE, basic_auth from neo4j.v1.compat import string -driver = GraphDatabase.driver("bolt://localhost", security=SECURITY_NONE) - +driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"), security=SECURITY_NONE) def send_string(text): session = driver.session() diff --git a/test/test_session.py b/test/test_session.py index 8ee09065d..ab4fbbaf2 100644 --- a/test/test_session.py +++ b/test/test_session.py @@ -27,6 +27,7 @@ from neo4j.v1.exceptions import CypherError, ResultError from neo4j.v1.session import GraphDatabase, Record, record from neo4j.v1.typesystem import Node, Relationship, Path +from neo4j.v1.auth import basic_auth from test.util import ServerTestCase @@ -34,13 +35,13 @@ class DriverTestCase(ServerTestCase): def test_healthy_session_will_be_returned_to_the_pool_on_close(self): - driver = GraphDatabase.driver("bolt://localhost") + driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")) assert len(driver.session_pool) == 0 driver.session().close() assert len(driver.session_pool) == 1 def test_unhealthy_session_will_not_be_returned_to_the_pool_on_close(self): - driver = GraphDatabase.driver("bolt://localhost") + driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")) assert len(driver.session_pool) == 0 session = driver.session() session.connection.defunct = True @@ -48,7 +49,7 @@ def test_unhealthy_session_will_not_be_returned_to_the_pool_on_close(self): assert len(driver.session_pool) == 0 def session_pool_cannot_exceed_max_size(self): - driver = GraphDatabase.driver("bolt://localhost", max_pool_size=1) + driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"), max_pool_size=1) assert len(driver.session_pool) == 0 driver.session().close() assert len(driver.session_pool) == 1 @@ -56,7 +57,7 @@ def session_pool_cannot_exceed_max_size(self): assert len(driver.session_pool) == 1 def test_session_that_dies_in_the_pool_will_not_be_given_out(self): - driver = GraphDatabase.driver("bolt://localhost") + driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")) session_1 = driver.session() session_1.close() assert len(driver.session_pool) == 1 @@ -66,10 +67,10 @@ def test_session_that_dies_in_the_pool_will_not_be_given_out(self): def test_must_use_valid_url_scheme(self): with self.assertRaises(ValueError): - GraphDatabase.driver("x://xxx") + GraphDatabase.driver("x://xxx", auth=basic_auth("neo4j", "neo4j")) def test_sessions_are_reused(self): - driver = GraphDatabase.driver("bolt://localhost") + driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")) session_1 = driver.session() session_1.close() session_2 = driver.session() @@ -77,7 +78,7 @@ def test_sessions_are_reused(self): assert session_1 is session_2 def test_sessions_are_not_reused_if_still_in_use(self): - driver = GraphDatabase.driver("bolt://localhost") + driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")) session_1 = driver.session() session_2 = driver.session() session_2.close() @@ -92,7 +93,7 @@ def test_default_session_uses_tofu(self): assert driver.security == SECURITY_TRUST_ON_FIRST_USE def test_insecure_session_uses_normal_socket(self): - driver = GraphDatabase.driver("bolt://localhost", security=SECURITY_NONE) + driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"), security=SECURITY_NONE) session = driver.session() connection = session.connection assert isinstance(connection.channel.socket, socket) @@ -100,7 +101,7 @@ def test_insecure_session_uses_normal_socket(self): session.close() def test_tofu_session_uses_secure_socket(self): - driver = GraphDatabase.driver("bolt://localhost", security=SECURITY_TRUST_ON_FIRST_USE) + driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"), security=SECURITY_TRUST_ON_FIRST_USE) session = driver.session() connection = session.connection assert isinstance(connection.channel.socket, SSLSocket) @@ -108,7 +109,7 @@ def test_tofu_session_uses_secure_socket(self): session.close() def test_tofu_session_trusts_certificate_after_first_use(self): - driver = GraphDatabase.driver("bolt://localhost", security=SECURITY_TRUST_ON_FIRST_USE) + driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"), security=SECURITY_TRUST_ON_FIRST_USE) session = driver.session() connection = session.connection certificate = connection.der_encoded_server_certificate @@ -131,7 +132,7 @@ def test_tofu_session_trusts_certificate_after_first_use(self): class RunTestCase(ServerTestCase): def test_can_run_simple_statement(self): - session = GraphDatabase.driver("bolt://localhost").session() + session = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")).session() count = 0 cursor = session.run("RETURN 1 AS n") assert cursor.position == -1 @@ -153,7 +154,7 @@ def test_can_run_simple_statement(self): assert count == 1 def test_can_run_simple_statement_with_params(self): - session = GraphDatabase.driver("bolt://localhost").session() + session = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")).session() count = 0 for record in session.run("RETURN {x} AS n", {"x": {"abc": ["d", "e", "f"]}}).stream(): assert record[0] == {"abc": ["d", "e", "f"]} @@ -165,17 +166,17 @@ def test_can_run_simple_statement_with_params(self): assert count == 1 def test_fails_on_bad_syntax(self): - session = GraphDatabase.driver("bolt://localhost").session() + session = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")).session() with self.assertRaises(CypherError): session.run("X").close() def test_fails_on_missing_parameter(self): - session = GraphDatabase.driver("bolt://localhost").session() + session = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")).session() with self.assertRaises(CypherError): session.run("RETURN {x}").close() def test_can_run_simple_statement_from_bytes_string(self): - session = GraphDatabase.driver("bolt://localhost").session() + session = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")).session() count = 0 for record in session.run(b"RETURN 1 AS n").stream(): assert record[0] == 1 @@ -187,7 +188,7 @@ def test_can_run_simple_statement_from_bytes_string(self): assert count == 1 def test_can_run_statement_that_returns_multiple_records(self): - session = GraphDatabase.driver("bolt://localhost").session() + session = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")).session() count = 0 for record in session.run("unwind(range(1, 10)) AS z RETURN z").stream(): assert 1 <= record[0] <= 10 @@ -196,14 +197,14 @@ def test_can_run_statement_that_returns_multiple_records(self): assert count == 10 def test_can_use_with_to_auto_close_session(self): - with GraphDatabase.driver("bolt://localhost").session() as session: + with GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")).session() as session: record_list = list(session.run("RETURN 1").stream()) assert len(record_list) == 1 for record in record_list: assert record[0] == 1 def test_can_return_node(self): - with GraphDatabase.driver("bolt://localhost").session() as session: + with GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")).session() as session: record_list = list(session.run("MERGE (a:Person {name:'Alice'}) RETURN a").stream()) assert len(record_list) == 1 for record in record_list: @@ -213,7 +214,7 @@ def test_can_return_node(self): assert alice.properties == {"name": "Alice"} def test_can_return_relationship(self): - with GraphDatabase.driver("bolt://localhost").session() as session: + with GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")).session() as session: reocrd_list = list(session.run("MERGE ()-[r:KNOWS {since:1999}]->() " "RETURN r").stream()) assert len(reocrd_list) == 1 @@ -224,7 +225,7 @@ def test_can_return_relationship(self): assert rel.properties == {"since": 1999} def test_can_return_path(self): - with GraphDatabase.driver("bolt://localhost").session() as session: + with GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")).session() as session: record_list = list(session.run("MERGE p=({name:'Alice'})-[:KNOWS]->({name:'Bob'}) " "RETURN p").stream()) assert len(record_list) == 1 @@ -238,19 +239,19 @@ def test_can_return_path(self): assert len(path.relationships) == 1 def test_can_handle_cypher_error(self): - with GraphDatabase.driver("bolt://localhost").session() as session: + with GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")).session() as session: with self.assertRaises(CypherError): session.run("X").close() def test_keys_are_available_before_and_after_stream(self): - with GraphDatabase.driver("bolt://localhost").session() as session: + with GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")).session() as session: cursor = session.run("UNWIND range(1, 10) AS n RETURN n") assert list(cursor.keys()) == ["n"] _ = list(cursor.stream()) assert list(cursor.keys()) == ["n"] def test_keys_with_an_error(self): - with GraphDatabase.driver("bolt://localhost").session() as session: + with GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")).session() as session: cursor = session.run("X") with self.assertRaises(CypherError): _ = list(cursor.keys()) @@ -259,7 +260,7 @@ def test_keys_with_an_error(self): class SummaryTestCase(ServerTestCase): def test_can_obtain_summary_after_consuming_result(self): - with GraphDatabase.driver("bolt://localhost").session() as session: + with GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")).session() as session: cursor = session.run("CREATE (n) RETURN n") list(cursor.stream()) summary = cursor.summary @@ -269,13 +270,13 @@ def test_can_obtain_summary_after_consuming_result(self): assert summary.counters.nodes_created == 1 def test_cannot_obtain_summary_without_consuming_result(self): - with GraphDatabase.driver("bolt://localhost").session() as session: + with GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")).session() as session: cursor = session.run("CREATE (n) RETURN n") with self.assertRaises(ResultError): _ = cursor.summary # def test_can_obtain_summary_immediately_if_empty_result(self): - # with GraphDatabase.driver("bolt://localhost").session() as session: + # with GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")).session() as session: # cursor = session.run("CREATE (n)") # summary = cursor.summary # assert summary.statement == "CREATE (n)" @@ -284,14 +285,14 @@ def test_cannot_obtain_summary_without_consuming_result(self): # assert summary.counters.nodes_created == 1 def test_no_plan_info(self): - with GraphDatabase.driver("bolt://localhost").session() as session: + with GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")).session() as session: cursor = session.run("CREATE (n) RETURN n") list(cursor.stream()) assert cursor.summary.plan is None assert cursor.summary.profile is None def test_can_obtain_plan_info(self): - with GraphDatabase.driver("bolt://localhost").session() as session: + with GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")).session() as session: cursor = session.run("EXPLAIN CREATE (n) RETURN n") list(cursor.stream()) plan = cursor.summary.plan @@ -303,7 +304,7 @@ def test_can_obtain_plan_info(self): assert len(plan.children) == 1 def test_can_obtain_profile_info(self): - with GraphDatabase.driver("bolt://localhost").session() as session: + with GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")).session() as session: cursor = session.run("PROFILE CREATE (n) RETURN n") list(cursor.stream()) profile = cursor.summary.profile @@ -317,14 +318,14 @@ def test_can_obtain_profile_info(self): assert len(profile.children) == 1 def test_no_notification_info(self): - with GraphDatabase.driver("bolt://localhost").session() as session: + with GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")).session() as session: cursor = session.run("CREATE (n) RETURN n") list(cursor.stream()) notifications = cursor.summary.notifications assert notifications == [] def test_can_obtain_notification_info(self): - with GraphDatabase.driver("bolt://localhost").session() as session: + with GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")).session() as session: cursor = session.run("EXPLAIN MATCH (n), (m) RETURN n, m") list(cursor.stream()) notifications = cursor.summary.notifications @@ -353,7 +354,7 @@ def test_can_obtain_notification_info(self): class ResetTestCase(ServerTestCase): def test_automatic_reset_after_failure(self): - with GraphDatabase.driver("bolt://localhost").session() as session: + with GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")).session() as session: try: session.run("X").close() except CypherError: @@ -365,7 +366,7 @@ def test_automatic_reset_after_failure(self): def test_defunct(self): from neo4j.v1.connection import ChunkChannel, ProtocolError - with GraphDatabase.driver("bolt://localhost").session() as session: + with GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")).session() as session: assert not session.connection.defunct with patch.object(ChunkChannel, "chunk_reader", side_effect=ProtocolError()): with self.assertRaises(ProtocolError): @@ -451,7 +452,7 @@ def test_record_repr(self): class TransactionTestCase(ServerTestCase): def test_can_commit_transaction(self): - with GraphDatabase.driver("bolt://localhost").session() as session: + with GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")).session() as session: tx = session.begin_transaction() # Create a node @@ -474,7 +475,7 @@ def test_can_commit_transaction(self): assert foo == "bar" def test_can_rollback_transaction(self): - with GraphDatabase.driver("bolt://localhost").session() as session: + with GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")).session() as session: tx = session.begin_transaction() # Create a node @@ -495,7 +496,7 @@ def test_can_rollback_transaction(self): assert len(list(cursor.stream())) == 0 def test_can_commit_transaction_using_with_block(self): - with GraphDatabase.driver("bolt://localhost").session() as session: + with GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")).session() as session: with session.begin_transaction() as tx: # Create a node cursor = tx.run("CREATE (a) RETURN id(a)") @@ -517,7 +518,7 @@ def test_can_commit_transaction_using_with_block(self): assert foo == "bar" def test_can_rollback_transaction_using_with_block(self): - with GraphDatabase.driver("bolt://localhost").session() as session: + with GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")).session() as session: with session.begin_transaction() as tx: # Create a node cursor = tx.run("CREATE (a) RETURN id(a)") diff --git a/test/util.py b/test/util.py index 9148b5898..b6af02240 100644 --- a/test/util.py +++ b/test/util.py @@ -30,6 +30,7 @@ KNOWN_HOSTS_BACKUP = KNOWN_HOSTS + ".backup" +from neo4j.v1 import GraphDatabase def watch(f): """ Decorator to enable log watching for the lifetime of a function. @@ -62,3 +63,29 @@ def setUp(self): def tearDown(self): if isfile(self.known_hosts_backup): rename(self.known_hosts_backup, self.known_hosts) + + +def change_password(user, password, new_password): + """Util only used for test for doing the initial password update + :param user: user name + :param password: current password + :param new_password: new password to set + """ + + token = _UpdateCredToken(user, password, new_password) + driver = GraphDatabase.driver("bolt://localhost", auth=token) + driver.session().close() + + +class _UpdateCredToken: + """Util only used for test for doing the initial password update""" + + def __init__(self, user, password, new_password): + self._user = user + self._password = password + self._new_password = new_password + + def _asdict(self): + return {"principal": self._user, "credentials": self._password, + "new-credentials": self._new_password, "scheme": "basic"} +