-
Notifications
You must be signed in to change notification settings - Fork 429
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Set grace period to a singular event (#4455)
- Loading branch information
Showing
4 changed files
with
123 additions
and
6 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
api/organisations/migrations/0056_create_organisation_breached_grace_period.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Generated by Django 3.2.25 on 2024-08-06 17:46 | ||
|
||
import django.db.models.deletion | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("organisations", "0055_alter_percent_usage"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="OrganisationBreachedGracePeriod", | ||
fields=[ | ||
( | ||
"id", | ||
models.AutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
("created_at", models.DateTimeField(auto_now_add=True, null=True)), | ||
("updated_at", models.DateTimeField(auto_now=True, null=True)), | ||
( | ||
"organisation", | ||
models.OneToOneField( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="breached_grace_period", | ||
to="organisations.organisation", | ||
), | ||
), | ||
], | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
Organisation, | ||
OrganisationAPIBilling, | ||
OrganisationAPIUsageNotification, | ||
OrganisationBreachedGracePeriod, | ||
OrganisationRole, | ||
OrganisationSubscriptionInformationCache, | ||
UserOrganisation, | ||
|
@@ -1410,6 +1411,64 @@ def test_restrict_use_due_to_api_limit_grace_period_over( | |
assert getattr(organisation, "api_limit_access_block", None) is None | ||
|
||
|
||
@pytest.mark.freeze_time("2023-01-19T09:09:47.325132+00:00") | ||
def test_restrict_use_due_to_api_limit_grace_period_breached( | ||
mocker: MockerFixture, | ||
organisation: Organisation, | ||
freezer: FrozenDateTimeFactory, | ||
mailoutbox: list[EmailMultiAlternatives], | ||
admin_user: FFAdminUser, | ||
staff_user: FFAdminUser, | ||
) -> None: | ||
# Given | ||
get_client_mock = mocker.patch("organisations.tasks.get_client") | ||
client_mock = MagicMock() | ||
get_client_mock.return_value = client_mock | ||
client_mock.get_identity_flags.return_value.is_feature_enabled.return_value = True | ||
|
||
now = timezone.now() | ||
|
||
OrganisationBreachedGracePeriod.objects.create(organisation=organisation) | ||
OrganisationSubscriptionInformationCache.objects.create( | ||
organisation=organisation, | ||
allowed_seats=10, | ||
allowed_projects=3, | ||
allowed_30d_api_calls=10_000, | ||
chargebee_email="[email protected]", | ||
) | ||
organisation.subscription.subscription_id = "fancy_sub_id23" | ||
organisation.subscription.plan = FREE_PLAN_ID | ||
organisation.subscription.save() | ||
|
||
mock_api_usage = mocker.patch( | ||
"organisations.tasks.get_current_api_usage", | ||
) | ||
mock_api_usage.return_value = 12_005 | ||
|
||
OrganisationAPIUsageNotification.objects.create( | ||
notified_at=now, | ||
organisation=organisation, | ||
percent_usage=100, | ||
) | ||
OrganisationAPIUsageNotification.objects.create( | ||
notified_at=now, | ||
organisation=organisation, | ||
percent_usage=120, | ||
) | ||
now = now + timedelta(days=API_USAGE_GRACE_PERIOD - 1) | ||
freezer.move_to(now) | ||
|
||
# When | ||
restrict_use_due_to_api_limit_grace_period_over() | ||
|
||
# Then | ||
organisation.refresh_from_db() | ||
|
||
assert organisation.stop_serving_flags is True | ||
assert organisation.block_access_to_admin is True | ||
assert organisation.api_limit_access_block | ||
|
||
|
||
@pytest.mark.freeze_time("2023-01-19T09:09:47.325132+00:00") | ||
def test_restrict_use_due_to_api_limit_grace_period_over_missing_subscription_information_cache( | ||
mocker: MockerFixture, | ||
|