support: Create RealmAuditLog when updating billing_method.

This commit also makes acting_user as a mandantory argument and fixes
the tests accordingly.
This commit is contained in:
Siddharth Asthana 2020-12-04 15:59:02 +05:30 committed by Tim Abbott
parent 44c34cb39a
commit 233c4d520c
5 changed files with 36 additions and 7 deletions

View File

@ -849,7 +849,7 @@ class TestSupportEndpoint(ZulipTestCase):
"/activity/support", "/activity/support",
{"realm_id": f"{iago.realm_id}", "billing_method": "charge_automatically"}, {"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( self.assert_in_success_response(
["Billing method of zulip updated to charge automatically"], result ["Billing method of zulip updated to charge automatically"], result
) )
@ -859,7 +859,7 @@ class TestSupportEndpoint(ZulipTestCase):
result = self.client_post( result = self.client_post(
"/activity/support", {"realm_id": f"{iago.realm_id}", "billing_method": "send_invoice"} "/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( self.assert_in_success_response(
["Billing method of zulip updated to pay by invoice"], result ["Billing method of zulip updated to pay by invoice"], result
) )

View File

@ -1318,12 +1318,16 @@ def support(request: HttpRequest) -> HttpResponse:
elif request.POST.get("billing_method", None) is not None: elif request.POST.get("billing_method", None) is not None:
billing_method = request.POST.get("billing_method") billing_method = request.POST.get("billing_method")
if billing_method == "send_invoice": 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[ context[
"success_message" "success_message"
] = f"Billing method of {realm.string_id} updated to pay by invoice." ] = f"Billing method of {realm.string_id} updated to pay by invoice."
elif billing_method == "charge_automatically": 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[ context[
"success_message" "success_message"
] = f"Billing method of {realm.string_id} updated to charge automatically." ] = f"Billing method of {realm.string_id} updated to charge automatically."

View File

@ -868,8 +868,19 @@ def void_all_open_invoices(realm: Realm) -> int:
return voided_invoices_count 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) plan = get_current_plan_by_realm(realm)
if plan is not None: if plan is not None:
plan.charge_automatically = charge_automatically plan.charge_automatically = charge_automatically
plan.save(update_fields=["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,
},
)

View File

@ -2391,13 +2391,26 @@ class StripeTest(StripeTestCase):
) )
self.assertEqual(plan.charge_automatically, False) 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() plan.refresh_from_db()
self.assertEqual(plan.charge_automatically, True) 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() plan.refresh_from_db()
self.assertEqual(plan.charge_automatically, False) 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): class RequiresBillingAccessTest(ZulipTestCase):

View File

@ -3189,6 +3189,7 @@ class AbstractRealmAuditLog(models.Model):
REALM_ICON_SOURCE_CHANGED = 208 REALM_ICON_SOURCE_CHANGED = 208
REALM_DISCOUNT_CHANGED = 209 REALM_DISCOUNT_CHANGED = 209
REALM_SPONSORSHIP_APPROVED = 210 REALM_SPONSORSHIP_APPROVED = 210
REALM_BILLING_METHOD_CHANGED = 211
SUBSCRIPTION_CREATED = 301 SUBSCRIPTION_CREATED = 301
SUBSCRIPTION_ACTIVATED = 302 SUBSCRIPTION_ACTIVATED = 302