44import responses
55from django .urls import reverse
66from pytest_lazyfixture import lazy_fixture # type: ignore[import-untyped]
7+ from pytest_mock import MockerFixture
78from rest_framework import status
89from rest_framework .test import APIClient
910
@@ -133,7 +134,9 @@ def test_update_multivariate_percentage__webhook_payload_includes_multivariate_v
133134 environment : int ,
134135 feature : int ,
135136 mv_option_50_percent : int ,
137+ mv_option_value : str ,
136138 webhook : str ,
139+ mocker : MockerFixture ,
137140) -> None :
138141 """
139142 Test for issue #6190: Webhook payloads do not include multivariate values.
@@ -143,8 +146,6 @@ def test_update_multivariate_percentage__webhook_payload_includes_multivariate_v
143146 with their percentage allocations.
144147 """
145148 # Given
146- responses .add (responses .POST , webhook , status = 200 )
147-
148149 # Get the feature state for this environment
149150 feature_states_url = reverse ("api-v1:features:featurestates-list" )
150151 feature_states_response = admin_client .get (
@@ -160,7 +161,10 @@ def test_update_multivariate_percentage__webhook_payload_includes_multivariate_v
160161 old_percentage = mv_fs_value ["percentage_allocation" ]
161162 new_percentage = 75
162163
163- # When - update only the multivariate percentage allocation
164+ responses .add (responses .POST , webhook , status = 200 )
165+
166+ # When
167+ # update only the multivariate percentage allocation
164168 url = reverse ("api-v1:features:featurestates-detail" , args = [feature_state_id ])
165169 data = {
166170 "id" : feature_state_id ,
@@ -178,28 +182,39 @@ def test_update_multivariate_percentage__webhook_payload_includes_multivariate_v
178182 }
179183 ],
180184 }
181- response = admin_client .put (
182- url , data = json .dumps (data ), content_type = "application/json"
183- )
185+ admin_client .put (url , data = json .dumps (data ), content_type = "application/json" )
184186
185187 # Then
186- assert response .status_code == status .HTTP_200_OK
187-
188- # Verify webhook was called
189- assert len (responses .calls ) >= 1
190- webhook_payload = json .loads (responses .calls [0 ].request .body )["data" ] # type: ignore[union-attr]
191-
192- # Verify the payload includes multivariate values
193- # This currently fails - issue #6190
194- assert "multivariate_feature_state_values" in webhook_payload ["new_state" ]
195- assert "multivariate_feature_state_values" in webhook_payload ["previous_state" ]
196-
197- new_mv_values = webhook_payload ["new_state" ]["multivariate_feature_state_values" ]
198- previous_mv_values = webhook_payload ["previous_state" ][
199- "multivariate_feature_state_values"
188+ # `FLAG_UPDATED`` webhook was called
189+ # (should be the last sent event)
190+ last_call = responses .calls [- 1 ]
191+ assert not isinstance (last_call , list )
192+ webhook_payload = json .loads (last_call .request .body )
193+ assert webhook_payload ["event_type" ] == "FLAG_UPDATED"
194+
195+ # the payload includes multivariate values
196+ event_data = webhook_payload ["data" ]
197+
198+ assert "multivariate_feature_state_values" in event_data ["new_state" ]
199+ assert "multivariate_feature_state_values" in event_data ["previous_state" ]
200+
201+ assert event_data ["new_state" ]["multivariate_feature_state_values" ] == [
202+ {
203+ "id" : mocker .ANY ,
204+ "multivariate_feature_option" : {
205+ "id" : mv_option_50_percent ,
206+ "value" : mv_option_value ,
207+ },
208+ "percentage_allocation" : new_percentage ,
209+ },
210+ ]
211+ assert event_data ["previous_state" ]["multivariate_feature_state_values" ] == [
212+ {
213+ "id" : mocker .ANY ,
214+ "multivariate_feature_option" : {
215+ "id" : mv_option_50_percent ,
216+ "value" : mv_option_value ,
217+ },
218+ "percentage_allocation" : old_percentage ,
219+ },
200220 ]
201-
202- assert len (new_mv_values ) == 1
203- assert len (previous_mv_values ) == 1
204- assert new_mv_values [0 ]["percentage_allocation" ] == new_percentage
205- assert previous_mv_values [0 ]["percentage_allocation" ] == old_percentage
0 commit comments