Skip to content

Commit 054c095

Browse files
committed
Fix docker credential parsing tests for adapter-based interface
1 parent e68cc7c commit 054c095

File tree

1 file changed

+105
-106
lines changed

1 file changed

+105
-106
lines changed

tests/unit/test_docker_credential_parsing.py

Lines changed: 105 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import pytest
1010

11+
from sqlit.db.providers import get_adapter_class
1112
from sqlit.services.docker_detector import (
1213
_get_container_credentials,
1314
_get_db_type_from_image,
@@ -96,38 +97,38 @@ def test_full_credentials(self):
9697
"POSTGRES_PASSWORD": "mypass",
9798
"POSTGRES_DB": "mydb",
9899
}
99-
creds = _get_container_credentials("postgresql", env)
100-
assert creds["user"] == "myuser"
101-
assert creds["password"] == "mypass"
102-
assert creds["database"] == "mydb"
100+
creds = _get_container_credentials(get_adapter_class("postgresql"), env)
101+
assert creds.user == "myuser"
102+
assert creds.password == "mypass"
103+
assert creds.database == "mydb"
103104

104105
def test_defaults_when_empty(self):
105106
"""Test default values when no env vars set."""
106-
creds = _get_container_credentials("postgresql", {})
107-
assert creds["user"] == "postgres"
108-
assert creds["password"] is None
109-
assert creds["database"] is None
107+
creds = _get_container_credentials(get_adapter_class("postgresql"), {})
108+
assert creds.user == "postgres"
109+
assert creds.password is None
110+
assert creds.database == "postgres"
110111

111112
def test_password_only(self):
112113
"""Test with only password set (common minimal config)."""
113114
env = {"POSTGRES_PASSWORD": "secret"}
114-
creds = _get_container_credentials("postgresql", env)
115-
assert creds["user"] == "postgres"
116-
assert creds["password"] == "secret"
117-
assert creds["database"] is None
115+
creds = _get_container_credentials(get_adapter_class("postgresql"), env)
116+
assert creds.user == "postgres"
117+
assert creds.password == "secret"
118+
assert creds.database == "postgres"
118119

119120
def test_user_without_password(self):
120121
"""Test custom user without password (trust auth)."""
121122
env = {"POSTGRES_USER": "devuser"}
122-
creds = _get_container_credentials("postgresql", env)
123-
assert creds["user"] == "devuser"
124-
assert creds["password"] is None
123+
creds = _get_container_credentials(get_adapter_class("postgresql"), env)
124+
assert creds.user == "devuser"
125+
assert creds.password is None
125126

126127
def test_empty_password_string(self):
127128
"""Test explicitly empty password."""
128129
env = {"POSTGRES_PASSWORD": ""}
129-
creds = _get_container_credentials("postgresql", env)
130-
assert creds["password"] == ""
130+
creds = _get_container_credentials(get_adapter_class("postgresql"), env)
131+
assert creds.password == ""
131132

132133

133134
class TestMySQLCredentials:
@@ -136,9 +137,9 @@ class TestMySQLCredentials:
136137
def test_root_password_only(self):
137138
"""Test MySQL with only root password (most common dev setup)."""
138139
env = {"MYSQL_ROOT_PASSWORD": "rootpass"}
139-
creds = _get_container_credentials("mysql", env)
140-
assert creds["user"] == "root"
141-
assert creds["password"] == "rootpass"
140+
creds = _get_container_credentials(get_adapter_class("mysql"), env)
141+
assert creds.user == "root"
142+
assert creds.password == "rootpass"
142143

143144
def test_user_credentials(self):
144145
"""Test MySQL with non-root user."""
@@ -147,10 +148,10 @@ def test_user_credentials(self):
147148
"MYSQL_PASSWORD": "apppass",
148149
"MYSQL_DATABASE": "appdb",
149150
}
150-
creds = _get_container_credentials("mysql", env)
151-
assert creds["user"] == "appuser"
152-
assert creds["password"] == "apppass"
153-
assert creds["database"] == "appdb"
151+
creds = _get_container_credentials(get_adapter_class("mysql"), env)
152+
assert creds.user == "appuser"
153+
assert creds.password == "apppass"
154+
assert creds.database == "appdb"
154155

155156
def test_user_takes_precedence_over_root(self):
156157
"""Test that MYSQL_USER is preferred over root."""
@@ -159,9 +160,9 @@ def test_user_takes_precedence_over_root(self):
159160
"MYSQL_USER": "appuser",
160161
"MYSQL_PASSWORD": "apppass",
161162
}
162-
creds = _get_container_credentials("mysql", env)
163-
assert creds["user"] == "appuser"
164-
assert creds["password"] == "apppass"
163+
creds = _get_container_credentials(get_adapter_class("mysql"), env)
164+
assert creds.user == "appuser"
165+
assert creds.password == "apppass"
165166

166167
def test_root_password_fallback(self):
167168
"""Test fallback to root password when user password missing."""
@@ -170,23 +171,23 @@ def test_root_password_fallback(self):
170171
"MYSQL_USER": "appuser",
171172
# No MYSQL_PASSWORD
172173
}
173-
creds = _get_container_credentials("mysql", env)
174+
creds = _get_container_credentials(get_adapter_class("mysql"), env)
174175
# User is set but no password, falls back to root password
175-
assert creds["user"] == "appuser"
176-
assert creds["password"] == "rootpass"
176+
assert creds.user == "appuser"
177+
assert creds.password == "rootpass"
177178

178179
def test_allow_empty_password(self):
179180
"""Test MYSQL_ALLOW_EMPTY_PASSWORD scenario."""
180181
env = {"MYSQL_ALLOW_EMPTY_PASSWORD": "yes"}
181-
creds = _get_container_credentials("mysql", env)
182-
assert creds["user"] == "root"
183-
assert creds["password"] is None
182+
creds = _get_container_credentials(get_adapter_class("mysql"), env)
183+
assert creds.user == "root"
184+
assert creds.password is None
184185

185186
def test_random_root_password(self):
186187
"""Test MYSQL_RANDOM_ROOT_PASSWORD (can't extract password)."""
187188
env = {"MYSQL_RANDOM_ROOT_PASSWORD": "yes"}
188-
creds = _get_container_credentials("mysql", env)
189-
assert creds["password"] is None
189+
creds = _get_container_credentials(get_adapter_class("mysql"), env)
190+
assert creds.password is None
190191

191192

192193
class TestMariaDBCredentials:
@@ -199,10 +200,10 @@ def test_mariadb_vars(self):
199200
"MARIADB_PASSWORD": "mariapass",
200201
"MARIADB_DATABASE": "mariadb",
201202
}
202-
creds = _get_container_credentials("mariadb", env)
203-
assert creds["user"] == "mariauser"
204-
assert creds["password"] == "mariapass"
205-
assert creds["database"] == "mariadb"
203+
creds = _get_container_credentials(get_adapter_class("mariadb"), env)
204+
assert creds.user == "mariauser"
205+
assert creds.password == "mariapass"
206+
assert creds.database == "mariadb"
206207

207208
def test_mysql_vars_fallback(self):
208209
"""Test fallback to MYSQL_* variables for compatibility."""
@@ -211,10 +212,10 @@ def test_mysql_vars_fallback(self):
211212
"MYSQL_PASSWORD": "mysqlpass",
212213
"MYSQL_DATABASE": "mysqldb",
213214
}
214-
creds = _get_container_credentials("mariadb", env)
215-
assert creds["user"] == "mysqluser"
216-
assert creds["password"] == "mysqlpass"
217-
assert creds["database"] == "mysqldb"
215+
creds = _get_container_credentials(get_adapter_class("mariadb"), env)
216+
assert creds.user == "mysqluser"
217+
assert creds.password == "mysqlpass"
218+
assert creds.database == "mysqldb"
218219

219220
def test_mariadb_takes_precedence(self):
220221
"""Test that MARIADB_* vars take precedence over MYSQL_*."""
@@ -223,22 +224,22 @@ def test_mariadb_takes_precedence(self):
223224
"MYSQL_USER": "mysqluser",
224225
"MARIADB_PASSWORD": "mariapass",
225226
}
226-
creds = _get_container_credentials("mariadb", env)
227-
assert creds["user"] == "mariauser"
227+
creds = _get_container_credentials(get_adapter_class("mariadb"), env)
228+
assert creds.user == "mariauser"
228229

229230
def test_mariadb_root_password(self):
230231
"""Test MariaDB root password variations."""
231232
env = {"MARIADB_ROOT_PASSWORD": "rootpass"}
232-
creds = _get_container_credentials("mariadb", env)
233-
assert creds["user"] == "root"
234-
assert creds["password"] == "rootpass"
233+
creds = _get_container_credentials(get_adapter_class("mariadb"), env)
234+
assert creds.user == "root"
235+
assert creds.password == "rootpass"
235236

236237
def test_mysql_root_password_fallback(self):
237238
"""Test fallback to MYSQL_ROOT_PASSWORD."""
238239
env = {"MYSQL_ROOT_PASSWORD": "rootpass"}
239-
creds = _get_container_credentials("mariadb", env)
240-
assert creds["user"] == "root"
241-
assert creds["password"] == "rootpass"
240+
creds = _get_container_credentials(get_adapter_class("mariadb"), env)
241+
assert creds.user == "root"
242+
assert creds.password == "rootpass"
242243

243244

244245
class TestOracleCredentials:
@@ -252,28 +253,28 @@ def test_oracle_app_user(self):
252253
"ORACLE_PASSWORD": "systempass",
253254
"ORACLE_DATABASE": "APPDB",
254255
}
255-
creds = _get_container_credentials("oracle", env)
256-
assert creds["user"] == "appuser"
257-
assert creds["password"] == "apppass"
258-
assert creds["database"] == "APPDB"
256+
creds = _get_container_credentials(get_adapter_class("oracle"), env)
257+
assert creds.user == "appuser"
258+
assert creds.password == "apppass"
259+
assert creds.database == "APPDB"
259260

260261
def test_oracle_defaults(self):
261262
"""Test Oracle defaults when no app user is set."""
262263
env = {"ORACLE_PASSWORD": "systempass"}
263-
creds = _get_container_credentials("oracle", env)
264-
assert creds["user"] == "SYSTEM"
265-
assert creds["password"] == "systempass"
266-
assert creds["database"] == "FREEPDB1"
264+
creds = _get_container_credentials(get_adapter_class("oracle"), env)
265+
assert creds.user == "SYSTEM"
266+
assert creds.password == "systempass"
267+
assert creds.database == "FREEPDB1"
267268

268269
def test_oracle_app_user_missing_password(self):
269270
"""Test fallback to SYSTEM when APP_USER has no password."""
270271
env = {
271272
"APP_USER": "appuser",
272273
"ORACLE_PASSWORD": "systempass",
273274
}
274-
creds = _get_container_credentials("oracle", env)
275-
assert creds["user"] == "SYSTEM"
276-
assert creds["password"] == "systempass"
275+
creds = _get_container_credentials(get_adapter_class("oracle"), env)
276+
assert creds.user == "SYSTEM"
277+
assert creds.password == "systempass"
277278

278279

279280
class TestSQLServerCredentials:
@@ -282,33 +283,33 @@ class TestSQLServerCredentials:
282283
def test_sa_password(self):
283284
"""Test standard SA_PASSWORD."""
284285
env = {"SA_PASSWORD": "StrongP@ss123"}
285-
creds = _get_container_credentials("mssql", env)
286-
assert creds["user"] == "sa"
287-
assert creds["password"] == "StrongP@ss123"
288-
assert creds["database"] is None
286+
creds = _get_container_credentials(get_adapter_class("mssql"), env)
287+
assert creds.user == "sa"
288+
assert creds.password == "StrongP@ss123"
289+
assert creds.database == "master"
289290

290291
def test_mssql_sa_password(self):
291292
"""Test alternative MSSQL_SA_PASSWORD."""
292293
env = {"MSSQL_SA_PASSWORD": "StrongP@ss123"}
293-
creds = _get_container_credentials("mssql", env)
294-
assert creds["user"] == "sa"
295-
assert creds["password"] == "StrongP@ss123"
294+
creds = _get_container_credentials(get_adapter_class("mssql"), env)
295+
assert creds.user == "sa"
296+
assert creds.password == "StrongP@ss123"
296297

297298
def test_sa_password_takes_precedence(self):
298299
"""Test SA_PASSWORD takes precedence over MSSQL_SA_PASSWORD."""
299300
env = {
300301
"SA_PASSWORD": "primary",
301302
"MSSQL_SA_PASSWORD": "secondary",
302303
}
303-
creds = _get_container_credentials("mssql", env)
304-
assert creds["password"] == "primary"
304+
creds = _get_container_credentials(get_adapter_class("mssql"), env)
305+
assert creds.password == "primary"
305306

306307
def test_accept_eula_only(self):
307308
"""Test container with only ACCEPT_EULA (no password = can't connect)."""
308309
env = {"ACCEPT_EULA": "Y"}
309-
creds = _get_container_credentials("mssql", env)
310-
assert creds["user"] == "sa"
311-
assert creds["password"] is None
310+
creds = _get_container_credentials(get_adapter_class("mssql"), env)
311+
assert creds.user == "sa"
312+
assert creds.password is None
312313

313314

314315
class TestClickHouseCredentials:
@@ -321,24 +322,24 @@ def test_full_credentials(self):
321322
"CLICKHOUSE_PASSWORD": "chpass",
322323
"CLICKHOUSE_DB": "chdb",
323324
}
324-
creds = _get_container_credentials("clickhouse", env)
325-
assert creds["user"] == "chuser"
326-
assert creds["password"] == "chpass"
327-
assert creds["database"] == "chdb"
325+
creds = _get_container_credentials(get_adapter_class("clickhouse"), env)
326+
assert creds.user == "chuser"
327+
assert creds.password == "chpass"
328+
assert creds.database == "chdb"
328329

329330
def test_defaults(self):
330331
"""Test ClickHouse defaults (default user, no password)."""
331-
creds = _get_container_credentials("clickhouse", {})
332-
assert creds["user"] == "default"
333-
assert creds["password"] is None
334-
assert creds["database"] is None
332+
creds = _get_container_credentials(get_adapter_class("clickhouse"), {})
333+
assert creds.user == "default"
334+
assert creds.password is None
335+
assert creds.database is None
335336

336337
def test_password_only(self):
337338
"""Test with only password set."""
338339
env = {"CLICKHOUSE_PASSWORD": "secret"}
339-
creds = _get_container_credentials("clickhouse", env)
340-
assert creds["user"] == "default"
341-
assert creds["password"] == "secret"
340+
creds = _get_container_credentials(get_adapter_class("clickhouse"), env)
341+
assert creds.user == "default"
342+
assert creds.password == "secret"
342343

343344

344345
class TestCockroachDBCredentials:
@@ -351,17 +352,17 @@ def test_full_credentials(self):
351352
"COCKROACH_PASSWORD": "crdbpass",
352353
"COCKROACH_DATABASE": "crdb",
353354
}
354-
creds = _get_container_credentials("cockroachdb", env)
355-
assert creds["user"] == "crdbuser"
356-
assert creds["password"] == "crdbpass"
357-
assert creds["database"] == "crdb"
355+
creds = _get_container_credentials(get_adapter_class("cockroachdb"), env)
356+
assert creds.user == "crdbuser"
357+
assert creds.password == "crdbpass"
358+
assert creds.database == "crdb"
358359

359360
def test_defaults_insecure_mode(self):
360361
"""Test CockroachDB defaults (often runs insecure in dev)."""
361-
creds = _get_container_credentials("cockroachdb", {})
362-
assert creds["user"] == "root"
363-
assert creds["password"] is None
364-
assert creds["database"] is None
362+
creds = _get_container_credentials(get_adapter_class("cockroachdb"), {})
363+
assert creds.user == "root"
364+
assert creds.password is None
365+
assert creds.database is None
365366

366367

367368
class TestEdgeCases:
@@ -370,33 +371,31 @@ class TestEdgeCases:
370371
def test_env_vars_with_whitespace(self):
371372
"""Test that whitespace in values is preserved."""
372373
env = {"POSTGRES_PASSWORD": " pass with spaces "}
373-
creds = _get_container_credentials("postgresql", env)
374-
assert creds["password"] == " pass with spaces "
374+
creds = _get_container_credentials(get_adapter_class("postgresql"), env)
375+
assert creds.password == " pass with spaces "
375376

376377
def test_env_vars_with_special_chars(self):
377378
"""Test passwords with special characters."""
378379
env = {"POSTGRES_PASSWORD": "p@ss=word!#$%^&*()"}
379-
creds = _get_container_credentials("postgresql", env)
380-
assert creds["password"] == "p@ss=word!#$%^&*()"
380+
creds = _get_container_credentials(get_adapter_class("postgresql"), env)
381+
assert creds.password == "p@ss=word!#$%^&*()"
381382

382383
def test_unicode_password(self):
383384
"""Test Unicode characters in password."""
384385
env = {"POSTGRES_PASSWORD": "密码123"}
385-
creds = _get_container_credentials("postgresql", env)
386-
assert creds["password"] == "密码123"
386+
creds = _get_container_credentials(get_adapter_class("postgresql"), env)
387+
assert creds.password == "密码123"
387388

388389
def test_unknown_db_type(self):
389-
"""Test graceful handling of unknown database type."""
390-
creds = _get_container_credentials("unknowndb", {"SOME_VAR": "value"})
391-
assert creds["user"] is None
392-
assert creds["password"] is None
393-
assert creds["database"] is None
390+
"""Test graceful handling of unknown database type - raises ValueError."""
391+
with pytest.raises(ValueError, match="Unknown database type"):
392+
get_adapter_class("unknowndb")
394393

395394
def test_case_sensitivity(self):
396395
"""Test that env var names are case-sensitive."""
397396
env = {
398397
"postgres_password": "lowercase", # Wrong case
399398
"POSTGRES_PASSWORD": "correct",
400399
}
401-
creds = _get_container_credentials("postgresql", env)
402-
assert creds["password"] == "correct"
400+
creds = _get_container_credentials(get_adapter_class("postgresql"), env)
401+
assert creds.password == "correct"

0 commit comments

Comments
 (0)