Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
support: Create RealmAuditLog when updating billing_method.
Siddharth Asthana233c4d520c00
diff --git a/analytics/tests/test_views.py b/analytics/tests/test_views.pyindex 858ec8f1664..c562910be14 100644--- a/analytics/tests/test_views.py+++ b/analytics/tests/test_views.py@@ -849,7 +849,7 @@ class TestSupportEndpoint(ZulipTestCase):"/activity/support",{"realm_id": f"{iago.realm_id}", "billing_method": "charge_automatically"},)- m.assert_called_once_with(get_realm("zulip"), charge_automatically=True)+ m.assert_called_once_with(get_realm("zulip"), charge_automatically=True, acting_user=iago)self.assert_in_success_response(["Billing method of zulip updated to charge automatically"], result)@@ -859,7 +859,7 @@ class TestSupportEndpoint(ZulipTestCase):result = self.client_post("/activity/support", {"realm_id": f"{iago.realm_id}", "billing_method": "send_invoice"})- m.assert_called_once_with(get_realm("zulip"), charge_automatically=False)+ m.assert_called_once_with(get_realm("zulip"), charge_automatically=False, acting_user=iago)self.assert_in_success_response(["Billing method of zulip updated to pay by invoice"], result)diff --git a/analytics/views.py b/analytics/views.pyindex fb43b671b95..d6d4fa7f7e6 100644--- a/analytics/views.py+++ b/analytics/views.py@@ -1318,12 +1318,16 @@ def support(request: HttpRequest) -> HttpResponse:elif request.POST.get("billing_method", None) is not None:billing_method = request.POST.get("billing_method")if billing_method == "send_invoice":- update_billing_method_of_current_plan(realm, charge_automatically=False)+ update_billing_method_of_current_plan(+ realm, charge_automatically=False, acting_user=request.user+ )context["success_message"] = f"Billing method of {realm.string_id} updated to pay by invoice."elif billing_method == "charge_automatically":- update_billing_method_of_current_plan(realm, charge_automatically=True)+ update_billing_method_of_current_plan(+ realm, charge_automatically=True, acting_user=request.user+ )context["success_message"] = f"Billing method of {realm.string_id} updated to charge automatically."diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.pyindex 405eef18a94..c157936f8a5 100644--- a/corporate/lib/stripe.py+++ b/corporate/lib/stripe.py@@ -868,8 +868,19 @@ def void_all_open_invoices(realm: Realm) -> int:return voided_invoices_count-def update_billing_method_of_current_plan(realm: Realm, charge_automatically: bool) -> None:+def update_billing_method_of_current_plan(+ realm: Realm, charge_automatically: bool, *, acting_user: Optional[UserProfile]+) -> None:plan = get_current_plan_by_realm(realm)if plan is not None:plan.charge_automatically = charge_automaticallyplan.save(update_fields=["charge_automatically"])+ RealmAuditLog.objects.create(+ realm=realm,+ acting_user=acting_user,+ event_type=RealmAuditLog.REALM_BILLING_METHOD_CHANGED,+ event_time=timezone_now(),+ extra_data={+ "charge_automatically": charge_automatically,+ },+ )diff --git a/corporate/tests/test_stripe.py b/corporate/tests/test_stripe.pyindex 8cf6078097b..0ea0f33aca0 100644--- a/corporate/tests/test_stripe.py+++ b/corporate/tests/test_stripe.py@@ -2391,13 +2391,26 @@ class StripeTest(StripeTestCase):)self.assertEqual(plan.charge_automatically, False)- update_billing_method_of_current_plan(realm, True)+ iago = self.example_user("iago")+ update_billing_method_of_current_plan(realm, True, acting_user=iago)plan.refresh_from_db()self.assertEqual(plan.charge_automatically, True)+ realm_audit_log = RealmAuditLog.objects.filter(+ event_type=RealmAuditLog.REALM_BILLING_METHOD_CHANGED+ ).last()+ expected_extra_data = {"charge_automatically": plan.charge_automatically}+ self.assertEqual(realm_audit_log.acting_user, iago)+ self.assertEqual(realm_audit_log.extra_data, str(expected_extra_data))- update_billing_method_of_current_plan(realm, False)+ update_billing_method_of_current_plan(realm, False, acting_user=iago)plan.refresh_from_db()self.assertEqual(plan.charge_automatically, False)+ realm_audit_log = RealmAuditLog.objects.filter(+ event_type=RealmAuditLog.REALM_BILLING_METHOD_CHANGED+ ).last()+ expected_extra_data = {"charge_automatically": plan.charge_automatically}+ self.assertEqual(realm_audit_log.acting_user, iago)+ self.assertEqual(realm_audit_log.extra_data, str(expected_extra_data))class RequiresBillingAccessTest(ZulipTestCase):diff --git a/zerver/models.py b/zerver/models.pyindex e0cba1ecf4a..14be9ee0599 100644--- a/zerver/models.py+++ b/zerver/models.py@@ -3189,6 +3189,7 @@ class AbstractRealmAuditLog(models.Model):REALM_ICON_SOURCE_CHANGED = 208REALM_DISCOUNT_CHANGED = 209REALM_SPONSORSHIP_APPROVED = 210+ REALM_BILLING_METHOD_CHANGED = 211SUBSCRIPTION_CREATED = 301SUBSCRIPTION_ACTIVATED = 302