@@ -319,12 +319,22 @@ def test_handle_api_usage_notification_for_organisation_when_cancellation_date_i
319319 mocker : MockerFixture ,
320320) -> None :
321321 # Given
322+ usage = 25
323+ allowance = 1_000_000
324+
322325 organisation .subscription .plan = SCALE_UP
323326 organisation .subscription .subscription_id = "fancy_id"
324327 organisation .subscription .cancellation_date = timezone .now ()
325328 organisation .subscription .save ()
326329 mock_api_usage = mocker .patch ("organisations.task_helpers.get_current_api_usage" )
327- mock_api_usage .return_value = 25
330+ mock_api_usage .return_value = usage
331+
332+ OrganisationSubscriptionInformationCache .objects .create (
333+ organisation = organisation ,
334+ allowed_30d_api_calls = allowance ,
335+ api_calls_30d = usage ,
336+ )
337+
328338 from organisations .task_helpers import logger
329339
330340 logger .addHandler (inspecting_handler )
@@ -353,10 +363,11 @@ def test_handle_api_usage_notification_for_organisation_when_billing_starts_at_i
353363 organisation = organisation ,
354364 allowed_30d_api_calls = 1_000_000 ,
355365 current_billing_term_starts_at = billing_term_starts_at ,
366+ api_calls_30d = 1_900_000 ,
356367 )
357368
358369 mock_api_usage = mocker .patch ("organisations.task_helpers.get_current_api_usage" )
359- mock_api_usage .return_value = 25
370+ mock_api_usage .return_value = 2_000_000
360371
361372 organisation .refresh_from_db ()
362373
@@ -379,12 +390,11 @@ def test_handle_api_usage_notifications_when_feature_flag_is_off(
379390 now = timezone .now ()
380391 OrganisationSubscriptionInformationCache .objects .create (
381392 organisation = organisation ,
382- allowed_seats = 10 ,
383- allowed_projects = 3 ,
384393 allowed_30d_api_calls = 100 ,
385394 chargebee_email = "test@example.com" ,
386395 current_billing_term_starts_at = now - timedelta (days = 45 ),
387396 current_billing_term_ends_at = now + timedelta (days = 320 ),
397+ api_calls_30d = 110 ,
388398 )
389399 mock_api_usage = mocker .patch (
390400 "organisations.tasks.get_current_api_usage" ,
@@ -431,12 +441,10 @@ def test_handle_api_usage_notifications_below_100(
431441 organisation .subscription .save ()
432442 OrganisationSubscriptionInformationCache .objects .create (
433443 organisation = organisation ,
434- allowed_seats = 10 ,
435- allowed_projects = 3 ,
436444 allowed_30d_api_calls = 100 ,
437- chargebee_email = "test@example.com" ,
438445 current_billing_term_starts_at = now - timedelta (days = 45 ),
439446 current_billing_term_ends_at = now + timedelta (days = 320 ),
447+ api_calls_30d = 90 ,
440448 )
441449 mock_api_usage = mocker .patch (
442450 "organisations.task_helpers.get_current_api_usage" ,
@@ -465,7 +473,7 @@ def test_handle_api_usage_notifications_below_100(
465473 handle_api_usage_notifications ()
466474
467475 # Then
468- assert len (mock_api_usage .call_args_list ) == 2
476+ assert len (mock_api_usage .call_args_list ) == 1
469477
470478 # We only care about the call for the main organisation,
471479 # not the call for 'another_organisation'
@@ -551,6 +559,7 @@ def test_handle_api_usage_notifications_below_api_usage_alert_thresholds(
551559 chargebee_email = "test@example.com" ,
552560 current_billing_term_starts_at = now - timedelta (days = 45 ),
553561 current_billing_term_ends_at = now + timedelta (days = 320 ),
562+ api_calls_30d = 70 ,
554563 )
555564 mock_api_usage = mocker .patch (
556565 "organisations.task_helpers.get_current_api_usage" ,
@@ -571,8 +580,6 @@ def test_handle_api_usage_notifications_below_api_usage_alert_thresholds(
571580 handle_api_usage_notifications ()
572581
573582 # Then
574- mock_api_usage .assert_called_once_with (organisation .id , now - timedelta (days = 14 ))
575-
576583 assert len (mailoutbox ) == 0
577584
578585 assert (
@@ -590,23 +597,25 @@ def test_handle_api_usage_notifications_above_100(
590597 mailoutbox : list [EmailMultiAlternatives ],
591598) -> None :
592599 # Given
600+ usage = 105
601+ allowance = 100
602+
593603 now = timezone .now ()
594604 organisation .subscription .plan = SCALE_UP
595605 organisation .subscription .subscription_id = "fancy_id"
596606 organisation .subscription .save ()
597607 OrganisationSubscriptionInformationCache .objects .create (
598608 organisation = organisation ,
599- allowed_seats = 10 ,
600- allowed_projects = 3 ,
601- allowed_30d_api_calls = 100 ,
609+ allowed_30d_api_calls = allowance ,
602610 chargebee_email = "test@example.com" ,
603611 current_billing_term_starts_at = now - timedelta (days = 45 ),
604612 current_billing_term_ends_at = now + timedelta (days = 320 ),
613+ api_calls_30d = usage ,
605614 )
606615 mock_api_usage = mocker .patch (
607616 "organisations.task_helpers.get_current_api_usage" ,
608617 )
609- mock_api_usage .return_value = 105
618+ mock_api_usage .return_value = usage
610619
611620 get_client_mock = mocker .patch ("organisations.tasks.get_client" )
612621 client_mock = MagicMock ()
@@ -693,12 +702,11 @@ def test_handle_api_usage_notifications_with_error(
693702 organisation .subscription .save ()
694703 OrganisationSubscriptionInformationCache .objects .create (
695704 organisation = organisation ,
696- allowed_seats = 10 ,
697- allowed_projects = 3 ,
698705 allowed_30d_api_calls = 100 ,
699706 chargebee_email = "test@example.com" ,
700707 current_billing_term_starts_at = now - timedelta (days = 45 ),
701708 current_billing_term_ends_at = now + timedelta (days = 320 ),
709+ api_calls_30d = 100 ,
702710 )
703711
704712 get_client_mock = mocker .patch ("organisations.tasks.get_client" )
@@ -738,15 +746,24 @@ def test_handle_api_usage_notifications_for_free_accounts(
738746 mailoutbox : list [EmailMultiAlternatives ],
739747) -> None :
740748 # Given
749+ allowance = MAX_SEATS_IN_FREE_PLAN
750+ usage = MAX_API_CALLS_IN_FREE_PLAN + 5_000
751+
741752 now = timezone .now ()
742753 assert organisation .is_paid is False
743754 assert organisation .subscription .is_free_plan is True
744755 assert organisation .subscription .max_api_calls == MAX_API_CALLS_IN_FREE_PLAN
745756
757+ OrganisationSubscriptionInformationCache .objects .create (
758+ organisation = organisation ,
759+ allowed_30d_api_calls = allowance ,
760+ api_calls_30d = usage ,
761+ )
762+
746763 mock_api_usage = mocker .patch (
747764 "organisations.task_helpers.get_current_api_usage" ,
748765 )
749- mock_api_usage .return_value = MAX_API_CALLS_IN_FREE_PLAN + 5_000
766+ mock_api_usage .return_value = usage
750767
751768 get_client_mock = mocker .patch ("organisations.tasks.get_client" )
752769 client_mock = MagicMock ()
@@ -831,9 +848,6 @@ def test_handle_api_usage_notifications_missing_info_cache(
831848 organisation .subscription .plan = SCALE_UP
832849 organisation .subscription .save ()
833850
834- from organisations .task_helpers import logger
835-
836- logger .addHandler (inspecting_handler )
837851 assert organisation .has_subscription_information_cache () is False
838852
839853 mock_api_usage = mocker .patch (
@@ -860,10 +874,6 @@ def test_handle_api_usage_notifications_missing_info_cache(
860874 organisation = organisation ,
861875 ).exists ()
862876
863- assert inspecting_handler .messages == [ # type: ignore[attr-defined]
864- f"Paid organisation { organisation .id } is missing subscription information cache"
865- ]
866-
867877
868878@pytest .mark .parametrize ("plan" , ("scale-up" , "scale-up-v2" , "scale-up-v3" ))
869879@pytest .mark .freeze_time ("2023-01-19T09:09:47.325132+00:00" )
0 commit comments