22import urllib
33from typing import Any
44from unittest import mock
5+ from urllib .parse import quote
56
67import pytest
78from common .environments .permissions import (
@@ -1431,7 +1432,7 @@ def test_SDKIdentitiesDeprecated__given_identifier__retrieves_identity(
14311432 pytest .param (True , False , True , 4 , id = "replica_db,old_identity,transient" ),
14321433 ],
14331434)
1434- def test_SDKIdentities_retrieves_identity_feature_states (
1435+ def test_SDKIdentities__retrieves_identity_feature_states (
14351436 api_client : APIClient ,
14361437 django_assert_num_queries : DjangoAssertNumQueries ,
14371438 environment : Environment ,
@@ -1458,3 +1459,105 @@ def test_SDKIdentities_retrieves_identity_feature_states(
14581459
14591460 # Then
14601461 assert response .status_code == status .HTTP_200_OK
1462+
1463+
1464+ @pytest .mark .parametrize (
1465+ ["transient_value" , "expected_status" ],
1466+ [
1467+ ("true" , status .HTTP_200_OK ),
1468+ ("false" , status .HTTP_200_OK ),
1469+ ("1" , status .HTTP_200_OK ),
1470+ ("0" , status .HTTP_200_OK ),
1471+ ("yes" , status .HTTP_200_OK ),
1472+ ("no" , status .HTTP_200_OK ),
1473+ ("foo" , status .HTTP_400_BAD_REQUEST ),
1474+ ("123" , status .HTTP_400_BAD_REQUEST ),
1475+ ],
1476+ )
1477+ def test_SDKIdentities__given_transient_value__responds_accordingly (
1478+ api_client : APIClient ,
1479+ environment : Environment ,
1480+ transient_value : str ,
1481+ expected_status : int ,
1482+ ) -> None :
1483+ # Given
1484+ api_client .credentials (HTTP_X_ENVIRONMENT_KEY = environment .api_key )
1485+
1486+ # When
1487+ response = api_client .get (
1488+ f"/api/v1/identities/?identifier=jamesbond&transient={ transient_value } "
1489+ )
1490+
1491+ # Then
1492+ assert response .status_code == expected_status
1493+
1494+
1495+ @pytest .mark .valid_identity_identifiers
1496+ def test_SDKIdentities__identifier_sanitization__accepts_valid_identifiers (
1497+ api_client : APIClient ,
1498+ environment : Environment ,
1499+ identifier : str ,
1500+ ) -> None :
1501+ # Given
1502+ api_client .credentials (HTTP_X_ENVIRONMENT_KEY = environment .api_key )
1503+
1504+ # When
1505+ response = api_client .get (f"/api/v1/identities/?identifier={ quote (identifier )} " )
1506+
1507+ # Then
1508+ assert response .status_code == status .HTTP_200_OK
1509+ assert response .json ()["identifier" ] == identifier
1510+
1511+
1512+ @pytest .mark .invalid_identity_identifiers
1513+ def test_SDKIdentities__identifier_sanitization__rejects_invalid_identifiers (
1514+ api_client : APIClient ,
1515+ environment : Environment ,
1516+ identifier : str ,
1517+ identifier_error_message : str ,
1518+ ) -> None :
1519+ # Given
1520+ api_client .credentials (HTTP_X_ENVIRONMENT_KEY = environment .api_key )
1521+
1522+ # When
1523+ response = api_client .get (f"/api/v1/identities/?identifier={ quote (identifier )} " )
1524+
1525+ # Then
1526+ assert response .status_code == status .HTTP_400_BAD_REQUEST
1527+ assert response .json () == {"identifier" : [identifier_error_message ]}
1528+
1529+
1530+ @pytest .mark .valid_identity_identifiers
1531+ def test_IdentityViewSet_create__accepts_valid_identifiers (
1532+ admin_client : APIClient ,
1533+ environment : Environment ,
1534+ identifier : str ,
1535+ ) -> None :
1536+ # When
1537+ response = admin_client .post (
1538+ f"/api/v1/environments/{ environment .api_key } /identities/" ,
1539+ data = {"identifier" : identifier },
1540+ )
1541+
1542+ # Then
1543+ assert response .status_code == status .HTTP_201_CREATED
1544+ assert response .json ()["identifier" ] == identifier
1545+ assert Identity .objects .filter (identifier = identifier ).exists ()
1546+
1547+
1548+ @pytest .mark .invalid_identity_identifiers
1549+ def test_IdentityViewSet_create__rejects_invalid_identifiers (
1550+ admin_client : APIClient ,
1551+ environment : Environment ,
1552+ identifier : str ,
1553+ identifier_error_message : str ,
1554+ ) -> None :
1555+ # When
1556+ response = admin_client .post (
1557+ f"/api/v1/environments/{ environment .api_key } /identities/" ,
1558+ data = {"identifier" : identifier },
1559+ )
1560+
1561+ # Then
1562+ assert response .status_code == status .HTTP_400_BAD_REQUEST
1563+ assert response .json () == {"identifier" : [identifier_error_message ]}
0 commit comments