diff --git a/analytics/tests/test_views.py b/analytics/tests/test_views.py index cac37f4b03..77808860d9 100644 --- a/analytics/tests/test_views.py +++ b/analytics/tests/test_views.py @@ -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() diff --git a/analytics/views.py b/analytics/views.py index e82fac72ea..d5bd20bd43 100644 --- a/analytics/views.py +++ b/analytics/views.py @@ -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": diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.py index c157936f8a..3dc895d805 100644 --- a/corporate/lib/stripe.py +++ b/corporate/lib/stripe.py @@ -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: diff --git a/corporate/tests/test_stripe.py b/corporate/tests/test_stripe.py index 0ea0f33aca..a4c2d6b3b1 100644 --- a/corporate/tests/test_stripe.py +++ b/corporate/tests/test_stripe.py @@ -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) diff --git a/corporate/views.py b/corporate/views.py index 41235fd5c3..69ef140315 100644 --- a/corporate/views.py +++ b/corporate/views.py @@ -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"]) diff --git a/zerver/models.py b/zerver/models.py index bc1cf53104..764edd3b7a 100644 --- a/zerver/models.py +++ b/zerver/models.py @@ -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