File tree Expand file tree Collapse file tree 2 files changed +13
-2
lines changed Expand file tree Collapse file tree 2 files changed +13
-2
lines changed Original file line number Diff line number Diff line change 3
3
from datetime import timedelta
4
4
5
5
from app_analytics .influxdb_wrapper import get_current_api_usage
6
+ from dateutil .relativedelta import relativedelta
6
7
from django .conf import settings
7
8
from django .core .mail import send_mail
8
9
from django .db .models import F , Max , Q
@@ -154,7 +155,7 @@ def charge_for_api_call_count_overages():
154
155
155
156
# Get the period where we're interested in any new API usage
156
157
# notifications for the relevant billing period (ie, this month).
157
- api_usage_notified_at = now - timedelta ( days = 30 )
158
+ api_usage_notified_at = now - relativedelta ( months = 1 )
158
159
159
160
# Since we're only interested in monthly billed accounts, set a wide
160
161
# threshold to catch as many billing periods that could be roughly
Original file line number Diff line number Diff line change 5
5
6
6
import pytest
7
7
from core .helpers import get_current_site_url
8
+ from dateutil .relativedelta import relativedelta
8
9
from django .core .mail .message import EmailMultiAlternatives
9
10
from django .template .loader import render_to_string
10
11
from django .utils import timezone
@@ -834,10 +835,19 @@ def test_charge_for_api_call_count_overages_scale_up(
834
835
organisation .subscription .subscription_id = "fancy_sub_id23"
835
836
organisation .subscription .plan = "scale-up-v2"
836
837
organisation .subscription .save ()
838
+
839
+ # In order to cover an edge case found in production use, we make the
840
+ # notification date just outside the previous 30 days, because we want
841
+ # to make sure that we cover the case where someone with very high usage
842
+ # is notified in the first day of their subscription period (in a 31-day
843
+ # month).
844
+ notification_date = now - (timedelta (days = 30 ) + timedelta (minutes = 30 ))
845
+ assert notification_date > now - relativedelta (months = 1 )
846
+
837
847
OrganisationAPIUsageNotification .objects .create (
838
848
organisation = organisation ,
839
849
percent_usage = 100 ,
840
- notified_at = now ,
850
+ notified_at = notification_date ,
841
851
)
842
852
843
853
mocker .patch ("organisations.chargebee.chargebee.chargebee.Subscription.retrieve" )
You can’t perform that action at this time.
0 commit comments