Skip to content

Commit 49cbc44

Browse files
kristjanvalurgerzse
authored andcommitted
Fix incorrect asserts in test and ensure connections are closed (#3004)
1 parent daab667 commit 49cbc44

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

tests/test_ssl.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ def test_ssl_with_invalid_cert(self, request):
2626
sslclient = redis.from_url(ssl_url)
2727
with pytest.raises(ConnectionError) as e:
2828
sslclient.ping()
29-
assert "SSL: CERTIFICATE_VERIFY_FAILED" in str(e)
29+
assert "SSL: CERTIFICATE_VERIFY_FAILED" in str(e)
30+
sslclient.close()
3031

3132
def test_ssl_connection(self, request):
3233
ssl_url = request.config.option.redis_ssl_url
3334
p = urlparse(ssl_url)[1].split(":")
3435
r = redis.Redis(host=p[0], port=p[1], ssl=True, ssl_cert_reqs="none")
3536
assert r.ping()
37+
r.close()
3638

3739
def test_ssl_connection_without_ssl(self, request):
3840
ssl_url = request.config.option.redis_ssl_url
@@ -41,7 +43,8 @@ def test_ssl_connection_without_ssl(self, request):
4143

4244
with pytest.raises(ConnectionError) as e:
4345
r.ping()
44-
assert "Connection closed by server" in str(e)
46+
assert "Connection closed by server" in str(e)
47+
r.close()
4548

4649
def test_validating_self_signed_certificate(self, request):
4750
ssl_url = request.config.option.redis_ssl_url
@@ -56,6 +59,7 @@ def test_validating_self_signed_certificate(self, request):
5659
ssl_ca_certs=self.SERVER_CERT,
5760
)
5861
assert r.ping()
62+
r.close()
5963

6064
def test_validating_self_signed_string_certificate(self, request):
6165
with open(self.SERVER_CERT) as f:
@@ -72,6 +76,7 @@ def test_validating_self_signed_string_certificate(self, request):
7276
ssl_ca_data=cert_data,
7377
)
7478
assert r.ping()
79+
r.close()
7580

7681
@pytest.mark.parametrize(
7782
"ssl_ciphers",
@@ -154,22 +159,25 @@ def _create_oscp_conn(self, request):
154159
def test_ssl_ocsp_called(self, request):
155160
r = self._create_oscp_conn(request)
156161
with pytest.raises(RedisError) as e:
157-
assert r.ping()
158-
assert "cryptography not installed" in str(e)
162+
r.ping()
163+
assert "cryptography is not installed" in str(e)
164+
r.close()
159165

160166
@skip_if_nocryptography()
161167
def test_ssl_ocsp_called_withcrypto(self, request):
162168
r = self._create_oscp_conn(request)
163169
with pytest.raises(ConnectionError) as e:
164170
assert r.ping()
165-
assert "No AIA information present in ssl certificate" in str(e)
171+
assert "No AIA information present in ssl certificate" in str(e)
172+
r.close()
166173

167174
# rediss://, url based
168175
ssl_url = request.config.option.redis_ssl_url
169176
sslclient = redis.from_url(ssl_url)
170177
with pytest.raises(ConnectionError) as e:
171178
sslclient.ping()
172-
assert "No AIA information present in ssl certificate" in str(e)
179+
assert "No AIA information present in ssl certificate" in str(e)
180+
sslclient.close()
173181

174182
@skip_if_nocryptography()
175183
def test_valid_ocsp_cert_http(self):
@@ -194,7 +202,7 @@ def test_revoked_ocsp_certificate(self):
194202
ocsp = OCSPVerifier(wrapped, hostname, 443)
195203
with pytest.raises(ConnectionError) as e:
196204
assert ocsp.is_valid()
197-
assert "REVOKED" in str(e)
205+
assert "REVOKED" in str(e)
198206

199207
@skip_if_nocryptography()
200208
def test_unauthorized_ocsp(self):
@@ -219,7 +227,7 @@ def test_ocsp_not_present_in_response(self):
219227
ocsp = OCSPVerifier(wrapped, hostname, 443)
220228
with pytest.raises(ConnectionError) as e:
221229
assert ocsp.is_valid()
222-
assert "from the" in str(e)
230+
assert "from the" in str(e)
223231

224232
@skip_if_nocryptography()
225233
def test_unauthorized_then_direct(self):
@@ -255,6 +263,7 @@ def test_mock_ocsp_staple(self, request):
255263

256264
with pytest.raises(RedisError):
257265
r.ping()
266+
r.close()
258267

259268
ctx = OpenSSL.SSL.Context(OpenSSL.SSL.SSLv23_METHOD)
260269
ctx.use_certificate_file(self.SERVER_CERT)
@@ -275,7 +284,8 @@ def test_mock_ocsp_staple(self, request):
275284

276285
with pytest.raises(ConnectionError) as e:
277286
r.ping()
278-
assert "no ocsp response present" in str(e)
287+
assert "no ocsp response present" in str(e)
288+
r.close()
279289

280290
r = redis.Redis(
281291
host=p[0],
@@ -290,4 +300,5 @@ def test_mock_ocsp_staple(self, request):
290300

291301
with pytest.raises(ConnectionError) as e:
292302
r.ping()
293-
assert "no ocsp response present" in str(e)
303+
assert "no ocsp response present" in str(e)
304+
r.close()

0 commit comments

Comments
 (0)