Skip to content
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

feat: Send users notification when api flags have been blocked #4338

Merged
22 changes: 22 additions & 0 deletions api/organisations/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,25 @@ def finish_subscription_cancellation() -> None:
subscription.save_as_free_subscription()


def send_api_flags_blocked_notification(organisation: Organisation) -> None:
recipient_list = FFAdminUser.objects.filter(
userorganisation__organisation=organisation,
)

context = {"organisation": organisation}
message = "organisations/api_flags_blocked_notification.txt"
html_message = "organisations/api_flags_blocked_notification.html"

send_mail(
subject="Flagsmith API use has been blocked due to overuse",
message=render_to_string(message, context),
from_email=settings.DEFAULT_FROM_EMAIL,
recipient_list=list(recipient_list.values_list("email", flat=True)),
html_message=render_to_string(html_message, context),
fail_silently=True,
)


def send_api_usage_notification(
organisation: Organisation, matched_threshold: int
) -> None:
Expand Down Expand Up @@ -390,6 +409,9 @@ def restrict_use_due_to_api_limit_grace_period_over() -> None:
organisation.stop_serving_flags = stop_serving
organisation.block_access_to_admin = block_access

if stop_serving:
send_api_flags_blocked_notification(organisation)

api_limit_access_blocks.append(APILimitAccessBlock(organisation=organisation))
update_organisations.append(organisation)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<table>

<tr>

<td>Hi there,</td>

</tr>

<tr>

<td>
As per previous warnings, we have blocked organisation {{ organisation.name }}.
Please visit app.flagsmith.com to upgrade your account and re-enable service.
</td>


</tr>

<tr>

<td>Thank you!</td>

</tr>

<tr>

<td>The Flagsmith Team</td>

</tr>

</table>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Hi there,

As per previous warnings, we have blocked organisation {{ organisation.name }}. Please visit app.flagsmith.com to upgrade your account and re-enable service.

Thank you!

The Flagsmith Team
51 changes: 51 additions & 0 deletions api/tests/unit/organisations/test_unit_organisations_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,9 @@ def test_restrict_use_due_to_api_limit_grace_period_over(
mocker: MockerFixture,
organisation: Organisation,
freezer: FrozenDateTimeFactory,
mailoutbox: list[EmailMultiAlternatives],
admin_user: FFAdminUser,
staff_user: FFAdminUser,
) -> None:
# Given
get_client_mock = mocker.patch("organisations.tasks.get_client")
Expand All @@ -1144,6 +1147,11 @@ def test_restrict_use_due_to_api_limit_grace_period_over(
organisation4 = Organisation.objects.create(name="Org #4")
organisation5 = Organisation.objects.create(name="Org #5")

# Add users to test email delivery
for org in [organisation2, organisation3, organisation4, organisation5]:
admin_user.add_organisation(org, role=OrganisationRole.ADMIN)
staff_user.add_organisation(org, role=OrganisationRole.USER)

organisation5.subscription.plan = "scale-up-v2"
organisation5.subscription.payment_method = CHARGEBEE
organisation5.subscription.subscription_id = "subscription-id"
Expand Down Expand Up @@ -1239,6 +1247,49 @@ def test_restrict_use_due_to_api_limit_grace_period_over(
),
]

assert len(mailoutbox) == 2
email1 = mailoutbox[0]
assert email1.subject == "Flagsmith API use has been blocked due to overuse"
assert email1.body == (
"Hi there,\n\n"
"As per previous warnings, we have blocked organisation Test Org. "
"Please visit app.flagsmith.com to upgrade your "
"account and re-enable service.\n\n"
"Thank you!\n\n"
"The Flagsmith Team\n"
)

email2 = mailoutbox[1]
assert email2.subject == "Flagsmith API use has been blocked due to overuse"
assert email2.body == (
"Hi there,\n\n"
"As per previous warnings, we have blocked organisation Org #2. "
"Please visit app.flagsmith.com to upgrade your "
"account and re-enable service.\n\n"
"Thank you!\n\n"
"The Flagsmith Team\n"
)

assert len(email2.alternatives) == 1
assert len(email2.alternatives[0]) == 2
assert email2.alternatives[0][1] == "text/html"

assert email2.alternatives[0][0] == (
"<table>\n\n <tr>\n\n <td>Hi there,"
"</td>\n\n </tr>\n\n <tr>\n\n "
" <td>\n As per previous warnings, we have"
" blocked organisation Org #2.\n Please "
"visit app.flagsmith.com to upgrade your account and "
"re-enable service.\n "
"</td>\n\n\n </tr>\n\n <tr>\n\n "
" <td>Thank you!</td>\n\n </tr>\n\n "
"<tr>\n\n <td>The Flagsmith Team</td>"
"\n\n </tr>\n\n</table>\n"
)

assert email2.from_email == "[email protected]"
assert email2.to == ["[email protected]", "[email protected]"]

# Organisations that change their subscription are unblocked.
organisation.subscription.plan = "scale-up-v2"
organisation.subscription.save()
Expand Down
Loading