-
Notifications
You must be signed in to change notification settings - Fork 467
fix: Flagsmith Environment Webhook incorrect payload values #6646
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
cb98140
reproduce the issue
khvn26 7787f0d
fix the issue + straighten the dispatching logic
khvn26 d6372f1
improve signal semantics
khvn26 24c8124
fix coverage
khvn26 2fac64a
simplify deleted feature states exclusion logic
khvn26 6357747
improve naming consistency
khvn26 f36996c
cleanup
khvn26 6fab906
improve mocking
khvn26 1986736
less hacks!
khvn26 a1d423f
simplify fixture
khvn26 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,7 @@ | ||
| import logging | ||
|
|
||
| from django.db.models.signals import post_save | ||
| from django.dispatch import Signal, receiver | ||
|
|
||
| # noinspection PyUnresolvedReferences | ||
| from .models import FeatureState | ||
| from .tasks import trigger_feature_state_change_webhooks | ||
| from django.dispatch import Signal | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| feature_state_change_went_live = Signal() | ||
|
|
||
|
|
||
| @receiver(post_save, sender=FeatureState) | ||
| def trigger_feature_state_change_webhooks_signal(instance, **kwargs): # type: ignore[no-untyped-def] | ||
| if instance.environment_feature_version_id or instance.deleted_at: | ||
| return | ||
| trigger_feature_state_change_webhooks(instance) |
89 changes: 89 additions & 0 deletions
89
api/tests/integration/features/featurestate/test_webhooks.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import json | ||
|
|
||
| from django.urls import reverse | ||
| from pytest_mock import MockerFixture | ||
| from rest_framework import status | ||
| from rest_framework.test import APIClient | ||
|
|
||
|
|
||
| def test_update_segment_override__webhook_payload_has_correct_previous_and_new_values( | ||
| admin_client: APIClient, | ||
| environment: int, | ||
| feature: int, | ||
| segment: int, | ||
| mocker: MockerFixture, | ||
| ) -> None: | ||
| """ | ||
| Test for issue #6050: Webhook payload shows incorrect previous_state values | ||
| when updating a segment override value via the API. | ||
|
|
||
| The bug occurs because: | ||
| 1. The webhook signal fires on FeatureState.post_save | ||
| 2. drf-writable-nested saves the parent (FeatureState) before nested (FeatureStateValue) | ||
| 3. So the webhook captures stale values | ||
| """ | ||
| # Given | ||
| old_value = 0 | ||
| new_value = 1 | ||
|
|
||
| # First create a feature_segment | ||
| feature_segment_url = reverse("api-v1:features:feature-segment-list") | ||
emyller marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| feature_segment_data = { | ||
| "feature": feature, | ||
| "segment": segment, | ||
| "environment": environment, | ||
| } | ||
| feature_segment_response = admin_client.post( | ||
| feature_segment_url, | ||
| data=json.dumps(feature_segment_data), | ||
| content_type="application/json", | ||
| ) | ||
| assert feature_segment_response.status_code == status.HTTP_201_CREATED | ||
| feature_segment_id = feature_segment_response.json()["id"] | ||
|
|
||
| # Create segment override with initial value via API | ||
| create_url = reverse("api-v1:features:featurestates-list") | ||
| create_data = { | ||
| "enabled": False, | ||
| "feature_state_value": {"type": "int", "integer_value": old_value}, | ||
| "environment": environment, | ||
| "feature": feature, | ||
| "feature_segment": feature_segment_id, | ||
| } | ||
| create_response = admin_client.post( | ||
| create_url, data=json.dumps(create_data), content_type="application/json" | ||
| ) | ||
| assert create_response.status_code == status.HTTP_201_CREATED | ||
| segment_override_id = create_response.json()["id"] | ||
|
|
||
| # Mock call_environment_webhooks to capture the actual payload | ||
| mock_call_environment_webhooks = mocker.patch( | ||
| "features.tasks.call_environment_webhooks" | ||
| ) | ||
| mocker.patch("features.tasks.call_organisation_webhooks") | ||
|
|
||
| # When - update the segment override via API | ||
| url = reverse("api-v1:features:featurestates-detail", args=[segment_override_id]) | ||
| data = { | ||
| "enabled": False, | ||
| "feature_state_value": {"type": "int", "integer_value": new_value}, | ||
| "environment": environment, | ||
| "feature": feature, | ||
| "feature_segment": feature_segment_id, | ||
| } | ||
| response = admin_client.put( | ||
| url, data=json.dumps(data), content_type="application/json" | ||
| ) | ||
|
|
||
| # Then | ||
| assert response.status_code == status.HTTP_200_OK | ||
|
|
||
| # Verify webhook was called | ||
| mock_call_environment_webhooks.delay.assert_called_once() | ||
| webhook_args = mock_call_environment_webhooks.delay.call_args.kwargs["args"] | ||
| webhook_payload = webhook_args[1] # (environment_id, data, event_type) | ||
|
|
||
| # Verify the payload has correct values | ||
| assert webhook_payload["new_state"]["feature_segment"] is not None | ||
| assert webhook_payload["new_state"]["feature_state_value"] == new_value | ||
| assert webhook_payload["previous_state"]["feature_state_value"] == old_value | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.