-
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.
feat: Remove all but first admin when subscription has reached cancel…
…lation date (#2965)
- Loading branch information
Showing
6 changed files
with
159 additions
and
7 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
from organisations.models import ( | ||
TRIAL_SUBSCRIPTION_ID, | ||
Organisation, | ||
OrganisationRole, | ||
OrganisationSubscriptionInformationCache, | ||
Subscription, | ||
) | ||
|
@@ -24,6 +25,7 @@ | |
) | ||
from organisations.subscriptions.metadata import BaseSubscriptionMetadata | ||
from organisations.subscriptions.xero.metadata import XeroSubscriptionMetadata | ||
from users.models import FFAdminUser | ||
|
||
|
||
@pytest.mark.django_db | ||
|
@@ -64,7 +66,8 @@ def test_cancel_subscription_cancels_chargebee_subscription( | |
): | ||
# Given | ||
organisation = Organisation.objects.create(name="Test org") | ||
|
||
user = FFAdminUser.objects.create(email="[email protected]") | ||
user.add_organisation(organisation, role=OrganisationRole.ADMIN) | ||
Subscription.objects.filter(organisation=organisation).update( | ||
subscription_id="subscription_id", payment_method=CHARGEBEE | ||
) | ||
|
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
74 changes: 74 additions & 0 deletions
74
api/tests/unit/organisations/test_unit_organisation_tasks.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,74 @@ | ||
import uuid | ||
from datetime import timedelta | ||
|
||
from django.utils import timezone | ||
|
||
from organisations.models import ( | ||
Organisation, | ||
OrganisationRole, | ||
UserOrganisation, | ||
) | ||
from organisations.tasks import finish_subscription_cancellation | ||
from users.models import FFAdminUser | ||
|
||
|
||
def test_finish_subscription_cancellation(db: None): | ||
organisation1 = Organisation.objects.create() | ||
organisation2 = Organisation.objects.create() | ||
organisation3 = Organisation.objects.create() | ||
organisation4 = Organisation.objects.create() | ||
|
||
# Far future cancellation will be unaffected. | ||
organisation_user_count = 3 | ||
for __ in range(organisation_user_count): | ||
UserOrganisation.objects.create( | ||
organisation=organisation1, | ||
user=FFAdminUser.objects.create(email=f"{uuid.uuid4()}@example.com"), | ||
role=OrganisationRole.ADMIN, | ||
) | ||
future = timezone.now() + timedelta(days=20) | ||
organisation1.subscription.cancel(cancellation_date=future) | ||
|
||
# Two organisations are impacted. | ||
for __ in range(organisation_user_count): | ||
UserOrganisation.objects.create( | ||
organisation=organisation2, | ||
user=FFAdminUser.objects.create(email=f"{uuid.uuid4()}@example.com"), | ||
role=OrganisationRole.ADMIN, | ||
) | ||
|
||
organisation2.subscription.cancel( | ||
cancellation_date=timezone.now() - timedelta(hours=2) | ||
) | ||
|
||
for __ in range(organisation_user_count): | ||
UserOrganisation.objects.create( | ||
organisation=organisation3, | ||
user=FFAdminUser.objects.create(email=f"{uuid.uuid4()}@example.com"), | ||
role=OrganisationRole.ADMIN, | ||
) | ||
organisation3.subscription.cancel( | ||
cancellation_date=timezone.now() - timedelta(hours=4) | ||
) | ||
|
||
# Remaining organisation4 has not canceled, should be left unaffected. | ||
for __ in range(organisation_user_count): | ||
UserOrganisation.objects.create( | ||
organisation=organisation4, | ||
user=FFAdminUser.objects.create(email=f"{uuid.uuid4()}@example.com"), | ||
role=OrganisationRole.ADMIN, | ||
) | ||
|
||
# When | ||
finish_subscription_cancellation() | ||
|
||
# Then | ||
organisation1.refresh_from_db() | ||
organisation2.refresh_from_db() | ||
organisation3.refresh_from_db() | ||
organisation4.refresh_from_db() | ||
|
||
assert organisation1.num_seats == organisation_user_count | ||
assert organisation2.num_seats == 1 | ||
assert organisation3.num_seats == 1 | ||
assert organisation4.num_seats == organisation_user_count |
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 |
---|---|---|
|
@@ -1172,7 +1172,7 @@ def test_make_user_group_admin_success( | |
|
||
|
||
def test_make_user_group_admin_forbidden( | ||
staff_client: FFAdminUser, | ||
staff_client: APIClient, | ||
organisation: Organisation, | ||
user_permission_group: UserPermissionGroup, | ||
): | ||
|
@@ -1238,7 +1238,7 @@ def test_remove_user_as_group_admin_success( | |
|
||
|
||
def test_remove_user_as_group_admin_forbidden( | ||
staff_client: FFAdminUser, | ||
staff_client: APIClient, | ||
organisation: Organisation, | ||
user_permission_group: UserPermissionGroup, | ||
): | ||
|
@@ -1340,3 +1340,39 @@ def test_list_my_groups(organisation, api_client): | |
"id": user_permission_group_1.id, | ||
"name": user_permission_group_1.name, | ||
} | ||
|
||
|
||
def test_when_subscription_is_cancelled_then_remove_all_but_the_first_user( | ||
staff_client: APIClient, | ||
subscription: Subscription, | ||
organisation: Organisation, | ||
): | ||
# Given | ||
cancellation_date = datetime.now(tz=UTC) | ||
data = { | ||
"content": { | ||
"subscription": { | ||
"status": "cancelled", | ||
"id": subscription.subscription_id, | ||
"current_term_end": datetime.timestamp(cancellation_date), | ||
}, | ||
"customer": { | ||
"email": "[email protected]", | ||
}, | ||
} | ||
} | ||
|
||
url = reverse("api-v1:chargebee-webhook") | ||
assert organisation.num_seats == 2 | ||
|
||
# When | ||
response = staff_client.post( | ||
url, data=json.dumps(data), content_type="application/json" | ||
) | ||
# Then | ||
assert response.status_code == 200 | ||
|
||
subscription.refresh_from_db() | ||
assert subscription.cancellation_date == cancellation_date | ||
organisation.refresh_from_db() | ||
assert organisation.num_seats == 1 |
6976f81
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
flagsmith-frontend-staging – ./frontend
flagsmith-frontend-staging-git-main-flagsmith.vercel.app
flagsmith-staging-frontend.vercel.app
staging.flagsmith.com
flagsmith-frontend-staging-flagsmith.vercel.app
6976f81
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
docs – ./docs
docs-git-main-flagsmith.vercel.app
docs.flagsmith.com
docs.bullet-train.io
docs-flagsmith.vercel.app
6976f81
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
flagsmith-frontend-preview – ./frontend
flagsmith-frontend-preview-flagsmith.vercel.app
flagsmith-frontend-production-native.vercel.app
flagsmith-frontend-preview-git-main-flagsmith.vercel.app