Skip to content

Commit

Permalink
fall back to uuid for empty identifiers with no traits
Browse files Browse the repository at this point in the history
  • Loading branch information
khvn26 committed Aug 8, 2024
1 parent 2f3af09 commit 21cc652
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
17 changes: 10 additions & 7 deletions api/environments/sdk/services.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import hashlib
import uuid
from itertools import chain
from operator import itemgetter
from typing import TypeAlias
Expand Down Expand Up @@ -91,13 +92,15 @@ def get_persisted_identity_and_traits(


def get_transient_identifier(sdk_trait_data: list[SDKTraitData]) -> str:
return hashlib.sha256(
"".join(
f'{trait["trait_key"]}{trait["trait_value"]["value"]}'
for trait in sorted(sdk_trait_data, key=itemgetter("trait_key"))
).encode(),
usedforsecurity=False,
).hexdigest()
if sdk_trait_data:
return hashlib.sha256(
"".join(
f'{trait["trait_key"]}{trait["trait_value"]["value"]}'
for trait in sorted(sdk_trait_data, key=itemgetter("trait_key"))
).encode(),
usedforsecurity=False,
).hexdigest()
return uuid.uuid4().hex


def _get_transient_identity(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,29 @@ def test_get_feature_states_for_identity__segment_match_expected(
assert flag_data["feature_state_value"] == "segment override"


def test_get_feature_states_for_identity__empty_traits__random_identifier_expected(
sdk_client: APIClient,
environment: int,
) -> None:
# Given
url = reverse("api-v1:sdk-identities")

# When
response_1 = sdk_client.post(
url,
data=json.dumps({"traits": []}),
content_type="application/json",
)
response_2 = sdk_client.post(
url,
data=json.dumps({"traits": []}),
content_type="application/json",
)

# Then
assert response_1.json()["identifier"] != response_2.json()["identifier"]


def test_get_feature_states_for_identity__transient_trait__segment_match_expected(
sdk_client: APIClient,
feature: int,
Expand Down

0 comments on commit 21cc652

Please sign in to comment.