From ccdf52fef6891c6d559c181d2f7ca6270ee24692 Mon Sep 17 00:00:00 2001 From: arpit551 Date: Mon, 13 Jul 2020 03:01:47 +0530 Subject: [PATCH] audit_log: Log RealmAuditLog for realm notification properties. Log RealmAuditLog for do_set_realm_notifications_stream and do_set_realm_signup_notifications_stream function. Added tests for the same. --- zerver/lib/actions.py | 26 +++++++++++++++++++++--- zerver/tests/test_audit_log.py | 36 ++++++++++++++++++++++++++++++++++ zerver/views/realm.py | 6 ++++-- 3 files changed, 63 insertions(+), 5 deletions(-) diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index 23ef21844d..cb20b7ade1 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 4e36cea69f..7420aa9c79 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 dc509805ab..17dfcbc658 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: