From f97093ba327e92ea3847fa2f5f7ac280d5ccf0ba Mon Sep 17 00:00:00 2001 From: Eeshan Garg Date: Tue, 14 Dec 2021 15:00:45 -0500 Subject: [PATCH] streams: Add RealmAuditLog entries for description changes. --- zerver/lib/actions.py | 22 +++++++++++++++++++--- zerver/models.py | 1 + zerver/tests/test_subs.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index 80815c5e1b..928febc98d 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -5149,9 +5149,25 @@ def do_change_stream_description( stream: Stream, new_description: str, *, acting_user: UserProfile ) -> None: old_description = stream.description - stream.description = new_description - stream.rendered_description = render_stream_description(new_description) - stream.save(update_fields=["description", "rendered_description"]) + + with transaction.atomic(): + stream.description = new_description + stream.rendered_description = render_stream_description(new_description) + stream.save(update_fields=["description", "rendered_description"]) + RealmAuditLog.objects.create( + realm=stream.realm, + acting_user=acting_user, + modified_stream=stream, + event_type=RealmAuditLog.STREAM_PROPERTY_CHANGED, + event_time=timezone_now(), + extra_data=orjson.dumps( + { + RealmAuditLog.OLD_VALUE: old_description, + RealmAuditLog.NEW_VALUE: new_description, + "property": "description", + } + ).decode(), + ) event = dict( type="stream", diff --git a/zerver/models.py b/zerver/models.py index 306684ddb3..12d0f29b2e 100644 --- a/zerver/models.py +++ b/zerver/models.py @@ -3915,6 +3915,7 @@ class AbstractRealmAuditLog(models.Model): STREAM_NAME_CHANGED = 603 STREAM_REACTIVATED = 604 STREAM_MESSAGE_RETENTION_DAYS_CHANGED = 605 + STREAM_PROPERTY_CHANGED = 607 # The following values are only for RemoteZulipServerAuditLog # Values should be exactly 10000 greater than the corresponding diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.py index a442a2df77..c9d62a2a91 100644 --- a/zerver/tests/test_subs.py +++ b/zerver/tests/test_subs.py @@ -1216,6 +1216,20 @@ class StreamAdminTest(ZulipTestCase): ) self.assertEqual(messages[-1].content, expected_notification) + realm_audit_log = RealmAuditLog.objects.filter( + event_type=RealmAuditLog.STREAM_PROPERTY_CHANGED, + modified_stream=stream, + ).last() + assert realm_audit_log is not None + expected_extra_data = orjson.dumps( + { + RealmAuditLog.OLD_VALUE: "Test description", + RealmAuditLog.NEW_VALUE: "a multi line description", + "property": "description", + } + ).decode() + self.assertEqual(realm_audit_log.extra_data, expected_extra_data) + # Verify that we don't render inline URL previews in this code path. with self.settings(INLINE_URL_EMBED_PREVIEW=True): result = self.client_patch( @@ -1264,6 +1278,20 @@ class StreamAdminTest(ZulipTestCase): ) self.assertEqual(messages[-1].content, expected_notification) + realm_audit_log = RealmAuditLog.objects.filter( + event_type=RealmAuditLog.STREAM_PROPERTY_CHANGED, + modified_stream=stream, + ).last() + assert realm_audit_log is not None + expected_extra_data = orjson.dumps( + { + RealmAuditLog.OLD_VALUE: "See https://zulip.com/team", + RealmAuditLog.NEW_VALUE: "Test description", + "property": "description", + } + ).decode() + self.assertEqual(realm_audit_log.extra_data, expected_extra_data) + def test_change_stream_description_requires_admin(self) -> None: user_profile = self.example_user("hamlet") self.login_user(user_profile)