|
25 | 25 |
|
26 | 26 | from neo4j.v1 import ServiceUnavailable, ProtocolError, READ_ACCESS, WRITE_ACCESS, \
|
27 | 27 | TRUST_ON_FIRST_USE, TRUST_CUSTOM_CA_SIGNED_CERTIFICATES, GraphDatabase, basic_auth, \
|
28 |
| - custom_auth, SSL_AVAILABLE, SessionExpired, DirectDriver |
| 28 | + custom_auth, SSL_AVAILABLE, SessionExpired, DirectDriver, RoutingDriver |
29 | 29 | from test.util import ServerTestCase, StubCluster
|
30 | 30 |
|
31 | 31 | BOLT_URI = "bolt://localhost:7687"
|
32 | 32 | BOLT_ROUTING_URI = "bolt+routing://localhost:7687"
|
33 |
| -AUTH_TOKEN = basic_auth("neotest", "neotest") |
34 | 33 |
|
| 34 | +TEST_USER = "neotest" |
| 35 | +TEST_PASSWORD = "neotest" |
| 36 | +AUTH_TOKEN = basic_auth(TEST_USER, TEST_PASSWORD) |
35 | 37 |
|
36 |
| -class DriverTestCase(ServerTestCase): |
37 | 38 |
|
38 |
| - def test_driver_with_block(self): |
39 |
| - with GraphDatabase.driver(BOLT_URI, auth=AUTH_TOKEN, encrypted=False) as driver: |
40 |
| - assert isinstance(driver, DirectDriver) |
| 39 | +class BasicAuthTestCase(ServerTestCase): |
| 40 | + |
| 41 | + def test_can_provide_realm_with_basic_auth_token(self): |
| 42 | + token = basic_auth(TEST_USER, TEST_PASSWORD, "native") |
| 43 | + with GraphDatabase.driver(BOLT_URI, auth=token) as driver: |
| 44 | + with driver.session() as session: |
| 45 | + result = session.run("RETURN 1").consume() |
| 46 | + assert result is not None |
| 47 | + |
| 48 | + |
| 49 | +class CustomAuthTestCase(ServerTestCase): |
| 50 | + |
| 51 | + def test_can_create_custom_auth_token(self): |
| 52 | + token = custom_auth(TEST_USER, TEST_PASSWORD, "native", "basic") |
| 53 | + with GraphDatabase.driver(BOLT_URI, auth=token) as driver: |
| 54 | + with driver.session() as session: |
| 55 | + result = session.run("RETURN 1").consume() |
| 56 | + assert result is not None |
| 57 | + |
| 58 | + def test_can_create_custom_auth_token_with_additional_parameters(self): |
| 59 | + token = custom_auth(TEST_USER, TEST_PASSWORD, "native", "basic", secret=42) |
| 60 | + with GraphDatabase.driver(BOLT_URI, auth=token) as driver: |
| 61 | + with driver.session() as session: |
| 62 | + result = session.run("RETURN 1").consume() |
| 63 | + assert result is not None |
| 64 | + |
| 65 | + |
| 66 | +class DriverTestCase(ServerTestCase): |
41 | 67 |
|
42 | 68 | def test_must_use_valid_url_scheme(self):
|
43 | 69 | with self.assertRaises(ProtocolError):
|
44 | 70 | GraphDatabase.driver("x://xxx", auth=AUTH_TOKEN)
|
45 | 71 |
|
46 | 72 | def test_connections_are_reused(self):
|
47 |
| - driver = GraphDatabase.driver(BOLT_URI, auth=AUTH_TOKEN) |
48 |
| - session_1 = driver.session() |
49 |
| - connection_1 = session_1.connection |
50 |
| - session_1.close() |
51 |
| - session_2 = driver.session() |
52 |
| - connection_2 = session_2.connection |
53 |
| - session_2.close() |
54 |
| - assert connection_1 is connection_2 |
55 |
| - |
56 |
| - def test_connections_are_not_shared_between_sessions(self): |
57 |
| - driver = GraphDatabase.driver(BOLT_URI, auth=AUTH_TOKEN) |
58 |
| - session_1 = driver.session() |
59 |
| - session_2 = driver.session() |
60 |
| - try: |
61 |
| - assert session_1.connection is not session_2.connection |
62 |
| - finally: |
| 73 | + with GraphDatabase.driver(BOLT_URI, auth=AUTH_TOKEN) as driver: |
| 74 | + session_1 = driver.session() |
| 75 | + connection_1 = session_1.connection |
63 | 76 | session_1.close()
|
| 77 | + session_2 = driver.session() |
| 78 | + connection_2 = session_2.connection |
64 | 79 | session_2.close()
|
| 80 | + assert connection_1 is connection_2 |
65 | 81 |
|
66 |
| - def test_fail_nicely_when_connecting_to_http_port(self): |
67 |
| - driver = GraphDatabase.driver("bolt://localhost:7474", auth=AUTH_TOKEN, encrypted=False) |
68 |
| - with self.assertRaises(ServiceUnavailable) as context: |
69 |
| - driver.session() |
70 |
| - |
71 |
| - def test_can_provide_realm_with_basic_auth_token(self): |
72 |
| - token = basic_auth("neotest", "neotest", "native") |
73 |
| - driver = GraphDatabase.driver("bolt://localhost", auth=token) |
74 |
| - session = driver.session() |
75 |
| - result = session.run("RETURN 1").consume() |
76 |
| - session.close() |
77 |
| - assert result is not None |
78 |
| - |
79 |
| - def test_can_create_custom_auth_token(self): |
80 |
| - token = custom_auth("neotest", "neotest", "native", "basic") |
81 |
| - driver = GraphDatabase.driver("bolt://localhost", auth=token) |
82 |
| - session = driver.session() |
83 |
| - result = session.run("RETURN 1").consume() |
84 |
| - session.close() |
85 |
| - assert result is not None |
| 82 | + def test_connections_are_not_shared_between_sessions(self): |
| 83 | + with GraphDatabase.driver(BOLT_URI, auth=AUTH_TOKEN) as driver: |
| 84 | + session_1 = driver.session() |
| 85 | + session_2 = driver.session() |
| 86 | + try: |
| 87 | + assert session_1.connection is not session_2.connection |
| 88 | + finally: |
| 89 | + session_1.close() |
| 90 | + session_2.close() |
86 | 91 |
|
87 |
| - def test_can_create_custom_auth_token_with_additional_parameters(self): |
88 |
| - token = custom_auth("neotest", "neotest", "native", "basic", secret=42) |
89 |
| - driver = GraphDatabase.driver("bolt://localhost", auth=token) |
90 |
| - session = driver.session() |
91 |
| - result = session.run("RETURN 1").consume() |
92 |
| - session.close() |
93 |
| - assert result is not None |
| 92 | + def test_fail_nicely_when_connecting_to_http_port(self): |
| 93 | + uri = "bolt://localhost:7474" |
| 94 | + with GraphDatabase.driver(uri, auth=AUTH_TOKEN, encrypted=False) as driver: |
| 95 | + with self.assertRaises(ServiceUnavailable): |
| 96 | + driver.session() |
94 | 97 |
|
95 | 98 |
|
96 | 99 | class DirectDriverTestCase(ServerTestCase):
|
97 | 100 |
|
98 |
| - def tearDown(self): |
99 |
| - self.await_all_servers() |
| 101 | + def test_bolt_uri_constructs_direct_driver(self): |
| 102 | + with GraphDatabase.driver(BOLT_URI, auth=AUTH_TOKEN, encrypted=False) as driver: |
| 103 | + assert isinstance(driver, DirectDriver) |
100 | 104 |
|
101 | 105 | def test_direct_disconnect_on_run(self):
|
102 |
| - self.start_stub_server(9001, "disconnect_on_run.script") |
103 |
| - uri = "bolt://127.0.0.1:9001" |
104 |
| - driver = GraphDatabase.driver(uri, auth=basic_auth("neo4j", "password"), encrypted=False) |
105 |
| - try: |
106 |
| - with driver.session() as session: |
107 |
| - with self.assertRaises(ServiceUnavailable): |
108 |
| - session.run("RETURN $x", {"x": 1}).consume() |
109 |
| - finally: |
110 |
| - driver.close() |
| 106 | + with StubCluster({9001: "disconnect_on_run.script"}): |
| 107 | + uri = "bolt://127.0.0.1:9001" |
| 108 | + with GraphDatabase.driver(uri, auth=AUTH_TOKEN, encrypted=False) as driver: |
| 109 | + with driver.session() as session: |
| 110 | + with self.assertRaises(ServiceUnavailable): |
| 111 | + session.run("RETURN $x", {"x": 1}).consume() |
111 | 112 |
|
112 | 113 | def test_direct_disconnect_on_pull_all(self):
|
113 |
| - self.start_stub_server(9001, "disconnect_on_pull_all.script") |
114 |
| - uri = "bolt://127.0.0.1:9001" |
115 |
| - driver = GraphDatabase.driver(uri, auth=basic_auth("neo4j", "password"), encrypted=False) |
116 |
| - try: |
117 |
| - with driver.session() as session: |
118 |
| - with self.assertRaises(ServiceUnavailable): |
119 |
| - session.run("RETURN $x", {"x": 1}).consume() |
120 |
| - finally: |
121 |
| - driver.close() |
| 114 | + with StubCluster({9001: "disconnect_on_pull_all.script"}): |
| 115 | + uri = "bolt://127.0.0.1:9001" |
| 116 | + with GraphDatabase.driver(uri, auth=AUTH_TOKEN, encrypted=False) as driver: |
| 117 | + with driver.session() as session: |
| 118 | + with self.assertRaises(ServiceUnavailable): |
| 119 | + session.run("RETURN $x", {"x": 1}).consume() |
122 | 120 |
|
123 | 121 |
|
124 | 122 | class RoutingDriverTestCase(ServerTestCase):
|
125 | 123 |
|
| 124 | + def test_bolt_plus_routing_uri_constructs_routing_driver(self): |
| 125 | + with StubCluster({9001: "router.script"}): |
| 126 | + uri = "bolt+routing://127.0.0.1:9001" |
| 127 | + with GraphDatabase.driver(uri, auth=AUTH_TOKEN, encrypted=False) as driver: |
| 128 | + assert isinstance(driver, RoutingDriver) |
| 129 | + |
126 | 130 | def test_cannot_discover_servers_on_non_router(self):
|
127 | 131 | with StubCluster({9001: "non_router.script"}):
|
128 | 132 | uri = "bolt+routing://127.0.0.1:9001"
|
129 |
| - auth_token = basic_auth("neo4j", "password") |
130 | 133 | with self.assertRaises(ServiceUnavailable):
|
131 |
| - GraphDatabase.driver(uri, auth=auth_token, encrypted=False) |
| 134 | + _ = GraphDatabase.driver(uri, auth=AUTH_TOKEN, encrypted=False) |
132 | 135 |
|
133 | 136 | def test_cannot_discover_servers_on_silent_router(self):
|
134 | 137 | with StubCluster({9001: "silent_router.script"}):
|
135 | 138 | uri = "bolt+routing://127.0.0.1:9001"
|
136 |
| - auth_token = basic_auth("neo4j", "password") |
137 | 139 | with self.assertRaises(ProtocolError):
|
138 |
| - GraphDatabase.driver(uri, auth=auth_token, encrypted=False) |
| 140 | + _ = GraphDatabase.driver(uri, auth=AUTH_TOKEN, encrypted=False) |
139 | 141 |
|
140 | 142 | def test_should_discover_servers_on_driver_construction(self):
|
141 | 143 | with StubCluster({9001: "router.script"}):
|
142 | 144 | uri = "bolt+routing://127.0.0.1:9001"
|
143 |
| - auth_token = basic_auth("neo4j", "password") |
144 |
| - driver = GraphDatabase.driver(uri, auth=auth_token, encrypted=False) |
145 |
| - try: |
| 145 | + with GraphDatabase.driver(uri, auth=AUTH_TOKEN, encrypted=False) as driver: |
146 | 146 | table = driver.pool.routing_table
|
147 | 147 | assert table.routers == {('127.0.0.1', 9001), ('127.0.0.1', 9002),
|
148 | 148 | ('127.0.0.1', 9003)}
|
149 | 149 | assert table.readers == {('127.0.0.1', 9004), ('127.0.0.1', 9005)}
|
150 | 150 | assert table.writers == {('127.0.0.1', 9006)}
|
151 |
| - finally: |
152 |
| - driver.close() |
153 | 151 |
|
154 | 152 | def test_should_be_able_to_read(self):
|
155 | 153 | with StubCluster({9001: "router.script", 9004: "return_1.script"}):
|
156 | 154 | uri = "bolt+routing://127.0.0.1:9001"
|
157 |
| - auth_token = basic_auth("neo4j", "password") |
158 |
| - driver = GraphDatabase.driver(uri, auth=auth_token, encrypted=False) |
159 |
| - try: |
| 155 | + with GraphDatabase.driver(uri, auth=AUTH_TOKEN, encrypted=False) as driver: |
160 | 156 | with driver.session(READ_ACCESS) as session:
|
161 | 157 | result = session.run("RETURN $x", {"x": 1})
|
162 | 158 | for record in result:
|
163 | 159 | assert record["x"] == 1
|
164 | 160 | assert session.connection.address == ('127.0.0.1', 9004)
|
165 |
| - finally: |
166 |
| - driver.close() |
167 | 161 |
|
168 | 162 | def test_should_be_able_to_write(self):
|
169 | 163 | with StubCluster({9001: "router.script", 9006: "create_a.script"}):
|
170 | 164 | uri = "bolt+routing://127.0.0.1:9001"
|
171 |
| - driver = GraphDatabase.driver(uri, auth=basic_auth("neo4j", "password"), encrypted=False) |
172 |
| - try: |
| 165 | + with GraphDatabase.driver(uri, auth=AUTH_TOKEN, encrypted=False) as driver: |
173 | 166 | with driver.session(WRITE_ACCESS) as session:
|
174 | 167 | result = session.run("CREATE (a $x)", {"x": {"name": "Alice"}})
|
175 | 168 | assert not list(result)
|
176 | 169 | assert session.connection.address == ('127.0.0.1', 9006)
|
177 |
| - finally: |
178 |
| - driver.close() |
179 | 170 |
|
180 | 171 | def test_should_be_able_to_write_as_default(self):
|
181 | 172 | with StubCluster({9001: "router.script", 9006: "create_a.script"}):
|
182 | 173 | uri = "bolt+routing://127.0.0.1:9001"
|
183 |
| - auth_token = basic_auth("neo4j", "password") |
184 |
| - driver = GraphDatabase.driver(uri, auth=auth_token, encrypted=False) |
185 |
| - try: |
| 174 | + with GraphDatabase.driver(uri, auth=AUTH_TOKEN, encrypted=False) as driver: |
186 | 175 | with driver.session() as session:
|
187 | 176 | result = session.run("CREATE (a $x)", {"x": {"name": "Alice"}})
|
188 | 177 | assert not list(result)
|
189 | 178 | assert session.connection.address == ('127.0.0.1', 9006)
|
190 |
| - finally: |
191 |
| - driver.close() |
192 | 179 |
|
193 | 180 | def test_routing_disconnect_on_run(self):
|
194 | 181 | with StubCluster({9001: "router.script", 9004: "disconnect_on_run.script"}):
|
195 | 182 | uri = "bolt+routing://127.0.0.1:9001"
|
196 |
| - auth_token = basic_auth("neo4j", "password") |
197 |
| - driver = GraphDatabase.driver(uri, auth=auth_token, encrypted=False) |
198 |
| - try: |
| 183 | + with GraphDatabase.driver(uri, auth=AUTH_TOKEN, encrypted=False) as driver: |
199 | 184 | with driver.session(READ_ACCESS) as session:
|
200 | 185 | with self.assertRaises(SessionExpired):
|
201 | 186 | session.run("RETURN $x", {"x": 1}).consume()
|
202 |
| - finally: |
203 |
| - driver.close() |
204 | 187 |
|
205 | 188 | def test_routing_disconnect_on_pull_all(self):
|
206 | 189 | with StubCluster({9001: "router.script", 9004: "disconnect_on_pull_all.script"}):
|
207 | 190 | uri = "bolt+routing://127.0.0.1:9001"
|
208 |
| - auth_token = basic_auth("neo4j", "password") |
209 |
| - driver = GraphDatabase.driver(uri, auth=auth_token, encrypted=False) |
210 |
| - try: |
| 191 | + with GraphDatabase.driver(uri, auth=AUTH_TOKEN, encrypted=False) as driver: |
211 | 192 | with driver.session(READ_ACCESS) as session:
|
212 | 193 | with self.assertRaises(SessionExpired):
|
213 | 194 | session.run("RETURN $x", {"x": 1}).consume()
|
214 |
| - finally: |
215 |
| - driver.close() |
216 | 195 |
|
217 | 196 |
|
218 | 197 | class SecurityTestCase(ServerTestCase):
|
219 | 198 |
|
220 | 199 | def test_insecure_session_uses_normal_socket(self):
|
221 |
| - driver = GraphDatabase.driver(BOLT_URI, auth=AUTH_TOKEN, encrypted=False) |
222 |
| - with driver.session() as session: |
223 |
| - connection = session.connection |
224 |
| - assert isinstance(connection.channel.socket, socket) |
225 |
| - assert connection.der_encoded_server_certificate is None |
| 200 | + with GraphDatabase.driver(BOLT_URI, auth=AUTH_TOKEN, encrypted=False) as driver: |
| 201 | + with driver.session() as session: |
| 202 | + connection = session.connection |
| 203 | + assert isinstance(connection.channel.socket, socket) |
| 204 | + assert connection.der_encoded_server_certificate is None |
226 | 205 |
|
227 | 206 | @skipUnless(SSL_AVAILABLE, "Bolt over TLS is not supported by this version of Python")
|
228 | 207 | def test_tofu_session_uses_secure_socket(self):
|
229 |
| - driver = GraphDatabase.driver(BOLT_URI, auth=AUTH_TOKEN, encrypted=True, trust=TRUST_ON_FIRST_USE) |
230 |
| - with driver.session() as session: |
231 |
| - connection = session.connection |
232 |
| - assert isinstance(connection.channel.socket, SSLSocket) |
233 |
| - assert connection.der_encoded_server_certificate is not None |
| 208 | + with GraphDatabase.driver(BOLT_URI, auth=AUTH_TOKEN, trust=TRUST_ON_FIRST_USE) as driver: |
| 209 | + with driver.session() as session: |
| 210 | + connection = session.connection |
| 211 | + assert isinstance(connection.channel.socket, SSLSocket) |
| 212 | + assert connection.der_encoded_server_certificate is not None |
234 | 213 |
|
235 | 214 | @skipUnless(SSL_AVAILABLE, "Bolt over TLS is not supported by this version of Python")
|
236 | 215 | def test_tofu_session_trusts_certificate_after_first_use(self):
|
237 |
| - driver = GraphDatabase.driver(BOLT_URI, auth=AUTH_TOKEN, encrypted=True, trust=TRUST_ON_FIRST_USE) |
238 |
| - with driver.session() as session: |
239 |
| - connection = session.connection |
240 |
| - certificate = connection.der_encoded_server_certificate |
241 |
| - with driver.session() as session: |
242 |
| - connection = session.connection |
243 |
| - assert connection.der_encoded_server_certificate == certificate |
| 216 | + with GraphDatabase.driver(BOLT_URI, auth=AUTH_TOKEN, trust=TRUST_ON_FIRST_USE) as driver: |
| 217 | + with driver.session() as session: |
| 218 | + connection = session.connection |
| 219 | + certificate = connection.der_encoded_server_certificate |
| 220 | + with driver.session() as session: |
| 221 | + connection = session.connection |
| 222 | + assert connection.der_encoded_server_certificate == certificate |
244 | 223 |
|
245 | 224 | def test_routing_driver_not_compatible_with_tofu(self):
|
246 | 225 | with self.assertRaises(ValueError):
|
247 |
| - GraphDatabase.driver(BOLT_ROUTING_URI, auth=AUTH_TOKEN, trust=TRUST_ON_FIRST_USE) |
| 226 | + _ = GraphDatabase.driver(BOLT_ROUTING_URI, auth=AUTH_TOKEN, trust=TRUST_ON_FIRST_USE) |
248 | 227 |
|
249 | 228 | def test_custom_ca_not_implemented(self):
|
250 | 229 | with self.assertRaises(NotImplementedError):
|
251 |
| - GraphDatabase.driver(BOLT_URI, auth=AUTH_TOKEN, |
252 |
| - trust=TRUST_CUSTOM_CA_SIGNED_CERTIFICATES) |
| 230 | + _ = GraphDatabase.driver(BOLT_URI, auth=AUTH_TOKEN, |
| 231 | + trust=TRUST_CUSTOM_CA_SIGNED_CERTIFICATES) |
0 commit comments