|
1 | 1 | import json |
2 | 2 | from unittest import mock |
3 | 3 |
|
| 4 | +import pytest |
4 | 5 | from common.environments.permissions import ( |
5 | 6 | MANAGE_IDENTITIES, |
6 | 7 | VIEW_IDENTITIES, |
@@ -1052,3 +1053,123 @@ def test_edge_identity_view_set_get_permissions(): # type: ignore[no-untyped-de |
1052 | 1053 | "partial_update": MANAGE_IDENTITIES, |
1053 | 1054 | "destroy": MANAGE_IDENTITIES, |
1054 | 1055 | } |
| 1056 | + |
| 1057 | + |
| 1058 | +@pytest.mark.valid_identity_identifiers |
| 1059 | +def test_SDKTraits_create__identifier_sanitization__accepts_valid_identifiers( |
| 1060 | + api_client: APIClient, |
| 1061 | + environment: Environment, |
| 1062 | + identifier: str, |
| 1063 | +) -> None: |
| 1064 | + # Given |
| 1065 | + api_client.credentials(HTTP_X_ENVIRONMENT_KEY=environment.api_key) |
| 1066 | + |
| 1067 | + # When |
| 1068 | + response = api_client.post( |
| 1069 | + "/api/v1/traits/", |
| 1070 | + data={ |
| 1071 | + "identity": {"identifier": identifier}, |
| 1072 | + "trait_key": "color", |
| 1073 | + "trait_value": "green", |
| 1074 | + }, |
| 1075 | + format="json", |
| 1076 | + ) |
| 1077 | + |
| 1078 | + # Then |
| 1079 | + assert response.status_code == status.HTTP_200_OK |
| 1080 | + assert response.json() == { |
| 1081 | + "identity": {"identifier": identifier}, |
| 1082 | + "trait_key": "color", |
| 1083 | + "trait_value": "green", |
| 1084 | + } |
| 1085 | + |
| 1086 | + |
| 1087 | +@pytest.mark.invalid_identity_identifiers |
| 1088 | +def test_SDKTraits_create__identifier_sanitization__rejects_invalid_identifiers( |
| 1089 | + api_client: APIClient, |
| 1090 | + environment: Environment, |
| 1091 | + identifier: str, |
| 1092 | + identifier_error_message: str, |
| 1093 | +) -> None: |
| 1094 | + # Given |
| 1095 | + api_client.credentials(HTTP_X_ENVIRONMENT_KEY=environment.api_key) |
| 1096 | + |
| 1097 | + # When |
| 1098 | + response = api_client.post( |
| 1099 | + "/api/v1/traits/", |
| 1100 | + data={ |
| 1101 | + "identity": {"identifier": identifier}, |
| 1102 | + "trait_key": "color", |
| 1103 | + "trait_value": "green", |
| 1104 | + }, |
| 1105 | + format="json", |
| 1106 | + ) |
| 1107 | + |
| 1108 | + # Then |
| 1109 | + assert response.status_code == status.HTTP_400_BAD_REQUEST |
| 1110 | + assert response.json() == { |
| 1111 | + "identity": {"identifier": [identifier_error_message]}, |
| 1112 | + } |
| 1113 | + |
| 1114 | + |
| 1115 | +@pytest.mark.valid_identity_identifiers |
| 1116 | +def test_SDKTraits_bulk_create__identifier_sanitization__accepts_valid_identifiers( |
| 1117 | + api_client: APIClient, |
| 1118 | + environment: Environment, |
| 1119 | + identifier: str, |
| 1120 | +) -> None: |
| 1121 | + # Given |
| 1122 | + api_client.credentials(HTTP_X_ENVIRONMENT_KEY=environment.api_key) |
| 1123 | + |
| 1124 | + # When |
| 1125 | + response = api_client.put( |
| 1126 | + "/api/v1/traits/bulk/", |
| 1127 | + data=[ |
| 1128 | + { |
| 1129 | + "identity": {"identifier": identifier}, |
| 1130 | + "trait_key": "color", |
| 1131 | + "trait_value": "green", |
| 1132 | + } |
| 1133 | + ], |
| 1134 | + format="json", |
| 1135 | + ) |
| 1136 | + |
| 1137 | + # Then |
| 1138 | + assert response.status_code == status.HTTP_200_OK |
| 1139 | + assert response.json() == [ |
| 1140 | + { |
| 1141 | + "identity": {"identifier": identifier}, |
| 1142 | + "trait_key": "color", |
| 1143 | + "trait_value": "green", |
| 1144 | + } |
| 1145 | + ] |
| 1146 | + |
| 1147 | + |
| 1148 | +@pytest.mark.invalid_identity_identifiers |
| 1149 | +def test_SDKTraits_bulk_create__identifier_sanitization__rejects_invalid_identifiers( |
| 1150 | + api_client: APIClient, |
| 1151 | + environment: Environment, |
| 1152 | + identifier: str, |
| 1153 | + identifier_error_message: str, |
| 1154 | +) -> None: |
| 1155 | + # Given |
| 1156 | + api_client.credentials(HTTP_X_ENVIRONMENT_KEY=environment.api_key) |
| 1157 | + |
| 1158 | + # When |
| 1159 | + response = api_client.put( |
| 1160 | + "/api/v1/traits/bulk/", |
| 1161 | + data=[ |
| 1162 | + { |
| 1163 | + "identity": {"identifier": identifier}, |
| 1164 | + "trait_key": "color", |
| 1165 | + "trait_value": "green", |
| 1166 | + } |
| 1167 | + ], |
| 1168 | + format="json", |
| 1169 | + ) |
| 1170 | + |
| 1171 | + # Then |
| 1172 | + assert response.status_code == status.HTTP_400_BAD_REQUEST |
| 1173 | + assert response.json() == [ |
| 1174 | + {"identity": {"identifier": [identifier_error_message]}}, |
| 1175 | + ] |
0 commit comments