Skip to content

Commit c8d5fd8

Browse files
committed
SSL: Add software tests validating ssl=true vs. sslmode=require
1 parent bb206c6 commit c8d5fd8

1 file changed

Lines changed: 59 additions & 15 deletions

File tree

tests/connection_test.py

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,31 @@
1818
# However, if you have executed another commercial license agreement
1919
# with Crate these terms will supersede the license and you may use the
2020
# software solely pursuant to the terms of the relevant commercial agreement.
21-
21+
import contextlib
22+
import warnings
2223
from unittest import TestCase
2324

2425
import sqlalchemy as sa
2526
from sqlalchemy.exc import NoSuchModuleError
2627

28+
from tests.util import ExtraAssertions
29+
2730

28-
class SqlAlchemyConnectionTest(TestCase):
31+
class SqlAlchemyConnectionTest(TestCase, ExtraAssertions):
2932
def test_connection_server_uri_unknown_sa_plugin(self):
3033
with self.assertRaises(NoSuchModuleError):
3134
sa.create_engine("foobar://otherhost:19201")
3235

33-
def test_default_connection(self):
36+
def test_connection_no_hostname_no_ssl(self):
3437
engine = sa.create_engine("crate://")
35-
conn = engine.raw_connection()
36-
self.assertEqual(
37-
"<Connection <Client ['http://127.0.0.1:4200']>>", repr(conn.driver_connection)
38-
)
39-
conn.close()
38+
servers = engine.raw_connection().driver_connection.client._active_servers
39+
self.assertEqual(["http://127.0.0.1:4200"], servers)
40+
engine.dispose()
41+
42+
def test_connection_no_hostname_with_ssl(self):
43+
engine = sa.create_engine("crate://?sslmode=require")
44+
servers = engine.raw_connection().driver_connection.client._active_servers
45+
self.assertEqual(["https://127.0.0.1:4200"], servers)
4046
engine.dispose()
4147

4248
def test_connection_server_uri_http(self):
@@ -48,13 +54,51 @@ def test_connection_server_uri_http(self):
4854
conn.close()
4955
engine.dispose()
5056

51-
def test_connection_server_uri_https(self):
52-
engine = sa.create_engine("crate://otherhost:19201/?ssl=true")
53-
conn = engine.raw_connection()
54-
self.assertEqual(
55-
"<Connection <Client ['https://otherhost:19201']>>", repr(conn.driver_connection)
56-
)
57-
conn.close()
57+
@contextlib.contextmanager
58+
def verify_user_warning_about_ssl_deprecation(self):
59+
"""
60+
The `ssl=true` option was flagged for deprecation. Verify that.
61+
"""
62+
with warnings.catch_warnings(record=True) as w:
63+
# Cause all warnings to always be triggered.
64+
warnings.simplefilter("always")
65+
66+
# Run workhorse body.
67+
yield
68+
69+
# Verify details of the deprecation warning.
70+
self.assertEqual(len(w), 1)
71+
self.assertIsSubclass(w[-1].category, DeprecationWarning)
72+
self.assertIn(
73+
"The `ssl=true` option will be deprecated, "
74+
"please use `sslmode=require` going forward.",
75+
str(w[-1].message),
76+
)
77+
78+
def test_connection_server_uri_https_ssl_enabled(self):
79+
with self.verify_user_warning_about_ssl_deprecation():
80+
engine = sa.create_engine("crate://otherhost:19201/?ssl=true")
81+
servers = engine.raw_connection().driver_connection.client._active_servers
82+
self.assertEqual(["https://otherhost:19201"], servers)
83+
engine.dispose()
84+
85+
def test_connection_server_uri_https_ssl_disabled(self):
86+
with self.verify_user_warning_about_ssl_deprecation():
87+
engine = sa.create_engine("crate://otherhost:19201/?ssl=false")
88+
servers = engine.raw_connection().driver_connection.client._active_servers
89+
self.assertEqual(["http://otherhost:19201"], servers)
90+
engine.dispose()
91+
92+
def test_connection_server_uri_https_sslmode_enabled(self):
93+
engine = sa.create_engine("crate://otherhost:19201/?sslmode=require")
94+
servers = engine.raw_connection().driver_connection.client._active_servers
95+
self.assertEqual(["https://otherhost:19201"], servers)
96+
engine.dispose()
97+
98+
def test_connection_server_uri_https_sslmode_disabled(self):
99+
engine = sa.create_engine("crate://otherhost:19201/?sslmode=disable")
100+
servers = engine.raw_connection().driver_connection.client._active_servers
101+
self.assertEqual(["http://otherhost:19201"], servers)
58102
engine.dispose()
59103

60104
def test_connection_server_uri_invalid_port(self):

0 commit comments

Comments
 (0)