Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: cds-snc/notification-utils
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 53.0.4
Choose a base ref
...
head repository: cds-snc/notification-utils
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 53.1.0
Choose a head ref
  • 1 commit
  • 4 files changed
  • 1 contributor

Commits on Jan 9, 2025

  1. Task/pre 3.12 recipient csv annual limit validation merge in to main (#…

    sastels authored Jan 9, 2025

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    ec2de5f View commit details
Showing with 119 additions and 7 deletions.
  1. +17 −2 notifications_utils/recipients.py
  2. +1 −1 pyproject.toml
  3. +11 −0 tests/conftest.py
  4. +90 −4 tests/test_recipient_csv.py
19 changes: 17 additions & 2 deletions notifications_utils/recipients.py
Original file line number Diff line number Diff line change
@@ -89,7 +89,9 @@ def __init__(
max_initial_rows_shown=10,
safelist=None,
template=None,
remaining_messages=sys.maxsize,
remaining_messages=sys.maxsize, # TODO FF_ANNUAL_LIMIT removal - remove this param
remaining_daily_messages=sys.maxsize,
remaining_annual_messages=sys.maxsize,
international_sms=False,
max_rows=50000,
user_language="en",
@@ -104,6 +106,8 @@ def __init__(
self.template = template if isinstance(template, Template) else None
self.international_sms = international_sms
self.remaining_messages = remaining_messages
self.remaining_daily_messages = remaining_daily_messages
self.remaining_annual_messages = remaining_annual_messages
self.rows_as_list = None
self.max_rows = max_rows

@@ -162,7 +166,9 @@ def has_errors(self):
return bool(
self.missing_column_headers
or self.duplicate_recipient_column_headers
or self.more_rows_than_can_send
or self.more_rows_than_can_send # TODO FF_ANNUAL_LIMIT removal - Remove this check
or self.more_rows_than_can_send_this_year
or self.more_rows_than_can_send_today
or self.too_many_rows
or (not self.allowed_to_send_to)
or any(self.rows_with_errors)
@@ -229,10 +235,19 @@ def get_rows(self):
else:
yield None

# TODO FF_ANNUAL_LIMIT removal - remove this property
@property
def more_rows_than_can_send(self):
return len(self) > self.remaining_messages

@property
def more_rows_than_can_send_today(self):
return len(self) > self.remaining_daily_messages

@property
def more_rows_than_can_send_this_year(self):
return len(self) > self.remaining_annual_messages

@property
def sms_fragment_count(self):
if self.template_type != "sms":
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "notifications-utils"
version = "53.0.4"
version = "53.1.0"
description = "Shared python code for Notification - Provides logging utils etc."
authors = ["Canadian Digital Service"]
license = "MIT license"
11 changes: 11 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from contextlib import contextmanager
from unittest.mock import Mock

import pytest
@@ -9,6 +10,16 @@ class FakeService:
id = "1234"


@contextmanager
def set_config(app, name, value):
old_val = app.config.get(name)
app.config[name] = value
try:
yield
finally:
app.config[name] = old_val


@pytest.fixture
def app():
flask_app = Flask(__name__)
94 changes: 90 additions & 4 deletions tests/test_recipient_csv.py
Original file line number Diff line number Diff line change
@@ -9,6 +9,8 @@
from notifications_utils.template import SMSMessageTemplate
from ordered_set import OrderedSet

from tests.conftest import set_config


def _index_rows(rows):
return set(row.index for row in rows)
@@ -873,17 +875,100 @@ def test_ignores_leading_whitespace_in_file(character, name):
assert not recipients.has_errors


def test_error_if_too_many_email_recipients():
def test_error_if_too_many_email_recipients_for_year(app):
with set_config(app, "FF_ANNUAL_LIMIT", True):
recipients = RecipientCSV(
"email address,\ntest@test.com,\ntest@test.com,\ntest@test.com,",
placeholders=["email_address"],
template_type="email",
remaining_annual_messages=2,
)
assert recipients.has_errors
assert recipients.more_rows_than_can_send_this_year


def test_error_if_too_many_sms_recipients_for_year(app):
with set_config(app, "FF_ANNUAL_LIMIT", True):
recipients = RecipientCSV(
"phone number,\n6502532222,\n6502532222,\n6502532222,",
placeholders=["phone_number"],
template_type="sms",
template=SMSMessageTemplate(
{"content": "test message", "template_type": "sms"},
sender=None,
prefix=None,
),
remaining_annual_messages=2,
)
assert recipients.has_errors
assert recipients.more_rows_than_can_send_this_year


def test_dont_error_if_too_many_recipients_not_specified_for_year(app):
with set_config(app, "FF_ANNUAL_LIMIT", True):
recipients = RecipientCSV(
"phone number,\n6502532222,\n6502532222,\n6502532222,", placeholders=["phone_number"], template_type="sms"
)
assert not recipients.has_errors
assert not recipients.more_rows_than_can_send_this_year


def test_error_if_too_many_email_recipients_for_today(app):
with set_config(app, "FF_ANNUAL_LIMIT", True):
recipients = RecipientCSV(
"email address,\ntest@test.com,\ntest@test.com,\ntest@test.com,",
placeholders=["email_address"],
template_type="email",
remaining_daily_messages=2,
)
assert recipients.has_errors
assert recipients.more_rows_than_can_send_today


def test_error_if_too_many_sms_recipients_for_today(app):
with set_config(app, "FF_ANNUAL_LIMIT", True):
recipients = RecipientCSV(
"phone number,\n6502532222,\n6502532222,\n6502532222,",
placeholders=["phone_number"],
template_type="sms",
template=SMSMessageTemplate(
{"content": "test message", "template_type": "sms"},
sender=None,
prefix=None,
),
remaining_daily_messages=2,
)
assert recipients.has_errors
assert recipients.more_rows_than_can_send_today


def test_dont_error_if_too_many_recipients_not_specified_for_today(app):
with set_config(app, "FF_ANNUAL_LIMIT", True):
recipients = RecipientCSV(
"phone number,\n6502532222,\n6502532222,\n6502532222,", placeholders=["phone_number"], template_type="sms"
)
assert not recipients.has_errors
assert not recipients.more_rows_than_can_send_today


# TODO: FF_ANNUAL_LIMIT removal - remove this test
def test_error_if_too_many_email_recipients(app):
recipients = RecipientCSV(
"email address,\ntest@test.com,\ntest@test.com,\ntest@test.com,",
placeholders=["email_address"],
template_type="email",
"phone number,\n6502532222,\n6502532222,\n6502532222,",
placeholders=["phone_number"],
template_type="sms",
template=SMSMessageTemplate(
{"content": "test message", "template_type": "sms"},
sender=None,
prefix=None,
),
remaining_messages=2,
)
assert recipients.has_errors
assert recipients.more_rows_than_can_send


# TODO: FF_ANNUAL_LIMIT removal - remove this test
def test_error_if_too_many_sms_recipients():
recipients = RecipientCSV(
"phone number,\n6502532222,\n6502532222,\n6502532222,",
@@ -900,6 +985,7 @@ def test_error_if_too_many_sms_recipients():
assert recipients.more_rows_than_can_send


# TODO: FF_ANNUAL_LIMIT removal - remove this test
def test_dont_error_if_too_many_recipients_not_specified():
recipients = RecipientCSV(
"phone number,\n6502532222,\n6502532222,\n6502532222,", placeholders=["phone_number"], template_type="sms"