|
| 1 | +from urllib.parse import urlparse |
| 2 | +import pytest |
| 3 | +import redis.anyio as redis |
| 4 | + |
| 5 | +# Skip test or not based on cryptography installation |
| 6 | +try: |
| 7 | + import cryptography # noqa |
| 8 | + |
| 9 | + skip_if_cryptography = pytest.mark.skipif(False, reason="") |
| 10 | + skip_if_nocryptography = pytest.mark.skipif(False, reason="") |
| 11 | +except ImportError: |
| 12 | + skip_if_cryptography = pytest.mark.skipif(True, reason="cryptography not installed") |
| 13 | + skip_if_nocryptography = pytest.mark.skipif( |
| 14 | + True, reason="cryptography not installed" |
| 15 | + ) |
| 16 | + |
| 17 | +pytestmark = pytest.mark.anyio |
| 18 | + |
| 19 | + |
| 20 | +@pytest.mark.ssl |
| 21 | +class TestSSL: |
| 22 | + """Tests for SSL connections in asyncio.""" |
| 23 | + |
| 24 | + @pytest.fixture |
| 25 | + async def _get_client(self, request): |
| 26 | + ssl_url = request.config.option.redis_ssl_url |
| 27 | + p = urlparse(ssl_url)[1].split(":") |
| 28 | + client = redis.Redis(host=p[0], port=p[1], ssl=True) |
| 29 | + yield client |
| 30 | + await client.aclose() |
| 31 | + |
| 32 | + async def test_ssl_with_invalid_cert(self, _get_client): |
| 33 | + """Test SSL connection with invalid certificate.""" |
| 34 | + pass |
| 35 | + |
| 36 | + async def test_cert_reqs_none_with_check_hostname(self, request): |
| 37 | + """Test that when ssl_cert_reqs=none is used with ssl_check_hostname=True, |
| 38 | + the connection is created successfully with check_hostname internally set to False""" |
| 39 | + ssl_url = request.config.option.redis_ssl_url |
| 40 | + parsed_url = urlparse(ssl_url) |
| 41 | + r = redis.Redis( |
| 42 | + host=parsed_url.hostname, |
| 43 | + port=parsed_url.port, |
| 44 | + ssl=True, |
| 45 | + ssl_cert_reqs="none", |
| 46 | + # Check that ssl_check_hostname is ignored, when ssl_cert_reqs=none |
| 47 | + ssl_check_hostname=True, |
| 48 | + ) |
| 49 | + try: |
| 50 | + # Connection should be successful |
| 51 | + assert await r.ping() |
| 52 | + # check_hostname should have been automatically set to False |
| 53 | + assert r.connection_pool.connection_class == redis.SSLConnection |
| 54 | + conn = r.connection_pool.make_connection() |
| 55 | + assert conn.check_hostname is False |
| 56 | + finally: |
| 57 | + await r.aclose() |
0 commit comments