Skip to content

Commit fd2666e

Browse files
pquentinsethmlarson
authored andcommitted
Use fixture to configure NO_SAN test certs
Switching to pytest fixtures for those tests will allow to switch to dynamically generated certificates in the future, without changing the test. Using fixtures is easier than the existing setup because it's easy to send information to the test about url, port and ca certs to use.
1 parent 6322ad5 commit fd2666e

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

test/conftest.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
1+
import collections
2+
import contextlib
3+
import threading
14
import platform
25
import sys
36

47
import pytest
58
import trustme
9+
from tornado import web, ioloop
610

11+
from dummyserver.handlers import TestingApp
12+
from dummyserver.server import run_tornado_app
713
from dummyserver.server import (
814
DEFAULT_CA,
915
DEFAULT_CA_KEY,
1016
CLIENT_INTERMEDIATE_PEM,
1117
CLIENT_NO_INTERMEDIATE_PEM,
1218
CLIENT_INTERMEDIATE_KEY,
19+
NO_SAN_CA,
20+
NO_SAN_CERTS,
1321
)
1422

1523

@@ -42,3 +50,27 @@ def certs_dir(tmp_path_factory):
4250
cert.cert_chain_pems[0].write_to_path(str(tmpdir / CLIENT_NO_INTERMEDIATE_PEM))
4351

4452
yield tmpdir
53+
54+
55+
ServerConfig = collections.namedtuple("ServerConfig", ["host", "port", "ca_certs"])
56+
57+
58+
@contextlib.contextmanager
59+
def run_server_in_thread(scheme, host, ca_certs, server_certs):
60+
io_loop = ioloop.IOLoop.current()
61+
app = web.Application([(r".*", TestingApp)])
62+
server, port = run_tornado_app(app, io_loop, server_certs, scheme, host)
63+
server_thread = threading.Thread(target=io_loop.start)
64+
server_thread.start()
65+
66+
yield ServerConfig(host, port, ca_certs)
67+
68+
io_loop.add_callback(server.stop)
69+
io_loop.add_callback(io_loop.stop)
70+
server_thread.join()
71+
72+
73+
@pytest.fixture
74+
def no_san_server(tmp_path_factory):
75+
with run_server_in_thread("https", "localhost", NO_SAN_CA, NO_SAN_CERTS) as cfg:
76+
yield cfg

test/with_dummyserver/test_https.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
DEFAULT_CA,
1919
DEFAULT_CA_BAD,
2020
DEFAULT_CERTS,
21-
NO_SAN_CERTS,
22-
NO_SAN_CA,
2321
IPV6_ADDR_CERTS,
2422
IPV6_ADDR_CA,
2523
HAS_IPV6,
@@ -707,15 +705,16 @@ class TestHTTPS_TLSv1_3(TestHTTPS):
707705
certs = TLSv1_3_CERTS
708706

709707

710-
class TestHTTPS_NoSAN(HTTPSDummyServerTestCase):
711-
certs = NO_SAN_CERTS
712-
713-
def test_warning_for_certs_without_a_san(self):
708+
class TestHTTPS_NoSAN:
709+
def test_warning_for_certs_without_a_san(self, no_san_server):
714710
"""Ensure that a warning is raised when the cert from the server has
715711
no Subject Alternative Name."""
716712
with mock.patch("warnings.warn") as warn:
717713
with HTTPSConnectionPool(
718-
self.host, self.port, cert_reqs="CERT_REQUIRED", ca_certs=NO_SAN_CA
714+
no_san_server.host,
715+
no_san_server.port,
716+
cert_reqs="CERT_REQUIRED",
717+
ca_certs=no_san_server.ca_certs,
719718
) as https_pool:
720719
r = https_pool.request("GET", "/")
721720
assert r.status == 200

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy