Skip to content

Commit e823d2b

Browse files
committed
Fix TLS options test for github.
1 parent 93645fc commit e823d2b

File tree

1 file changed

+84
-65
lines changed

1 file changed

+84
-65
lines changed

tests/unit/test_environment.py

Lines changed: 84 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -83,68 +83,87 @@ def test_disconnect_timeout(monkeypatch: pytest.MonkeyPatch) -> None:
8383

8484

8585
def test_tls_options(monkeypatch: pytest.MonkeyPatch) -> None:
86-
tls_options: TlsOptions = TlsOptions()
87-
assert tls_options.enabled is False
88-
assert tls_options.ca_cert_path is None
89-
assert tls_options.client_cert_path is None
90-
assert tls_options.client_key_path is None
91-
92-
ca_cert_path: str = "/tmp/ca.pem"
93-
client_cert_path: str = "/tmp/client.pem"
94-
client_key_path: str = "/tmp/client.key"
95-
ca_cert_path2: str = "/tmp/ca2.pem"
96-
client_cert_path2: str = "/tmp/client2.pem"
97-
client_key_path2: str = "/tmp/client2.key"
98-
99-
tls_options = TlsOptions(
100-
enabled=True, ca_cert_path=ca_cert_path, client_cert_path=client_cert_path, client_key_path=client_key_path
101-
)
102-
assert tls_options.enabled
103-
assert tls_options.ca_cert_path == ca_cert_path
104-
assert tls_options.client_cert_path == client_cert_path
105-
assert tls_options.client_key_path == client_key_path
106-
107-
monkeypatch.setenv(name=TlsOptions.ENV_CA_CERT, value=ca_cert_path)
108-
monkeypatch.setenv(name=TlsOptions.ENV_CLIENT_CERT, value=client_cert_path)
109-
monkeypatch.setenv(name=TlsOptions.ENV_CLIENT_KEY, value=client_key_path)
110-
111-
tls_options = TlsOptions()
112-
assert tls_options.enabled is False
113-
assert tls_options.ca_cert_path == ca_cert_path
114-
assert tls_options.client_cert_path == client_cert_path
115-
assert tls_options.client_key_path == client_key_path
116-
117-
tls_options = TlsOptions(
118-
enabled=True, ca_cert_path=ca_cert_path2, client_cert_path=client_cert_path2, client_key_path=client_key_path2
119-
)
120-
assert tls_options.enabled
121-
assert tls_options.ca_cert_path == ca_cert_path
122-
assert tls_options.client_cert_path == client_cert_path
123-
assert tls_options.client_key_path == client_key_path
124-
125-
monkeypatch.delenv(name=TlsOptions.ENV_CA_CERT)
126-
tls_options = TlsOptions(
127-
enabled=True, ca_cert_path=ca_cert_path2, client_cert_path=client_cert_path2, client_key_path=client_key_path2
128-
)
129-
assert tls_options.enabled
130-
assert tls_options.ca_cert_path == ca_cert_path2
131-
assert tls_options.client_cert_path == client_cert_path
132-
assert tls_options.client_key_path == client_key_path
133-
134-
monkeypatch.delenv(name=TlsOptions.ENV_CLIENT_CERT)
135-
tls_options = TlsOptions(
136-
enabled=True, ca_cert_path=ca_cert_path2, client_cert_path=client_cert_path2, client_key_path=client_key_path2
137-
)
138-
assert tls_options.enabled
139-
assert tls_options.ca_cert_path == ca_cert_path2
140-
assert tls_options.client_cert_path == client_cert_path2
141-
assert tls_options.client_key_path == client_key_path
142-
143-
monkeypatch.delenv(name=TlsOptions.ENV_CLIENT_KEY)
144-
tls_options = TlsOptions(
145-
enabled=True, ca_cert_path=ca_cert_path2, client_cert_path=client_cert_path2, client_key_path=client_key_path2
146-
)
147-
assert tls_options.enabled
148-
assert tls_options.ca_cert_path == ca_cert_path2
149-
assert tls_options.client_cert_path == client_cert_path2
150-
assert tls_options.client_key_path == client_key_path2
86+
try:
87+
monkeypatch.delenv(name=TlsOptions.ENV_CA_CERT, raising=False)
88+
monkeypatch.delenv(name=TlsOptions.ENV_CLIENT_CERT, raising=False)
89+
monkeypatch.delenv(name=TlsOptions.ENV_CLIENT_KEY, raising=False)
90+
91+
tls_options: TlsOptions = TlsOptions()
92+
assert tls_options.enabled is False
93+
assert tls_options.ca_cert_path is None
94+
assert tls_options.client_cert_path is None
95+
assert tls_options.client_key_path is None
96+
97+
ca_cert_path: str = "/tmp/ca.pem"
98+
client_cert_path: str = "/tmp/client.pem"
99+
client_key_path: str = "/tmp/client.key"
100+
ca_cert_path2: str = "/tmp/ca2.pem"
101+
client_cert_path2: str = "/tmp/client2.pem"
102+
client_key_path2: str = "/tmp/client2.key"
103+
104+
tls_options = TlsOptions(
105+
enabled=True, ca_cert_path=ca_cert_path, client_cert_path=client_cert_path, client_key_path=client_key_path
106+
)
107+
assert tls_options.enabled
108+
assert tls_options.ca_cert_path == ca_cert_path
109+
assert tls_options.client_cert_path == client_cert_path
110+
assert tls_options.client_key_path == client_key_path
111+
112+
monkeypatch.setenv(name=TlsOptions.ENV_CA_CERT, value=ca_cert_path)
113+
monkeypatch.setenv(name=TlsOptions.ENV_CLIENT_CERT, value=client_cert_path)
114+
monkeypatch.setenv(name=TlsOptions.ENV_CLIENT_KEY, value=client_key_path)
115+
116+
tls_options = TlsOptions()
117+
assert tls_options.enabled is False
118+
assert tls_options.ca_cert_path == ca_cert_path
119+
assert tls_options.client_cert_path == client_cert_path
120+
assert tls_options.client_key_path == client_key_path
121+
122+
tls_options = TlsOptions(
123+
enabled=True,
124+
ca_cert_path=ca_cert_path2,
125+
client_cert_path=client_cert_path2,
126+
client_key_path=client_key_path2,
127+
)
128+
assert tls_options.enabled
129+
assert tls_options.ca_cert_path == ca_cert_path
130+
assert tls_options.client_cert_path == client_cert_path
131+
assert tls_options.client_key_path == client_key_path
132+
133+
monkeypatch.delenv(name=TlsOptions.ENV_CA_CERT)
134+
tls_options = TlsOptions(
135+
enabled=True,
136+
ca_cert_path=ca_cert_path2,
137+
client_cert_path=client_cert_path2,
138+
client_key_path=client_key_path2,
139+
)
140+
assert tls_options.enabled
141+
assert tls_options.ca_cert_path == ca_cert_path2
142+
assert tls_options.client_cert_path == client_cert_path
143+
assert tls_options.client_key_path == client_key_path
144+
145+
monkeypatch.delenv(name=TlsOptions.ENV_CLIENT_CERT)
146+
tls_options = TlsOptions(
147+
enabled=True,
148+
ca_cert_path=ca_cert_path2,
149+
client_cert_path=client_cert_path2,
150+
client_key_path=client_key_path2,
151+
)
152+
assert tls_options.enabled
153+
assert tls_options.ca_cert_path == ca_cert_path2
154+
assert tls_options.client_cert_path == client_cert_path2
155+
assert tls_options.client_key_path == client_key_path
156+
157+
monkeypatch.delenv(name=TlsOptions.ENV_CLIENT_KEY)
158+
tls_options = TlsOptions(
159+
enabled=True,
160+
ca_cert_path=ca_cert_path2,
161+
client_cert_path=client_cert_path2,
162+
client_key_path=client_key_path2,
163+
)
164+
assert tls_options.enabled
165+
assert tls_options.ca_cert_path == ca_cert_path2
166+
assert tls_options.client_cert_path == client_cert_path2
167+
assert tls_options.client_key_path == client_key_path2
168+
finally:
169+
monkeypatch.undo()

0 commit comments

Comments
 (0)