Skip to content

Commit

Permalink
audit_log: Record RealmAuditLog in do_change_icon_source.
Browse files Browse the repository at this point in the history
Removed logging with  log_event and used RealmAuditLog instead.
Added tests in test_audit_log for the same.
  • Loading branch information
arpit551 authored and timabbott committed Jul 24, 2020
1 parent 12a19b0 commit e6f54a3
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 8 deletions.
12 changes: 7 additions & 5 deletions zerver/lib/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3361,15 +3361,17 @@ def do_delete_avatar_image(user: UserProfile, acting_user: Optional[UserProfile]
do_change_avatar_fields(user, UserProfile.AVATAR_FROM_GRAVATAR, acting_user=acting_user)
delete_avatar_image(user)

def do_change_icon_source(realm: Realm, icon_source: str, log: bool=True) -> None:
def do_change_icon_source(realm: Realm, icon_source: str, acting_user: Optional[UserProfile]=None) -> None:
realm.icon_source = icon_source
realm.icon_version += 1
realm.save(update_fields=["icon_source", "icon_version"])

if log:
log_event({'type': 'realm_change_icon',
'realm': realm.string_id,
'icon_source': icon_source})
event_time = timezone_now()
RealmAuditLog.objects.create(realm=realm,
event_type=RealmAuditLog.REALM_ICON_SOURCE_CHANGED,
extra_data={'icon_source': icon_source,
'icon_version': realm.icon_version},
event_time=event_time, acting_user=acting_user)

send_event(realm,
dict(type='realm',
Expand Down
1 change: 1 addition & 0 deletions zerver/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2720,6 +2720,7 @@ class AbstractRealmAuditLog(models.Model):
REALM_LOGO_CHANGED = 205
REALM_EXPORTED = 206
REALM_PROPERTY_CHANGED = 207
REALM_ICON_SOURCE_CHANGED = 208

SUBSCRIPTION_CREATED = 301
SUBSCRIPTION_ACTIVATED = 302
Expand Down
17 changes: 17 additions & 0 deletions zerver/tests/test_audit_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
do_activate_user,
do_change_avatar_fields,
do_change_bot_owner,
do_change_icon_source,
do_change_password,
do_change_tos_version,
do_change_user_delivery_email,
Expand Down Expand Up @@ -326,3 +327,19 @@ def test_set_realm_signup_notifications_stream(self) -> None:
RealmAuditLog.OLD_VALUE: {'property': 'signup_notifications_stream', 'value': old_value},
RealmAuditLog.NEW_VALUE: {'property': 'signup_notifications_stream', 'value': stream.id}
})).count(), 1)

def test_change_icon_source(self) -> None:
test_start = timezone_now()
realm = get_realm('zulip')
user = self.example_user('hamlet')
icon_source = 'G'
do_change_icon_source(realm, icon_source, acting_user=user)
audit_entries = RealmAuditLog.objects.filter(
realm=realm,
event_type=RealmAuditLog.REALM_ICON_SOURCE_CHANGED,
acting_user=user,
event_time__gte=test_start)
self.assertEqual(len(audit_entries), 1)
self.assertEqual(icon_source, realm.icon_source)
self.assertEqual(audit_entries.first().extra_data,
"{'icon_source': 'G', 'icon_version': 2}")
2 changes: 1 addition & 1 deletion zerver/tests/test_import_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def _setup_export_files(self, realm: Realm) -> Tuple[str, str, str, bytes]:

with get_test_image_file('img.png') as img_file:
upload.upload_backend.upload_realm_icon_image(img_file, user_profile)
do_change_icon_source(realm, Realm.ICON_UPLOADED, False)
do_change_icon_source(realm, Realm.ICON_UPLOADED)

with get_test_image_file('img.png') as img_file:
upload.upload_backend.upload_realm_logo_image(img_file, user_profile, night=False)
Expand Down
4 changes: 2 additions & 2 deletions zerver/views/realm_icon.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def upload_icon(request: HttpRequest, user_profile: UserProfile) -> HttpResponse
settings.MAX_ICON_FILE_SIZE,
))
upload_icon_image(icon_file, user_profile)
do_change_icon_source(user_profile.realm, user_profile.realm.ICON_UPLOADED)
do_change_icon_source(user_profile.realm, user_profile.realm.ICON_UPLOADED, acting_user=user_profile)
icon_url = realm_icon_url(user_profile.realm)

json_result = dict(
Expand All @@ -38,7 +38,7 @@ def delete_icon_backend(request: HttpRequest, user_profile: UserProfile) -> Http
# We don't actually delete the icon because it might still
# be needed if the URL was cached and it is rewrited
# in any case after next update.
do_change_icon_source(user_profile.realm, user_profile.realm.ICON_FROM_GRAVATAR)
do_change_icon_source(user_profile.realm, user_profile.realm.ICON_FROM_GRAVATAR, acting_user=user_profile)
gravatar_url = realm_icon_url(user_profile.realm)
json_result = dict(
icon_url=gravatar_url,
Expand Down

0 comments on commit e6f54a3

Please sign in to comment.