2020from core .constants import STRING
2121from core .request_origin import RequestOrigin
2222from environments .identities .models import Identity
23+ from environments .enums import EnvironmentDocumentCacheMode
24+ from flagsmith_schemas .api import V1EnvironmentDocumentResponse
2325from environments .metrics import CACHE_HIT , CACHE_MISS
2426from environments .models import (
2527 Environment ,
@@ -1267,7 +1269,7 @@ def test_environment_clone_from_non_versioned_environment_with_use_v2_feature_ve
12671269 # When
12681270 new_environment = environment .clone (name = "new-environment" )
12691271
1270- # Then
1272+ # Then
12711273 assert new_environment .use_v2_feature_versioning
12721274
12731275 # we only expect a single environment feature version as we are essentially
@@ -1280,3 +1282,34 @@ def test_environment_clone_from_non_versioned_environment_with_use_v2_feature_ve
12801282 latest_feature_states = get_environment_flags_queryset (new_environment )
12811283 assert latest_feature_states .count () == 2
12821284 assert {fs .environment_feature_version for fs in latest_feature_states } == {efv }
1285+
1286+ @mock .patch ("environments.models.environment_document_cache" )
1287+ def test_write_environment_documents_strips_internal_fields_from_cache (
1288+ mock_document_cache : MagicMock ,
1289+ environment : Environment ,
1290+ settings : typing .Any
1291+ ) -> None :
1292+ # 1. SETUP: Force the persistent caching mode
1293+ settings .CACHE_ENVIRONMENT_DOCUMENT_MODE = EnvironmentDocumentCacheMode .PERSISTENT
1294+
1295+ # 2. ACTION: Trigger the document builder
1296+ Environment .write_environment_documents (environment_id = environment .id )
1297+
1298+ # 3. INTERCEPT: Grab the exact dictionary the code tried to save to Redis
1299+ # call_args[0][0] gets the dictionary passed into set_many()
1300+ cache_payload = mock_document_cache .set_many .call_args [0 ][0 ]
1301+ cached_document = cache_payload [environment .api_key ]
1302+
1303+ # 4. ASSERT: The internal/dangerous fields MUST NOT be in the payload
1304+ assert "compress_dynamo_documents" not in cached_document
1305+ assert "use_v2_feature_versioning" not in cached_document
1306+
1307+ # 5. SCHEMA VALIDATION: Prove the dictionary perfectly matches the TypedDict
1308+ # required by the API. If this raises an error, the fix failed.
1309+ clean_doc : V1EnvironmentDocumentResponse = {
1310+ "api_key" : cached_document ["api_key" ],
1311+ "feature_states" : cached_document ["feature_states" ],
1312+ "identity_overrides" : cached_document ["identity_overrides" ],
1313+ "name" : cached_document ["name" ],
1314+ "project" : cached_document ["project" ],
1315+ }
0 commit comments