Skip to content

Commit 3e1d84a

Browse files
committed
SSL: Add software tests validating ssl=true vs. sslmode=require
1 parent 8906904 commit 3e1d84a

1 file changed

Lines changed: 62 additions & 15 deletions

File tree

tests/connection_test.py

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,34 @@
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

25+
import pytest
2426
import sqlalchemy as sa
2527
from sqlalchemy.exc import NoSuchModuleError
2628

29+
from sqlalchemy_cratedb import SA_1_4, SA_VERSION
30+
from tests.util import ExtraAssertions
31+
2732

28-
class SqlAlchemyConnectionTest(TestCase):
33+
class SqlAlchemyConnectionTest(TestCase, ExtraAssertions):
2934
def test_connection_server_uri_unknown_sa_plugin(self):
3035
with self.assertRaises(NoSuchModuleError):
3136
sa.create_engine("foobar://otherhost:19201")
3237

33-
def test_default_connection(self):
38+
def test_connection_no_hostname_no_ssl(self):
3439
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()
40+
servers = engine.raw_connection().driver_connection.client._active_servers
41+
self.assertEqual(["http://127.0.0.1:4200"], servers)
42+
engine.dispose()
43+
44+
@pytest.mark.skipif(SA_VERSION < SA_1_4, reason="Not supported by SQLAlchemy 1.3")
45+
def test_connection_no_hostname_with_ssl(self):
46+
engine = sa.create_engine("crate://?sslmode=require")
47+
servers = engine.raw_connection().driver_connection.client._active_servers
48+
self.assertEqual(["https://127.0.0.1:4200"], servers)
4049
engine.dispose()
4150

4251
def test_connection_server_uri_http(self):
@@ -48,13 +57,51 @@ def test_connection_server_uri_http(self):
4857
conn.close()
4958
engine.dispose()
5059

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

60107
def test_connection_server_uri_invalid_port(self):

0 commit comments

Comments
 (0)