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

fix: ensure that usage notification logic is independent of other organisations notifications #4480

Merged
merged 1 commit into from
Aug 12, 2024
Merged
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
1 change: 1 addition & 0 deletions api/organisations/task_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def handle_api_usage_notification_for_organisation(organisation: Organisation) -
return

if OrganisationAPIUsageNotification.objects.filter(
organisation_id=organisation.id,
notified_at__gt=period_starts_at,
percent_usage__gte=matched_threshold,
).exists():
Expand Down
26 changes: 24 additions & 2 deletions api/tests/unit/organisations/test_unit_organisations_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,11 +362,28 @@ def test_handle_api_usage_notifications_below_100(
organisation=organisation,
).exists()

# Create an OrganisationApiUsageNotification object for another organisation
# to verify that only the correct organisation's notifications are taken into
# account.
another_organisation = Organisation.objects.create(name="Another Organisation")
OrganisationAPIUsageNotification.objects.create(
organisation=another_organisation,
percent_usage=100,
notified_at=now - timedelta(days=1),
)

# When
handle_api_usage_notifications()

# Then
mock_api_usage.assert_called_once_with(organisation.id, now - timedelta(days=14))
assert len(mock_api_usage.call_args_list) == 2

# We only care about the call for the main organisation,
# not the call for 'another_organisation'
assert mock_api_usage.call_args_list[0].args == (
organisation.id,
now - timedelta(days=14),
)

assert len(mailoutbox) == 1
email = mailoutbox[0]
Expand Down Expand Up @@ -410,7 +427,12 @@ def test_handle_api_usage_notifications_below_100(
).count()
== 1
)
assert OrganisationAPIUsageNotification.objects.first() == api_usage_notification
assert (
OrganisationAPIUsageNotification.objects.filter(
organisation=organisation
).first()
== api_usage_notification
)


@pytest.mark.freeze_time("2023-01-19T09:09:47.325132+00:00")
Expand Down
Loading