Skip to content

Commit

Permalink
fix: test fix (clear chache b4 test)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zicchio committed Nov 4, 2024
1 parent 3788d0d commit 097777d
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 63 deletions.
78 changes: 40 additions & 38 deletions pyeudiw/tests/tools/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@

import datetime
import requests
import sys
import unittest.mock

import freezegun
import pytest

from pyeudiw.tools.utils import exp_from_now, iat_now, make_timezone_aware, random_token
from pyeudiw.tools.utils import cacheable_get_http_url, _lru_cached_get_http_url


def test_make_timezone_aware():
Expand Down Expand Up @@ -79,41 +82,40 @@ def test_random_token(n):
_hex = int(rand, 16)
assert type(_hex) is int

# import unittest.mock
# import requests
# from pyeudiw.tools.utils import cacheable_get_http_url, _lru_cached_get_http_url
# def test_cacheable_get_http_url():
# # DEV NOTE: for some reson, this test fails in the github action but works ok locally. This needs further investigation.
# tries = 5
# ok_response = requests.Response()
# ok_response.status_code = 200
# ok_response.headers.update({"Content-Type": "text/plain"})
# ok_response._content = b'Hello automated test'
# mocked_endpoint = unittest.mock.patch(
# "pyeudiw.tools.utils.get_http_url",
# return_value=[ok_response]
# )

# cache_ttl: int = 60*60*24*365 # 1 year
# httpc_p = {
# "connection": {
# "ssl": False,
# },
# "session": {
# "timeout": 1
# }
# }

# mocked_endpoint.start()
# for _ in range(tries):
# resp = cacheable_get_http_url(cache_ttl, "http://location.example", httpc_p, http_async=False)
# assert resp.status_code == 200
# assert resp._content == b'Hello automated test'
# mocked_endpoint.stop()

# cache_misses = _lru_cached_get_http_url.cache_info().misses
# exp_cache_misses = 1
# cache_hits = _lru_cached_get_http_url.cache_info().hits
# exp_cache_hits = tries - 1
# assert cache_misses == exp_cache_misses, f"cache missed more that {exp_cache_misses} time: {cache_misses}"
# assert cache_hits == exp_cache_hits, f"cache hit less than {exp_cache_hits} times: {cache_hits}"

def test_cacheable_get_http_url():
# DEV NOTE: for some reson, this test fails in the github action but works ok locally. This needs further investigation.
tries = 5
ok_response = requests.Response()
ok_response.status_code = 200
ok_response.headers.update({"Content-Type": "text/plain"})
ok_response._content = b'Hello automated test'
mocked_endpoint = unittest.mock.patch(
"pyeudiw.tools.utils.get_http_url",
return_value=[ok_response]
)

cache_ttl: int = 60*60*24*365 # 1 year
httpc_p = {
"connection": {
"ssl": False,
},
"session": {
"timeout": 1
}
}

_lru_cached_get_http_url.cache_clear() # clear cache so that it is not polluted from prev tests
mocked_endpoint.start()
for _ in range(tries):
resp = cacheable_get_http_url(cache_ttl, "http://location.example", httpc_p, http_async=False)
assert resp.status_code == 200
assert resp._content == b'Hello automated test'
mocked_endpoint.stop()

cache_misses = _lru_cached_get_http_url.cache_info().misses
exp_cache_misses = 1
cache_hits = _lru_cached_get_http_url.cache_info().hits
exp_cache_hits = tries - 1
assert cache_misses == exp_cache_misses, f"cache missed more that {exp_cache_misses} time: {cache_misses}; {_lru_cached_get_http_url.cache_info()}"
assert cache_hits == exp_cache_hits, f"cache hit less than {exp_cache_hits} times: {cache_hits}"
51 changes: 27 additions & 24 deletions pyeudiw/tests/trust/default/test_direct_trust.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import requests

from pyeudiw.tools.utils import _lru_cached_get_http_url
from pyeudiw.trust.default import DEFAULT_DIRECT_TRUST_SD_JWC_VC_PARAMS
from pyeudiw.trust.default.direct_trust_sd_jwt_vc import DirectTrustSdJwtVc, InvalidJwkMetadataException

Expand Down Expand Up @@ -118,27 +119,29 @@ def test_direct_trust_jwk():
assert expected_jwk == obtained_jwks[0]


# from pyeudiw.tools.utils import _lru_cached_get_http_url
# def test_direct_trust_cache():
# # DEV NOTE: for some reson, this test fails in the github action but works ok locally. This needs further investigation.
# cache_ttl = 60*60*24*365 # 1 year
# tries = 5
# trust_source = DirectTrustSdJwtVc(cache_ttl=cache_ttl, **DEFAULT_DIRECT_TRUST_SD_JWC_VC_PARAMS)

# mocked_issuer_jwt_vc_issuer_endpoint = unittest.mock.patch(
# "pyeudiw.tools.utils.get_http_url",
# return_value=[jwt_vc_issuer_endpoint_response]
# )
# mocked_issuer_jwt_vc_issuer_endpoint.start()
# for _ in range(tries):
# obtained_jwks = trust_source.get_public_keys(issuer)
# assert len(obtained_jwks) == 1, f"expected 1 jwk, obtained {len(obtained_jwks)}"
# assert expected_jwk == obtained_jwks[0]
# mocked_issuer_jwt_vc_issuer_endpoint.stop()

# cache_misses = _lru_cached_get_http_url.cache_info().misses
# exp_cache_misses = 1
# cache_hits = _lru_cached_get_http_url.cache_info().hits
# exp_cache_hits = tries - 1
# assert cache_misses == exp_cache_misses, f"cache missed more that {exp_cache_misses} time: {cache_misses}"
# assert cache_hits == exp_cache_hits, f"cache hit less than {exp_cache_hits} times: {cache_hits}"
def test_direct_trust_cache():
# DEV NOTE: for some reson, this test fails in the github action but works ok locally. This needs further investigation.
cache_ttl = 60*60*24*365 # 1 year
tries = 5
trust_source = DirectTrustSdJwtVc(cache_ttl=cache_ttl, **DEFAULT_DIRECT_TRUST_SD_JWC_VC_PARAMS)

mocked_issuer_jwt_vc_issuer_endpoint = unittest.mock.patch(
"pyeudiw.tools.utils.get_http_url",
return_value=[jwt_vc_issuer_endpoint_response]
)
mocked_issuer_jwt_vc_issuer_endpoint.start()

_lru_cached_get_http_url.cache_clear() # clear cache so that it is not polluted from prev tests
for _ in range(tries):
obtained_jwks = trust_source.get_public_keys(issuer)
assert len(obtained_jwks) == 1, f"expected 1 jwk, obtained {len(obtained_jwks)}"
assert expected_jwk == obtained_jwks[0]
mocked_issuer_jwt_vc_issuer_endpoint.stop()

cache_misses = _lru_cached_get_http_url.cache_info().misses
exp_cache_misses = 1
cache_hits = _lru_cached_get_http_url.cache_info().hits
exp_cache_hits = tries - 1

assert cache_misses == exp_cache_misses, f"cache missed more that {exp_cache_misses} time: {cache_misses}; {_lru_cached_get_http_url.cache_info()}"
assert cache_hits == exp_cache_hits, f"cache hit less than {exp_cache_hits} times: {cache_hits}"
2 changes: 1 addition & 1 deletion pyeudiw/tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def cacheable_get_http_url(cache_ttl: int, url: str, httpc_params: dict, http_as
ssl: bool | None = httpc_params.get("connection", {}).get("ssl", None)
timeout: int | None = httpc_params.get("session", {}).get("timeout", None)
if (ssl is None) or (timeout is None):
raise ValueError(f"invalid parameter {httpc_params=}: ['connection']['ssl'] and ['session'].['timeout'] MUST be defined")
raise ValueError(f"invalid parameter {httpc_params=}: ['connection']['ssl'] and ['session']['timeout'] MUST be defined")
curr_time_s = time.time_ns() // 1_000_000_000
if cache_ttl != 0:
ttl_timestamp = curr_time_s // cache_ttl
Expand Down

0 comments on commit 097777d

Please sign in to comment.