corporate: Create license ledger for automanaged plan migrations.

If we move a paid plan from a remote server to a remote realm, and
the plan has automated license management, then we create an updated
license ledger entry when we move the plan for the remote realm
billing data so that we have an accurate user count for licenses
when the plan is next invoiced.
This commit is contained in:
Lauryn Menard
2024-12-31 15:13:41 +01:00
committed by Tim Abbott
parent c01787f41a
commit e65f3cf657
2 changed files with 49 additions and 3 deletions

View File

@@ -17,6 +17,7 @@ from corporate.lib.remote_billing_util import (
from corporate.lib.stripe import RemoteRealmBillingSession, RemoteServerBillingSession, add_months
from corporate.models import (
CustomerPlan,
LicenseLedger,
get_current_plan_by_customer,
get_customer_by_remote_realm,
get_customer_by_remote_server,
@@ -1019,6 +1020,15 @@ class RemoteBillingAuthenticationTest(RemoteRealmBillingTestCase):
billing_schedule=CustomerPlan.BILLING_SCHEDULE_ANNUAL,
tier=CustomerPlan.TIER_SELF_HOSTED_BUSINESS,
status=CustomerPlan.ACTIVE,
automanage_licenses=True,
)
initial_license_count = 100
LicenseLedger.objects.create(
plan=server_plan,
is_renewal=True,
event_time=timezone_now(),
licenses=initial_license_count,
licenses_at_next_renewal=initial_license_count,
)
self.server.plan_type = RemoteZulipServer.PLAN_TYPE_BUSINESS
self.server.save(update_fields=["plan_type"])
@@ -1096,6 +1106,16 @@ class RemoteBillingAuthenticationTest(RemoteRealmBillingTestCase):
self.assertEqual(plan.tier, CustomerPlan.TIER_SELF_HOSTED_BUSINESS)
self.assertEqual(plan.status, CustomerPlan.ACTIVE)
# Check that an updated license ledger entry was created.
billing_session = RemoteRealmBillingSession(remote_realm=remote_realm_with_plan)
license_ledger = billing_session.get_last_ledger_for_automanaged_plan_if_exists()
billable_licenses = billing_session.get_billable_licenses_for_customer(customer, plan.tier)
assert license_ledger is not None
self.assertNotEqual(initial_license_count, billable_licenses)
self.assertEqual(license_ledger.licenses, initial_license_count)
self.assertEqual(license_ledger.licenses_at_next_renewal, billable_licenses)
self.assertFalse(license_ledger.is_renewal)
@responses.activate
def test_transfer_plan_from_server_to_realm_edge_cases(self) -> None:
self.login("desdemona")