Skip to content

Commit

Permalink
fix: Rebuild environments when stop serving flags changed (#2944)
Browse files Browse the repository at this point in the history
  • Loading branch information
zachaysan committed Nov 9, 2023
1 parent b78842b commit 7d16197
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
11 changes: 11 additions & 0 deletions api/organisations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,17 @@ def clear_environment_caches(self):
)
)

@hook(AFTER_SAVE, when="stop_serving_flags", has_changed=True)
def rebuild_environments(self):
# Avoid circular imports.
from environments.models import Environment
from environments.tasks import rebuild_environment_document

for environment_id in Environment.objects.filter(
project__organisation=self
).values_list("id", flat=True):
rebuild_environment_document.delay(args=(environment_id,))


class UserOrganisation(models.Model):
user = models.ForeignKey("users.FFAdminUser", on_delete=models.CASCADE)
Expand Down
37 changes: 37 additions & 0 deletions api/organisations/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

import pytest
from django.test import TestCase
from pytest_mock import MockerFixture
from rest_framework.test import override_settings

from environments.models import Environment
from organisations.chargebee.metadata import ChargebeeObjMetadata
from organisations.models import (
TRIAL_SUBSCRIPTION_ID,
Expand Down Expand Up @@ -83,6 +85,41 @@ def test_cancel_subscription_cancels_chargebee_subscription(
assert subscription.cancellation_date


def test_organisation_rebuild_environment_document_on_stop_serving_flags_changed(
environment: Environment, organisation: Organisation, mocker: MockerFixture
):
# Given
mocked_rebuild_environment_document = mocker.patch(
"environments.tasks.rebuild_environment_document"
)
assert organisation.stop_serving_flags is False

# When
organisation.stop_serving_flags = True
organisation.save()

# Then
mocked_rebuild_environment_document.delay.assert_called_once_with(
args=(environment.id,)
)


def test_organisation_rebuild_environment_document_on_stop_serving_flags_unchanged(
environment: Environment, organisation: Organisation, mocker: MockerFixture
):
# Given
mocked_rebuild_environment_document = mocker.patch(
"environments.tasks.rebuild_environment_document"
)

# When saving something irrelevant
organisation.alerted_over_plan_limit = True
organisation.save()

# Then the task should not be called
mocked_rebuild_environment_document.delay.assert_not_called()


def test_organisation_over_plan_seats_limit_returns_false_if_not_over_plan_seats_limit(
organisation, chargebee_subscription, mocker
):
Expand Down

3 comments on commit 7d16197

@vercel
Copy link

@vercel vercel bot commented on 7d16197 Nov 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 7d16197 Nov 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 7d16197 Nov 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

docs – ./docs

docs-git-main-flagsmith.vercel.app
docs.flagsmith.com
docs-flagsmith.vercel.app
docs.bullet-train.io

Please sign in to comment.