Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion api/features/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,17 @@ def validate_feature_segment(
return feature_segment

def validate(self, attrs): # type: ignore[no-untyped-def]
environment = attrs.get("environment") or self.context["environment"]
# A partial update may omit the environment, and not every view
# provides one in the context, so fall back to the feature state's own.
environment = (
attrs.get("environment")
or self.context.get("environment")
or getattr(self.instance, "environment", None)
)
if environment is None:
raise serializers.ValidationError(
{"environment": "This field is required."}
)
identity = attrs.get("identity")
feature_segment = attrs.get("feature_segment")
identifier = attrs.pop("identifier", None)
Expand Down
16 changes: 16 additions & 0 deletions api/tests/unit/features/test_unit_features_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,19 @@ def test_feature_state_serializer_basic__save__dispatches_gitlab_state_change(

# Then
mock_dispatch.assert_called_once_with(feature_state)


def test_feature_state_serializer_basic__no_environment_anywhere__returns_validation_error(
feature: Feature,
) -> None:
# Given
serializer = FeatureStateSerializerBasic(
data={"feature": feature.id, "enabled": True}, context={}
)

# When
is_valid = serializer.is_valid()

# Then
assert is_valid is False
assert serializer.errors == {"environment": ["This field is required."]}
23 changes: 23 additions & 0 deletions api/tests/unit/features/test_unit_features_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3968,6 +3968,29 @@ def test_update_segment_override__using_simple_feature_state_viewset__allows_man
).exists()


def test_partial_update_segment_override__without_environment__updates_feature_state(
staff_client: APIClient,
with_environment_permissions: WithEnvironmentPermissionsCallable,
segment_featurestate: FeatureState,
) -> None:
# Given
with_environment_permissions([MANAGE_SEGMENT_OVERRIDES]) # type: ignore[call-arg]
url = reverse(
"api-v1:features:featurestates-detail", args=[segment_featurestate.id]
)
enabled = not segment_featurestate.enabled

# When
response = staff_client.patch(
url, data=json.dumps({"enabled": enabled}), content_type="application/json"
)

# Then
assert response.status_code == status.HTTP_200_OK
segment_featurestate.refresh_from_db()
assert segment_featurestate.enabled is enabled


def test_update_segment_override__using_simple_feature_state_viewset__denies_update_feature_state(
staff_client: APIClient,
with_environment_permissions: WithEnvironmentPermissionsCallable,
Expand Down
Loading