Skip to content

Commit

Permalink
audit_log: Log RealmAuditLog for do_set_realm_message_editing.
Browse files Browse the repository at this point in the history
Log RealmAuditLog for do_set_realm_message_editing. Added tests for
same.
  • Loading branch information
arpit551 authored and timabbott committed Jul 24, 2020
1 parent ea7effb commit 422fa0f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
33 changes: 24 additions & 9 deletions zerver/lib/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,22 +738,37 @@ def do_set_realm_authentication_methods(realm: Realm,
def do_set_realm_message_editing(realm: Realm,
allow_message_editing: bool,
message_content_edit_limit_seconds: int,
allow_community_topic_editing: bool) -> None:
allow_community_topic_editing: bool,
acting_user: Optional[UserProfile]=None) -> None:
old_values = dict(allow_message_editing=realm.allow_message_editing,
message_content_edit_limit_seconds=realm.message_content_edit_limit_seconds,
allow_community_topic_editing=realm.allow_community_topic_editing)

realm.allow_message_editing = allow_message_editing
realm.message_content_edit_limit_seconds = message_content_edit_limit_seconds
realm.allow_community_topic_editing = allow_community_topic_editing
realm.save(update_fields=['allow_message_editing',
'allow_community_topic_editing',
'message_content_edit_limit_seconds',
],
)

event_time = timezone_now()
updated_properties = dict(allow_message_editing=allow_message_editing,
message_content_edit_limit_seconds=message_content_edit_limit_seconds,
allow_community_topic_editing=allow_community_topic_editing)

for updated_property, updated_value in updated_properties.items():
if updated_value == old_values[updated_property]:
continue
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': updated_property, 'value': old_values[updated_property]},
RealmAuditLog.NEW_VALUE: {'property': updated_property, 'value': updated_value}
}))

realm.save(update_fields=list(updated_properties.keys()))
event = dict(
type="realm",
op="update_dict",
property="default",
data=dict(allow_message_editing=allow_message_editing,
message_content_edit_limit_seconds=message_content_edit_limit_seconds,
allow_community_topic_editing=allow_community_topic_editing),
data=updated_properties,
)
send_event(realm, event, active_user_ids(realm.id))

Expand Down
26 changes: 26 additions & 0 deletions zerver/tests/test_audit_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
do_reactivate_user,
do_regenerate_api_key,
do_set_realm_authentication_methods,
do_set_realm_message_editing,
get_last_message_id,
get_streams_traffic,
)
Expand Down Expand Up @@ -264,3 +265,28 @@ def test_get_last_message_id(self) -> None:
Message.objects.all().delete()

self.assertEqual(get_last_message_id(), -1)

def test_set_realm_message_editing(self) -> None:
now = timezone_now()
realm = get_realm('zulip')
user = self.example_user('hamlet')
old_values_expected = [{'property': 'message_content_edit_limit_seconds', 'value': realm.message_content_edit_limit_seconds},
{'property': 'allow_community_topic_editing', 'value': realm.allow_community_topic_editing}]

do_set_realm_message_editing(realm, True, 1000, False, acting_user=user)
realm_audit_logs = RealmAuditLog.objects.filter(realm=realm, event_type=RealmAuditLog.REALM_PROPERTY_CHANGED,
event_time__gte=now, acting_user=user)
self.assertEqual(realm_audit_logs.count(), 2)

# allow_message_editing was already True.
new_values_expected = [{'property': 'message_content_edit_limit_seconds', 'value': 1000},
{'property': 'allow_community_topic_editing', 'value': False}]
new_values_seen = []
old_values_seen = []
for realm_audit_log in realm_audit_logs:
extra_data = ujson.loads(realm_audit_log.extra_data)
new_values_seen.append(extra_data[RealmAuditLog.NEW_VALUE])
old_values_seen.append(extra_data[RealmAuditLog.OLD_VALUE])

self.assertEqual(new_values_seen, new_values_expected)
self.assertEqual(old_values_seen, old_values_expected)
3 changes: 2 additions & 1 deletion zerver/views/realm.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ def update_realm(
allow_community_topic_editing = realm.allow_community_topic_editing
do_set_realm_message_editing(realm, allow_message_editing,
message_content_edit_limit_seconds,
allow_community_topic_editing)
allow_community_topic_editing,
acting_user=user_profile)
data['allow_message_editing'] = allow_message_editing
data['message_content_edit_limit_seconds'] = message_content_edit_limit_seconds
data['allow_community_topic_editing'] = allow_community_topic_editing
Expand Down

0 comments on commit 422fa0f

Please sign in to comment.