From dd1dd327c99d3be15eb3395eac6a95a87a1c24d7 Mon Sep 17 00:00:00 2001 From: Zach Aysan Date: Fri, 27 Sep 2024 10:45:47 -0400 Subject: [PATCH] feat: Add domain to API flags blocked notification (#4574) Co-authored-by: Matthew Elwell --- api/organisations/task_helpers.py | 3 +++ .../api_flags_blocked_notification.html | 2 +- .../api_flags_blocked_notification.txt | 2 +- .../test_unit_organisations_tasks.py | 17 +++++++++++++---- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/api/organisations/task_helpers.py b/api/organisations/task_helpers.py index 2466e3c4d377..d2984113afe4 100644 --- a/api/organisations/task_helpers.py +++ b/api/organisations/task_helpers.py @@ -2,6 +2,7 @@ from datetime import timedelta from app_analytics.influxdb_wrapper import get_current_api_usage +from core.helpers import get_current_site_url from dateutil.relativedelta import relativedelta from django.conf import settings from django.core.mail import send_mail @@ -26,9 +27,11 @@ def send_api_flags_blocked_notification(organisation: Organisation) -> None: userorganisation__organisation=organisation, ) + url = get_current_site_url() context = { "organisation": organisation, "grace_period": not hasattr(organisation, "breached_grace_period"), + "url": url, } message = "organisations/api_flags_blocked_notification.txt" html_message = "organisations/api_flags_blocked_notification.html" diff --git a/api/organisations/templates/organisations/api_flags_blocked_notification.html b/api/organisations/templates/organisations/api_flags_blocked_notification.html index 03fdfaa16897..12b0a498574d 100644 --- a/api/organisations/templates/organisations/api_flags_blocked_notification.html +++ b/api/organisations/templates/organisations/api_flags_blocked_notification.html @@ -9,7 +9,7 @@ - This is a system generated notification related to your Flagsmith API Usage. As per previous warnings, we have had to block your company {{ organisation.name }}{% if grace_period %} after the 7 day grace period{% endif %}. Flags are not currently being served for your organization, and will continue to be blocked until your billing period resets or you upgrade your account. You can upgrade your account at app.flagsmith.com. + This is a system generated notification related to your Flagsmith API Usage. As per previous warnings, we have had to block your company {{ organisation.name }}{% if grace_period %} after the 7 day grace period{% endif %}. Flags are not currently being served for your organization, and will continue to be blocked until your billing period resets or you upgrade your account. You can upgrade your account at {{url}}. diff --git a/api/organisations/templates/organisations/api_flags_blocked_notification.txt b/api/organisations/templates/organisations/api_flags_blocked_notification.txt index 12d4b624efba..4e92dbe670e9 100644 --- a/api/organisations/templates/organisations/api_flags_blocked_notification.txt +++ b/api/organisations/templates/organisations/api_flags_blocked_notification.txt @@ -1,6 +1,6 @@ Hi there, -This is a system generated notification related to your Flagsmith API Usage. As per previous warnings, we have had to block your company {{ organisation.name }}{% if grace_period %} after the 7 day grace period{% endif %}. Flags are not currently being served for your organization, and will continue to be blocked until your billing period resets or you upgrade your account. You can upgrade your account at app.flagsmith.com. +This is a system generated notification related to your Flagsmith API Usage. As per previous warnings, we have had to block your company {{ organisation.name }}{% if grace_period %} after the 7 day grace period{% endif %}. Flags are not currently being served for your organization, and will continue to be blocked until your billing period resets or you upgrade your account. You can upgrade your account at {{ url }}. Thank you! diff --git a/api/tests/unit/organisations/test_unit_organisations_tasks.py b/api/tests/unit/organisations/test_unit_organisations_tasks.py index a307976008bb..179f55c21bb9 100644 --- a/api/tests/unit/organisations/test_unit_organisations_tasks.py +++ b/api/tests/unit/organisations/test_unit_organisations_tasks.py @@ -4,6 +4,7 @@ from unittest.mock import MagicMock, call import pytest +from core.helpers import get_current_site_url from django.core.mail.message import EmailMultiAlternatives from django.template.loader import render_to_string from django.utils import timezone @@ -1646,13 +1647,13 @@ def test_restrict_use_due_to_api_limit_grace_period_over( assert email1.subject == "Flagsmith API use has been blocked due to overuse" assert email1.body == render_to_string( "organisations/api_flags_blocked_notification.txt", - context={"organisation": organisation}, + context={"organisation": organisation, "url": get_current_site_url()}, ) email2 = mailoutbox[1] assert email2.subject == "Flagsmith API use has been blocked due to overuse" assert email2.body == render_to_string( "organisations/api_flags_blocked_notification.txt", - context={"organisation": organisation2}, + context={"organisation": organisation2, "url": get_current_site_url()}, ) assert len(email2.alternatives) == 1 @@ -1661,7 +1662,11 @@ def test_restrict_use_due_to_api_limit_grace_period_over( assert email2.alternatives[0][0] == render_to_string( "organisations/api_flags_blocked_notification.html", - context={"organisation": organisation2, "grace_period": False}, + context={ + "organisation": organisation2, + "grace_period": False, + "url": get_current_site_url(), + }, ) assert email2.from_email == "noreply@flagsmith.com" assert email2.to == ["admin@example.com", "staff@example.com"] @@ -1674,7 +1679,11 @@ def test_restrict_use_due_to_api_limit_grace_period_over( assert email3.alternatives[0][0] == render_to_string( "organisations/api_flags_blocked_notification.html", - context={"organisation": organisation6, "grace_period": False}, + context={ + "organisation": organisation6, + "grace_period": False, + "url": get_current_site_url(), + }, ) assert email3.from_email == "noreply@flagsmith.com" assert email3.to == ["admin@example.com", "staff@example.com"]