|
| 1 | +# tests/rhosocial/activerecord_sqlserver_test/feature/backend/test_ssl_connection_params.py |
| 2 | +"""Regression tests: SQL Server TLS settings must reach the ODBC connection string. |
| 3 | +
|
| 4 | +``build_connection_string()`` is the only place ``pyodbc`` gets its |
| 5 | +configuration, so anything the config exposes for TLS has to appear there. |
| 6 | +""" |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from rhosocial.activerecord.backend.impl.sqlserver.config import SQLServerConnectionConfig |
| 11 | + |
| 12 | + |
| 13 | +def make_config(**kwargs): |
| 14 | + defaults = dict(host="db.example.com", database="test_db", username="sa", password="secret") |
| 15 | + defaults.update(kwargs) |
| 16 | + return SQLServerConnectionConfig(**defaults) |
| 17 | + |
| 18 | + |
| 19 | +def test_encryption_is_on_by_default(): |
| 20 | + assert "Encrypt=yes" in make_config().build_connection_string() |
| 21 | + assert "TrustServerCertificate=no" in make_config().build_connection_string() |
| 22 | + |
| 23 | + |
| 24 | +def test_encrypt_can_be_disabled(): |
| 25 | + conn_str = make_config(encrypt=False).build_connection_string() |
| 26 | + assert "Encrypt=no" in conn_str |
| 27 | + |
| 28 | + |
| 29 | +def test_trust_server_certificate_is_forwarded(): |
| 30 | + conn_str = make_config(trust_server_certificate=True).build_connection_string() |
| 31 | + assert "TrustServerCertificate=yes" in conn_str |
| 32 | + |
| 33 | + |
| 34 | +def test_client_certificate_is_forwarded(): |
| 35 | + conn_str = make_config( |
| 36 | + client_certificate="/certs/client.pem", |
| 37 | + client_key="/certs/client.key", |
| 38 | + ).build_connection_string() |
| 39 | + assert "ClientCertificate=/certs/client.pem" in conn_str |
| 40 | + assert "ClientKey=/certs/client.key" in conn_str |
| 41 | + |
| 42 | + |
| 43 | +def test_host_name_in_certificate_is_forwarded(): |
| 44 | + """Needed when the server certificate CN is not the connect host.""" |
| 45 | + conn_str = make_config(host_name_in_certificate="db.internal").build_connection_string() |
| 46 | + assert "HostNameInCertificate=db.internal" in conn_str |
| 47 | + |
| 48 | + |
| 49 | +def test_client_key_password_is_forwarded(): |
| 50 | + conn_str = make_config( |
| 51 | + client_certificate="/certs/client.pfx", |
| 52 | + client_key_password="pfx-secret", |
| 53 | + ).build_connection_string() |
| 54 | + assert "ClientKeyPassword=pfx-secret" in conn_str |
| 55 | + |
| 56 | + |
| 57 | +def test_no_client_tls_keys_when_unset(): |
| 58 | + conn_str = make_config().build_connection_string() |
| 59 | + for key in ("ClientCertificate", "ClientKey", "ClientKeyPassword", "HostNameInCertificate"): |
| 60 | + assert key not in conn_str |
| 61 | + |
| 62 | + |
| 63 | +def test_client_key_without_certificate_is_rejected(): |
| 64 | + with pytest.raises(ValueError, match="client_key requires client_certificate"): |
| 65 | + make_config(client_key="/certs/client.key").validate_config() |
0 commit comments