From c0916c45748ddd24f6848f13d023a3420dca902e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82?= Date: Thu, 21 Sep 2023 19:26:12 +0200 Subject: [PATCH] test: catch loguru json issues (#27) --- Makefile | 2 +- pki_tools/ocsp.py | 4 ++-- test/conftest.py | 22 ++++++++++++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 0d2b82c6..de081b47 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ format: .PHONY: pytest pytest: - poetry run python -m pytest ./test + poetry run python -m pytest -s --capture=no ./test make clean .PHONY: docs-gen diff --git a/pki_tools/ocsp.py b/pki_tools/ocsp.py index 028ab2dc..723557da 100644 --- a/pki_tools/ocsp.py +++ b/pki_tools/ocsp.py @@ -96,7 +96,7 @@ def is_revoked( log = logger.bind( cert=pki_tools.pem_from_cert(cert), - serial=cert.serial_number, + serial=pki_tools.get_cert_serial(cert), ) try: @@ -114,7 +114,7 @@ def is_revoked( return _check_ocsp_status(aia_exs, req_path, cert) except exceptions.OcspInvalidResponseStatus: log.bind(alg=alg.name).debug( - "OCSP check with failed, trying another algorithm" + "OCSP check failed, trying another algorithm" ) if i + 1 == len(OCSP_ALGORITHMS_TO_CHECK): log.bind( diff --git a/test/conftest.py b/test/conftest.py index 080f2be4..1daaa6fc 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -1,3 +1,4 @@ +import json import os from cryptography.hazmat.primitives.asymmetric import rsa @@ -12,6 +13,7 @@ from cryptography.hazmat.primitives import hashes, serialization from cryptography.hazmat.primitives._serialization import Encoding from cryptography.x509 import ocsp +from loguru import logger from pki_tools.crl import _get_crl_from_url from pki_tools.ocsp import _get_issuer_from_uri @@ -22,6 +24,26 @@ CURRENT_DIR = os.path.dirname(os.path.realpath(__file__)) +def test_loguru_sink(message): + try: + rec = message.record + extras = json.dumps(rec["extra"]) + print(f"{rec['time']} - {rec['level']} - {extras} - {rec['message']}") + except Exception as e: + print(f"Record was: {message.record}") + pytest.fail(f"Loguru error: {str(e)}") + + +@pytest.fixture(scope="function", autouse=True) +def setup_loguru_logging(request): + # Create a custom Loguru logger configuration that outputs to stdout + logger.remove() + logger.add( + sink=test_loguru_sink, # Custom sink to print to stdout + level="DEBUG", # Set the log level as desired + ) + + @pytest.fixture() def mocked_requests_get(mocker): _get_crl_from_url.cache_clear()