Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

audit_log: Log RealmAuditLog for realm notification properties.

arpit551ccdf52fef689
diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py
index 23ef21844dd..cb20b7ade18 100644
--- a/zerver/lib/actions.py
+++ b/zerver/lib/actions.py
@@ -772,9 +772,20 @@ def do_set_realm_message_editing(realm: Realm,
)
send_event(realm, event, active_user_ids(realm.id))
-def do_set_realm_notifications_stream(realm: Realm, stream: Optional[Stream], stream_id: int) -> None:
+def do_set_realm_notifications_stream(realm: Realm, stream: Optional[Stream], stream_id: int,
+ acting_user: Optional[UserProfile]=None) -> None:
+ old_value = getattr(realm, 'notifications_stream')
realm.notifications_stream = stream
realm.save(update_fields=['notifications_stream'])
+
+ event_time = timezone_now()
+ RealmAuditLog.objects.create(
+ realm=realm, event_type=RealmAuditLog.REALM_PROPERTY_CHANGED, event_time=event_time,
+ acting_user=acting_user, extra_data=ujson.dumps({
+ RealmAuditLog.OLD_VALUE: {'property': 'notifications_stream', 'value': old_value},
+ RealmAuditLog.NEW_VALUE: {'property': 'notifications_stream', 'value': stream_id}
+ }))
+
event = dict(
type="realm",
op="update",
@@ -783,10 +794,19 @@ def do_set_realm_notifications_stream(realm: Realm, stream: Optional[Stream], st
)
send_event(realm, event, active_user_ids(realm.id))
-def do_set_realm_signup_notifications_stream(realm: Realm, stream: Optional[Stream],
- stream_id: int) -> None:
+def do_set_realm_signup_notifications_stream(realm: Realm, stream: Optional[Stream], stream_id: int,
+ acting_user: Optional[UserProfile]=None) -> None:
+ old_value = getattr(realm, 'signup_notifications_stream')
realm.signup_notifications_stream = stream
realm.save(update_fields=['signup_notifications_stream'])
+
+ event_time = timezone_now()
+ RealmAuditLog.objects.create(
+ realm=realm, event_type=RealmAuditLog.REALM_PROPERTY_CHANGED, event_time=event_time,
+ acting_user=acting_user, extra_data=ujson.dumps({
+ RealmAuditLog.OLD_VALUE: {'property': 'signup_notifications_stream', 'value': old_value},
+ RealmAuditLog.NEW_VALUE: {'property': 'signup_notifications_stream', 'value': stream_id}
+ }))
event = dict(
type="realm",
op="update",
diff --git a/zerver/tests/test_audit_log.py b/zerver/tests/test_audit_log.py
index 4e36cea69ff..7420aa9c794 100644
--- a/zerver/tests/test_audit_log.py
+++ b/zerver/tests/test_audit_log.py
@@ -25,6 +25,8 @@ from zerver.lib.actions import (
do_regenerate_api_key,
do_set_realm_authentication_methods,
do_set_realm_message_editing,
+ do_set_realm_notifications_stream,
+ do_set_realm_signup_notifications_stream,
get_last_message_id,
get_streams_traffic,
)
@@ -290,3 +292,37 @@ class TestRealmAuditLog(ZulipTestCase):
self.assertEqual(new_values_seen, new_values_expected)
self.assertEqual(old_values_seen, old_values_expected)
+
+ def test_set_realm_notifications_stream(self) -> None:
+ now = timezone_now()
+ realm = get_realm('zulip')
+ user = self.example_user('hamlet')
+ old_value = getattr(realm, 'notifications_stream')
+ stream_name = 'test'
+ stream = self.make_stream(stream_name, realm)
+
+ do_set_realm_notifications_stream(realm, stream, stream.id, acting_user=user)
+ self.assertEqual(RealmAuditLog.objects.filter(
+ realm=realm, event_type=RealmAuditLog.REALM_PROPERTY_CHANGED,
+ event_time__gte=now, acting_user=user,
+ extra_data=ujson.dumps({
+ RealmAuditLog.OLD_VALUE: {'property': 'notifications_stream', 'value': old_value},
+ RealmAuditLog.NEW_VALUE: {'property': 'notifications_stream', 'value': stream.id}
+ })).count(), 1)
+
+ def test_set_realm_signup_notifications_stream(self) -> None:
+ now = timezone_now()
+ realm = get_realm('zulip')
+ user = self.example_user('hamlet')
+ old_value = getattr(realm, 'signup_notifications_stream')
+ stream_name = 'test'
+ stream = self.make_stream(stream_name, realm)
+
+ do_set_realm_signup_notifications_stream(realm, stream, stream.id, acting_user=user)
+ self.assertEqual(RealmAuditLog.objects.filter(
+ realm=realm, event_type=RealmAuditLog.REALM_PROPERTY_CHANGED,
+ event_time__gte=now, acting_user=user,
+ extra_data=ujson.dumps({
+ RealmAuditLog.OLD_VALUE: {'property': 'signup_notifications_stream', 'value': old_value},
+ RealmAuditLog.NEW_VALUE: {'property': 'signup_notifications_stream', 'value': stream.id}
+ })).count(), 1)
diff --git a/zerver/views/realm.py b/zerver/views/realm.py
index dc509805ab0..17dfcbc658d 100644
--- a/zerver/views/realm.py
+++ b/zerver/views/realm.py
@@ -169,7 +169,8 @@ def update_realm(
(new_notifications_stream, recipient, sub) = access_stream_by_id(
user_profile, notifications_stream_id)
do_set_realm_notifications_stream(realm, new_notifications_stream,
- notifications_stream_id)
+ notifications_stream_id,
+ acting_user=user_profile)
data['notifications_stream_id'] = notifications_stream_id
if signup_notifications_stream_id is not None:
@@ -180,7 +181,8 @@ def update_realm(
(new_signup_notifications_stream, recipient, sub) = access_stream_by_id(
user_profile, signup_notifications_stream_id)
do_set_realm_signup_notifications_stream(realm, new_signup_notifications_stream,
- signup_notifications_stream_id)
+ signup_notifications_stream_id,
+ acting_user=user_profile)
data['signup_notifications_stream_id'] = signup_notifications_stream_id
if default_code_block_language is not None: