support: Create RealmAuditLog when updating sponsorship status.

This commit is contained in:
Siddharth Asthana 2020-12-04 16:44:51 +05:30 committed by Tim Abbott
parent ddbc6d7662
commit 80c9243c6a
6 changed files with 32 additions and 5 deletions

View File

@ -941,7 +941,7 @@ class TestSupportEndpoint(ZulipTestCase):
def test_approve_sponsorship(self) -> None:
lear_realm = get_realm("lear")
update_sponsorship_status(lear_realm, True)
update_sponsorship_status(lear_realm, True, acting_user=None)
king_user = self.lear_user("king")
king_user.role = UserProfile.ROLE_REALM_OWNER
king_user.save()

View File

@ -1334,10 +1334,10 @@ def support(request: HttpRequest) -> HttpResponse:
elif request.POST.get("sponsorship_pending", None) is not None:
sponsorship_pending = request.POST.get("sponsorship_pending")
if sponsorship_pending == "true":
update_sponsorship_status(realm, True)
update_sponsorship_status(realm, True, acting_user=request.user)
context["success_message"] = f"{realm.string_id} marked as pending sponsorship."
elif sponsorship_pending == "false":
update_sponsorship_status(realm, False)
update_sponsorship_status(realm, False, acting_user=request.user)
context["success_message"] = f"{realm.string_id} is no longer pending sponsorship."
elif request.POST.get("approve_sponsorship") is not None:
if request.POST.get("approve_sponsorship") == "approve_sponsorship":

View File

@ -761,10 +761,21 @@ def attach_discount_to_realm(
)
def update_sponsorship_status(realm: Realm, sponsorship_pending: bool) -> None:
def update_sponsorship_status(
realm: Realm, sponsorship_pending: bool, *, acting_user: Optional[UserProfile]
) -> None:
customer, _ = Customer.objects.get_or_create(realm=realm)
customer.sponsorship_pending = sponsorship_pending
customer.save(update_fields=["sponsorship_pending"])
RealmAuditLog.objects.create(
realm=realm,
acting_user=acting_user,
event_type=RealmAuditLog.REALM_SPONSORSHIP_PENDING_STATUS_CHANGED,
event_time=timezone_now(),
extra_data={
"sponsorship_pending": sponsorship_pending,
},
)
def approve_sponsorship(realm: Realm, *, acting_user: Optional[UserProfile]) -> None:

View File

@ -44,6 +44,7 @@ from corporate.lib.stripe import (
update_license_ledger_for_automanaged_plan,
update_license_ledger_if_needed,
update_or_create_stripe_customer,
update_sponsorship_status,
void_all_open_invoices,
)
from corporate.models import (
@ -1668,6 +1669,20 @@ class StripeTest(StripeTestCase):
self.assertEqual(message.recipient.type, Recipient.PERSONAL)
self.assertEqual(message.recipient_id, recipient_id)
def test_update_sponsorship_status(self) -> None:
lear = get_realm("lear")
iago = self.example_user("iago")
update_sponsorship_status(lear, True, acting_user=iago)
customer = get_customer_by_realm(realm=lear)
assert customer is not None
self.assertTrue(customer.sponsorship_pending)
realm_audit_log = RealmAuditLog.objects.filter(
event_type=RealmAuditLog.REALM_SPONSORSHIP_PENDING_STATUS_CHANGED
).last()
expected_extra_data = {"sponsorship_pending": True}
self.assertEqual(realm_audit_log.extra_data, str(expected_extra_data))
self.assertEqual(realm_audit_log.acting_user, iago)
def test_get_discount_for_realm(self) -> None:
user = self.example_user("hamlet")
self.assertEqual(get_discount_for_realm(user.realm), None)

View File

@ -265,7 +265,7 @@ def sponsorship(
context=context,
)
update_sponsorship_status(realm, True)
update_sponsorship_status(realm, True, acting_user=user)
user.is_billing_admin = True
user.save(update_fields=["is_billing_admin"])

View File

@ -3191,6 +3191,7 @@ class AbstractRealmAuditLog(models.Model):
REALM_SPONSORSHIP_APPROVED = 210
REALM_BILLING_METHOD_CHANGED = 211
REALM_REACTIVATION_EMAIL_SENT = 212
REALM_SPONSORSHIP_PENDING_STATUS_CHANGED = 213
SUBSCRIPTION_CREATED = 301
SUBSCRIPTION_ACTIVATED = 302