|
17 | 17 | from rest_framework.permissions import IsAuthenticated |
18 | 18 | from rest_framework.test import APIClient |
19 | 19 |
|
20 | | -from core.constants import FLAGSMITH_UPDATED_AT_HEADER, STRING |
| 20 | +from core.constants import ( |
| 21 | + FLAGSMITH_UPDATED_AT_HEADER, |
| 22 | + SDK_ENVIRONMENT_KEY_HEADER, |
| 23 | + STRING, |
| 24 | +) |
21 | 25 | from environments.identities.helpers import ( |
22 | 26 | get_hashed_percentage_for_object_ids, |
23 | 27 | ) |
@@ -338,6 +342,54 @@ def test_identities_endpoint_returns_all_feature_states_for_identity_if_feature_ |
338 | 342 | assert len(response.data["flags"]) == 2 |
339 | 343 |
|
340 | 344 |
|
| 345 | +def test_get_flags_for_identities_with_cache( |
| 346 | + environment: Environment, |
| 347 | + feature: Feature, |
| 348 | + django_assert_num_queries: DjangoAssertNumQueries, |
| 349 | + use_local_mem_cache_for_cache_middleware: None, |
| 350 | + project_two_feature: Feature, |
| 351 | + project_two_environment: Environment, |
| 352 | +) -> None: |
| 353 | + # Given |
| 354 | + base_url = reverse("api-v1:sdk-identities") |
| 355 | + url = base_url + "?identifier=some-identifier" |
| 356 | + |
| 357 | + # Create clients for two separate environments |
| 358 | + environment_one_client = APIClient( |
| 359 | + headers={SDK_ENVIRONMENT_KEY_HEADER: environment.api_key} |
| 360 | + ) |
| 361 | + project_two_environment_client = APIClient( |
| 362 | + headers={SDK_ENVIRONMENT_KEY_HEADER: project_two_environment.api_key} |
| 363 | + ) |
| 364 | + |
| 365 | + # Fetch flags for both environments once to warm the cache |
| 366 | + environment_one_response = environment_one_client.get(url) |
| 367 | + assert environment_one_response.status_code == status.HTTP_200_OK |
| 368 | + |
| 369 | + project_two_environment_response = project_two_environment_client.get(url) |
| 370 | + assert project_two_environment_response.status_code == status.HTTP_200_OK |
| 371 | + |
| 372 | + # When |
| 373 | + with django_assert_num_queries(0): |
| 374 | + for _ in range(10): |
| 375 | + environment_one_response = environment_one_client.get(url) |
| 376 | + assert environment_one_response.status_code == status.HTTP_200_OK |
| 377 | + |
| 378 | + project_two_environment_response = project_two_environment_client.get(url) |
| 379 | + assert project_two_environment_response.status_code == status.HTTP_200_OK |
| 380 | + |
| 381 | + # Then |
| 382 | + # Each response must return the correct feature for its environment |
| 383 | + assert ( |
| 384 | + environment_one_response.json()["flags"][0]["feature"]["id"] |
| 385 | + == feature.id |
| 386 | + ) |
| 387 | + assert ( |
| 388 | + project_two_environment_response.json()["flags"][0]["feature"]["id"] |
| 389 | + == project_two_feature.id |
| 390 | + ) |
| 391 | + |
| 392 | + |
341 | 393 | @mock.patch("integrations.amplitude.amplitude.AmplitudeWrapper.identify_user_async") |
342 | 394 | def test_identities_endpoint_get_all_feature_amplitude_called( |
343 | 395 | mock_amplitude_wrapper: mock.MagicMock, |
|
0 commit comments