"""Integration tests for POST /cache/bust/.""" from typing import Any, Collection, Dict import pytest import requests from pdp.connectors.redis_client import RedisConnector from pdp.constants.constants import CACHE_ENTRY_CERBOS_POLICY_METADATA from tests.integration import config from tests.integration.utils import get_bearer_token_identity_uuid # check_authorization_infra reads this key (seeded by # autouse_seed_policy_metadata_cache), so it's always present alongside the # test's own cache entries. CERBOS_POLICY_METADATA_KEY = CACHE_ENTRY_CERBOS_POLICY_METADATA.encode() @pytest.mark.parametrize( "body, expected_status_code, expected_response, expected_remaining_keys", [ pytest.param( {"keys": ["tenant_hierarchy_45125167-d387-4e84-a899-f11543f22dfb"]}, 200, { "items": [ { "key": "tenant_hierarchy_45125167-d387-4e84-a899-f11543f22dfb", "value": "a string value", "deleted": True, } ] }, { b"key1", b"tenant_hierarchy_fff741c2-6def-4493-bfdf-c2bcb1128e02", CERBOS_POLICY_METADATA_KEY, }, id="cache_bust will delete and return cache items of any type", ), pytest.param( { "keys": [ "tenant_hierarchy_fff741c2-6def-4493-bfdf-c2bcb1128e02", "tenant_hierarchy_45125167-d387-4e84-a899-f11543f22dfb", ] }, 200, { "items": [ { "key": "tenant_hierarchy_fff741c2-6def-4493-bfdf-c2bcb1128e02", "value": '{"key": "value"}', "deleted": True, }, { "key": "tenant_hierarchy_45125167-d387-4e84-a899-f11543f22dfb", "value": "a string value", "deleted": True, }, ] }, {b"key1", CERBOS_POLICY_METADATA_KEY}, id="cache_bust will delete and return multiple cache items", ), pytest.param( {"keys": ["nonexistent_key"]}, 200, {"items": [{"key": "nonexistent_key", "value": {}, "deleted": False}]}, { b"key1", b"tenant_hierarchy_fff741c2-6def-4493-bfdf-c2bcb1128e02", b"tenant_hierarchy_45125167-d387-4e84-a899-f11543f22dfb", CERBOS_POLICY_METADATA_KEY, }, id="cache_bust response will have deleted == False for non-existent keys", ), ], ) async def test_cache_bust( bearer_token_pdptest_user: str, local_redis_connector: RedisConnector, body: Dict[str, Any], expected_status_code: int, expected_response: Dict[str, Any], expected_remaining_keys: Collection[bytes], ) -> None: """POST /cache/bust/ endpoint.""" url = f"{config.QA_BASE_URL}/cache/bust/" headers = {"Authorization": f"Bearer {bearer_token_pdptest_user}"} # Set up the test data test_key_values = { "tenant_hierarchy_fff741c2-6def-4493-bfdf-c2bcb1128e02": '{"key": "value"}', "tenant_hierarchy_45125167-d387-4e84-a899-f11543f22dfb": "a string value", "key1": "value1", } await local_redis_connector.client.mset(test_key_values) response = requests.post(url, json=body, headers=headers) assert response.status_code == expected_status_code, f"Response: {response.text}" assert response.json() == expected_response, response.text remaining_keys = await local_redis_connector.client.keys("*") assert set(remaining_keys) == expected_remaining_keys async def test_cache_bust_unauthorized( bearer_token_pdptest_rap_admin_user: str, ) -> None: """POST /cache/bust/ endpoint with unauthorized user token.""" url = f"{config.QA_BASE_URL}/cache/bust/" principal_uuid = get_bearer_token_identity_uuid(bearer_token_pdptest_rap_admin_user) headers = {"Authorization": f"Bearer {bearer_token_pdptest_rap_admin_user}"} body = {"keys": ["tenant_hierarchy_45125167-d387-4e84-a899-f11543f22dfb"]} response = requests.post(url, json=body, headers=headers) assert response.status_code == 403, f"Response: {response.text}" assert response.json() == { "code": "bad_request", "message": f"Principal {principal_uuid} not authorized to bust_cache.", }