From caeeaf3c3fe25a603eab787b3ee844e615913faa Mon Sep 17 00:00:00 2001 From: Lauryn Menard Date: Tue, 3 Sep 2024 15:33:25 +0200 Subject: [PATCH] audit-log: Move user event types to AuditLogEventType enum. Event types moved: USER_SOFT_ACTIVATED, USER_SOFT_DEACTIVATED USER_PASSWORD_CHANGED, USER_AVATAR_SOURCE_CHANGED, USER_FULL_NAME_CHANGED USER_EMAIL_CHANGED, USER_TERMS_OF_SERVICE_VERSION_CHANGED USER_API_KEY_CHANGED, USER_BOT_OWNER_CHANGED, USER_DEFAULT_SENDING_STREAM_CHANGED, USER_DEFAULT_REGISTER_STREAM_CHANGED USER_DEFAULT_ALL_PUBLIC_STREAMS_CHANGED, USER_SETTING_CHANGED USER_DIGEST_EMAIL_CREATED --- analytics/tests/test_counts.py | 6 +++--- zerver/actions/bots.py | 9 ++++---- zerver/actions/create_user.py | 2 +- zerver/actions/user_settings.py | 15 +++++++------ zerver/lib/digest.py | 5 +++-- zerver/lib/soft_deactivation.py | 5 +++-- zerver/models/realm_audit_logs.py | 30 +++++++++++++------------- zerver/tests/test_audit_log.py | 27 ++++++++++++----------- zerver/tests/test_digest.py | 3 ++- zerver/tests/test_events.py | 5 +++-- zerver/tests/test_soft_deactivation.py | 3 ++- 11 files changed, 59 insertions(+), 51 deletions(-) diff --git a/analytics/tests/test_counts.py b/analytics/tests/test_counts.py index 4f2c9d4916..c5539af7a2 100644 --- a/analytics/tests/test_counts.py +++ b/analytics/tests/test_counts.py @@ -1938,7 +1938,7 @@ class TestActiveUsersAudit(AnalyticsTestCase): # doesn't go through do_create_user. Mainly just want to make sure that # that situation doesn't throw an error. def test_empty_realm_or_user_with_no_relevant_activity(self) -> None: - self.add_event(RealmAuditLog.USER_SOFT_ACTIVATED, 1) + self.add_event(AuditLogEventType.USER_SOFT_ACTIVATED, 1) self.create_user(skip_auditlog=True) # also test a user with no RealmAuditLog entries do_create_realm(string_id="moo", name="moo") do_fill_count_stat_at_hour(self.stat, self.TIME_ZERO) @@ -1946,14 +1946,14 @@ class TestActiveUsersAudit(AnalyticsTestCase): def test_max_audit_entry_is_unrelated(self) -> None: self.add_event(AuditLogEventType.USER_CREATED, 1) - self.add_event(RealmAuditLog.USER_SOFT_ACTIVATED, 0.5) + self.add_event(AuditLogEventType.USER_SOFT_ACTIVATED, 0.5) do_fill_count_stat_at_hour(self.stat, self.TIME_ZERO) self.assertTableState(RealmCount, ["subgroup"], [["false"]]) # Simultaneous related audit entries should not be allowed, and so not testing for that. def test_simultaneous_unrelated_audit_entry(self) -> None: self.add_event(AuditLogEventType.USER_CREATED, 1) - self.add_event(RealmAuditLog.USER_SOFT_ACTIVATED, 1) + self.add_event(AuditLogEventType.USER_SOFT_ACTIVATED, 1) do_fill_count_stat_at_hour(self.stat, self.TIME_ZERO) self.assertTableState(RealmCount, ["subgroup"], [["false"]]) diff --git a/zerver/actions/bots.py b/zerver/actions/bots.py index 6fa1a6027a..3f2e3d61e7 100644 --- a/zerver/actions/bots.py +++ b/zerver/actions/bots.py @@ -5,6 +5,7 @@ from zerver.actions.create_user import created_bot_event from zerver.actions.streams import bulk_remove_subscriptions from zerver.lib.streams import get_subscribed_private_streams_for_user from zerver.models import RealmAuditLog, Stream, UserProfile +from zerver.models.realm_audit_logs import AuditLogEventType from zerver.models.users import active_user_ids, bot_owner_user_ids from zerver.tornado.django_api import send_event_on_commit @@ -110,7 +111,7 @@ def do_change_bot_owner( realm=user_profile.realm, acting_user=acting_user, modified_user=user_profile, - event_type=RealmAuditLog.USER_BOT_OWNER_CHANGED, + event_type=AuditLogEventType.USER_BOT_OWNER_CHANGED, event_time=event_time, ) @@ -130,7 +131,7 @@ def do_change_default_sending_stream( event_time = timezone_now() RealmAuditLog.objects.create( realm=user_profile.realm, - event_type=RealmAuditLog.USER_DEFAULT_SENDING_STREAM_CHANGED, + event_type=AuditLogEventType.USER_DEFAULT_SENDING_STREAM_CHANGED, event_time=event_time, modified_user=user_profile, acting_user=acting_user, @@ -171,7 +172,7 @@ def do_change_default_events_register_stream( event_time = timezone_now() RealmAuditLog.objects.create( realm=user_profile.realm, - event_type=RealmAuditLog.USER_DEFAULT_REGISTER_STREAM_CHANGED, + event_type=AuditLogEventType.USER_DEFAULT_REGISTER_STREAM_CHANGED, event_time=event_time, modified_user=user_profile, acting_user=acting_user, @@ -213,7 +214,7 @@ def do_change_default_all_public_streams( event_time = timezone_now() RealmAuditLog.objects.create( realm=user_profile.realm, - event_type=RealmAuditLog.USER_DEFAULT_ALL_PUBLIC_STREAMS_CHANGED, + event_type=AuditLogEventType.USER_DEFAULT_ALL_PUBLIC_STREAMS_CHANGED, event_time=event_time, modified_user=user_profile, acting_user=acting_user, diff --git a/zerver/actions/create_user.py b/zerver/actions/create_user.py index a629794ab8..446f4436cf 100644 --- a/zerver/actions/create_user.py +++ b/zerver/actions/create_user.py @@ -706,7 +706,7 @@ def do_reactivate_user(user_profile: UserProfile, *, acting_user: UserProfile | realm=user_profile.realm, acting_user=acting_user, modified_user=user_profile, - event_type=RealmAuditLog.USER_BOT_OWNER_CHANGED, + event_type=AuditLogEventType.USER_BOT_OWNER_CHANGED, event_time=event_time, ) bot_owner_changed = True diff --git a/zerver/actions/user_settings.py b/zerver/actions/user_settings.py index 887d9b0b20..8f5454f0d5 100644 --- a/zerver/actions/user_settings.py +++ b/zerver/actions/user_settings.py @@ -41,6 +41,7 @@ from zerver.models import ( UserProfile, ) from zerver.models.clients import get_client +from zerver.models.realm_audit_logs import AuditLogEventType from zerver.models.users import bot_owner_user_ids, get_user_profile_by_id from zerver.tornado.django_api import send_event_on_commit @@ -141,7 +142,7 @@ def do_change_user_delivery_email(user_profile: UserProfile, new_email: str) -> realm=user_profile.realm, acting_user=user_profile, modified_user=user_profile, - event_type=RealmAuditLog.USER_EMAIL_CHANGED, + event_type=AuditLogEventType.USER_EMAIL_CHANGED, event_time=event_time, ) @@ -208,7 +209,7 @@ def do_change_password(user_profile: UserProfile, password: str, commit: bool = realm=user_profile.realm, acting_user=user_profile, modified_user=user_profile, - event_type=RealmAuditLog.USER_PASSWORD_CHANGED, + event_type=AuditLogEventType.USER_PASSWORD_CHANGED, event_time=event_time, ) @@ -228,7 +229,7 @@ def do_change_full_name( realm=user_profile.realm, acting_user=acting_user, modified_user=user_profile, - event_type=RealmAuditLog.USER_FULL_NAME_CHANGED, + event_type=AuditLogEventType.USER_FULL_NAME_CHANGED, event_time=event_time, extra_data={RealmAuditLog.OLD_VALUE: old_name, RealmAuditLog.NEW_VALUE: full_name}, ) @@ -290,7 +291,7 @@ def do_change_tos_version(user_profile: UserProfile, tos_version: str | None) -> realm=user_profile.realm, acting_user=user_profile, modified_user=user_profile, - event_type=RealmAuditLog.USER_TERMS_OF_SERVICE_VERSION_CHANGED, + event_type=AuditLogEventType.USER_TERMS_OF_SERVICE_VERSION_CHANGED, event_time=event_time, ) @@ -312,7 +313,7 @@ def do_regenerate_api_key(user_profile: UserProfile, acting_user: UserProfile) - realm=user_profile.realm, acting_user=acting_user, modified_user=user_profile, - event_type=RealmAuditLog.USER_API_KEY_CHANGED, + event_type=AuditLogEventType.USER_API_KEY_CHANGED, event_time=event_time, ) @@ -391,7 +392,7 @@ def do_change_avatar_fields( RealmAuditLog.objects.create( realm=user_profile.realm, modified_user=user_profile, - event_type=RealmAuditLog.USER_AVATAR_SOURCE_CHANGED, + event_type=AuditLogEventType.USER_AVATAR_SOURCE_CHANGED, extra_data={"avatar_source": avatar_source}, event_time=event_time, acting_user=acting_user, @@ -447,7 +448,7 @@ def do_change_user_setting( RealmAuditLog.objects.create( realm=user_profile.realm, - event_type=RealmAuditLog.USER_SETTING_CHANGED, + event_type=AuditLogEventType.USER_SETTING_CHANGED, event_time=event_time, acting_user=acting_user, modified_user=user_profile, diff --git a/zerver/lib/digest.py b/zerver/lib/digest.py index bc4e5c4e9a..d5436f4fd8 100644 --- a/zerver/lib/digest.py +++ b/zerver/lib/digest.py @@ -30,6 +30,7 @@ from zerver.models import ( UserActivityInterval, UserProfile, ) +from zerver.models.realm_audit_logs import AuditLogEventType from zerver.models.streams import get_active_streams logger = logging.getLogger(__name__) @@ -127,7 +128,7 @@ def _enqueue_emails_for_realm(realm: Realm, cutoff: datetime) -> None: sent_recent_digest=Exists( RealmAuditLog.objects.filter( realm_id=realm.id, - event_type=RealmAuditLog.USER_DIGEST_EMAIL_CREATED, + event_type=AuditLogEventType.USER_DIGEST_EMAIL_CREATED, event_time__gt=twelve_hours_ago, modified_user_id=OuterRef("id"), ) @@ -434,7 +435,7 @@ def bulk_write_realm_audit_logs(users: list[UserProfile]) -> None: modified_user_id=user.id, event_last_message_id=last_message_id, event_time=now, - event_type=RealmAuditLog.USER_DIGEST_EMAIL_CREATED, + event_type=AuditLogEventType.USER_DIGEST_EMAIL_CREATED, ) for user in users ] diff --git a/zerver/lib/soft_deactivation.py b/zerver/lib/soft_deactivation.py index b4739a4433..d136305989 100644 --- a/zerver/lib/soft_deactivation.py +++ b/zerver/lib/soft_deactivation.py @@ -25,6 +25,7 @@ from zerver.models import ( UserMessage, UserProfile, ) +from zerver.models.realm_audit_logs import AuditLogEventType from zerver.models.scheduled_jobs import NotificationTriggers logger = logging.getLogger("zulip.soft_deactivation") @@ -284,7 +285,7 @@ def do_soft_deactivate_users( log = RealmAuditLog( realm=user.realm, modified_user=user, - event_type=RealmAuditLog.USER_SOFT_DEACTIVATED, + event_type=AuditLogEventType.USER_SOFT_DEACTIVATED, event_time=event_time, ) realm_logs.append(log) @@ -324,7 +325,7 @@ def reactivate_user_if_soft_deactivated(user_profile: UserProfile) -> UserProfil RealmAuditLog.objects.create( realm=user_profile.realm, modified_user=user_profile, - event_type=RealmAuditLog.USER_SOFT_ACTIVATED, + event_type=AuditLogEventType.USER_SOFT_ACTIVATED, event_time=timezone_now(), ) logger.info("Soft reactivated user %s", user_profile.id) diff --git a/zerver/models/realm_audit_logs.py b/zerver/models/realm_audit_logs.py index 29ec634bd8..e08054733b 100644 --- a/zerver/models/realm_audit_logs.py +++ b/zerver/models/realm_audit_logs.py @@ -21,6 +21,21 @@ class AuditLogEventType(IntEnum): USER_DELETED = 106 USER_DELETED_PRESERVING_MESSAGES = 107 + USER_SOFT_ACTIVATED = 120 + USER_SOFT_DEACTIVATED = 121 + USER_PASSWORD_CHANGED = 122 + USER_AVATAR_SOURCE_CHANGED = 123 + USER_FULL_NAME_CHANGED = 124 + USER_EMAIL_CHANGED = 125 + USER_TERMS_OF_SERVICE_VERSION_CHANGED = 126 + USER_API_KEY_CHANGED = 127 + USER_BOT_OWNER_CHANGED = 128 + USER_DEFAULT_SENDING_STREAM_CHANGED = 129 + USER_DEFAULT_REGISTER_STREAM_CHANGED = 130 + USER_DEFAULT_ALL_PUBLIC_STREAMS_CHANGED = 131 + USER_SETTING_CHANGED = 132 + USER_DIGEST_EMAIL_CREATED = 133 + class AbstractRealmAuditLog(models.Model): """Defines fields common to RealmAuditLog and RemoteRealmAuditLog.""" @@ -41,21 +56,6 @@ class AbstractRealmAuditLog(models.Model): extra_data = models.JSONField(default=dict, encoder=DjangoJSONEncoder) # Event types - USER_SOFT_ACTIVATED = 120 - USER_SOFT_DEACTIVATED = 121 - USER_PASSWORD_CHANGED = 122 - USER_AVATAR_SOURCE_CHANGED = 123 - USER_FULL_NAME_CHANGED = 124 - USER_EMAIL_CHANGED = 125 - USER_TERMS_OF_SERVICE_VERSION_CHANGED = 126 - USER_API_KEY_CHANGED = 127 - USER_BOT_OWNER_CHANGED = 128 - USER_DEFAULT_SENDING_STREAM_CHANGED = 129 - USER_DEFAULT_REGISTER_STREAM_CHANGED = 130 - USER_DEFAULT_ALL_PUBLIC_STREAMS_CHANGED = 131 - USER_SETTING_CHANGED = 132 - USER_DIGEST_EMAIL_CREATED = 133 - REALM_DEACTIVATED = 201 REALM_REACTIVATED = 202 REALM_SCRUBBED = 203 diff --git a/zerver/tests/test_audit_log.py b/zerver/tests/test_audit_log.py index c322eb211d..fcdc322734 100644 --- a/zerver/tests/test_audit_log.py +++ b/zerver/tests/test_audit_log.py @@ -266,7 +266,7 @@ class TestRealmAuditLog(ZulipTestCase): do_change_password(user, password) self.assertEqual( RealmAuditLog.objects.filter( - event_type=RealmAuditLog.USER_PASSWORD_CHANGED, event_time__gte=now + event_type=AuditLogEventType.USER_PASSWORD_CHANGED, event_time__gte=now ).count(), 1, ) @@ -280,7 +280,7 @@ class TestRealmAuditLog(ZulipTestCase): do_change_user_delivery_email(user, new_email) self.assertEqual( RealmAuditLog.objects.filter( - event_type=RealmAuditLog.USER_EMAIL_CHANGED, event_time__gte=now + event_type=AuditLogEventType.USER_EMAIL_CHANGED, event_time__gte=now ).count(), 1, ) @@ -288,11 +288,11 @@ class TestRealmAuditLog(ZulipTestCase): # Test the RealmAuditLog stringification audit_entry = RealmAuditLog.objects.get( - event_type=RealmAuditLog.USER_EMAIL_CHANGED, event_time__gte=now + event_type=AuditLogEventType.USER_EMAIL_CHANGED, event_time__gte=now ) self.assertTrue( repr(audit_entry).startswith( - f" {RealmAuditLog.USER_EMAIL_CHANGED} " + f" {AuditLogEventType.USER_EMAIL_CHANGED} " ) ) @@ -303,7 +303,7 @@ class TestRealmAuditLog(ZulipTestCase): do_change_avatar_fields(user, avatar_source, acting_user=user) self.assertEqual( RealmAuditLog.objects.filter( - event_type=RealmAuditLog.USER_AVATAR_SOURCE_CHANGED, + event_type=AuditLogEventType.USER_AVATAR_SOURCE_CHANGED, modified_user=user, acting_user=user, event_time__gte=now, @@ -320,7 +320,7 @@ class TestRealmAuditLog(ZulipTestCase): result = self.client_patch("/json/users/{}".format(self.example_user("hamlet").id), req) self.assertTrue(result.status_code == 200) query = RealmAuditLog.objects.filter( - event_type=RealmAuditLog.USER_FULL_NAME_CHANGED, event_time__gte=start + event_type=AuditLogEventType.USER_FULL_NAME_CHANGED, event_time__gte=start ) self.assertEqual(query.count(), 1) @@ -331,7 +331,8 @@ class TestRealmAuditLog(ZulipTestCase): do_change_tos_version(user, tos_version) self.assertEqual( RealmAuditLog.objects.filter( - event_type=RealmAuditLog.USER_TERMS_OF_SERVICE_VERSION_CHANGED, event_time__gte=now + event_type=AuditLogEventType.USER_TERMS_OF_SERVICE_VERSION_CHANGED, + event_time__gte=now, ).count(), 1, ) @@ -345,7 +346,7 @@ class TestRealmAuditLog(ZulipTestCase): do_change_bot_owner(bot, bot_owner, admin) self.assertEqual( RealmAuditLog.objects.filter( - event_type=RealmAuditLog.USER_BOT_OWNER_CHANGED, event_time__gte=now + event_type=AuditLogEventType.USER_BOT_OWNER_CHANGED, event_time__gte=now ).count(), 1, ) @@ -357,7 +358,7 @@ class TestRealmAuditLog(ZulipTestCase): do_regenerate_api_key(user, user) self.assertEqual( RealmAuditLog.objects.filter( - event_type=RealmAuditLog.USER_API_KEY_CHANGED, event_time__gte=now + event_type=AuditLogEventType.USER_API_KEY_CHANGED, event_time__gte=now ).count(), 1, ) @@ -706,7 +707,7 @@ class TestRealmAuditLog(ZulipTestCase): self.assertEqual( RealmAuditLog.objects.filter( realm=user.realm, - event_type=RealmAuditLog.USER_DEFAULT_SENDING_STREAM_CHANGED, + event_type=AuditLogEventType.USER_DEFAULT_SENDING_STREAM_CHANGED, event_time__gte=now, acting_user=user, extra_data={ @@ -723,7 +724,7 @@ class TestRealmAuditLog(ZulipTestCase): self.assertEqual( RealmAuditLog.objects.filter( realm=user.realm, - event_type=RealmAuditLog.USER_DEFAULT_REGISTER_STREAM_CHANGED, + event_type=AuditLogEventType.USER_DEFAULT_REGISTER_STREAM_CHANGED, event_time__gte=now, acting_user=user, extra_data={ @@ -740,7 +741,7 @@ class TestRealmAuditLog(ZulipTestCase): self.assertEqual( RealmAuditLog.objects.filter( realm=user.realm, - event_type=RealmAuditLog.USER_DEFAULT_ALL_PUBLIC_STREAMS_CHANGED, + event_type=AuditLogEventType.USER_DEFAULT_ALL_PUBLIC_STREAMS_CHANGED, event_time__gte=now, acting_user=user, extra_data={RealmAuditLog.OLD_VALUE: old_value, RealmAuditLog.NEW_VALUE: False}, @@ -801,7 +802,7 @@ class TestRealmAuditLog(ZulipTestCase): self.assertEqual( RealmAuditLog.objects.filter( realm=user.realm, - event_type=RealmAuditLog.USER_SETTING_CHANGED, + event_type=AuditLogEventType.USER_SETTING_CHANGED, event_time__gte=now, acting_user=user, modified_user=user, diff --git a/zerver/tests/test_digest.py b/zerver/tests/test_digest.py index e74f15cf93..a4e7f4fff5 100644 --- a/zerver/tests/test_digest.py +++ b/zerver/tests/test_digest.py @@ -26,6 +26,7 @@ from zerver.lib.message import get_last_message_id from zerver.lib.streams import create_stream_if_needed from zerver.lib.test_classes import ZulipTestCase from zerver.models import Message, Realm, RealmAuditLog, Stream, UserActivityInterval, UserProfile +from zerver.models.realm_audit_logs import AuditLogEventType from zerver.models.realms import get_realm from zerver.models.streams import get_stream @@ -266,7 +267,7 @@ class TestDigestEmailMessages(ZulipTestCase): for digest_user in digest_users: log_rows = RealmAuditLog.objects.filter( modified_user_id=digest_user.id, - event_type=RealmAuditLog.USER_DIGEST_EMAIL_CREATED, + event_type=AuditLogEventType.USER_DIGEST_EMAIL_CREATED, ) (log,) = log_rows self.assertEqual(log.event_last_message_id, last_message_id) diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py index 0680d16e0f..3be8b13138 100644 --- a/zerver/tests/test_events.py +++ b/zerver/tests/test_events.py @@ -248,6 +248,7 @@ from zerver.models import ( ) from zerver.models.clients import get_client from zerver.models.groups import SystemGroups +from zerver.models.realm_audit_logs import AuditLogEventType from zerver.models.streams import get_stream from zerver.models.users import get_user_by_delivery_email from zerver.openapi.openapi import validate_against_openapi_schema @@ -2088,7 +2089,7 @@ class NormalActionsTest(BaseAction): self.assertEqual( RealmAuditLog.objects.filter( realm=self.user_profile.realm, - event_type=RealmAuditLog.USER_FULL_NAME_CHANGED, + event_type=AuditLogEventType.USER_FULL_NAME_CHANGED, event_time__gte=now, acting_user=self.user_profile, ).count(), @@ -2101,7 +2102,7 @@ class NormalActionsTest(BaseAction): self.assertEqual( RealmAuditLog.objects.filter( realm=self.user_profile.realm, - event_type=RealmAuditLog.USER_FULL_NAME_CHANGED, + event_type=AuditLogEventType.USER_FULL_NAME_CHANGED, event_time__gte=now, acting_user=self.user_profile, ).count(), diff --git a/zerver/tests/test_soft_deactivation.py b/zerver/tests/test_soft_deactivation.py index 30eb79ffca..593d28550d 100644 --- a/zerver/tests/test_soft_deactivation.py +++ b/zerver/tests/test_soft_deactivation.py @@ -29,6 +29,7 @@ from zerver.models import ( UserMessage, UserProfile, ) +from zerver.models.realm_audit_logs import AuditLogEventType from zerver.models.realms import get_realm from zerver.models.streams import get_stream @@ -328,7 +329,7 @@ class SoftDeactivationMessageTest(ZulipTestCase): reactivate_user_if_soft_deactivated(long_term_idle_user) self.assertFalse(long_term_idle_user.long_term_idle) self.assertEqual( - last_realm_audit_log_entry(RealmAuditLog.USER_SOFT_ACTIVATED).modified_user, + last_realm_audit_log_entry(AuditLogEventType.USER_SOFT_ACTIVATED).modified_user, long_term_idle_user, ) idle_user_msg_list = get_user_messages(long_term_idle_user)