Skip to content

Commit ec914f8

Browse files
authored
Merge pull request #1137 from ethho/dev-tests-plat-167-tls
PLAT-167: Migrate test_tls
2 parents 20c5039 + 1e3ba5c commit ec914f8

File tree

2 files changed

+49
-11
lines changed

2 files changed

+49
-11
lines changed

tests/conftest.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ def enable_filepath_feature(monkeypatch):
5656
monkeypatch.delenv(FILEPATH_FEATURE_SWITCH, raising=True)
5757

5858

59+
@pytest.fixture(scope="session")
60+
def db_creds_test() -> Dict:
61+
return dict(
62+
host=os.getenv("DJ_TEST_HOST", "fakeservices.datajoint.io"),
63+
user=os.getenv("DJ_TEST_USER", "datajoint"),
64+
password=os.getenv("DJ_TEST_PASSWORD", "datajoint"),
65+
)
66+
67+
5968
@pytest.fixture(scope="session")
6069
def db_creds_root() -> Dict:
6170
return dict(
@@ -142,12 +151,9 @@ def connection_root(connection_root_bare):
142151

143152

144153
@pytest.fixture(scope="session")
145-
def connection_test(connection_root):
154+
def connection_test(connection_root, db_creds_test):
146155
"""Test user database connection."""
147156
database = f"{PREFIX}%%"
148-
credentials = dict(
149-
host=os.getenv("DJ_HOST"), user="datajoint", password="datajoint"
150-
)
151157
permission = "ALL PRIVILEGES"
152158

153159
# Create MySQL users
@@ -157,14 +163,14 @@ def connection_test(connection_root):
157163
# create user if necessary on mysql8
158164
connection_root.query(
159165
f"""
160-
CREATE USER IF NOT EXISTS '{credentials["user"]}'@'%%'
161-
IDENTIFIED BY '{credentials["password"]}';
166+
CREATE USER IF NOT EXISTS '{db_creds_test["user"]}'@'%%'
167+
IDENTIFIED BY '{db_creds_test["password"]}';
162168
"""
163169
)
164170
connection_root.query(
165171
f"""
166172
GRANT {permission} ON `{database}`.*
167-
TO '{credentials["user"]}'@'%%';
173+
TO '{db_creds_test["user"]}'@'%%';
168174
"""
169175
)
170176
else:
@@ -173,14 +179,14 @@ def connection_test(connection_root):
173179
connection_root.query(
174180
f"""
175181
GRANT {permission} ON `{database}`.*
176-
TO '{credentials["user"]}'@'%%'
177-
IDENTIFIED BY '{credentials["password"]}';
182+
TO '{db_creds_test["user"]}'@'%%'
183+
IDENTIFIED BY '{db_creds_test["password"]}';
178184
"""
179185
)
180186

181-
connection = dj.Connection(**credentials)
187+
connection = dj.Connection(**db_creds_test)
182188
yield connection
183-
connection_root.query(f"""DROP USER `{credentials["user"]}`""")
189+
connection_root.query(f"""DROP USER `{db_creds_test["user"]}`""")
184190
connection.close()
185191

186192

tests/test_tls.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import pytest
2+
import datajoint as dj
3+
from pymysql.err import OperationalError
4+
5+
6+
def test_secure_connection(db_creds_test, connection_test):
7+
result = (
8+
dj.conn(reset=True, **db_creds_test)
9+
.query("SHOW STATUS LIKE 'Ssl_cipher';")
10+
.fetchone()[1]
11+
)
12+
assert len(result) > 0
13+
14+
15+
def test_insecure_connection(db_creds_test, connection_test):
16+
result = (
17+
dj.conn(use_tls=False, reset=True, **db_creds_test)
18+
.query("SHOW STATUS LIKE 'Ssl_cipher';")
19+
.fetchone()[1]
20+
)
21+
assert result == ""
22+
23+
24+
def test_reject_insecure(db_creds_test, connection_test):
25+
with pytest.raises(OperationalError):
26+
dj.conn(
27+
db_creds_test["host"],
28+
user="djssl",
29+
password="djssl",
30+
use_tls=False,
31+
reset=True,
32+
).query("SHOW STATUS LIKE 'Ssl_cipher';").fetchone()[1]

0 commit comments

Comments
 (0)