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
This commit is contained in:
Lauryn Menard 2024-09-03 15:33:25 +02:00 committed by Tim Abbott
parent e5daa3470f
commit caeeaf3c3f
11 changed files with 59 additions and 51 deletions

View File

@ -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"]])

View File

@ -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,

View File

@ -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

View File

@ -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,

View File

@ -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
]

View File

@ -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)

View File

@ -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

View File

@ -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: <UserProfile: {user.email} {user.realm!r}> {RealmAuditLog.USER_EMAIL_CHANGED} "
f"<RealmAuditLog: <UserProfile: {user.email} {user.realm!r}> {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,

View File

@ -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)

View File

@ -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(),

View File

@ -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)