Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
realm: Create RealmAuditLog entry when removing realm playgrounds.
Sahil Batrab86b9bdc0236
diff --git a/zerver/lib/actions.py b/zerver/lib/actions.pyindex 2e6cd631c8d..7c76d176cfc 100644--- a/zerver/lib/actions.py+++ b/zerver/lib/actions.py@@ -8135,9 +8135,31 @@ def do_add_realm_playground(return realm_playground.id-def do_remove_realm_playground(realm: Realm, realm_playground: RealmPlayground) -> None:+def do_remove_realm_playground(+ realm: Realm, realm_playground: RealmPlayground, *, acting_user: Optional[UserProfile]+) -> None:+ removed_playground = {+ "name": realm_playground.name,+ "pygments_language": realm_playground.pygments_language,+ "url_prefix": realm_playground.url_prefix,+ }+realm_playground.delete()realm_playgrounds = get_realm_playgrounds(realm)++ RealmAuditLog.objects.create(+ realm=realm,+ acting_user=acting_user,+ event_type=RealmAuditLog.REALM_PLAYGROUND_REMOVED,+ event_time=timezone_now(),+ extra_data=orjson.dumps(+ {+ "realm_playgrounds": realm_playgrounds,+ "removed_playground": removed_playground,+ }+ ).decode(),+ )+notify_realm_playgrounds(realm, realm_playgrounds)diff --git a/zerver/models.py b/zerver/models.pyindex 62e70f05e52..1d1527da5c7 100644--- a/zerver/models.py+++ b/zerver/models.py@@ -4107,6 +4107,7 @@ class AbstractRealmAuditLog(models.Model):REALM_DOMAIN_CHANGED = 219REALM_DOMAIN_REMOVED = 220REALM_PLAYGROUND_ADDED = 221+ REALM_PLAYGROUND_REMOVED = 222SUBSCRIPTION_CREATED = 301SUBSCRIPTION_ACTIVATED = 302diff --git a/zerver/tests/test_audit_log.py b/zerver/tests/test_audit_log.pyindex 439d2c73a0a..d9b98b90844 100644--- a/zerver/tests/test_audit_log.py+++ b/zerver/tests/test_audit_log.py@@ -33,6 +33,7 @@ from zerver.lib.actions import (do_reactivate_user,do_regenerate_api_key,do_remove_realm_domain,+ do_remove_realm_playground,do_rename_stream,do_set_realm_authentication_methods,do_set_realm_message_editing,@@ -47,6 +48,7 @@ from zerver.models import (Message,Realm,RealmAuditLog,+ RealmPlayground,Recipient,Subscription,UserProfile,@@ -763,3 +765,30 @@ class TestRealmAuditLog(ZulipTestCase):).count(),1,)++ now = timezone_now()+ realm_playground = RealmPlayground.objects.get(id=playground_id)+ do_remove_realm_playground(+ user.realm,+ realm_playground,+ acting_user=user,+ )+ removed_playground = {+ "name": "Python playground",+ "pygments_language": "Python",+ "url_prefix": "https://python.example.com",+ }+ expected_extra_data = {+ "realm_playgrounds": intial_playgrounds,+ "removed_playground": removed_playground,+ }+ self.assertEqual(+ RealmAuditLog.objects.filter(+ realm=user.realm,+ event_type=RealmAuditLog.REALM_PLAYGROUND_REMOVED,+ event_time__gte=now,+ acting_user=user,+ extra_data=orjson.dumps(expected_extra_data).decode(),+ ).count(),+ 1,+ )diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.pyindex 91634a06ffd..ada9a2ec482 100644--- a/zerver/tests/test_events.py+++ b/zerver/tests/test_events.py@@ -1772,7 +1772,9 @@ class NormalActionsTest(BaseAction):last_id = last_realm_playground.idrealm_playground = access_playground_by_id(self.user_profile.realm, last_id)events = self.verify_action(- lambda: do_remove_realm_playground(self.user_profile.realm, realm_playground)+ lambda: do_remove_realm_playground(+ self.user_profile.realm, realm_playground, acting_user=None+ ))check_realm_playgrounds("events[0]", events[0])diff --git a/zerver/views/realm_playgrounds.py b/zerver/views/realm_playgrounds.pyindex 842ffbd15f0..21b83cef409 100644--- a/zerver/views/realm_playgrounds.py+++ b/zerver/views/realm_playgrounds.py@@ -62,5 +62,5 @@ def delete_realm_playground(request: HttpRequest, user_profile: UserProfile, playground_id: int) -> HttpResponse:realm_playground = access_playground_by_id(user_profile.realm, playground_id)- do_remove_realm_playground(user_profile.realm, realm_playground)+ do_remove_realm_playground(user_profile.realm, realm_playground, acting_user=user_profile)return json_success(request)