Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
realm: Create RealmAuditLog entry when adding realm playgrounds.
Sahil Batradea33890454e
diff --git a/zerver/lib/actions.py b/zerver/lib/actions.pyindex 635b4e588d0..2e6cd631c8d 100644--- a/zerver/lib/actions.py+++ b/zerver/lib/actions.py@@ -8104,7 +8104,9 @@ def notify_realm_playgrounds(send_event(realm, event, active_user_ids(realm.id))-def do_add_realm_playground(realm: Realm, **kwargs: Any) -> int:+def do_add_realm_playground(+ realm: Realm, *, acting_user: Optional[UserProfile], **kwargs: Any+) -> int:realm_playground = RealmPlayground(realm=realm, **kwargs)# We expect full_clean to always pass since a thorough input validation# is performed in the view (using check_url, check_pygments_language, etc)@@ -8112,8 +8114,24 @@ def do_add_realm_playground(realm: Realm, **kwargs: Any) -> int:realm_playground.full_clean()realm_playground.save()realm_playgrounds = get_realm_playgrounds(realm)+ RealmAuditLog.objects.create(+ realm=realm,+ acting_user=acting_user,+ event_type=RealmAuditLog.REALM_PLAYGROUND_ADDED,+ event_time=timezone_now(),+ extra_data=orjson.dumps(+ {+ "realm_playgrounds": realm_playgrounds,+ "added_playground": {+ "id": realm_playground.id,+ "name": realm_playground.name,+ "pygments_language": realm_playground.pygments_language,+ "url_prefix": realm_playground.url_prefix,+ },+ }+ ).decode(),+ )notify_realm_playgrounds(realm, realm_playgrounds)-return realm_playground.iddiff --git a/zerver/models.py b/zerver/models.pyindex 3d6a107750e..62e70f05e52 100644--- a/zerver/models.py+++ b/zerver/models.py@@ -4106,6 +4106,7 @@ class AbstractRealmAuditLog(models.Model):REALM_DOMAIN_ADDED = 218REALM_DOMAIN_CHANGED = 219REALM_DOMAIN_REMOVED = 220+ REALM_PLAYGROUND_ADDED = 221SUBSCRIPTION_CREATED = 301SUBSCRIPTION_ACTIVATED = 302diff --git a/zerver/openapi/curl_param_value_generators.py b/zerver/openapi/curl_param_value_generators.pyindex d98cab6d96f..ad05c934c3c 100644--- a/zerver/openapi/curl_param_value_generators.py+++ b/zerver/openapi/curl_param_value_generators.py@@ -302,7 +302,7 @@ def remove_realm_playground() -> Dict[str, object]:pygments_language="Python",url_prefix="https://python.example.com",)- playground_id = do_add_realm_playground(get_realm("zulip"), **playground_info)+ playground_id = do_add_realm_playground(get_realm("zulip"), acting_user=None, **playground_info)return {"playground_id": playground_id,}diff --git a/zerver/tests/test_audit_log.py b/zerver/tests/test_audit_log.pyindex 85230c80de1..439d2c73a0a 100644--- a/zerver/tests/test_audit_log.py+++ b/zerver/tests/test_audit_log.py@@ -11,6 +11,7 @@ from zerver.lib.actions import (bulk_remove_subscriptions,do_activate_mirror_dummy_user,do_add_realm_domain,+ do_add_realm_playground,do_change_avatar_fields,do_change_bot_owner,do_change_default_all_public_streams,@@ -51,6 +52,7 @@ from zerver.models import (UserProfile,get_realm,get_realm_domains,+ get_realm_playgrounds,get_stream,)@@ -729,3 +731,35 @@ class TestRealmAuditLog(ZulipTestCase):).count(),1,)++ def test_realm_playground_entries(self) -> None:+ user = self.example_user("iago")+ intial_playgrounds = get_realm_playgrounds(user.realm)+ now = timezone_now()+ playground_id = do_add_realm_playground(+ user.realm,+ acting_user=user,+ name="Python playground",+ pygments_language="Python",+ url_prefix="https://python.example.com",+ )+ added_playground: Dict[str, Union[int, str]] = {+ "id": playground_id,+ "name": "Python playground",+ "pygments_language": "Python",+ "url_prefix": "https://python.example.com",+ }+ expected_extra_data = {+ "realm_playgrounds": intial_playgrounds + [added_playground],+ "added_playground": added_playground,+ }+ self.assertEqual(+ RealmAuditLog.objects.filter(+ realm=user.realm,+ event_type=RealmAuditLog.REALM_PLAYGROUND_ADDED,+ 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 a12bc3eeb87..91634a06ffd 100644--- a/zerver/tests/test_events.py+++ b/zerver/tests/test_events.py@@ -1761,7 +1761,9 @@ class NormalActionsTest(BaseAction):url_prefix="https://python.example.com",)events = self.verify_action(- lambda: do_add_realm_playground(self.user_profile.realm, **playground_info)+ lambda: do_add_realm_playground(+ self.user_profile.realm, acting_user=None, **playground_info+ ))check_realm_playgrounds("events[0]", events[0])diff --git a/zerver/tests/test_realm_playgrounds.py b/zerver/tests/test_realm_playgrounds.pyindex c8c4e0408db..6fcab68471b 100644--- a/zerver/tests/test_realm_playgrounds.py+++ b/zerver/tests/test_realm_playgrounds.py@@ -99,7 +99,7 @@ class RealmPlaygroundTests(ZulipTestCase):pygments_language="Python",url_prefix="https://python.example.com",)- playground_id = do_add_realm_playground(realm, **playground_info)+ playground_id = do_add_realm_playground(realm, acting_user=iago, **playground_info)self.assertTrue(RealmPlayground.objects.filter(name="Python playground").exists())result = self.api_delete(iago, f"/json/realm/playgrounds/{playground_id + 1}")diff --git a/zerver/views/realm_playgrounds.py b/zerver/views/realm_playgrounds.pyindex 3d25da3efca..842ffbd15f0 100644--- a/zerver/views/realm_playgrounds.py+++ b/zerver/views/realm_playgrounds.py@@ -46,6 +46,7 @@ def add_realm_playground(try:playground_id = do_add_realm_playground(realm=user_profile.realm,+ acting_user=user_profile,name=name.strip(),pygments_language=pygments_language.strip(),url_prefix=url_prefix.strip(),