From 233c4d520c00117b54ada0a8f42accf51591925f Mon Sep 17 00:00:00 2001 From: Siddharth Asthana Date: Fri, 4 Dec 2020 15:59:02 +0530 Subject: [PATCH] support: Create RealmAuditLog when updating billing_method. This commit also makes acting_user as a mandantory argument and fixes the tests accordingly. --- analytics/tests/test_views.py | 4 ++-- analytics/views.py | 8 ++++++-- corporate/lib/stripe.py | 13 ++++++++++++- corporate/tests/test_stripe.py | 17 +++++++++++++++-- zerver/models.py | 1 + 5 files changed, 36 insertions(+), 7 deletions(-) diff --git a/analytics/tests/test_views.py b/analytics/tests/test_views.py index 858ec8f166..c562910be1 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.py index fb43b671b9..d6d4fa7f7e 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.py index 405eef18a9..c157936f8a 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_automatically plan.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.py index 8cf6078097..0ea0f33aca 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.py index e0cba1ecf4..14be9ee059 100644 --- a/zerver/models.py +++ b/zerver/models.py @@ -3189,6 +3189,7 @@ class AbstractRealmAuditLog(models.Model): REALM_ICON_SOURCE_CHANGED = 208 REALM_DISCOUNT_CHANGED = 209 REALM_SPONSORSHIP_APPROVED = 210 + REALM_BILLING_METHOD_CHANGED = 211 SUBSCRIPTION_CREATED = 301 SUBSCRIPTION_ACTIVATED = 302