Skip to content

Commit

Permalink
test: catch loguru json issues (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
fulder authored Sep 21, 2023
1 parent 62b3af6 commit c0916c4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pki_tools/ocsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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(
Expand Down
22 changes: 22 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import os

from cryptography.hazmat.primitives.asymmetric import rsa
Expand All @@ -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
Expand All @@ -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()
Expand Down

0 comments on commit c0916c4

Please sign in to comment.