Skip to content

Commit 4bf82b6

Browse files
use pytest-httpbin also for SSL tests
1 parent deaf361 commit 4bf82b6

File tree

5 files changed

+29
-37
lines changed

5 files changed

+29
-37
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,6 @@ jobs:
5656
runs-on: ${{ matrix.os }}
5757
name: test-${{ matrix.python-version }} ${{ matrix.nox-session }}
5858
continue-on-error: ${{ matrix.experimental }}
59-
services:
60-
elasticsearch:
61-
image: docker.elastic.co/elasticsearch/elasticsearch:9.0.2
62-
env:
63-
discovery.type: single-node
64-
ports:
65-
- 9200:9200
6659
steps:
6760
- name: Checkout repository
6861
uses: actions/checkout@v2

tests/conftest.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,24 @@ async def perform_request(self, *args, **kwargs):
6666
return NodeApiResponse(meta, self.body)
6767

6868

69-
@pytest.fixture(scope="session", params=[True, False])
70-
def cert_fingerprint(request) -> str:
71-
"""Gets the SHA256 fingerprint of the certificate for localhost:9200"""
72-
sock = socket.create_connection(("localhost", 9200))
69+
@pytest.fixture(
70+
scope="session", params=["short-lower", "short-upper", "long-lower", "long-upper"]
71+
)
72+
def cert_fingerprint(request, httpbin_secure) -> str:
73+
"""Gets the SHA256 fingerprint of the certificate for the secure httpbin"""
74+
sock = socket.create_connection((httpbin_secure.host, httpbin_secure.port))
7375
ctx = ssl.create_default_context()
7476
ctx.check_hostname = False
7577
ctx.verify_mode = ssl.CERT_NONE
7678
sock = ctx.wrap_socket(sock)
7779
digest = hashlib.sha256(sock.getpeercert(binary_form=True)).hexdigest()
7880
assert len(digest) == 64
7981
sock.close()
80-
if request.param:
82+
if "upper" in request.param:
83+
digest = digest.upper()
84+
else:
85+
digest = digest.lower()
86+
if "short" in request.param:
8187
return digest
8288
else:
8389
return ":".join([digest[i : i + 2] for i in range(0, len(digest), 2)])

tests/node/test_http_aiohttp.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,19 +290,19 @@ async def test_head_workaround(self, aiohttp_fixed_head_bug):
290290

291291

292292
@pytest.mark.asyncio
293-
async def test_ssl_assert_fingerprint(cert_fingerprint):
293+
async def test_ssl_assert_fingerprint(cert_fingerprint, httpbin_secure):
294294
with warnings.catch_warnings(record=True) as w:
295295
node = AiohttpHttpNode(
296296
NodeConfig(
297297
scheme="https",
298-
host="localhost",
299-
port=9200,
298+
host=httpbin_secure.host,
299+
port=httpbin_secure.port,
300300
ssl_assert_fingerprint=cert_fingerprint,
301301
)
302302
)
303303
resp, _ = await node.perform_request("GET", "/")
304304

305-
assert resp.status == 401
305+
assert resp.status == 200
306306
assert [str(x.message) for x in w if x.category != DeprecationWarning] == []
307307

308308

tests/node/test_http_httpx.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,13 @@ async def test_merge_headers(self):
145145
assert request.headers["h3"] == "v3"
146146

147147

148-
def test_ssl_assert_fingerprint(cert_fingerprint):
148+
def test_ssl_assert_fingerprint(cert_fingerprint, httpbin_secure):
149149
with pytest.raises(ValueError, match="httpx does not support certificate pinning"):
150150
HttpxAsyncHttpNode(
151151
NodeConfig(
152152
scheme="https",
153-
host="localhost",
154-
port=9200,
153+
host=httpbin_secure.host,
154+
port=httpbin_secure.port,
155155
ssl_assert_fingerprint=cert_fingerprint,
156156
)
157157
)

tests/node/test_urllib3_chain_certs.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@
3030

3131
@requires_ssl_assert_fingerprint_in_chain
3232
@pytest.mark.parametrize("node_cls", [Urllib3HttpNode, RequestsHttpNode])
33-
def test_ssl_assert_fingerprint_invalid_length(node_cls):
33+
def test_ssl_assert_fingerprint_invalid_length(node_cls, httpbin_secure):
3434
with pytest.raises(ValueError) as e:
3535
node_cls(
3636
NodeConfig(
3737
"https",
38-
"localhost",
39-
9200,
38+
httpbin_secure.host,
39+
httpbin_secure.port,
4040
ssl_assert_fingerprint="0000",
4141
)
4242
)
@@ -49,22 +49,14 @@ def test_ssl_assert_fingerprint_invalid_length(node_cls):
4949

5050
@requires_ssl_assert_fingerprint_in_chain
5151
@pytest.mark.parametrize("node_cls", [Urllib3HttpNode, RequestsHttpNode])
52-
@pytest.mark.parametrize(
53-
"ssl_assert_fingerprint",
54-
[
55-
"18efbd94dda87e3598a1251f9440cd2f4fd1dbf08be007c1012e992e830ca262",
56-
"18:EF:BD:94:DD:A8:7E:35:98:A1:25:1F:94:40:CD:2F:4F:D1:DB:F0:8B:E0:07:C1:01:2E:99:2E:83:0C:A2:62",
57-
"18EFBD94DDA87E3598A1251F9440CD2F4FD1DBF08BE007C1012E992E830CA262",
58-
],
59-
)
60-
def test_assert_fingerprint_in_cert_chain(node_cls, ssl_assert_fingerprint):
52+
def test_assert_fingerprint_in_cert_chain(node_cls, cert_fingerprint, httpbin_secure):
6153
with warnings.catch_warnings(record=True) as w:
6254
node = node_cls(
6355
NodeConfig(
6456
"https",
65-
"www.elastic.co",
66-
443,
67-
ssl_assert_fingerprint=ssl_assert_fingerprint,
57+
httpbin_secure.host,
58+
httpbin_secure.port,
59+
ssl_assert_fingerprint=cert_fingerprint,
6860
)
6961
)
7062
meta, _ = node.perform_request("GET", "/")
@@ -75,7 +67,9 @@ def test_assert_fingerprint_in_cert_chain(node_cls, ssl_assert_fingerprint):
7567

7668
@requires_ssl_assert_fingerprint_in_chain
7769
@pytest.mark.parametrize("node_cls", [Urllib3HttpNode, RequestsHttpNode])
78-
def test_assert_fingerprint_in_cert_chain_failure(node_cls):
70+
def test_assert_fingerprint_in_cert_chain_failure(
71+
node_cls, httpbin_secure, cert_fingerprint
72+
):
7973
node = node_cls(
8074
NodeConfig(
8175
"https",
@@ -89,12 +83,11 @@ def test_assert_fingerprint_in_cert_chain_failure(node_cls):
8983
node.perform_request("GET", "/")
9084

9185
err = str(e.value)
92-
print(err)
9386
assert "Fingerprints did not match." in err
9487
# This is the bad value we "expected"
9588
assert (
9689
'Expected "0000000000000000000000000000000000000000000000000000000000000000",'
9790
in err
9891
)
9992
# This is the root CA for httpbin.org with a leading comma to denote more than one cert was listed.
100-
assert '"18efbd94dda87e3598a1251f9440cd2f4fd1dbf08be007c1012e992e830ca262"' in err
93+
assert ', "cbb522d7b7f127ad6a0113865bdf1cd4102e7d0759af635a7cf4720dc963c53b"' in err

0 commit comments

Comments
 (0)