Esempio n. 1
0
    def save(self, *args, **kwargs):
        # prevent duplicate feature states being created for an environment
        if not self.pk and FeatureState.objects.filter(environment=self.environment, feature=self.feature).exists() \
                and not (self.identity or self.feature_segment):
            raise ValidationError(
                'Feature state already exists for this environment and feature'
            )

        super(FeatureState, self).save(*args, **kwargs)

        # create default feature state value for feature state
        # note: this is get_or_create since feature state values are updated separately, and hence if this is set to
        # update_or_create, it overwrites the FSV with the initial value again
        FeatureStateValue.objects.get_or_create(feature_state=self,
                                                defaults=self._get_defaults())
        # TODO: move this to an async call using celery or django-rq
        trigger_feature_state_change_webhooks(self)
Esempio n. 2
0
def test_trigger_feature_state_change_webhooks(MockThread):
    # Given
    initial_value = "initial"
    new_value = "new"

    organisation = Organisation.objects.create(name="Test organisation")
    project = Project.objects.create(name="Test project",
                                     organisation=organisation)
    environment = Environment.objects.create(name="Test environment",
                                             project=project)
    feature = Feature.objects.create(name="Test feature",
                                     project=project,
                                     initial_value=initial_value,
                                     type=CONFIG)
    feature_state = FeatureState.objects.get(feature=feature,
                                             environment=environment)

    # update the feature state value and save both objects to ensure that the history is updated
    feature_state.feature_state_value.string_value = new_value
    feature_state.feature_state_value.save()
    feature_state.save()

    MockThread.reset_mock(
    )  # reset mock as it will have been called when setting up the data

    # When
    trigger_feature_state_change_webhooks(feature_state)

    # Then
    call_list = MockThread.call_args_list

    environment_webhook_call_args = call_list[0]
    organisation_webhook_call_args = call_list[1]

    # verify that the data for both calls is the same
    assert (environment_webhook_call_args[1]["args"][1] ==
            organisation_webhook_call_args[1]["args"][1])

    data = environment_webhook_call_args[1]["args"][1]
    assert data["new_state"]["feature_state_value"] == new_value
    assert data["previous_state"]["feature_state_value"] == initial_value
Esempio n. 3
0
 def trigger_feature_state_change_webhooks(self):
     trigger_feature_state_change_webhooks(self)