From edfb4389dd513fa61f44333a374061657fc97855 Mon Sep 17 00:00:00 2001 From: Kim Gustyr Date: Tue, 6 Aug 2024 23:20:22 +0100 Subject: [PATCH] return identifier --- api/environments/identities/serializers.py | 1 + api/environments/identities/views.py | 1 + api/environments/sdk/serializers.py | 2 +- .../identities/test_integration_identities.py | 35 +++++++++++++++---- 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/api/environments/identities/serializers.py b/api/environments/identities/serializers.py index bd4b1ffaed1f..6c0d9df9229e 100644 --- a/api/environments/identities/serializers.py +++ b/api/environments/identities/serializers.py @@ -59,6 +59,7 @@ class _TraitSerializer(serializers.Serializer): help_text="Can be of type string, boolean, float or integer." ) + identifier = serializers.CharField() flags = serializers.ListField(child=SDKFeatureStateSerializer()) traits = serializers.ListSerializer(child=_TraitSerializer()) diff --git a/api/environments/identities/views.py b/api/environments/identities/views.py index f403b8001dc1..bbf9e2ce566d 100644 --- a/api/environments/identities/views.py +++ b/api/environments/identities/views.py @@ -309,6 +309,7 @@ def _get_all_feature_states_for_user_response( serializer = serializer_class( { "flags": all_feature_states, + "identifier": identity.identifier, "traits": identity.identity_traits.all(), }, context=self.get_serializer_context(), diff --git a/api/environments/sdk/serializers.py b/api/environments/sdk/serializers.py index 7099f18b7486..e1c516163e75 100644 --- a/api/environments/sdk/serializers.py +++ b/api/environments/sdk/serializers.py @@ -131,7 +131,6 @@ class IdentifyWithTraitsSerializer( HideSensitiveFieldsSerializerMixin, serializers.Serializer ): identifier = serializers.CharField( - write_only=True, required=False, allow_blank=True, allow_null=True, @@ -185,6 +184,7 @@ def save(self, **kwargs): return { "identity": identity, + "identifier": identity.identifier, "traits": traits, "flags": all_feature_states, } diff --git a/api/tests/integration/environments/identities/test_integration_identities.py b/api/tests/integration/environments/identities/test_integration_identities.py index 845228ae7c87..c459ae0d8f5d 100644 --- a/api/tests/integration/environments/identities/test_integration_identities.py +++ b/api/tests/integration/environments/identities/test_integration_identities.py @@ -1,10 +1,11 @@ import json -from typing import Any +from typing import Any, Generator from unittest import mock import pytest from django.urls import reverse from pytest_lazyfixture import lazy_fixture +from pytest_mock import MockerFixture from rest_framework import status from rest_framework.test import APIClient @@ -234,6 +235,15 @@ def existing_identity_identifier_data( return {"identifier": identity_identifier} +@pytest.fixture +def transient_random_identifier( + mocker: MockerFixture, +) -> Generator[str, None, None]: + uuid_mock = mocker.patch("environments.sdk.services.uuid", autospec=True) + uuid_mock.uuid4.return_value = identifier = "1199c22c-4dcb-4505-9857-5db5f258469c" + yield identifier + + @pytest.mark.parametrize( "transient_data", [ @@ -243,16 +253,27 @@ def existing_identity_identifier_data( ], ) @pytest.mark.parametrize( - "identifier_data", + "identifier_data,expected_identifier", [ pytest.param( lazy_fixture("existing_identity_identifier_data"), + lazy_fixture("identity_identifier"), id="existing-identifier", ), - pytest.param({"identifier": "unseen"}, id="new-identifier"), - pytest.param({"identifier": ""}, id="blank-identifier"), - pytest.param({"identifier": None}, id="null-identifier"), - pytest.param({}, id="missing-identifier"), + pytest.param({"identifier": "unseen"}, "unseen", id="new-identifier"), + pytest.param( + {"identifier": ""}, + lazy_fixture("transient_random_identifier"), + id="blank-identifier", + ), + pytest.param( + {"identifier": None}, + lazy_fixture("transient_random_identifier"), + id="null-identifier", + ), + pytest.param( + {}, lazy_fixture("transient_random_identifier"), id="missing-identifier" + ), ], ) def test_get_feature_states_for_identity__segment_match_expected( @@ -264,6 +285,7 @@ def test_get_feature_states_for_identity__segment_match_expected( segment_featurestate: int, identifier_data: dict[str, Any], transient_data: dict[str, Any], + expected_identifier: str, ) -> None: # Given url = reverse("api-v1:sdk-identities") @@ -291,6 +313,7 @@ def test_get_feature_states_for_identity__segment_match_expected( # Then assert response.status_code == status.HTTP_200_OK response_json = response.json() + assert response_json["identifier"] == expected_identifier assert ( flag_data := next( (