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

Bounce rate limit notifications when a client's service is suspended. #1717

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ class Config(object):
NEAR_DAILY_SMS_LIMIT_TEMPLATE_ID = "a796568f-a89b-468e-b635-8105554301b9"
REACHED_DAILY_SMS_LIMIT_TEMPLATE_ID = "a646e614-c527-4f94-a955-ed7185d577f4"
DAILY_SMS_LIMIT_UPDATED_TEMPLATE_ID = "6ec12dd0-680a-4073-8d58-91d17cc8442f"
BOUNCE_RATE_EXCEEDED_ID = "2dd32fab-0dd5-411b-9fa2-d23469eabbaf"
BOUNCE_RATE_WARNING_ID = "60d67125-e088-4793-a260-70d43982ec5a"
SERVICE_RESUMED_ID = "b393b19f-957f-4b4a-84b7-494dc1c615c3"

# Allowed service IDs able to send HTML through their templates.
ALLOW_HTML_SERVICE_IDS: List[str] = [id.strip() for id in os.getenv("ALLOW_HTML_SERVICE_IDS", "").split(",")]
Expand Down
80 changes: 79 additions & 1 deletion app/service/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from app import redis_store
from app.clients.zendesk_sell import ZenDeskSell
from app.config import QueueNames
from app.config import Config, QueueNames
from app.dao import fact_notification_status_dao, notifications_dao
from app.dao.api_key_dao import (
expire_api_key,
Expand Down Expand Up @@ -706,10 +706,15 @@ def suspend_service(service_id):
:param service_id:
:return:
"""

service = dao_fetch_service_by_id(service_id)

if service.active:
dao_suspend_service(service.id)
# TODO: Check if the service's bounce rate has been exceeded
# will depend on agreed upon method for fetching / storing
# a services bounce rate.
notify_bounce_rate_exceeded()

return "", 204

Expand All @@ -726,6 +731,7 @@ def resume_service(service_id):

if not service.active:
dao_resume_service(service.id)
notify_service_resumed(dao_fetch_service_by_id(service_id))

return "", 204

Expand Down Expand Up @@ -1123,3 +1129,75 @@ def check_if_reply_to_address_already_in_use(service_id, email_address):
},
status_code=400,
)


def notify_bounce_rate_exceeded(user_service):
service = Service.query.get(current_app.config["NOTIFY_SERVICE_ID"])
recipient = dao_fetch_service_creator(user_service.id).email_address
template = dao_get_template_by_id(current_app.config["BOUNCE_RATE_EXCEEDED_ID"])
reply_to = template.service.get_default_reply_to_email_address()

saved_notification = persist_notification(
template_id=template.id,
template_version=template.version,
recipient=recipient,
service=service,
personalisation={
"service_name": user_service.name,
"contact_us_url": f"{Config.ADMIN_BASE_URL}/contact",
},
notification_type=template.template_type,
api_key_id=None,
key_type=KEY_TYPE_NORMAL,
reply_to_text=reply_to,
)

send_notification_to_queue(saved_notification, False, queue=QueueNames.NOTIFY)


def notify_bounce_rate_warning(user_service):
service = Service.query.get(current_app.config["NOTIFY_SERVICE_ID"])
user = dao_fetch_service_creator(user_service.id)
template = dao_get_template_by_id(current_app.config["BOUNCE_RATE_LIMIT_WARNING_ID"])
reply_to = template.service.get_default_reply_to_email_address()

saved_notification = persist_notification(
template_id=template.id,
template_version=template.version,
recipient=user.email_address,
service=service,
personalisation={
"name": user.name,
"service_name": user_service.name,
},
notification_type=template.template_type,
api_key_id=None,
key_type=KEY_TYPE_NORMAL,
reply_to_text=reply_to,
)

send_notification_to_queue(saved_notification, False, queue=QueueNames.NOTIFY)


def notify_service_resumed(user_service):
service = Service.query.get(current_app.config["NOTIFY_SERVICE_ID"])
user = dao_fetch_service_creator(user_service.id)
template = dao_get_template_by_id(current_app.config["SERVICE_RESUMED_ID"])
reply_to = template.service.get_default_reply_to_email_address()

saved_notification = persist_notification(
template_id=template.id,
template_version=template.version,
recipient=user.email_address,
service=service,
personalisation={
"name": user.name,
"service_name": user_service.name,
},
notification_type=template.template_type,
api_key_id=None,
key_type=KEY_TYPE_NORMAL,
reply_to_text=reply_to,
)

send_notification_to_queue(saved_notification, False, queue=QueueNames.NOTIFY)
115 changes: 115 additions & 0 deletions migrations/versions/0425_service_suspend_resume.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
"""
Revision ID: 0425_bounce_rate_limits

Revises: 0424_sms_templates_in_redacted

Create Date: 2022-12-28 00:00:00
"""

from datetime import datetime

from alembic import op
from flask import current_app

revision = "0425_service_suspend_resume"
down_revision = "0424_sms_templates_in_redacted"

bounce_rate_exceeded = current_app.config["BOUNCE_RATE_EXCEEDED_ID"]
bounce_rate_warning = current_app.config["BOUNCE_RATE_WARNING_ID"]
resume_service = current_app.config["SERVICE_RESUMED_ID"]

templates = [
{
"id": bounce_rate_exceeded,
"name": "Bounce Rate Exceeded",
"type": "email",
"subject": "We’ve suspended your service",
"content_lines": [
"The email bounce rate has reached 10% for “((service_name))”. We’ve suspended the service to maintain our operations. While the service is under suspension, it will not be able to send email or text messages.",
"",
"You need to update and verify your list of recipient’s email addresses. Once you’ve taken these steps, you can request to resume service by [contacting us]((contact_us_url)). ",
"",
"The bounce rate for each service on GC Notify contributes to our total bounce rate. A high bounce rate suggests we’re emailing recipients without their consent. Then email providers send messages from GC Notify to the spam folder.",
"",
"An email may bounce if:",
"",
"(1) The recipient or their email provider has blocked sends from your service.",
"(2) You send to an email address that does not exist." "",

Check warning

Code scanning / CodeQL

Implicit string concatenation in a list

Implicit string concatenation. Maybe missing a comma?
"The GC Notify team",
],
},
{
"id": bounce_rate_warning,
"name": "Bounce Rate Warning",
"type": "email",
"subject": "Your bounce rate has exceeded 5%",
"content_lines": [
"Hello ((name))," "",

Check warning

Code scanning / CodeQL

Implicit string concatenation in a list

Implicit string concatenation. Maybe missing a comma?
"The bounce rate has exceeded 5% for “((service_name))”. You should update your list of recipient’s email addresses."
"",
Comment on lines +48 to +49

Check warning

Code scanning / CodeQL

Implicit string concatenation in a list

Implicit string concatenation. Maybe missing a comma?
"An email may bounce if:",
"(1) The recipient or their email provider has blocked sends from your service.",
"(2) You send to an email address that does not exist.",
"",
"The bounce rate for each service on GC Notify contributes to our total bounce rate. A high bounce rate suggests we’re emailing recipients without their consent. Then email providers send messages from GC Notify to the spam folder.",
"",
"To maintain our operations, we’ll suspend the service if its bounce rate reaches 10%. While the service is under suspension, it will not be able to send email or text messages."
"",
Comment on lines +56 to +57

Check warning

Code scanning / CodeQL

Implicit string concatenation in a list

Implicit string concatenation. Maybe missing a comma?
"The GC Notify team",
],
},
{
"id": resume_service,
"name": "Resume Service",
"type": "email",
"subject": "We’ve resumed your service",
"content_lines": [
"Hello ((name)),",
"",
"“((service_name))” can send messages again. We’ve removed the suspension." "",

Check warning

Code scanning / CodeQL

Implicit string concatenation in a list

Implicit string concatenation. Maybe missing a comma?
"The GC Notify Team",
],
},
]


def upgrade():

insert = """
INSERT INTO {} (id, name, template_type, created_at, content, archived, service_id, subject,
created_by_id, version, process_type, hidden)
VALUES ('{}', '{}', '{}', current_timestamp, '{}', False, '{}', '{}', '{}', 1, '{}', false)
"""

for template in templates:
for table_name in "templates", "templates_history":
op.execute(
insert.format(
table_name,
template["id"],
template["name"],
template["type"],
"\n".join(template["content_lines"]),
current_app.config["NOTIFY_SERVICE_ID"],
template.get("subject"),
current_app.config["NOTIFY_USER_ID"],
"normal",
)
)
op.execute(
f"""
INSERT INTO template_redacted(
template_id,
redact_personalisation,
updated_at,
updated_by_id
) VALUES ('{template["id"]}', false, current_timestamp, '{current_app.config["NOTIFY_USER_ID"]}')
"""
)


def downgrade():
for template in templates:
for table in "templates", "template_history":
op.execute(f"DELETE FROM {table} where template_id = '{template['id']}'")
op.execute(f"DELETE FROM template_redacted WHERE template_id = '{template['id']}'")
31 changes: 31 additions & 0 deletions tests/app/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,12 @@ def sample_service(
)


@pytest.fixture(scope="function")
def sample_inactive_service(sample_service):
sample_service.active = False
return sample_service


@pytest.fixture(scope="function", name="sample_service_full_permissions")
def _sample_service_full_permissions(notify_db_session):
service = create_service(
Expand Down Expand Up @@ -1200,6 +1206,31 @@ def no_reply_template(notify_db, notify_db_session):
)


@pytest.fixture(scope="function")
def bounce_rate_suspend_resume_templates(notify_db, notify_db_session):
service, user = notify_service(notify_db, notify_db_session)
import importlib

bounce_exceeded = importlib.import_module("migrations.versions.0425_service_suspend_resume")

return {
config_name: create_custom_template(
service,
user,
config_name,
"email",
content="\n".join(
next(x for x in bounce_exceeded.templates if x["id"] == current_app.config[config_name])["content_lines"]
),
)
for config_name in [
"BOUNCE_RATE_EXCEEDED_ID",
"BOUNCE_RATE_WARNING_ID",
"SERVICE_RESUMED_ID",
]
}


@pytest.fixture(scope="function")
def mou_signed_templates(notify_db, notify_db_session):
service, user = notify_service(notify_db, notify_db_session)
Expand Down
27 changes: 12 additions & 15 deletions tests/app/dao/test_permissions_dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,18 @@
def test_get_permissions_by_user_id_returns_all_permissions(sample_service):
permissions = permission_dao.get_permissions_by_user_id(user_id=sample_service.users[0].id)
assert len(permissions) == 8
assert (
sorted(
[
"manage_users",
"manage_templates",
"manage_settings",
"send_texts",
"send_emails",
"send_letters",
"manage_api_keys",
"view_activity",
]
)
== sorted([i.permission for i in permissions])
)
assert sorted(
[
"manage_users",
"manage_templates",
"manage_settings",
"send_texts",
"send_emails",
"send_letters",
"manage_api_keys",
"view_activity",
]
) == sorted([i.permission for i in permissions])


def test_get_permissions_by_user_id_returns_only_active_service(notify_db, notify_db_session, sample_user):
Expand Down
Loading