support: Create RealmAuditLog when approving sponsorship.

This commit is contained in:
Siddharth Asthana 2020-12-04 15:46:33 +05:30 committed by Tim Abbott
parent 6945ed3587
commit 44c34cb39a
3 changed files with 24 additions and 2 deletions

View File

@ -767,7 +767,7 @@ def update_sponsorship_status(realm: Realm, sponsorship_pending: bool) -> None:
customer.save(update_fields=["sponsorship_pending"])
def approve_sponsorship(realm: Realm, acting_user: Optional[UserProfile]) -> None:
def approve_sponsorship(realm: Realm, *, acting_user: Optional[UserProfile]) -> None:
from zerver.lib.actions import do_change_plan_type, internal_send_private_message
do_change_plan_type(realm, Realm.STANDARD_FREE, acting_user=acting_user)
@ -775,6 +775,12 @@ def approve_sponsorship(realm: Realm, acting_user: Optional[UserProfile]) -> Non
if customer is not None and customer.sponsorship_pending:
customer.sponsorship_pending = False
customer.save(update_fields=["sponsorship_pending"])
RealmAuditLog.objects.create(
realm=realm,
acting_user=acting_user,
event_type=RealmAuditLog.REALM_SPONSORSHIP_APPROVED,
event_time=timezone_now(),
)
notification_bot = get_system_bot(settings.NOTIFICATION_BOT)
for billing_admin in realm.get_human_billing_admin_users():
with override_language(billing_admin.default_language):

View File

@ -25,6 +25,7 @@ from corporate.lib.stripe import (
InvalidBillingSchedule,
StripeCardError,
add_months,
approve_sponsorship,
attach_discount_to_realm,
catch_stripe_errors,
compute_plan_parameters,
@ -65,7 +66,7 @@ from zerver.lib.actions import (
from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.test_helpers import reset_emails_in_zulip_realm
from zerver.lib.timestamp import datetime_to_timestamp, timestamp_to_datetime
from zerver.models import Realm, RealmAuditLog, UserProfile, get_realm
from zerver.models import Message, Realm, RealmAuditLog, Recipient, UserProfile, get_realm
CallableT = TypeVar("CallableT", bound=Callable[..., Any])
@ -1653,6 +1654,20 @@ class StripeTest(StripeTestCase):
self.assertEqual(realm_audit_log.extra_data, expected_extra_data)
self.assertEqual(realm_audit_log.acting_user, user)
def test_approve_sponsorship(self) -> None:
user = self.example_user("hamlet")
approve_sponsorship(user.realm, acting_user=user)
realm = get_realm("zulip")
self.assertEqual(realm.plan_type, Realm.STANDARD_FREE)
expected_message = "Your organization's request for sponsored hosting has been approved! :tada:.\nYou have been upgraded to Zulip Cloud Standard, free of charge."
sender = UserProfile.objects.filter(email=settings.NOTIFICATION_BOT).first()
recipient_id = UserProfile.objects.filter(email="desdemona@zulip.com").first().recipient_id
message = Message.objects.filter(sender=sender.id).first()
self.assertEqual(message.content, expected_message)
self.assertEqual(message.recipient.type, Recipient.PERSONAL)
self.assertEqual(message.recipient_id, recipient_id)
def test_get_discount_for_realm(self) -> None:
user = self.example_user("hamlet")
self.assertEqual(get_discount_for_realm(user.realm), None)

View File

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