"""Integration tests for POST /cache/list/.""" from typing import Any, Dict import pytest import requests from pdp.connectors.redis_client import RedisConnector from tests.integration import config from tests.integration.utils import ( get_bearer_token_identity_uuid, ) @pytest.mark.parametrize( "body, expected_status_code, expected_response", [ pytest.param( {"keys": ["tenant_hierarchy_45125167-d387-4e84-a899-f11543f22dfb"]}, 200, { "items": [ { "key": "tenant_hierarchy_45125167-d387-4e84-a899-f11543f22dfb", "value": "a string value", } ] }, id="cache_list will 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"}', }, { "key": "tenant_hierarchy_45125167-d387-4e84-a899-f11543f22dfb", "value": "a string value", }, ] }, id="cache_list will return multiple cache items", ), pytest.param( {"keys": ["nonexistent_key"]}, 200, {"items": [{"key": "nonexistent_key", "value": {}}]}, id="cache_list will return an empty dict for non-existent keys", ), ], ) async def test_cache_list( bearer_token_pdptest_user: str, local_redis_connector: RedisConnector, body: Dict[Any, Any], expected_status_code: int, expected_response: Dict[Any, Any], ) -> None: """POST /cache/list/ endpoint.""" url = f"{config.QA_BASE_URL}/cache/list/" 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", } 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 async def test_cache_list_unauthorized( bearer_token_pdptest_rap_admin_user: str, ) -> None: """POST /cache/list/ endpoint with unauthorized user token.""" url = f"{config.QA_BASE_URL}/cache/list/" 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 list_cache.", }