Skip to content

Commit

Permalink
Merge pull request #354 from jsmolar/jsmolar-fix-branch
Browse files Browse the repository at this point in the history
Fixed caching tests
  • Loading branch information
pehala authored Mar 4, 2024
2 parents ab6e405 + 2ce36fe commit 79c6f65
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
"""Tests metadata caching"""

from time import sleep

import pytest

from testsuite.policy.authorization import ValueFrom, Cache
from testsuite.utils import extract_response


pytestmark = [pytest.mark.authorino]


@pytest.fixture(scope="module")
def cache_ttl():
"""Returns TTL in seconds for Cached Metadata"""
return 5


@pytest.fixture(scope="module")
def authorization(authorization, module_label, expectation_path, cache_ttl):
def authorization(authorization, module_label, expectation_path):
"""Adds Cached Metadata to the AuthConfig"""
meta_cache = Cache(cache_ttl, ValueFrom("context.request.http.path"))
meta_cache = Cache(5, ValueFrom("context.request.http.path"))
authorization.metadata.add_http(module_label, expectation_path, "GET", cache=meta_cache)
return authorization

Expand All @@ -40,17 +31,3 @@ def test_cached(client, auth, module_label, mockserver):
assert data is not None
assert data == cached_data
assert len(mockserver.retrieve_requests(module_label)) == 1


def test_cached_ttl(client, auth, module_label, cache_ttl, mockserver):
"""Tests that cached value expires after ttl"""
response = client.get("/get", auth=auth)
data = extract_response(response)[module_label]["uuid"] % None
sleep(cache_ttl)
response = client.get("/get", auth=auth)
cached_data = extract_response(response)[module_label]["uuid"] % None

assert cached_data is not None
assert data is not None
assert data != cached_data
assert len(mockserver.retrieve_requests(module_label)) == 2
38 changes: 38 additions & 0 deletions testsuite/tests/kuadrant/authorino/caching/metadata/test_ttl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""Tests TTL for metadata caching"""

from time import sleep

import pytest

from testsuite.policy.authorization import ValueFrom, Cache
from testsuite.utils import extract_response

pytestmark = [pytest.mark.authorino]


@pytest.fixture(scope="module")
def cache_ttl():
"""Returns TTL in seconds for Cached Metadata"""
return 5


@pytest.fixture(scope="module")
def authorization(authorization, module_label, expectation_path, cache_ttl):
"""Adds Cached Metadata to the AuthConfig"""
meta_cache = Cache(cache_ttl, ValueFrom("context.request.http.path"))
authorization.metadata.add_http(module_label, expectation_path, "GET", cache=meta_cache)
return authorization


def test_cached_ttl(client, auth, module_label, cache_ttl, mockserver):
"""Tests that cached value expires after ttl"""
response = client.get("/get", auth=auth)
data = extract_response(response)[module_label]["uuid"] % None
sleep(cache_ttl)
response = client.get("/get", auth=auth)
cached_data = extract_response(response)[module_label]["uuid"] % None

assert cached_data is not None
assert data is not None
assert data != cached_data
assert len(mockserver.retrieve_requests(module_label)) == 2

0 comments on commit 79c6f65

Please sign in to comment.