Visitar URL original
mesh-2092: added unit test coverage · NHSDigital/mesh-client@62f438f · GitHub
Skip to content

Commit 62f438f

Browse files
mesh-2092: added unit test coverage
1 parent cab80c0 commit 62f438f

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

‎tests/ssl_context_adapter_tests.py‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from __future__ import annotations
2+
3+
from unittest.mock import Mock
4+
5+
import pytest
6+
7+
import mesh_client
8+
9+
10+
class SSLContextStub:
11+
def __init__(self) -> None:
12+
self.load_cert_chain = Mock()
13+
self.load_verify_locations = Mock()
14+
self.minimum_version = None
15+
self.check_hostname = None
16+
self.verify_mode = None
17+
self.hostname_checks_common_name = None
18+
19+
20+
@pytest.mark.parametrize(
21+
("certs", "loads_cert_chain_called"),
22+
[
23+
(("client.crt", "client.key"), True),
24+
(None, False),
25+
],
26+
)
27+
def test_create_ssl_context_loads_cert_chain(monkeypatch, certs: tuple[str, str] | None, loads_cert_chain_called: bool):
28+
29+
dummy_context = SSLContextStub()
30+
monkeypatch.setattr(mesh_client, "create_urllib3_context", lambda: dummy_context)
31+
32+
adapter = mesh_client.SSLContextAdapter(cert=certs, verify=None)
33+
dummy_context.load_cert_chain.reset_mock()
34+
35+
context = adapter.create_ssl_context()
36+
37+
assert context is dummy_context
38+
39+
if loads_cert_chain_called:
40+
assert certs is not None
41+
dummy_context.load_cert_chain.assert_called_with(*certs)
42+
else:
43+
dummy_context.load_cert_chain.assert_not_called()
44+
45+
46+
@pytest.mark.parametrize("verify", [True, False, "/path/to/ca.pem"])
47+
def test_create_ssl_context_loads_verify_locations_when_verify_is_provided(monkeypatch, verify: bool | str | None):
48+
dummy_context = SSLContextStub()
49+
monkeypatch.setattr(mesh_client, "create_urllib3_context", lambda: dummy_context)
50+
51+
adapter = mesh_client.SSLContextAdapter(cert=None, verify=verify)
52+
context = adapter.create_ssl_context()
53+
54+
assert context is dummy_context
55+
56+
if verify == "/path/to/ca.pem":
57+
dummy_context.load_verify_locations.assert_called_with(verify)
58+
else:
59+
dummy_context.load_verify_locations.assert_not_called()

0 commit comments

Comments
 (0)