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.
This commit is contained in:
arpit551 2020-07-13 03:01:47 +05:30 committed by Tim Abbott
parent 8f6a1c3f40
commit ccdf52fef6
3 changed files with 63 additions and 5 deletions

View File

@ -772,9 +772,20 @@ def do_set_realm_message_editing(realm: Realm,
) )
send_event(realm, event, active_user_ids(realm.id)) 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.notifications_stream = stream
realm.save(update_fields=['notifications_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( event = dict(
type="realm", type="realm",
op="update", 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)) send_event(realm, event, active_user_ids(realm.id))
def do_set_realm_signup_notifications_stream(realm: Realm, stream: Optional[Stream], def do_set_realm_signup_notifications_stream(realm: Realm, stream: Optional[Stream], stream_id: int,
stream_id: int) -> None: acting_user: Optional[UserProfile]=None) -> None:
old_value = getattr(realm, 'signup_notifications_stream')
realm.signup_notifications_stream = stream realm.signup_notifications_stream = stream
realm.save(update_fields=['signup_notifications_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( event = dict(
type="realm", type="realm",
op="update", op="update",

View File

@ -25,6 +25,8 @@ from zerver.lib.actions import (
do_regenerate_api_key, do_regenerate_api_key,
do_set_realm_authentication_methods, do_set_realm_authentication_methods,
do_set_realm_message_editing, do_set_realm_message_editing,
do_set_realm_notifications_stream,
do_set_realm_signup_notifications_stream,
get_last_message_id, get_last_message_id,
get_streams_traffic, get_streams_traffic,
) )
@ -290,3 +292,37 @@ class TestRealmAuditLog(ZulipTestCase):
self.assertEqual(new_values_seen, new_values_expected) self.assertEqual(new_values_seen, new_values_expected)
self.assertEqual(old_values_seen, old_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)

View File

@ -169,7 +169,8 @@ def update_realm(
(new_notifications_stream, recipient, sub) = access_stream_by_id( (new_notifications_stream, recipient, sub) = access_stream_by_id(
user_profile, notifications_stream_id) user_profile, notifications_stream_id)
do_set_realm_notifications_stream(realm, new_notifications_stream, 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 data['notifications_stream_id'] = notifications_stream_id
if signup_notifications_stream_id is not None: 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( (new_signup_notifications_stream, recipient, sub) = access_stream_by_id(
user_profile, signup_notifications_stream_id) user_profile, signup_notifications_stream_id)
do_set_realm_signup_notifications_stream(realm, new_signup_notifications_stream, 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 data['signup_notifications_stream_id'] = signup_notifications_stream_id
if default_code_block_language is not None: if default_code_block_language is not None: