Skip to content

Commit

Permalink
return identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
khvn26 committed Aug 6, 2024
1 parent a1a34d7 commit edfb438
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
1 change: 1 addition & 0 deletions api/environments/identities/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
1 change: 1 addition & 0 deletions api/environments/identities/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion api/environments/sdk/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ class IdentifyWithTraitsSerializer(
HideSensitiveFieldsSerializerMixin, serializers.Serializer
):
identifier = serializers.CharField(
write_only=True,
required=False,
allow_blank=True,
allow_null=True,
Expand Down Expand Up @@ -185,6 +184,7 @@ def save(self, **kwargs):

return {
"identity": identity,
"identifier": identity.identifier,
"traits": traits,
"flags": all_feature_states,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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",
[
Expand All @@ -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(
Expand All @@ -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")
Expand Down Expand Up @@ -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(
(
Expand Down

0 comments on commit edfb438

Please sign in to comment.