Skip to content

Commit 9801c2a

Browse files
committed
Fix tests
1 parent 772f5bd commit 9801c2a

2 files changed

Lines changed: 43 additions & 80 deletions

File tree

api/tests/unit/app_analytics/test_analytics_db_service.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,7 @@ def test_get_usage_data__no_analytics_configured__no_calls_expected(
538538
mocked_get_usage_data_from_local_db.assert_not_called()
539539

540540

541+
@pytest.mark.freeze_time("2023-01-19T09:09:47.325132+00:00")
541542
def test_get_total_events_count__postgres_not_configured__calls_influx( # type: ignore[no-untyped-def]
542543
mocker, settings, organisation
543544
):
@@ -553,7 +554,9 @@ def test_get_total_events_count__postgres_not_configured__calls_influx( # type:
553554
# Then
554555
assert total_events_count == mocked_get_events_for_organisation.return_value
555556
mocked_get_events_for_organisation.assert_called_once_with(
556-
organisation_id=organisation.id
557+
organisation.id,
558+
date_start=datetime(2022, 12, 20, 0, 0, tzinfo=UTC),
559+
date_stop=datetime(2023, 1, 19, 0, 0, tzinfo=UTC),
557560
)
558561

559562

api/tests/unit/organisations/test_unit_organisations_tasks.py

Lines changed: 39 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
send_org_subscription_cancelled_alert,
5252
unrestrict_after_api_limit_grace_period_is_stale,
5353
)
54+
from tests.types import EnableFeaturesFixture
5455
from users.models import FFAdminUser
5556

5657

@@ -885,6 +886,7 @@ def test_charge_for_api_call_count_overages__scale_up_plan__charges_correct_addo
885886
organisation: Organisation,
886887
mocker: MockerFixture,
887888
plan: str,
889+
enable_features: EnableFeaturesFixture,
888890
) -> None:
889891
# Given
890892
now = timezone.now()
@@ -924,10 +926,7 @@ def test_charge_for_api_call_count_overages__scale_up_plan__charges_correct_addo
924926
autospec=True,
925927
)
926928

927-
get_client_mock = mocker.patch("organisations.tasks.get_client")
928-
client_mock = MagicMock()
929-
get_client_mock.return_value = client_mock
930-
client_mock.get_identity_flags.return_value.is_feature_enabled.return_value = True
929+
enable_features("api_usage_overage_charges")
931930

932931
mock_api_usage = mocker.patch(
933932
"organisations.tasks.get_current_api_usage",
@@ -1071,6 +1070,7 @@ def test_charge_for_api_call_count_overages__flagsmith_feature_disabled__skips_c
10711070
def test_charge_for_api_call_count_overages__within_grace_period__creates_breached_record_without_charging(
10721071
organisation: Organisation,
10731072
mocker: MockerFixture,
1073+
enable_features: EnableFeaturesFixture,
10741074
) -> None:
10751075
# Given
10761076
now = timezone.now()
@@ -1092,10 +1092,7 @@ def test_charge_for_api_call_count_overages__within_grace_period__creates_breach
10921092
notified_at=now,
10931093
)
10941094

1095-
get_client_mock = mocker.patch("organisations.tasks.get_client")
1096-
client_mock = MagicMock()
1097-
get_client_mock.return_value = client_mock
1098-
client_mock.get_identity_flags.return_value.is_feature_enabled.return_value = True
1095+
enable_features("api_usage_overage_charges")
10991096

11001097
mock_chargebee_update = mocker.patch(
11011098
"organisations.chargebee.chargebee.chargebee_client.Subscription.update",
@@ -1121,6 +1118,7 @@ def test_charge_for_api_call_count_overages__within_grace_period__creates_breach
11211118
def test_charge_for_api_call_count_overages__grace_period_previously_breached__charges_overage(
11221119
organisation: Organisation,
11231120
mocker: MockerFixture,
1121+
enable_features: EnableFeaturesFixture,
11241122
) -> None:
11251123
# Given
11261124
now = timezone.now()
@@ -1143,10 +1141,7 @@ def test_charge_for_api_call_count_overages__grace_period_previously_breached__c
11431141
)
11441142

11451143
OrganisationBreachedGracePeriod.objects.create(organisation=organisation)
1146-
get_client_mock = mocker.patch("organisations.tasks.get_client")
1147-
client_mock = MagicMock()
1148-
get_client_mock.return_value = client_mock
1149-
client_mock.get_identity_flags.return_value.is_feature_enabled.return_value = True
1144+
enable_features("api_usage_overage_charges")
11501145

11511146
mock_chargebee_update = mocker.patch(
11521147
"organisations.chargebee.chargebee.chargebee_client.Subscription.update",
@@ -1180,6 +1175,7 @@ def test_charge_for_api_call_count_overages__grace_period_previously_breached__c
11801175
def test_charge_for_api_call_count_overages__uncovered_plan__does_not_charge(
11811176
organisation: Organisation,
11821177
mocker: MockerFixture,
1178+
enable_features: EnableFeaturesFixture,
11831179
) -> None:
11841180
# Given
11851181
now = timezone.now()
@@ -1204,10 +1200,7 @@ def test_charge_for_api_call_count_overages__uncovered_plan__does_not_charge(
12041200
notified_at=now,
12051201
)
12061202

1207-
get_client_mock = mocker.patch("organisations.tasks.get_client")
1208-
client_mock = MagicMock()
1209-
get_client_mock.return_value = client_mock
1210-
client_mock.get_identity_flags.return_value.is_feature_enabled.return_value = True
1203+
enable_features("api_usage_overage_charges")
12111204

12121205
mocker.patch(
12131206
"organisations.chargebee.chargebee.chargebee_client.Subscription.retrieve",
@@ -1236,6 +1229,7 @@ def test_charge_for_api_call_count_overages__uncovered_plan__does_not_charge(
12361229
def test_charge_for_api_call_count_overages__usage_under_api_limit__does_not_charge(
12371230
organisation: Organisation,
12381231
mocker: MockerFixture,
1232+
enable_features: EnableFeaturesFixture,
12391233
) -> None:
12401234
# Given
12411235
now = timezone.now()
@@ -1257,10 +1251,7 @@ def test_charge_for_api_call_count_overages__usage_under_api_limit__does_not_cha
12571251
notified_at=now,
12581252
)
12591253

1260-
get_client_mock = mocker.patch("organisations.tasks.get_client")
1261-
client_mock = MagicMock()
1262-
get_client_mock.return_value = client_mock
1263-
client_mock.get_identity_flags.return_value.is_feature_enabled.return_value = True
1254+
enable_features("api_usage_overage_charges")
12641255

12651256
mocker.patch(
12661257
"organisations.chargebee.chargebee.chargebee_client.Subscription.retrieve",
@@ -1289,6 +1280,7 @@ def test_charge_for_api_call_count_overages__usage_under_api_limit__does_not_cha
12891280
def test_charge_for_api_call_count_overages__startup_plan__charges_correct_addon_quantity(
12901281
organisation: Organisation,
12911282
mocker: MockerFixture,
1283+
enable_features: EnableFeaturesFixture,
12921284
) -> None:
12931285
# Given
12941286
now = timezone.now()
@@ -1311,10 +1303,7 @@ def test_charge_for_api_call_count_overages__startup_plan__charges_correct_addon
13111303
notified_at=now,
13121304
)
13131305

1314-
get_client_mock = mocker.patch("organisations.tasks.get_client")
1315-
client_mock = MagicMock()
1316-
get_client_mock.return_value = client_mock
1317-
client_mock.get_identity_flags.return_value.is_feature_enabled.return_value = True
1306+
enable_features("api_usage_overage_charges")
13181307
mocker.patch(
13191308
"organisations.chargebee.chargebee.chargebee_client.Subscription.retrieve",
13201309
autospec=True,
@@ -1381,6 +1370,7 @@ def test_charge_for_api_call_count_overages__non_standard_plan__logs_unknown_pla
13811370
organisation: Organisation,
13821371
mocker: MockerFixture,
13831372
inspecting_handler: logging.Handler,
1373+
enable_features: EnableFeaturesFixture,
13841374
) -> None:
13851375
# Given
13861376
now = timezone.now()
@@ -1407,10 +1397,7 @@ def test_charge_for_api_call_count_overages__non_standard_plan__logs_unknown_pla
14071397
notified_at=now,
14081398
)
14091399

1410-
get_client_mock = mocker.patch("organisations.tasks.get_client")
1411-
client_mock = MagicMock()
1412-
get_client_mock.return_value = client_mock
1413-
client_mock.get_identity_flags.return_value.is_feature_enabled.return_value = True
1400+
enable_features("api_usage_overage_charges")
14141401
mocker.patch(
14151402
"organisations.chargebee.chargebee.chargebee_client.Subscription.retrieve",
14161403
autospec=True,
@@ -1444,6 +1431,7 @@ def test_charge_for_api_call_count_overages__billing_exception__logs_error_and_d
14441431
organisation: Organisation,
14451432
mocker: MockerFixture,
14461433
inspecting_handler: logging.Handler,
1434+
enable_features: EnableFeaturesFixture,
14471435
) -> None:
14481436
# Given
14491437
now = timezone.now()
@@ -1469,10 +1457,7 @@ def test_charge_for_api_call_count_overages__billing_exception__logs_error_and_d
14691457
notified_at=now,
14701458
)
14711459

1472-
get_client_mock = mocker.patch("organisations.tasks.get_client")
1473-
client_mock = MagicMock()
1474-
get_client_mock.return_value = client_mock
1475-
client_mock.get_identity_flags.return_value.is_feature_enabled.return_value = True
1460+
enable_features("api_usage_overage_charges")
14761461
mocker.patch(
14771462
"organisations.chargebee.chargebee.chargebee_client.Subscription.retrieve",
14781463
autospec=True,
@@ -1506,6 +1491,7 @@ def test_charge_for_api_call_count_overages__billing_exception__logs_error_and_d
15061491
def test_charge_for_api_call_count_overages__startup_plan_with_existing_billing__charges_remaining_overage(
15071492
organisation: Organisation,
15081493
mocker: MockerFixture,
1494+
enable_features: EnableFeaturesFixture,
15091495
) -> None:
15101496
# Given
15111497
now = timezone.now()
@@ -1534,10 +1520,7 @@ def test_charge_for_api_call_count_overages__startup_plan_with_existing_billing_
15341520
billed_at=now,
15351521
)
15361522

1537-
get_client_mock = mocker.patch("organisations.tasks.get_client")
1538-
client_mock = MagicMock()
1539-
get_client_mock.return_value = client_mock
1540-
client_mock.get_identity_flags.return_value.is_feature_enabled.return_value = True
1523+
enable_features("api_usage_overage_charges")
15411524
mocker.patch(
15421525
"organisations.chargebee.chargebee.chargebee_client.Subscription.retrieve",
15431526
autospec=True,
@@ -1681,12 +1664,13 @@ def test_restrict_use_due_to_api_limit_grace_period_over__multiple_organisations
16811664
mailoutbox: list[EmailMultiAlternatives],
16821665
admin_user: FFAdminUser,
16831666
staff_user: FFAdminUser,
1667+
enable_features: EnableFeaturesFixture,
16841668
) -> None:
16851669
# Given
1686-
get_client_mock = mocker.patch("organisations.tasks.get_client")
1687-
client_mock = MagicMock()
1688-
get_client_mock.return_value = client_mock
1689-
client_mock.get_identity_flags.return_value.is_feature_enabled.return_value = True
1670+
enable_features(
1671+
"api_limiting_stop_serving_flags",
1672+
"api_limiting_block_access_to_admin",
1673+
)
16901674

16911675
now = timezone.now()
16921676
organisation2 = Organisation.objects.create(name="Org #2")
@@ -1821,33 +1805,6 @@ def test_restrict_use_due_to_api_limit_grace_period_over__multiple_organisations
18211805
assert organisation6.block_access_to_admin is True
18221806
assert organisation6.api_limit_access_block
18231807

1824-
client_mock.get_identity_flags.call_args_list == [
1825-
call(
1826-
f"org.{organisation.id}",
1827-
traits={
1828-
"organisation.id": organisation.id,
1829-
"organisation.name": organisation.name,
1830-
"subscription.plan": organisation.subscription.plan,
1831-
},
1832-
),
1833-
call(
1834-
f"org.{organisation2.id}",
1835-
traits={
1836-
"organisation.id": organisation2.id,
1837-
"organisation.name": organisation2.name,
1838-
"subscription.plan": organisation2.subscription.plan,
1839-
},
1840-
),
1841-
call(
1842-
f"org.{organisation6.id}",
1843-
traits={
1844-
"organisation.id": organisation6.id,
1845-
"organisation.name": organisation6.name,
1846-
"subscription.plan": organisation6.subscription.plan,
1847-
},
1848-
),
1849-
]
1850-
18511808
assert len(mailoutbox) == 3
18521809
email1 = mailoutbox[0]
18531810
assert email1.subject == "Flagsmith API use has been blocked due to overuse"
@@ -1921,12 +1878,13 @@ def test_restrict_use_due_to_api_limit_grace_period_over__previously_breached__b
19211878
mailoutbox: list[EmailMultiAlternatives],
19221879
admin_user: FFAdminUser,
19231880
staff_user: FFAdminUser,
1881+
enable_features: EnableFeaturesFixture,
19241882
) -> None:
19251883
# Given
1926-
get_client_mock = mocker.patch("organisations.tasks.get_client")
1927-
client_mock = MagicMock()
1928-
get_client_mock.return_value = client_mock
1929-
client_mock.get_identity_flags.return_value.is_feature_enabled.return_value = True
1884+
enable_features(
1885+
"api_limiting_stop_serving_flags",
1886+
"api_limiting_block_access_to_admin",
1887+
)
19301888

19311889
now = timezone.now()
19321890

@@ -1977,14 +1935,15 @@ def test_restrict_use_due_to_api_limit_grace_period_over__missing_subscription_c
19771935
organisation: Organisation,
19781936
freezer: FrozenDateTimeFactory,
19791937
mailoutbox: list[EmailMultiAlternatives],
1938+
enable_features: EnableFeaturesFixture,
19801939
) -> None:
19811940
# Given
19821941
assert not organisation.has_subscription_information_cache()
19831942

1984-
get_client_mock = mocker.patch("organisations.tasks.get_client")
1985-
client_mock = MagicMock()
1986-
get_client_mock.return_value = client_mock
1987-
client_mock.get_identity_flags.return_value.is_feature_enabled.return_value = True
1943+
enable_features(
1944+
"api_limiting_stop_serving_flags",
1945+
"api_limiting_block_access_to_admin",
1946+
)
19881947

19891948
now = timezone.now()
19901949
organisation.subscription.subscription_id = "fancy_sub_id23"
@@ -2020,6 +1979,7 @@ def test_restrict_use_due_to_api_limit_grace_period_over__reduced_api_usage__doe
20201979
freezer: FrozenDateTimeFactory,
20211980
mailoutbox: list[EmailMultiAlternatives],
20221981
inspecting_handler: logging.Handler,
1982+
enable_features: EnableFeaturesFixture,
20231983
) -> None:
20241984
# Given
20251985
assert not organisation.has_subscription_information_cache()
@@ -2028,10 +1988,10 @@ def test_restrict_use_due_to_api_limit_grace_period_over__reduced_api_usage__doe
20281988

20291989
logger.addHandler(inspecting_handler)
20301990

2031-
get_client_mock = mocker.patch("organisations.tasks.get_client")
2032-
client_mock = MagicMock()
2033-
get_client_mock.return_value = client_mock
2034-
client_mock.get_identity_flags.return_value.is_feature_enabled.return_value = True
1991+
enable_features(
1992+
"api_limiting_stop_serving_flags",
1993+
"api_limiting_block_access_to_admin",
1994+
)
20351995

20361996
now = timezone.now()
20371997

0 commit comments

Comments
 (0)